Beispiel #1
0
        public void ChunkRecieved(SimpleMesh mesh)
        {
            MeshCollider collider = (MeshCollider)this.collider;
            Mesh oldmesh = filter.sharedMesh;

            filter.sharedMesh = null;
            collider.sharedMesh = null;

            if (oldmesh != null) Mesh.Destroy(oldmesh);

            Mesh m = mesh.CreateMesh();

            MeshRenderer r = GetComponent<MeshRenderer>();

            if(mesh.Submeshes.Count > 0)
            {
                r.sharedMaterials = mesh.Submeshes.Select(me => me.RenderMaterial).ToArray();
            }
            else
            {
                r.sharedMaterial = mesh.RenderMaterial;
            }

            filter.sharedMesh = m;
            collider.sharedMesh = m;
        }
Beispiel #2
0
        private static void AddMesh(ref SimpleMesh mesh, int side, Vector3 offset, int blockId)
        {
            Vector3[] Face = Sides[side];



            foreach (Vector3 v in Face)
            {
                Vector3 position = v + offset;
                mesh.Vertices.Add(position);
            }
            Block b = Game.BlockRegistry[blockId];

            float mat1yp = (byte)b.Data.TextureIDs[2] / 255.0f;
            float mat1yn = (byte)b.Data.TextureIDs[3] / 255.0f;

            Color c32;

            c32.r = (byte)b.Data.TextureIDs[0] / 255f; //xz+ xz-
            c32.g = (byte)0;
            c32.b = 0;                                 //Blend.  0 for now
            c32.a = 0;



            for (int i = 0; i < 4; i++)
            {
                mesh.Colors.Add(c32);
            }

            for (int i = 0; i < 4; i++)
            {
                mesh.UV1.Add(new Vector2(mat1yp, mat1yn));
                mesh.UV2.Add(new Vector2(0, 0));
            }



            int meshcount = ((mesh.Vertices.Count / 4) - 1) * Face.Length;

            foreach (int t in FaceTriangles)
            {
                mesh.Triangles.Add(t + meshcount);
            }
        }
        public void GenerateMesh(IVoxelDataSource<VoxelData> data, Bounds cellSize, out SimpleMesh mesh)
        {
            mesh = new SimpleMesh();
            mesh.RenderMaterial = Game.BlockRegistry.AtlasMaterial;

            int xvol = (int)cellSize.size.x;
            int yvol = (int)cellSize.size.y;
            int zvol = (int)cellSize.size.y;

            int xoff = (int)cellSize.min.x;
            int yoff = (int)cellSize.min.y;
            int zoff = (int)cellSize.min.z;

            //Determine how many distinct blocks we have in this chunk
            //VoxelData[, ,] chunkData = data.SampleSpace(cellSize);

            for (int x = 0; x < xvol; x++)
            {
                for (int y = 0; y < yvol; y++)
                {
                    for (int z = 0; z < zvol; z++)
                    {
                        int xx = xoff + x;
                        int yy = yoff + y;
                        int zz = zoff + z;

                        int id = data.Sample(xx, yy, zz);
                        if (id <= 0) continue;

                        Vector3 vec = new Vector3(x, y, z);
                        //Add Back Face
                        if (data.Sample(xx, yy, zz + 1) <= 0)
                        {
                            AddMesh(ref mesh, 1, vec, id);

                        }
                        //Add Front Face
                        if (data.Sample(xx, yy, zz - 1) <= 0)
                        {
                            AddMesh(ref mesh, 0, vec, id);
                        }
                        //Add Top Face
                        if (data.Sample(xx, yy + 1, zz) <= 0)
                        {
                            AddMesh(ref mesh, 4, vec, id);
                        }

                        //Add Bottom Face
                        if (data.Sample(xx, yy - 1, zz) <= 0)
                        {
                            AddMesh(ref mesh, 5, vec, id);
                        }

                        //Add Left Face
                        if (data.Sample(xx + 1, yy, zz) <= 0)
                        {
                            AddMesh(ref mesh, 3, vec, id);
                        }

                        //Add Right Face
                        if (data.Sample(xx - 1, yy, zz) <= 0)
                        {
                            AddMesh(ref mesh, 2, vec, id);
                        }
                    }
                }
            }
        }
        private static void AddMesh(ref SimpleMesh mesh, int side, Vector3 offset, int blockId)
        {
            Vector3[] Face = Sides[side];

            foreach (Vector3 v in Face)
            {
                Vector3 position = v + offset;
                mesh.Vertices.Add(position);
            }
            Block b = Game.BlockRegistry[blockId];

            float mat1yp = (byte)b.Data.TextureIDs[2] / 255.0f;
            float mat1yn = (byte)b.Data.TextureIDs[3] / 255.0f;

            Color c32;
            c32.r = (byte)b.Data.TextureIDs[0] / 255f; //xz+ xz-
            c32.g = (byte)0;
            c32.b = 0; //Blend.  0 for now
            c32.a = 0;

            for (int i = 0; i < 4; i++)
            {
                mesh.Colors.Add(c32);
            }

            for(int i = 0; i < 4; i++)
            {
                mesh.UV1.Add(new Vector2(mat1yp, mat1yn));
                mesh.UV2.Add(new Vector2(0, 0));
            }

            int meshcount = ((mesh.Vertices.Count / 4) - 1) * Face.Length;
            foreach (int t in FaceTriangles)
            {
                mesh.Triangles.Add(t + meshcount);
            }
        }
Beispiel #5
0
        public void GenerateMesh(IVoxelDataSource <VoxelData> data, Bounds cellSize, out SimpleMesh mesh)
        {
            mesh = new SimpleMesh();
            mesh.RenderMaterial = Game.BlockRegistry.AtlasMaterial;

            int xvol = (int)cellSize.size.x;
            int yvol = (int)cellSize.size.y;
            int zvol = (int)cellSize.size.y;

            int xoff = (int)cellSize.min.x;
            int yoff = (int)cellSize.min.y;
            int zoff = (int)cellSize.min.z;

            //Determine how many distinct blocks we have in this chunk
            //VoxelData[, ,] chunkData = data.SampleSpace(cellSize);

            for (int x = 0; x < xvol; x++)
            {
                for (int y = 0; y < yvol; y++)
                {
                    for (int z = 0; z < zvol; z++)
                    {
                        int xx = xoff + x;
                        int yy = yoff + y;
                        int zz = zoff + z;

                        int id = data.Sample(xx, yy, zz);
                        if (id <= 0)
                        {
                            continue;
                        }

                        Vector3 vec = new Vector3(x, y, z);
                        //Add Back Face
                        if (data.Sample(xx, yy, zz + 1) <= 0)
                        {
                            AddMesh(ref mesh, 1, vec, id);
                        }
                        //Add Front Face
                        if (data.Sample(xx, yy, zz - 1) <= 0)
                        {
                            AddMesh(ref mesh, 0, vec, id);
                        }
                        //Add Top Face
                        if (data.Sample(xx, yy + 1, zz) <= 0)
                        {
                            AddMesh(ref mesh, 4, vec, id);
                        }

                        //Add Bottom Face
                        if (data.Sample(xx, yy - 1, zz) <= 0)
                        {
                            AddMesh(ref mesh, 5, vec, id);
                        }

                        //Add Left Face
                        if (data.Sample(xx + 1, yy, zz) <= 0)
                        {
                            AddMesh(ref mesh, 3, vec, id);
                        }

                        //Add Right Face
                        if (data.Sample(xx - 1, yy, zz) <= 0)
                        {
                            AddMesh(ref mesh, 2, vec, id);
                        }
                    }
                }
            }
        }
Beispiel #6
0
        public void GenerateMesh(IVoxelDataSource <VoxelData> source, Bounds bounds, out SimpleMesh mesh)
        {
            mesh = new SimpleMesh {
                RenderMaterial = Chunk.mcMaterial
            };
            // Create a new bounds object for the volume to work with
            Bounds volume = new Bounds();

            volume.SetMinMax(bounds.min, bounds.max + new Vector3(1, 1, 1));
            VoxelData[, ,] data = source.SampleSpace(volume);
            int index;


            IEnumerable <VoxelData> distinctElements = data.Cast <VoxelData>().Where(v => v.Data > 0).Distinct();

            int numTypes = distinctElements.Count();
            Dictionary <int, int> indices = new Dictionary <int, int>();


            for (int i = 0; i < numTypes; i++)
            {
                int blockID = distinctElements.First().Material;

                SimpleMesh submesh = new SimpleMesh {
                    RenderMaterial = Game.BlockRegistry[blockID].Data.RenderMaterial
                };
                mesh.Submeshes.Add(submesh);

                indices[blockID] = i;
                distinctElements = distinctElements.Skip(1);
            }

            SimpleMesh s = new SimpleMesh {
                RenderMaterial = Game.BlockRegistry[1].Data.RenderMaterial
            };

            mesh.Submeshes.Add(s);


            int[] idxs = new int[numTypes + 1];


            for (int x = 0; x < volume.size.x - 1; x++)
            {
                for (int y = 0; y < volume.size.y - 1; y++)
                {
                    for (int z = 0; z < volume.size.z - 1; z++)
                    {
                        int cubeIndex = 0;
                        if (data[x, y, z] != 0)
                        {
                            cubeIndex |= 16;
                        }
                        if (data[x + 1, y, z] != 0)
                        {
                            cubeIndex |= 32;
                        }
                        if (data[x, y + 1, z] != 0)
                        {
                            cubeIndex |= 1;
                        }
                        if (data[x + 1, y + 1, z] != 0)
                        {
                            cubeIndex |= 2;
                        }
                        if (data[x, y, z + 1] != 0)
                        {
                            cubeIndex |= 128;
                        }
                        if (data[x + 1, y, z + 1] != 0)
                        {
                            cubeIndex |= 64;
                        }
                        if (data[x, y + 1, z + 1] != 0)
                        {
                            cubeIndex |= 8;
                        }
                        if (data[x + 1, y + 1, z + 1] != 0)
                        {
                            cubeIndex |= 4;
                        }


                        int inx = data[x, y, z];
                        if (inx <= 0)
                        {
                            int dx = (cubeIndex & 32) == 32 ? 1 : 0;
                            int dy = (cubeIndex & 1) == 1 ? 1 : 0;
                            int dz = (cubeIndex & 128) == 128 ? 1 : 0;

                            inx = data[x + dx, y + dy, z + dz];
                        }
                        SimpleMesh m = mesh.Submeshes[numTypes];
                        int        l = numTypes;
                        if (inx > 0)
                        {
                            m = mesh.Submeshes[indices[inx]];
                            l = indices[inx];
                        }



                        int edgeVal = GenericTable.MC_Edges[cubeIndex];
                        if (edgeVal == 0)
                        {
                            continue;
                        }
                        Vector3[] points = new Vector3[12];

                        if ((edgeVal & 1) > 0)
                        {
                            points[0] = new Vector3(x + 0.5f, y + 1, z);
                        }
                        if ((edgeVal & 2) > 0)
                        {
                            points[1] = new Vector3(x + 1, y + 1, z + 0.5f);
                        }
                        if ((edgeVal & 4) > 0)
                        {
                            points[2] = new Vector3(x + 0.5f, y + 1, z + 1);
                        }
                        if ((edgeVal & 8) > 0)
                        {
                            points[3] = new Vector3(x, y + 1, z + 0.5f);
                        }
                        if ((edgeVal & 16) > 0)
                        {
                            points[4] = new Vector3(x + 0.5f, y, z);
                        }
                        if ((edgeVal & 32) > 0)
                        {
                            points[5] = new Vector3(x + 1, y, z + 0.5f);
                        }
                        if ((edgeVal & 64) > 0)
                        {
                            points[6] = new Vector3(x + 0.5f, y, z + 1);
                        }
                        if ((edgeVal & 128) > 0)
                        {
                            points[7] = new Vector3(x, y, z + 0.5f);
                        }
                        if ((edgeVal & 256) > 0)
                        {
                            points[8] = new Vector3(x, y + 0.5f, z);
                        }
                        if ((edgeVal & 512) > 0)
                        {
                            points[9] = new Vector3(x + 1, y + 0.5f, z);
                        }
                        if ((edgeVal & 1024) > 0)
                        {
                            points[10] = new Vector3(x + 1, y + 0.5f, z + 1);
                        }
                        if ((edgeVal & 2048) > 0)
                        {
                            points[11] = new Vector3(x, y + 0.5f, z + 1);
                        }

                        short[] tris = GenericTable.MC_Triangles[cubeIndex];

                        for (int i = 0; tris[i] != -1; i += 3)
                        {
                            Vector2 uv1 = new Vector2(points[tris[i]].x, points[tris[i]].z);
                            Vector2 uv2 = new Vector2(points[tris[i + 2]].x, points[tris[i + 2]].z);
                            Vector2 uv3 = new Vector2(points[tris[i + 1]].x, points[tris[i + 1]].z);

                            index = idxs[l];
                            m.Vertices.Add(points[tris[i]]);
                            m.Vertices.Add(points[tris[i + 2]]);
                            m.Vertices.Add(points[tris[i + 1]]);
                            m.UV1.Add(uv1);
                            m.UV1.Add(uv2);
                            m.UV1.Add(uv3);
                            m.Triangles.Add(index);
                            m.Triangles.Add(index + 1);
                            m.Triangles.Add(index + 2);
                            idxs[l] += 3;
                        }
                    }
                }
            }
        }
        private void MeshDone(SimpleMesh mesh)
        {
            Mesh m = mesh.CreateMesh();

            MeshRenderer r = GetComponent<MeshRenderer>();

            if (mesh.Submeshes.Count > 0)
            {
                r.materials = mesh.Submeshes.Select(me => me.RenderMaterial).ToArray();
            }
            else
            {
                r.material = mesh.RenderMaterial;
            }

            filter.sharedMesh = m;
            MeshCollider collider = (MeshCollider)this.collider;
            if(collider != null) collider.sharedMesh = m;
        }