unsafe public OcclusionMesh(ref OcclusionMeshAsset sharedMesh, Occluder occluder) { vertexCount = sharedMesh.vertexCount; indexCount = sharedMesh.indexCount; vertexData = sharedMesh.vertexData; indexData = sharedMesh.indexData; int size = UnsafeUtility.SizeOf <float4>() * vertexCount; var data = Memory.Unmanaged.Allocate(size, 64, Allocator.Persistent); transformedVertexData = BlobAssetReference <float4> .Create(data, size); screenMin = float.MaxValue; screenMax = -float.MaxValue; localToWorld = occluder.localTransform; }
unsafe protected void AddComponents(Entity entity, Mesh mesh, int submeshIndex) { var subMesh = mesh.GetSubMesh(submeshIndex); var verts = mesh.vertices; var occlusionMeshAsset = new OcclusionMeshAsset(); var vertices = new NativeArray <float4>(subMesh.vertexCount, Allocator.Temp); for (int i = 0; i < subMesh.vertexCount; ++i) { vertices[i] = new float4(verts[i + subMesh.firstVertex], 1.0f); } occlusionMeshAsset.vertexCount = subMesh.vertexCount; occlusionMeshAsset.vertexData = BlobAssetReference <float4> .Create( vertices.GetUnsafeReadOnlyPtr(), sizeof(float4) *vertices.Length); vertices.Dispose(); var indices = new NativeArray <int>(subMesh.indexCount, Allocator.Temp); fixed(int *srcIndices = mesh.triangles) { for (int i = 0; i < indices.Length; i++) { indices[i] = srcIndices[subMesh.indexStart + i] - subMesh.firstVertex; if (indices[i] < 0 || indices[i] > subMesh.vertexCount) { Debug.Log($"broken index at {i}: {indices[i]}"); } } } occlusionMeshAsset.indexCount = indices.Length; occlusionMeshAsset.indexData = BlobAssetReference <int> .Create( indices.GetUnsafeReadOnlyPtr(), sizeof(int) *indices.Length); indices.Dispose(); DstEntityManager.AddComponentData(entity, occlusionMeshAsset); }