-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCsgjsScript.cs
304 lines (264 loc) · 9.99 KB
/
CsgjsScript.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlaxEngine;
namespace FlaxCsgjs.Source
{
//[ExecuteInEditMode]
public class CsgjsScript : Script
{
private Csgjs _combinedCsg;
private StaticModel _virtualModelActor;
private Model _virtualModel;
[Serialize]
private CsgjsNodeType _nodeType;
public enum CsgjsActionType
{
Union,
Subtract,
Intersect
}
public enum CsgjsNodeType
{
Root,
Cube,
Sphere,
Cylinder
}
[EditorOrder(0)]
public CsgjsActionType ActionType { get; set; }
[EditorOrder(10)]
[NoSerialize]
public CsgjsNodeType NodeType
{
get { return _nodeType; }
set
{
_nodeType = value;
// TODO: Refactor this
if (_nodeType == CsgjsNodeType.Root)
{
}
else if (_nodeType == CsgjsNodeType.Cube)
{
Brush = new CsgjsCubeBrush(this);
}
else if (_nodeType == CsgjsNodeType.Sphere)
{
Brush = new CsgjsSphereBrush(this);
}
else if (_nodeType == CsgjsNodeType.Cylinder)
{
Brush = new CsgjsCylinderBrush(this);
}
}
}
[EditorOrder(20)]
[Serialize]
[EditorDisplay("Brush", EditorDisplayAttribute.InlineStyle)]
[ExpandGroups]
public CsgjsBrush Brush { get; set; }
public bool IsRoot => NodeType == CsgjsNodeType.Root;
public bool IsModel => NodeType != CsgjsNodeType.Root;
public override void OnEnable()
{
var parentScript = Actor.Parent?.GetScript<CsgjsScript>();
if (parentScript && NodeType == CsgjsNodeType.Root)
{
NodeType = CsgjsNodeType.Cube;
}
if (IsRoot && !_virtualModel)
{
// Create dynamic model with a single LOD with one mesh
_virtualModel = Content.CreateVirtualAsset<Model>();
_virtualModel.SetupLODs(1);
_virtualModelActor = Actor.GetOrAddChild<StaticModel>();
_virtualModelActor.HideFlags = HideFlags.DontSave;
_virtualModelActor.Model = _virtualModel;
}
}
public override void OnUpdate()
{
Execute();
}
public override void OnDisable()
{
Destroy(ref _virtualModelActor);
Destroy(ref _virtualModel);
_combinedCsg = null;
}
public void Execute()
{
if (NodeType == CsgjsNodeType.Root)
{
var csgResult = DoCsg();
_combinedCsg = csgResult;
if (csgResult.Polygons.Count == 0)
{
for (int i = 0; i < _virtualModelActor.Entries.Length; i++)
{
_virtualModelActor.Entries[i].Visible = false;
}
}
else
{
for (int i = 0; i < _virtualModelActor.Entries.Length; i++)
{
_virtualModelActor.Entries[i].Visible = true;
}
Triangulate(csgResult, _virtualModel);
}
}
}
/*
public override void OnDebugDraw()
{
_combinedCsgNode?.Polygons?.ForEach(p => p?.Vertices.ForEach(v =>
{
BoundingSphere sphere = new BoundingSphere(v.Position, 1);
DebugDraw.DrawSphere(sphere, Color.Blue);
}));
}
*/
public override void OnDebugDrawSelected()
{
Brush?.OnDebugDraw();
}
public bool Raycast(ref Ray mouseRay, out float distance, out CsgjsScript script)
{
Csgjs csgNode;
if (IsRoot)
{
csgNode = _combinedCsg;
}
else if (IsModel)
{
csgNode = Brush.GetCsg();
}
else
{
throw new NotSupportedException();
}
script = null;
distance = float.PositiveInfinity;
for (int i = 0; i < csgNode.Polygons.Count; i++)
{
if (csgNode.Polygons[i].Intersects(ref mouseRay, out float intersectionDistance) && intersectionDistance <= distance)
{
distance = intersectionDistance;
script = (csgNode.Polygons[i].Shared.SurfaceData as CsgjsBrush.CsgjsBrushSurface)?.Script;
}
}
return script != null;
}
public Csgjs DoCsg()
{
Csgjs csgResult = Brush?.GetCsg() ?? new Csgjs();
for (int i = 0; i < Actor.ChildrenCount; i++)
{
var childScript = Actor.Children[i].GetScript<CsgjsScript>();
if (childScript && childScript.Enabled && childScript.Actor.IsActiveInHierarchy && childScript.IsModel)
{
var childCsgNode = childScript.DoCsg();
// Do csg between myself and the child
if (childScript.ActionType == CsgjsActionType.Union)
{
csgResult = csgResult.Union(childCsgNode);
}
else if (childScript.ActionType == CsgjsActionType.Subtract)
{
csgResult = csgResult.Subtract(childCsgNode);
}
else if (childScript.ActionType == CsgjsActionType.Intersect)
{
csgResult = csgResult.Intersect(childCsgNode);
}
}
}
return csgResult;
}
public static void Triangulate(Csgjs csgNode, Model model)
{
HashSet<MaterialBase> materials = new HashSet<MaterialBase>();
for (int i = 0; i < csgNode.Polygons.Count; i++)
{
if (csgNode.Polygons[i].Shared.SurfaceData is CsgjsBrush.CsgjsBrushSurface surface)
{
materials.Add(surface.Material);
}
else
{
materials.Add(null);
}
}
// TODO: This could be better?
model.SetupLODs(materials.Count);
model.SetupMaterialSlots(materials.Count);
int meshIndex = 0;
foreach (var material in materials)
{
List<Vector3> vertices = new List<Vector3>();
List<Vector3> normals = new List<Vector3>();
List<Vector2> uvs = new List<Vector2>();
List<int> triangles = new List<int>();
for (int i = 0; i < csgNode.Polygons.Count; i++)
{
var polygon = csgNode.Polygons[i];
var polygonMaterial = (csgNode.Polygons[i].Shared.SurfaceData as CsgjsBrush.CsgjsBrushSurface)?.Material;
if (material != polygonMaterial)
{
continue;
}
int offset = vertices.Count;
Vector3 origin = polygon.Vertices[0].Position;
Vector3 normal = polygon.Plane.Normal;
Vector3 pointOnPlane = polygon.Vertices[1].Position;
vertices.AddRange(polygon.Vertices.Select(v => v.Position));
normals.AddRange(polygon.Vertices.Select(v => v.Normal));
uvs.AddRange(polygon.Vertices.Select(v => v.Uv));
List<Vector2> verts = polygon.Vertices
.Select(v => ToLocalPosition(v.Position, origin, normal, pointOnPlane))
.ToList();
List<Int3> tris = Earcutjs.Earcut(verts);
for (int j = 0; j < tris.Count; j++)
{
triangles.Add(tris[j].X + offset);
// Flip the winding order
triangles.Add(tris[j].Z + offset);
triangles.Add(tris[j].Y + offset);
}
}
model.LODs[0].Meshes[meshIndex].UpdateMesh(vertices, triangles, normals, null, uvs);
model.LODs[0].Meshes[meshIndex].MaterialSlotIndex = meshIndex;
model.MaterialSlots[meshIndex].Material = material;
meshIndex++;
}
}
// Taken from EdgeLoopPiece.cs
public static Vector3 ToGlobalPosition(Vector2 localPosition, Vector3 origin, Vector3 normal, Vector3 pointOnPlane)
{
// Direction X and Y vectors
Vector3 directionX = pointOnPlane - origin;
directionX.Normalize();
// Perpendicular to directionX
Vector3.Cross(ref directionX, ref normal, out Vector3 directionY);
return origin + localPosition.X * directionX + localPosition.Y * directionY;
}
public static Vector2 ToLocalPosition(Vector3 globalPosition, Vector3 origin, Vector3 normal, Vector3 pointOnPlane)
{
// Direction X and Y vectors
Vector3 directionX = pointOnPlane - origin;
directionX.Normalize();
// Perpendicular to directionX
Vector3.Cross(ref directionX, ref normal, out Vector3 directionY);
// Project it onto the direction vectors
Vector3 localPosition = globalPosition - origin;
return new Vector2(
Vector3.Dot(ref localPosition, ref directionX),
Vector3.Dot(ref localPosition, ref directionY)
);
}
}
}