-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGenerateSurfaceTrianglesJob.cs
More file actions
495 lines (428 loc) · 17.6 KB
/
GenerateSurfaceTrianglesJob.cs
File metadata and controls
495 lines (428 loc) · 17.6 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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using Unity.Burst;
using Unity.Collections;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using Unity.Mathematics;
using Debug = UnityEngine.Debug;
using ReadOnlyAttribute = Unity.Collections.ReadOnlyAttribute;
using Unity.Entities;
using andywiecko.BurstTriangulator.LowLevel.Unsafe;
using andywiecko.BurstTriangulator;
namespace Chisel.Core
{
//[BurstCompile(CompileSynchronously = true)] // FIXME: If enabled, it causes more missing triangles in the mesh
struct GenerateSurfaceTrianglesJob : IJobParallelForDefer
{
// Read
// 'Required' for scheduling with index count
[NoAlias, ReadOnly] public NativeList<IndexOrder> allUpdateBrushIndexOrders;
[NoAlias, ReadOnly] public NativeList<BlobAssetReference<BasePolygonsBlob>> basePolygonCache;
[NoAlias, ReadOnly] public NativeList<NodeTransformations> transformationCache;
[NoAlias, ReadOnly] public NativeStream.Reader input;
[NoAlias, ReadOnly] public NativeArray<MeshQuery> meshQueries;
[NoAlias, ReadOnly] public CompactHierarchyManagerInstance.ReadOnlyInstanceIDLookup instanceIDLookup;
[NoAlias, ReadOnly] public bool subtractiveWorkflow;
[NoAlias, ReadOnly] public float normalSmoothingAngle;
// Write
[NativeDisableParallelForRestriction]
[NoAlias] public NativeList<BlobAssetReference<ChiselBrushRenderBuffer>> brushRenderBufferCache;
[BurstDiscard]
public static void InvalidFinalCategory(CategoryIndex _interiorCategory)
{
Debug.Assert(false, $"Invalid final category {_interiorCategory}");
}
struct CompareSortByBasePlaneIndex : System.Collections.Generic.IComparer<ChiselQuerySurface>
{
public readonly int Compare(ChiselQuerySurface x, ChiselQuerySurface y)
{
var diff = x.surfaceParameter - y.surfaceParameter;
if (diff != 0)
return diff;
return x.surfaceIndex - y.surfaceIndex;
}
}
readonly static CompareSortByBasePlaneIndex kCompareSortByBasePlaneIndex = new();
public unsafe void Execute(int index)
{
var count = input.BeginForEachIndex(index);
if (count == 0)
return;
// Read brush data
var brushIndexOrder = input.Read<IndexOrder>();
var brushNodeOrder = brushIndexOrder.nodeOrder;
var vertexCount = input.Read<int>();
// Pre-check: need at least 3 vertices
if (vertexCount < 3)
{
input.EndForEachIndex();
return;
}
HashedVertices brushVertices;
using var _brushVertices = brushVertices = new HashedVertices(vertexCount, Allocator.Temp);
for (int v = 0; v < vertexCount; v++)
{
var vertex = input.Read<float3>();
brushVertices.AddNoResize(vertex);
}
// Read surface loops
var surfaceOuterCount = input.Read<int>();
NativeList<UnsafeList<int>> surfaceLoopIndices;
using var _surfaceLoopIndices = surfaceLoopIndices = new NativeList<UnsafeList<int>>(surfaceOuterCount, Allocator.Temp);
surfaceLoopIndices.Resize(surfaceOuterCount, NativeArrayOptions.ClearMemory);
try
{
for (int o = 0; o < surfaceOuterCount; o++)
{
var countInner = input.Read<int>();
if (countInner > 0)
{
var inner = new UnsafeList<int>(countInner, Allocator.Temp);
for (int i = 0; i < countInner; i++)
{
inner.AddNoResize(input.Read<int>());
}
surfaceLoopIndices[o] = inner;
}
else
surfaceLoopIndices[o] = default;
}
// Read loop infos and edges
var loopCount = input.Read<int>();
NativeArray<SurfaceInfo> surfaceLoopAllInfos;
using var _surfaceLoopAllInfos = surfaceLoopAllInfos = new NativeArray<SurfaceInfo>(loopCount, Allocator.Temp);
NativeList<UnsafeList<Edge>> surfaceLoopAllEdges;
using var _surfaceLoopAllEdges = surfaceLoopAllEdges = new NativeList<UnsafeList<Edge>>(loopCount, Allocator.Temp);
surfaceLoopAllEdges.Resize(loopCount, NativeArrayOptions.ClearMemory);
try
{
for (int l = 0; l < loopCount; l++)
{
surfaceLoopAllInfos[l] = input.Read<SurfaceInfo>();
var edgeCount = input.Read<int>();
if (edgeCount > 0)
{
var edges = new UnsafeList<Edge>(edgeCount, Allocator.Temp);
for (int e = 0; e < edgeCount; e++)
{
edges.AddNoResize(input.Read<Edge>());
}
surfaceLoopAllEdges[l] = edges;
}
}
input.EndForEachIndex();
if (!basePolygonCache[brushNodeOrder].IsCreated)
return;
int instanceID = instanceIDLookup.SafeGetNodeInstanceID(brushIndexOrder.compactNodeID);
// Compute maximum sizes
int maxLoops = 0, maxIndices = 0;
for (int s = 0; s < surfaceLoopIndices.Length; s++)
{
if (!surfaceLoopIndices[s].IsCreated)
continue;
var length = surfaceLoopIndices[s].Length;
maxIndices += length;
maxLoops = math.max(maxLoops, length);
}
ref var baseSurfaces = ref basePolygonCache[brushNodeOrder].Value.surfaces;
var transform = transformationCache[brushNodeOrder];
var treeToNode = transform.treeToNode;
var nodeToTreeInvTrans = math.transpose(treeToNode);
// Scratch allocators
Vertex2DRemapper vertex2DRemapper;
using var _vertex2DRemapper = vertex2DRemapper = new()
{
lookup = new NativeList<int>(64, Allocator.Temp),
positions2D = new NativeList<double2>(64, Allocator.Temp),
edgeIndices = new NativeList<int>(128, Allocator.Temp)
};
UniqueVertexMapper uniqueVertexMapper;
using var _uniqueVertexMapper = uniqueVertexMapper = new()
{
indexRemap = new NativeArray<int>(brushVertices.Length, Allocator.Temp),
surfaceColliderVertices = new NativeList<float3>(brushVertices.Length, Allocator.Temp),
surfaceSelectVertices = new NativeList<SelectVertex>(brushVertices.Length, Allocator.Temp),
surfaceRenderVertices = new NativeList<RenderVertex>(brushVertices.Length, Allocator.Temp)
};
Args settings = new(
autoHolesAndBoundary: true,
concentricShellsParameter: 0.001f,
preprocessor: Preprocessor.None,
refineMesh: false,
restoreBoundary: true,
sloanMaxIters: 1_000_000,
validateInput: true,
verbose: true,
refinementThresholdAngle: math.radians(5),
refinementThresholdArea: 1f
);
NativeList<int> surfaceIndexList;
using var _surfaceIndexList = surfaceIndexList = new NativeList<int>(maxIndices, Allocator.Temp);
NativeList<int> loops;
using var _loops = loops = new NativeList<int>(maxLoops, Allocator.Temp);
NativeList<int> triangles;
using var _triangles = triangles = new NativeList<int>(64, Allocator.Temp);
NativeReference<Status> status;
using var _status = status = new NativeReference<Status>(Allocator.Temp);
var output = new andywiecko.BurstTriangulator.LowLevel.Unsafe.OutputData<double2>()
{
Triangles = triangles,
Status = status
};
using var builder = new BlobBuilder(Allocator.Temp, 4096);
ref var root = ref builder.ConstructRoot<ChiselBrushRenderBuffer>();
var surfaceBuffers = builder.Allocate(ref root.surfaces, surfaceLoopIndices.Length);
var triangulator = new UnsafeTriangulator<double2>();
for (int surf = 0; surf < surfaceLoopIndices.Length; surf++)
{
if (!surfaceLoopIndices[surf].IsCreated) continue;
loops.Clear(); uniqueVertexMapper.Reset();
// Collect valid loops
var loopIndices = surfaceLoopIndices[surf];
for (int l = 0; l < loopIndices.Length; l++)
{
var loopIdx = loopIndices[l];
var edges = surfaceLoopAllEdges[loopIdx];
if (edges.Length < 3) continue;
loops.AddNoResize(loopIdx);
}
if (loops.Length == 0) continue;
// We need to convert our UV matrix from tree-space, to brush local-space, to plane-space
// since the vertices of the polygons, at this point, are in tree-space.
var plane = baseSurfaces[surf].localPlane;
var localToPlane = MathExtensions.GenerateLocalToPlaneSpaceMatrix(plane);
var treeToPlane = math.mul(localToPlane, treeToNode);
var planeNormalMap = math.mul(nodeToTreeInvTrans, plane);
var map3DTo2D = new Map3DTo2D(planeNormalMap.xyz);
// Normal flip logic preparation
float3 finalFaceNormal = map3DTo2D.normal;
if (subtractiveWorkflow)
{
// Flip the normal direction so lighting is correct for the "inside"
finalFaceNormal = -finalFaceNormal;
}
surfaceIndexList.Clear();
for (int li = 0; li < loops.Length; li++)
{
var loopIdx = loops[li];
var edges = surfaceLoopAllEdges[loopIdx];
var info = surfaceLoopAllInfos[loopIdx];
Debug.Assert(surf == info.basePlaneIndex, "surfaceIndex != loopInfo.basePlaneIndex");
// Convert 3D -> 2D
vertex2DRemapper.ConvertToPlaneSpace(*brushVertices.m_Vertices, edges, map3DTo2D);
vertex2DRemapper.RemoveDuplicates();
if (vertex2DRemapper.CheckForSelfIntersections())
{
#if UNITY_EDITOR && DEBUG
Debug.LogWarning($"Self-intersection detected in surface {surf}, loop index {loopIdx}.");
#endif
vertex2DRemapper.RemoveSelfIntersectingEdges();
}
var roVerts = vertex2DRemapper.AsReadOnly();
// Pre-check: need enough points and edges
if (roVerts.positions2D.Length < 3 || roVerts.edgeIndices.Length < 3)
continue;
// Check for degenerate edges
if (IsDegenerate(roVerts.positions2D))
continue;
try
{
output.Triangles.Clear();
triangulator.Triangulate(
new andywiecko.BurstTriangulator.LowLevel.Unsafe.InputData<double2>
{
Positions = roVerts.positions2D,
ConstraintEdges = roVerts.edgeIndices
},
output,
settings,
Allocator.Temp);
#if UNITY_EDITOR && DEBUG
// Inside the loop after calling Triangulate:
if (output.Status.Value != Status.OK)
{
// Log the specific status enum value/name for more detail!
Debug.LogError($"Triangulator failed for surface {surf}, loop index {loopIdx} with status {output.Status.Value.ToString()} ({output.Status.Value})");
}
else if (output.Triangles.Length == 0)
{
Debug.LogError($"Triangulator returned zero triangles for surface {surf}, loop index {loopIdx}.");
}
#endif
if (output.Status.Value != Status.OK || output.Triangles.Length == 0)
continue;
// Winding order flip for subtractive
if (subtractiveWorkflow)
{
// Flip winding order (0,1,2 -> 0,2,1) to face inwards
for (int ti = 0; ti < output.Triangles.Length; ti += 3)
{
(output.Triangles[ti + 1], output.Triangles[ti + 2]) =
(output.Triangles[ti + 2], output.Triangles[ti + 1]);
}
}
// Map triangles back
var prevCount = surfaceIndexList.Length;
var interiorCat = (CategoryIndex)info.interiorCategory;
roVerts.RemapTriangles(interiorCat, output.Triangles, surfaceIndexList);
// Register vertices (Pass calculated/flipped normal)
uniqueVertexMapper.RegisterVertices(
surfaceIndexList,
prevCount,
*brushVertices.m_Vertices,
finalFaceNormal,
instanceID,
interiorCat);
// Normal smoothing logic (across the entire object)
if (normalSmoothingAngle > 0.0001f)
{
var renderVertices = uniqueVertexMapper.surfaceRenderVertices;
var positions = uniqueVertexMapper.surfaceColliderVertices;
float smoothingCos = math.cos(math.radians(normalSmoothingAngle));
int totalVerts = renderVertices.Length;
for (int v = 0; v < totalVerts; v++)
{
float3 vertPos = positions[v];
float3 smoothedNormal = finalFaceNormal;
// Iterate ALL brushes to smooth across the entire model
for (int otherBrushIdx = 0; otherBrushIdx < basePolygonCache.Length; otherBrushIdx++)
{
if (!basePolygonCache[otherBrushIdx].IsCreated) continue;
ref var otherSurfaces = ref basePolygonCache[otherBrushIdx].Value.surfaces;
var otherTransform = transformationCache[otherBrushIdx];
var otherNodeToTreeInvTrans = math.transpose(otherTransform.treeToNode);
for(int otherSurf = 0; otherSurf < otherSurfaces.Length; otherSurf++)
{
// Optimization: if we are checking against ourselves (same brush, same surface), skip
// However, we are in a loop over all brushes.
// 'brushNodeOrder' is the index of the current brush in basePolygonCache?
// 'brushIndexOrder.nodeOrder' was used to get current brush.
if (otherBrushIdx == brushNodeOrder && otherSurf == surf) continue;
float4 otherPlaneLocal = otherSurfaces[otherSurf].localPlane;
float4 otherPlaneTree = math.mul(otherNodeToTreeInvTrans, otherPlaneLocal);
float3 otherNormal = otherPlaneTree.xyz;
// If subtractive workflow, we must flip the neighbor normal effectively
// to compare "Inwards vs Inwards" rather than "Inwards vs Outwards"
float3 comparisonNormal = subtractiveWorkflow ? -otherNormal : otherNormal;
// Normal Alignment Check
// This now compares the correctly oriented normals
float dotAngle = math.dot(finalFaceNormal, comparisonNormal);
if (dotAngle < smoothingCos)
continue;
// Plane Distance Check
// distance = dot(N_raw, P) + D_raw.
// We use the raw plane normal and D from the cache for geometric distance.
float dist = math.dot(otherNormal, vertPos) + otherPlaneTree.w;
if (math.abs(dist) < 0.005f)
{
smoothedNormal += comparisonNormal;
}
}
}
var rv = renderVertices[v];
rv.normal = math.normalize(smoothedNormal);
renderVertices[v] = rv;
}
}
}
catch (System.Exception ex) { Debug.LogException(ex); }
}
if (surfaceIndexList.Length == 0) continue;
var flags = baseSurfaces[surf].destinationFlags;
var parms = baseSurfaces[surf].destinationParameters;
var UV0 = baseSurfaces[surf].UV0;
var uvMat = math.mul(UV0.ToFloat4x4(), treeToPlane);
// Normals are now correct (flipped/smoothed) before Tangents are calculated
MeshAlgorithms.ComputeUVs(uniqueVertexMapper.surfaceRenderVertices, uvMat);
MeshAlgorithms.ComputeTangents(surfaceIndexList, uniqueVertexMapper.surfaceRenderVertices);
ref var buf = ref surfaceBuffers[surf];
buf.Construct(builder, surfaceIndexList,
uniqueVertexMapper.surfaceColliderVertices,
uniqueVertexMapper.surfaceSelectVertices,
uniqueVertexMapper.surfaceRenderVertices,
surf, flags, parms);
}
using var queryList = new NativeList<ChiselQuerySurface>(surfaceBuffers.Length, Allocator.Temp);
var queryArr = builder.Allocate(ref root.querySurfaces, meshQueries.Length);
for (int t = 0; t < meshQueries.Length; t++)
{
var meshQuery = meshQueries[t];
var layerQueryMask = meshQuery.LayerQueryMask;
var layerQuery = meshQuery.LayerQuery;
var surfaceParameterIndex = (meshQuery.LayerParameterIndex >= SurfaceParameterIndex.Parameter1 &&
meshQuery.LayerParameterIndex <= SurfaceParameterIndex.MaxParameterIndex) ?
(int)meshQuery.LayerParameterIndex - 1 : -1;
queryList.Clear();
for (int s = 0; s < surfaceBuffers.Length; s++)
{
ref var buffer = ref surfaceBuffers[s];
var destinationFlags = buffer.destinationFlags;
if ((destinationFlags & layerQueryMask) != layerQuery)
continue;
queryList.AddNoResize(new ChiselQuerySurface
{
surfaceIndex = buffer.surfaceIndex,
surfaceParameter = surfaceParameterIndex < 0 ? 0 : buffer.destinationParameters.parameters[surfaceParameterIndex],
vertexCount = buffer.vertexCount,
indexCount = buffer.indexCount,
surfaceHashValue = buffer.surfaceHashValue,
geometryHashValue = buffer.geometryHashValue
});
}
queryList.Sort(kCompareSortByBasePlaneIndex);
builder.Construct(ref queryArr[t].surfaces, queryList);
queryArr[t].brushNodeID = brushIndexOrder.compactNodeID;
}
root.surfaceOffset = 0;
root.surfaceCount = surfaceBuffers.Length;
var brushRenderBuffer = builder.CreateBlobAssetReference<ChiselBrushRenderBuffer>(Allocator.Persistent);
if (brushRenderBufferCache[brushNodeOrder].IsCreated)
{
brushRenderBufferCache[brushNodeOrder].Dispose();
brushRenderBufferCache[brushNodeOrder] = default;
}
brushRenderBufferCache[brushNodeOrder] = brushRenderBuffer;
}
finally
{
if (surfaceLoopAllEdges.IsCreated)
{
for (int i = 0; i < surfaceLoopAllEdges.Length; i++)
{
if (surfaceLoopAllEdges[i].IsCreated)
surfaceLoopAllEdges[i].Dispose();
surfaceLoopAllEdges[i] = default;
}
}
}
}
finally
{
if (surfaceLoopIndices.IsCreated)
{
for (int i = 0; i < surfaceLoopIndices.Length; i++)
{
if (surfaceLoopIndices[i].IsCreated)
surfaceLoopIndices[i].Dispose();
surfaceLoopIndices[i] = default;
}
}
}
}
static bool IsDegenerate(NativeArray<double2> verts)
{
if (verts.Length < 3)
return true;
double2 min = verts[0];
double2 max = verts[0];
for (int i = 1; i < verts.Length; i++)
{
var v = verts[i];
min = math.min(min, v);
max = math.max(max, v);
}
// A zero-area axis-aligned box means every point sits on a line
return math.abs(max.x - min.x) <= double.Epsilon ||
math.abs(max.y - min.y) <= double.Epsilon;
}
}
}