Example #1
0
    static BlobAssetReference <Collider> CreateMeshTerrain(NativeArray <float> heights, int2 size, float3 scale)
    {
        var vertices    = new NativeList <float3>(Allocator.Temp);
        var triangles   = new NativeList <int3>(Allocator.Temp);
        var vertexIndex = 0;

        for (int i = 0; i < size.x - 1; i++)
        {
            for (int j = 0; j < size.y - 1; j++)
            {
                int    i0 = i;
                int    i1 = i + 1;
                int    j0 = j;
                int    j1 = j + 1;
                float3 v0 = new float3(i0, heights[i0 + size.x * j0], j0) * scale;
                float3 v1 = new float3(i1, heights[i1 + size.x * j0], j0) * scale;
                float3 v2 = new float3(i0, heights[i0 + size.x * j1], j1) * scale;
                float3 v3 = new float3(i1, heights[i1 + size.x * j1], j1) * scale;

                vertices.Add(v1);
                vertices.Add(v0);
                vertices.Add(v2);
                vertices.Add(v1);
                vertices.Add(v2);
                vertices.Add(v3);

                triangles.Add(new int3(vertexIndex++, vertexIndex++, vertexIndex++));
                triangles.Add(new int3(vertexIndex++, vertexIndex++, vertexIndex++));
            }
        }

        return(MeshCollider.Create(vertices, triangles));
    }
        void ProcessFrameCache()
        {
            Profiler.BeginSample("Create Mesh Entities");
            while (_frameCaches.Count > 0)
            {
                var cached  = _frameCaches.Dequeue();
                var id      = cached.Identity;
                var results = cached.Results;

                var renderEntities = GetResizedCache(id, results.Length);
                InitializeEntities(renderEntities,
                                   id.ChunkId * UnivoxDefine.AxisSize); //ChunkID doubles as unscaled position


                for (var j = 0; j < renderEntities.Length; j++)
                {
                    var renderEntity = renderEntities[j];


                    var meshData   = EntityManager.GetComponentData <ChunkRenderMesh>(renderEntity);
                    var mesh       = results[j].Mesh;
                    var materialId = results[j].Material;
                    var batchId    = new BatchGroupIdentity()
                    {
                        Chunk = id, MaterialIdentity = materialId
                    };

                    var meshVerts       = mesh.vertices;
                    var nativeMeshVerts = new NativeArray <float3>(meshVerts.Length, Allocator.Temp,
                                                                   NativeArrayOptions.UninitializedMemory);
                    for (var i = 0; i < meshVerts.Length; i++)
                    {
                        nativeMeshVerts[i] = meshVerts[i];
                    }

                    var meshTris       = mesh.triangles;
                    var nativeMeshTris = new NativeArray <int>(meshTris.Length, Allocator.Temp,
                                                               NativeArrayOptions.UninitializedMemory);
                    for (var i = 0; i < meshTris.Length; i++)
                    {
                        nativeMeshTris[i] = meshTris[i];
                    }

                    var collider = MeshCollider.Create(nativeMeshVerts, nativeMeshTris, CollisionFilter.Default);
//                    var physSys = EntityManager.World.GetOrCreateSystem<BuildPhysicsWorld>();


                    nativeMeshTris.Dispose();
                    nativeMeshVerts.Dispose();

//                    var physicsData = new PhysicsCollider()
//                    {
//                        Material = new BlobAssetReference<Collider>()
//                    };
//                    var temp = new BlobAssetReference<Collider>()
//                    {
//                        Material = new Collider()
//                        {
//                            Filter = new CollisionFilter()
//                            {
//
//                            },
//                        }
//                    };


                    meshData.CastShadows    = ShadowCastingMode.On;
                    meshData.ReceiveShadows = true;
//                    meshData.layer = VoxelLayer //TODO
                    mesh.UploadMeshData(true);
//                    meshData.Mesh = mesh;
                    meshData.Batch = batchId;

                    _renderSystem.UploadMesh(batchId, mesh);
//                    if (GameManager.Registry.ArrayMaterials.TryGetValue(materialIdentity, out var arrayMaterial))
//                    {
//                        meshData.material = arrayMaterial.Material;
//                    }
//                    else
//                    {
//                        meshData.material = _defaultMaterial;
//                    }


                    EntityManager.SetComponentData(renderEntity, new PhysicsCollider()
                    {
                        Value = collider
                    });
                    EntityManager.SetComponentData(renderEntity, meshData);
                }
            }

            Profiler.EndSample();
        }