-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeformable_terrain.gd
More file actions
91 lines (68 loc) · 1.28 KB
/
Copy pathdeformable_terrain.gd
File metadata and controls
91 lines (68 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
extends StaticBody
onready var terrain_mesh = get_parent()
var mesh_data = MeshDataTool.new()
func getDigged(amount,ray):
if !ray.is_colliding():
return
var hit_pos = terrain_mesh.to_local(
ray.get_collision_point()
)
var radius = 55.0
var old_mesh = terrain_mesh.mesh as ArrayMesh
if old_mesh == null:
return
var materials = []
for i in range(
old_mesh.get_surface_count()
):
materials.append(
old_mesh.surface_get_material(i)
)
mesh_data.clear()
mesh_data.create_from_surface(
old_mesh,
0
)
for i in range(
mesh_data.get_vertex_count()
):
var v = mesh_data.get_vertex(i)
var dist = v.distance_to(
hit_pos
)
if dist < radius:
var falloff = (
1.0 -
(dist/radius)
)
v.y -= (
amount *
falloff
)
mesh_data.set_vertex(
i,
v
)
var rebuilt = ArrayMesh.new()
mesh_data.commit_to_surface(
rebuilt
)
for i in range(
rebuilt.get_surface_count()
):
if i < materials.size():
rebuilt.surface_set_material(
i,
materials[i]
)
terrain_mesh.mesh = rebuilt
updateCollision()
func updateCollision():
if has_node(
"CollisionShape"
):
var shape = ConcavePolygonShape.new()
shape.set_faces(
terrain_mesh.mesh.get_faces()
)
$CollisionShape.shape = shape