Esempio n. 1
0
    private void SetOrAddTile(int x, int y, int z, CubeMaterialType materialType)
    {
        if (IsValidTilePosition(x, y, z))
        {
            SetTile(x, y, z, materialType);
        }
        else
        {
            int rootX = 0, rootY = 0, rootZ = 0;

            int newSizeX = Mathf.Max(sizeX, x + 1);
            int newSizeY = Mathf.Max(sizeY, y + 1);
            int newSizeZ = Mathf.Max(sizeZ, z + 1);

            if (x < 0)
            {
                rootX     = -x;
                x         = 0;
                newSizeX += rootX;
            }

            if (y < 0)
            {
                rootY     = -y;
                y         = 0;
                newSizeY += rootY;
            }

            if (z < 0)
            {
                rootZ     = -z;
                z         = 0;
                newSizeZ += rootZ;
            }

            byte[] newData = new byte[newSizeX * newSizeY * newSizeZ];

            for (int dz = 0; dz < sizeZ; dz++)
            {
                for (int dy = 0; dy < sizeY; dy++)
                {
                    for (int dx = 0; dx < sizeX; dx++)
                    {
                        newData[(dz + rootZ) * (newSizeX * newSizeY) + (dy + rootY) * (newSizeX) + (dx + rootX)] = data[dz * (sizeX * sizeY) + dy * (sizeX) + dx];
                    }
                }
            }

            newData[z * (newSizeX * newSizeY) + y * (newSizeX) + x] = (byte)materialType;

            Vector3 oldCubeCenter = MeshUtils.GetCubeCenter(sizeX, sizeY, sizeZ) + new Vector3(rootX, rootY, rootZ) * MeshUtils.TILE_SIZE; //  new Vector3((sizeX + rootX * 2.0f) * 0.5f - 0.5f, (sizeY + rootY * 2.0f) * 0.5f - 0.5f, (sizeZ + rootZ * 2.0f) * 0.5f - 0.5f);
            Vector3 newCubeCenter = MeshUtils.GetCubeCenter(newSizeX, newSizeY, newSizeZ);                                                 //  new Vector3(newSizeX * 0.5f - 0.5f, newSizeY * 0.5f - 0.5f, newSizeZ * 0.5f - 0.5f);
            this.transform.localPosition += Vector3.Scale(newCubeCenter - oldCubeCenter, transform.localScale);

            this.sizeX = newSizeX;
            this.sizeY = newSizeY;
            this.sizeZ = newSizeZ;
            this.data  = newData;
        }
    }
Esempio n. 2
0
 public void SetTile(int x, int y, int z, CubeMaterialType val)
 {
     data[z * (sizeX * sizeY) + y * (sizeX) + x] = (byte)val;
 }
Esempio n. 3
0
    protected override void UpdateMesh()
    {
        MeshUtils.InitStaticValues();

        Vector3[]      faceVectors     = MeshUtils.faceVectorsNormal;
        TilePosition[] faceNormalsTile = MeshUtils.faceNormalsTile;
        Vector3[]      faceNormals     = MeshUtils.faceNormals;

        mesh.Clear();

        vertices.Clear();
        normals.Clear();
        triangles.Clear();
        uvs.Clear();

        triangleCubeMap.Clear();
        triangleCubeFaceNumber.Clear();

        int index      = 0;
        int dataOffset = 0;

        Vector3 cubeCenter = MeshUtils.GetCubeCenter(sizeX, sizeY, sizeZ);

        for (int z = 0; z < sizeZ; z++)
        {
            for (int y = 0; y < sizeY; y++)
            {
                for (int x = 0; x < sizeX; x++)
                {
                    CubeMaterialType materialType = (CubeMaterialType)data[dataOffset++];

                    if (materialType == CubeMaterialType.Empty)
                    {
                        continue;
                    }

                    TilePosition pos    = new TilePosition(x, y, z);
                    Vector3      offset = new Vector3(x, y, z) * MeshUtils.TILE_SIZE - cubeCenter;

                    float fromTX = (1.0f / 8.0f) * (((int)materialType) % 8);
                    float toTX   = fromTX + (1.0f / 8.0f);

                    float fromTY = 1.0f - (1.0f / 8.0f) * (((int)materialType) / 8);
                    float toTY   = fromTY - (1.0f / 8.0f);

                    for (int face = 0; face < 6; face++)
                    {
                        TilePosition normalInt = faceNormalsTile[face];
                        TilePosition near      = pos + normalInt;

                        //We draw this face only if there isn't a visible tile in the direction of the face
                        if (!IsValidTilePosition(near.x, near.y, near.z) || GetTile(near.x, near.y, near.z) == CubeMaterialType.Empty)
                        {
                            Vector3 faceNormal = faceNormals[face];

                            //for (int i = 0; i < 4; i++)
                            //{
                            //    normals.Add(faceNormal);
                            //    vertices.Add(faceVectors[(face << 2) + i] + offset);
                            //}

                            //START MANUAL LOOP UNROLLING OF LOOP ABOVE
                            normals.Add(faceNormal);
                            normals.Add(faceNormal);
                            normals.Add(faceNormal);
                            normals.Add(faceNormal);

                            vertices.Add(faceVectors[(face << 2) + 0] + offset);
                            vertices.Add(faceVectors[(face << 2) + 1] + offset);
                            vertices.Add(faceVectors[(face << 2) + 2] + offset);
                            vertices.Add(faceVectors[(face << 2) + 3] + offset);
                            //END MANUAL LOOP UNROLLING

                            uvs.Add(new Vector2(fromTX, fromTY));
                            uvs.Add(new Vector2(fromTX, toTY));
                            uvs.Add(new Vector2(toTX, toTY));
                            uvs.Add(new Vector2(toTX, fromTY));

                            triangles.Add(index + 0);
                            triangles.Add(index + 1);
                            triangles.Add(index + 2);

                            triangles.Add(index + 2);
                            triangles.Add(index + 3);
                            triangles.Add(index + 0);

                            triangleCubeMap.Add(dataOffset - 1);
                            triangleCubeMap.Add(dataOffset - 1);

                            triangleCubeFaceNumber.Add((byte)face);
                            triangleCubeFaceNumber.Add((byte)face);

                            index += 4;
                        }
                    }
                }
            }
        }

        mesh.vertices  = vertices.ToArray();
        mesh.uv        = uvs.ToArray();
        mesh.normals   = normals.ToArray();
        mesh.triangles = triangles.ToArray();

        if (meshFilter)
        {
            meshFilter.sharedMesh = mesh;
        }

        if (meshCollider)
        {
            meshCollider.sharedMesh = null;
            meshCollider.sharedMesh = mesh;
        }
    }