-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathChiselBrushRenderBuffer.cs
More file actions
189 lines (154 loc) · 6.14 KB
/
ChiselBrushRenderBuffer.cs
File metadata and controls
189 lines (154 loc) · 6.14 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
using System;
using Unity.Collections;
using Unity.Entities;
using Unity.Mathematics;
using Debug = UnityEngine.Debug;
namespace Chisel.Core
{
internal struct ChiselSurfaceRenderBuffer
{
public int surfaceIndex;
public SurfaceDestinationFlags destinationFlags;
public SurfaceDestinationParameters destinationParameters;
public int vertexCount;
public int indexCount;
public uint geometryHashValue;
public uint surfaceHashValue;
public MinMaxAABB aabb;
public BlobArray<Int32> indices;
public BlobArray<RenderVertex> renderVertices;
public BlobArray<SelectVertex> selectVertices;
public BlobArray<float3> colliderVertices;
public readonly void FixUpOrdering(NativeList<Int32> indices,
NativeList<float3> colliderVertices,
NativeList<SelectVertex> selectVertices,
NativeList<RenderVertex> renderVertices)
{
UnityEngine.Debug.Assert(colliderVertices.Length == selectVertices.Length);
UnityEngine.Debug.Assert(renderVertices.Length == selectVertices.Length);
var indexRemap = new NativeList<int>(colliderVertices.Length, Allocator.TempJob);
indexRemap.Resize(colliderVertices.Length, NativeArrayOptions.ClearMemory);
for (int i = 0; i < colliderVertices.Length; i++)
{
indexRemap[i] = i;
}
for (int i = 0; i < colliderVertices.Length - 1; i++)
{
uint i_hash = math.hash(colliderVertices[i]);
for (int j = i + 1; j < colliderVertices.Length; j++)
{
uint j_hash = math.hash(colliderVertices[j]);
if (i_hash >= j_hash)
continue;
var index_i = indexRemap[i];
var index_j = indexRemap[j];
(colliderVertices[index_i], colliderVertices[index_j]) = (colliderVertices[index_j], colliderVertices[index_i]);
(selectVertices[index_i], selectVertices[index_j]) = (selectVertices[index_j], selectVertices[index_i]);
(renderVertices[index_i], renderVertices[index_j]) = (renderVertices[index_j], renderVertices[index_i]);
(indexRemap[i], indexRemap[j]) = (indexRemap[j], indexRemap[i]);
(i_hash, _) = (j_hash, i_hash);
Debug.Assert(indexRemap[i] >= 0 && indexRemap[i] < colliderVertices.Length);
Debug.Assert(indexRemap[j] >= 0 && indexRemap[j] < colliderVertices.Length);
}
}
for (int i = 0; i < indices.Length; i+=3)
{
var a = indexRemap[indices[i + 0]];
var b = indexRemap[indices[i + 1]];
var c = indexRemap[indices[i + 2]];
if (a < b && a < c)
{
indices[i + 0] = a;
indices[i + 1] = b;
indices[i + 2] = c;
} else
if (b < a && b < c)
{
indices[i + 0] = b;
indices[i + 1] = c;
indices[i + 2] = a;
} else
{
indices[i + 0] = c;
indices[i + 1] = a;
indices[i + 2] = b;
}
}
indexRemap.Dispose();
for (int i = 0; i < indices.Length - 3; i += 3)
{
uint i_hash = math.hash(new int3(indices[i+0], indices[i + 1], indices[i + 2]));
for (int j = i + 3; j < indices.Length; j += 3)
{
uint j_hash = math.hash(new int3(indices[j + 0], indices[j + 1], indices[j + 2]));
if (i_hash >= j_hash)
continue;
(indices[i + 0], indices[j + 0]) = (indices[j + 0], indices[i + 0]);
(indices[i + 1], indices[j + 1]) = (indices[j + 1], indices[i + 1]);
(indices[i + 2], indices[j + 2]) = (indices[j + 2], indices[i + 2]);
(i_hash, _) = (j_hash, i_hash);
}
}
}
[GenerateTestsForBurstCompatibility]
public void Construct(BlobBuilder builder,
NativeList<Int32> indices,
NativeList<float3> colliderVertices,
NativeList<SelectVertex> selectVertices,
NativeList<RenderVertex> renderVertices,
int surfaceIndex,
SurfaceDestinationFlags destinationFlags,
SurfaceDestinationParameters destinationParameters)
{
FixUpOrdering(indices, colliderVertices, selectVertices, renderVertices);
var vertexHashValue = colliderVertices.Hash();
var indicesHashValue = indices.Hash();
var geometryHashValue = math.hash(new uint2(vertexHashValue, indicesHashValue));
this.surfaceIndex = surfaceIndex;
this.destinationFlags = destinationFlags;
this.destinationParameters = destinationParameters;
this.vertexCount = colliderVertices.Length;
this.indexCount = indices.Length;
uint surfaceHash = 0;
for (int i = 0; i < renderVertices.Length; i++)
{
var renderVertex = renderVertices[i];
surfaceHash = math.hash(new uint2(surfaceHash, math.hash(renderVertex.normal)));
surfaceHash = math.hash(new uint2(surfaceHash, math.hash(renderVertex.tangent)));
surfaceHash = math.hash(new uint2(surfaceHash, math.hash(renderVertex.uv0)));
}
this.surfaceHashValue = surfaceHash;
this.geometryHashValue = geometryHashValue;
this.aabb = colliderVertices.GetMinMax();
var outputIndices = builder.Construct(ref this.indices, indices);
var outputColliderVertices = builder.Construct(ref this.colliderVertices, colliderVertices);
var outputRenderVertices = builder.Construct(ref this.renderVertices, renderVertices);
var outputSelectVertices = builder.Construct(ref this.selectVertices, selectVertices);
UnityEngine.Debug.Assert(outputColliderVertices.Length == this.vertexCount);
UnityEngine.Debug.Assert(outputRenderVertices.Length == this.vertexCount);
UnityEngine.Debug.Assert(outputSelectVertices.Length == this.vertexCount);
Debug.Assert(outputIndices.Length == this.indexCount);
}
};
internal struct ChiselQuerySurface
{
public int surfaceIndex;
public int surfaceParameter;
public int vertexCount;
public int indexCount;
public uint geometryHashValue;
public uint surfaceHashValue;
}
internal struct ChiselQuerySurfaces
{
public CompactNodeID brushNodeID;
public BlobArray<ChiselQuerySurface> surfaces;
}
internal struct ChiselBrushRenderBuffer
{
public BlobArray<ChiselSurfaceRenderBuffer> surfaces;
public BlobArray<ChiselQuerySurfaces> querySurfaces;
public int surfaceOffset;
public int surfaceCount;
};
}