Advertisement
Levi971

Deformation Mesh Godot 4

Jan 17th, 2024 (edited)
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.87 KB | Gaming | 0 0
  1. @tool
  2. extends MeshInstance3D
  3. @export var Color_POI : StandardMaterial3D
  4. @export var Point_of_impact = Vector3.ZERO
  5. @export var Distance_Max = 5
  6. @export var Puissance_Multi = 1.0
  7. @export var Noisee : FastNoiseLite
  8. @export var CrushN = false
  9. @onready var Mesh_Pos = MeshInstance3D.new()
  10. @onready var Ball = SphereMesh.new()
  11.  
  12. @export var Crush_Away = false
  13. @export var Without_noise = false
  14. @export var Pure_Distance = false
  15. @export var mesh_pos_for_dir = false
  16.  
  17. # Called when the node enters the scene tree for the first time.
  18. func _ready():
  19.    
  20.     Mesh_Pos.mesh = Ball
  21.     Mesh_Pos.set_surface_override_material(0,Color_POI)
  22.     add_child(Mesh_Pos)
  23.     pass # Replace with function body.
  24.  
  25.  
  26. # Called every frame. 'delta' is the elapsed time since the previous frame.
  27. func _process(delta):
  28.     Mesh_Pos.mesh.height = (Distance_Max) *2.0
  29.     Mesh_Pos.mesh.radius = (float(Distance_Max)/2.0) * 2.0
  30. #   print(Mesh_Pos.mesh.height ," / ",Mesh_Pos.mesh.radius )
  31.    
  32.     Mesh_Pos.position = Point_of_impact
  33.    
  34.    
  35.     if CrushN:
  36.         Crush()
  37.         CrushN = false
  38.     pass
  39.  
  40. func Crush():
  41.     var spare_mesh = mesh.duplicate()
  42.     var mdt = MeshDataTool.new()
  43.     var numbre_surfaces = mesh.get_surface_count()
  44.     print("il y a ", numbre_surfaces," surfaces")
  45.     mesh.clear_surfaces()
  46.    
  47.     for a in numbre_surfaces:
  48.         print("Travaille sur surface ", a)
  49.        
  50.         print(a ,"possède ",mdt.get_vertex_count() ," vertex")
  51.         mdt.create_from_surface(spare_mesh,a)
  52.         for i in range(mdt.get_vertex_count()):
  53.             var vertex = mdt.get_vertex(i)
  54. #           var distance = absf(Distance_Max - (Point_of_impact.distance_to(vertex)))
  55.             var distance = 1 - (Point_of_impact.distance_to(vertex) / Distance_Max)
  56.             distance = clampf(distance,0,1)
  57.            
  58.             if Pure_Distance:
  59.                 distance = (Distance_Max - Point_of_impact.distance_to(vertex))
  60.                 if Point_of_impact.distance_to(vertex) > Distance_Max:
  61.                     distance = 0
  62.                    
  63. #           print(distance)
  64.            
  65.            
  66.            
  67.             var deform_x = Noisee.get_noise_3d(i,0,0)
  68.             var deform_y = Noisee.get_noise_3d(0,i,0)
  69.             var deform_z = Noisee.get_noise_3d(0,0,i)
  70.            
  71.             var crush_dir = (Point_of_impact - vertex)
  72.             if mesh_pos_for_dir:
  73.                 crush_dir = (Point_of_impact)
  74. #           crush_dir.y = 0
  75.            
  76.             crush_dir = crush_dir.normalized()
  77.             var crush_pow = (absf(deform_x) * -crush_dir) * distance
  78.            
  79.             if Pure_Distance:
  80.                 crush_pow = -crush_dir * distance
  81. #           print("verpos = ",vertex ,"dis = ",distance)
  82.            
  83.             if not Crush_Away:
  84.                 if not Without_noise:
  85.                     vertex.x += (deform_x * Puissance_Multi) * distance
  86.                     vertex.y += (deform_y * Puissance_Multi) * distance
  87.                     vertex.z += (deform_z * Puissance_Multi) * distance
  88.                 if Without_noise:
  89.                     vertex.x += ( Puissance_Multi) * distance
  90.                     vertex.y += ( Puissance_Multi) * distance
  91.                     vertex.z += ( Puissance_Multi) * distance
  92.             else:
  93.                 vertex += (crush_pow * Puissance_Multi)
  94.            
  95.             mdt.set_vertex(i,vertex)
  96.        
  97.        
  98.         mdt.commit_to_surface(mesh)
  99.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement