Skip to content

Commit 40ee414

Browse files
committed
Trimeshes faster
made tri meshes be much faster! Plus they also now dont cause memory leaks!
1 parent 3926edb commit 40ee414

File tree

4 files changed

+107
-43
lines changed

4 files changed

+107
-43
lines changed

Assets/New Terrain.asset

544 KB
Binary file not shown.

Assets/New Terrain.asset.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scenes/pond_testing.unity

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8100,7 +8100,7 @@ GameObject:
81008100
m_Icon: {fileID: 0}
81018101
m_NavMeshLayer: 0
81028102
m_StaticEditorFlags: 0
8103-
m_IsActive: 0
8103+
m_IsActive: 1
81048104
--- !u!33 &1979672019083397417
81058105
MeshFilter:
81068106
m_ObjectHideFlags: 0
@@ -8268,7 +8268,7 @@ Transform:
82688268
m_PrefabAsset: {fileID: 0}
82698269
m_GameObject: {fileID: 384974586029215134}
82708270
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
8271-
m_LocalPosition: {x: 16.15, y: -1.68, z: 36.97}
8271+
m_LocalPosition: {x: 20.97, y: -0.59, z: 4.47}
82728272
m_LocalScale: {x: 1, y: 1, z: 1}
82738273
m_ConstrainProportionsScale: 0
82748274
m_Children: []
@@ -8291,26 +8291,14 @@ MonoBehaviour:
82918291
Shape: 4
82928292
PhaseSettings: 16777216
82938293
ShapeMysteryPower: 0
8294-
Dynamic: 0
8294+
Dynamic: 1
82958295
Trigger: 0
82968296
TriMesh: {fileID: -2432090755550338912, guid: 233f2689b63bcc34aa56a377edba3f51,
82978297
type: 3}
8298-
DetectCollision: 1
8298+
DetectCollision: 0
82998299
MethodToRunOnDetectCollision:
83008300
m_PersistentCalls:
8301-
m_Calls:
8302-
- m_Target: {fileID: 8926586527191735975}
8303-
m_TargetAssemblyTypeName: ParticlePainter, Assembly-CSharp
8304-
m_MethodName: PaintParticle
8305-
m_Mode: 0
8306-
m_Arguments:
8307-
m_ObjectArgument: {fileID: 0}
8308-
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
8309-
m_IntArgument: 0
8310-
m_FloatArgument: 0
8311-
m_StringArgument:
8312-
m_BoolArgument: 0
8313-
m_CallState: 2
8301+
m_Calls: []
83148302
BatchSize: 256
83158303
debug: 0
83168304
--- !u!114 &8926586527191735975

Assets/Scripts/FlexStuff/FlexCollider.cs

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void Start()
8383

8484
if (Shape == NvFlexCollisionShapeType.eNvFlexShapeTriangleMesh)
8585
{
86-
//Container.MappingQueue += InitTri;
86+
Container.MappingQueue += InitTri;
8787
//Container.MappingQueue += MapTri;
8888
//Container.UnmappingQueue += UnMapTri;
8989
Container.DestroyQueue += DestroyTri;
@@ -140,25 +140,40 @@ void DealWithShape()
140140
break;
141141
case NvFlexCollisionShapeType.eNvFlexShapeTriangleMesh:
142142

143-
Vertices = Methods.NvFlexAllocBuffer(Container.Library, TriMesh.vertexCount, sizeof(Vector4), NvFlexBufferType.eNvFlexBufferHost);
144-
Indices = Methods.NvFlexAllocBuffer(Container.Library, TriMesh.triangles.Length, sizeof(int), NvFlexBufferType.eNvFlexBufferHost);
143+
//Vertices = Methods.NvFlexAllocBuffer(Container.Library, TriMesh.vertexCount, sizeof(Vector4), NvFlexBufferType.eNvFlexBufferHost);
144+
//Indices = Methods.NvFlexAllocBuffer(Container.Library, TriMesh.triangles.Length, sizeof(int), NvFlexBufferType.eNvFlexBufferHost);
145145

146-
MeshId = Methods.NvFlexCreateTriangleMesh(Container.Library);
146+
//MeshId = Methods.NvFlexCreateTriangleMesh(Container.Library);
147147

148148
RWVertices = (Vector4*)Methods.NvFlexMap(Vertices, (int)NvFlexMapFlags.eNvFlexMapWait);
149149
RWIndices = (int*)Methods.NvFlexMap(Indices, (int)NvFlexMapFlags.eNvFlexMapWait);
150150

151-
for (int i = 0; i < TriMesh.vertices.Length; i++)
151+
//for (int i = 0; i < TriMesh.vertices.Length; i++)
152+
//{
153+
//RWVertices[i] = TriMesh.vertices[i];
154+
//}
155+
156+
VertsLooping VertJob = new VertsLooping();
157+
VertJob.RWVerts = RWVertices;
158+
159+
NativeArray<Vector3> Verts;
160+
161+
using (var dataArray = Mesh.AcquireReadOnlyMeshData(TriMesh))
152162
{
153-
RWVertices[i] = TriMesh.vertices[i];
163+
var data = dataArray[0];
164+
Verts = new NativeArray<Vector3>(TriMesh.vertexCount, Allocator.TempJob);
165+
data.GetVertices(Verts);
166+
VertJob.Verts = Verts;
154167
}
155168

156-
for (int i = 0; i < TriMesh.triangles.Length; i++)
157-
{
158-
RWIndices[i] = TriMesh.triangles[i];
169+
JobHandle VertHandle = VertJob.Schedule(TriMesh.vertices.Length, BatchSize);
159170

160-
//Debug.DrawLine(RWVertices[RWIndices[i]], RWVertices[RWIndices[i + 1]], Color.blue, float.PositiveInfinity);
161-
}
171+
//for (int i = 0; i < TriMesh.triangles.Length; i++)
172+
//{
173+
//RWIndices[i] = TriMesh.triangles[i];
174+
175+
//Debug.DrawLine(RWVertices[RWIndices[i]], RWVertices[RWIndices[i + 1]], Color.blue, float.PositiveInfinity);
176+
//}
162177

163178
//for (int i = 0; i < TriMesh.triangles.Length; i += 3)
164179
//{
@@ -167,10 +182,25 @@ void DealWithShape()
167182
// Debug.DrawLine(RWVertices[RWIndices[i + 2]], RWVertices[RWIndices[i]], Color.blue, float.PositiveInfinity);
168183
//}
169184

170-
var min = TriMesh.bounds.min * 2;
185+
IndisLooping IndiJob = new IndisLooping();
186+
IndiJob.RWIndis = RWIndices;
187+
188+
NativeArray<int> Indis;
189+
190+
using (var dataArray = Mesh.AcquireReadOnlyMeshData(TriMesh))
191+
{
192+
var data = dataArray[0];
193+
Indis = new NativeArray<int>(TriMesh.triangles.Length, Allocator.TempJob);
194+
data.GetIndices(Indis, 0);
195+
IndiJob.Indis = Indis;
196+
}
197+
198+
JobHandle IndiHandle = IndiJob.Schedule(TriMesh.triangles.Length, BatchSize);
199+
200+
var min = TriMesh.bounds.min;
171201
var LowerBoundsPtr = &min;
172202

173-
var max = TriMesh.bounds.max * 2;
203+
var max = TriMesh.bounds.max;
174204
var UpperBoundsPtr = &max;
175205

176206
Container.SBuf.Geometry.data[ShapeIndex].triMesh.mesh = MeshId;
@@ -183,6 +213,12 @@ void DealWithShape()
183213
Methods.NvFlexUnmap(Vertices);
184214
Methods.NvFlexUnmap(Indices);
185215

216+
VertHandle.Complete();
217+
IndiHandle.Complete();
218+
219+
Verts.Dispose();
220+
Indis.Dispose();
221+
186222
//Methods.NvFlexUpdateTriangleMesh(Container.Library, MeshId, Vertices, Indices, TriMesh.vertices.Length, TriMesh.triangles.Length / 3, null, null);
187223
Methods.NvFlexUpdateTriangleMesh(Container.Library, MeshId, Vertices, Indices, TriMesh.vertices.Length, TriMesh.triangles.Length / 3, (float*)LowerBoundsPtr, (float*)UpperBoundsPtr);
188224

@@ -203,29 +239,61 @@ unsafe void InitTri()
203239
Debug.Log("attempting to create a tri mesh");
204240
}
205241

206-
unsafe void MapTri()
242+
//unsafe void MapTri()
243+
//{
244+
// RWVertices = (Vector4*)Methods.NvFlexMap(Vertices, (int)NvFlexMapFlags.eNvFlexMapWait);
245+
// RWIndices = (int*)Methods.NvFlexMap(Indices, (int)NvFlexMapFlags.eNvFlexMapWait);
246+
//}
247+
248+
//unsafe void UnMapTri()
249+
//{
250+
// Methods.NvFlexUnmap(Vertices);
251+
// Methods.NvFlexUnmap(Indices);
252+
253+
// if (transform.hasChanged)
254+
// {
255+
// Methods.NvFlexUpdateTriangleMesh(Container.Library, MeshId, Vertices, Indices, TriMesh.vertices.Length, TriMesh.triangles.Length / 3, null, null);
256+
// transform.hasChanged = false;
257+
// }
258+
//}
259+
260+
unsafe void DestroyTri()
207261
{
208-
RWVertices = (Vector4*)Methods.NvFlexMap(Vertices, (int)NvFlexMapFlags.eNvFlexMapWait);
209-
RWIndices = (int*)Methods.NvFlexMap(Indices, (int)NvFlexMapFlags.eNvFlexMapWait);
262+
Methods.NvFlexDestroyTriangleMesh(Container.Library, MeshId);
263+
Methods.NvFlexFreeBuffer(Vertices);
264+
Methods.NvFlexFreeBuffer(Indices);
210265
}
211266

212-
unsafe void UnMapTri()
267+
[BurstCompile]
268+
public unsafe struct VertsLooping : IJobParallelFor
213269
{
214-
Methods.NvFlexUnmap(Vertices);
215-
Methods.NvFlexUnmap(Indices);
270+
[WriteOnly]
271+
[NativeDisableUnsafePtrRestriction]
272+
public Vector4* RWVerts;
216273

217-
if (transform.hasChanged)
274+
[ReadOnly]
275+
public NativeArray<Vector3> Verts;
276+
277+
public void Execute(int i)
218278
{
219-
Methods.NvFlexUpdateTriangleMesh(Container.Library, MeshId, Vertices, Indices, TriMesh.vertices.Length, TriMesh.triangles.Length / 3, null, null);
220-
transform.hasChanged = false;
279+
RWVerts[i] = Verts[i];
221280
}
222281
}
223282

224-
unsafe void DestroyTri()
283+
[BurstCompile]
284+
public unsafe struct IndisLooping : IJobParallelFor
225285
{
226-
Methods.NvFlexDestroyTriangleMesh(Container.Library, MeshId);
227-
Methods.NvFlexFreeBuffer(Vertices);
228-
Methods.NvFlexFreeBuffer(Indices);
286+
[WriteOnly]
287+
[NativeDisableUnsafePtrRestriction]
288+
public int* RWIndis;
289+
290+
[ReadOnly]
291+
public NativeArray<int> Indis;
292+
293+
public void Execute(int i)
294+
{
295+
RWIndis[i] = Indis[i];
296+
}
229297
}
230298

231299
unsafe void DealWithCollisions()

0 commit comments

Comments
 (0)