@@ -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