forked from kalysti/terrain-tool-godot4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TerrainGizmoPlugin.cs
256 lines (212 loc) · 9.17 KB
/
TerrainGizmoPlugin.cs
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
using Godot;
using System;
using System.Linq;
namespace TerrainEditor
{
#if TOOLS
[Tool]
public partial class TerrainGizmoPlugin : EditorNode3DGizmoPlugin
{
public bool showCollider = false;
public bool showAabb = false;
public TerrainGizmoPlugin() : base()
{
CreateMaterial("main", new Color(1, 0, 0));
CreateMaterial("collider", new Color(0, 1, 0));
CreateHandleMaterial("handles");
Color gizmo_color = new Color(0.5f, 0.7f, 1);
CreateMaterial("shape_material", gizmo_color);
}
public override bool _HasGizmo(Node3D spatial)
{
return spatial is Terrain3D;
}
public override string _GetGizmoName()
{
return "TerrainGizmo";
}
void GetEdge(int p_edge, Vector3 position, Vector3 size, ref Vector3 r_from, ref Vector3 r_to)
{
switch (p_edge)
{
case 0:
{
r_from = new Vector3(position.X + size.X, position.Y, position.Z);
r_to = new Vector3(position.X, position.Y, position.Z);
}
break;
case 1:
{
r_from = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
r_to = new Vector3(position.X + size.X, position.Y, position.Z);
}
break;
case 2:
{
r_from = new Vector3(position.X, position.Y, position.Z + size.Z);
r_to = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
}
break;
case 3:
{
r_from = new Vector3(position.X, position.Y, position.Z);
r_to = new Vector3(position.X, position.Y, position.Z + size.Z);
}
break;
case 4:
{
r_from = new Vector3(position.X, position.Y + size.Y, position.Z);
r_to = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
}
break;
case 5:
{
r_from = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
r_to = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
}
break;
case 6:
{
r_from = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
r_to = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
}
break;
case 7:
{
r_from = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
r_to = new Vector3(position.X, position.Y + size.Y, position.Z);
}
break;
case 8:
{
r_from = new Vector3(position.X, position.Y, position.Z + size.Z);
r_to = new Vector3(position.X, position.Y + size.Y, position.Z + size.Z);
}
break;
case 9:
{
r_from = new Vector3(position.X, position.Y, position.Z);
r_to = new Vector3(position.X, position.Y + size.Y, position.Z);
}
break;
case 10:
{
r_from = new Vector3(position.X + size.X, position.Y, position.Z);
r_to = new Vector3(position.X + size.X, position.Y + size.Y, position.Z);
}
break;
case 11:
{
r_from = new Vector3(position.X + size.X, position.Y, position.Z + size.Z);
r_to = new Vector3(position.X + size.X, position.Y + size.Y, position.Z + size.Z);
}
break;
}
}
private StandardMaterial3D getDebugMaterial()
{
StandardMaterial3D line_material = new StandardMaterial3D();
line_material.ShadingMode = StandardMaterial3D.ShadingModeEnum.Unshaded;
line_material.Transparency = BaseMaterial3D.TransparencyEnum.Alpha;
line_material.AlbedoColor = new Color(0, 1, 1, 1);
line_material.AlbedoTextureForceSrgb = true;
line_material.VertexColorIsSrgb = true;
line_material.VertexColorUseAsAlbedo = true;
return line_material;
}
public ArrayMesh GetDebugMesh(Vector3[] lines)
{
var debug_mesh_cache = new ArrayMesh();
Godot.Collections.Array arr = new Godot.Collections.Array();
arr.Resize((int)Mesh.ArrayType.Max);
arr[(int)Mesh.ArrayType.Vertex] = Variant.CreateFrom(lines);
debug_mesh_cache.AddSurfaceFromArrays(Mesh.PrimitiveType.Lines, arr);
debug_mesh_cache.SurfaceSetMaterial(0, getDebugMaterial());
return debug_mesh_cache;
}
public Vector3[] GetDebugMeshLines(TerrainPatch patch)
{
float[]? heightField = patch.CacheHeightData();
int map_width = patch.info.heightMapSize;
int map_depth = patch.info.heightMapSize;
Vector3[]? points = Array.Empty<Vector3>();
if ((map_width != 0) && (map_depth != 0))
{
// This will be slow for large maps...
// also we'll have to figure out how well bullet centers this shape...
Vector2 size = new Vector2(map_width - 1, map_depth - 1);
Vector2 start = size * -0.5f;
// reserve some memory for our points..
points = new Vector3[((map_width - 1) * map_depth * 2) + (map_width * (map_depth - 1) * 2)];
// now set our points
int r_offset = 0;
int w_offset = 0;
for (int d = 0; d < map_depth; d++)
{
Vector3 height = new Vector3(start.X, 0.0f, start.Y);
for (int w = 0; w < map_width; w++)
{
height.Y = heightField[r_offset++];
if (w != map_width - 1)
{
points[w_offset++] = height;
points[w_offset++] = new Vector3(height.X + 1.0f, heightField[r_offset], height.Z);
}
if (d != map_depth - 1)
{
points[w_offset++] = height;
points[w_offset++] = new Vector3(height.X, heightField[r_offset + map_width - 1], height.Z + 1.0f);
}
height.X += 1.0f;
}
start.Y += 1.0f;
}
}
return points;
}
public override void _Redraw(EditorNode3DGizmo gizmo)
{
gizmo.Clear();
var spatial = gizmo.GetNode3D() as Terrain3D;
if (spatial == null || spatial.Visible == false || !spatial.IsInsideTree())
return;
foreach (TerrainPatch? patch in spatial.terrainPatches)
{
if (showAabb)
{
var lines = new Godot.Collections.Array<Vector3>();
Aabb Aabb = patch.getBounds();
for (int i = 0; i < 8; i++)
{
Vector3 a, b;
a = new Vector3();
b = new Vector3();
GetEdge(i, Aabb.Position, Aabb.Size, ref a, ref b);
lines.Add(a);
lines.Add(b);
}
gizmo.AddLines(lines.ToArray(), GetMaterial("main", gizmo));
}
if (showCollider)
{
Vector3[]? meshLines = GetDebugMeshLines(patch);
SurfaceTool st = new SurfaceTool();
Transform3D tf = patch.GetColliderPosition(spatial, false); //todo: fix scaling gizmo
tf.Origin = tf.Origin - spatial.GlobalTransform.Origin;
st.AppendFrom(GetDebugMesh(meshLines), 0, tf);
gizmo.AddMesh(st.Commit(), GetMaterial("shape_material", gizmo));
}
}
}
ArrayMesh get_debug_mesh(Godot.Collections.Array<Vector3> lines)
{
var mt = new ArrayMesh();
var arr = new Godot.Collections.Array();
arr.Resize((int)ArrayMesh.ArrayType.Max);
arr[(int)ArrayMesh.ArrayType.Vertex] = lines;
mt.AddSurfaceFromArrays(ArrayMesh.PrimitiveType.Lines, arr);
return mt;
}
}
#endif
}