//GenerateVoxelMesh - for adding all voxel data into meshes of same type, and storing these meshes in meshList.
    void GenerateVoxelMesh()
    {
        int count = 0;

        //loop through z, y and x axis of data
        for (int z = 0; z < data.Height; z++)
        {
            for (int y = 0; y < data.Depth; y++)
            {
                for (int x = 0; x < data.Width; x++)
                {
                    int type = data.GetCell(x, y, z) - 1;
                    //ignore Previous Decay and Empty Space
                    if (
                        type != -1 && type != 7
                        )
                    {
                        count++;
                        int meshNum = GetFreeMesh(type);
                        MakeCube(adjScale, new Vector3((float)x * scale, (float)y * scale, (float)z * scale), x, y, z, data, type, meshNum); //now we make the cube.
                    }
                }
            }
        }
        Debug.Log(count);
    }
    void RadialAdd(VoxelData map, int range, Vector3 origin)
    {
        var rangef = (float)range;

        /*The basics is make sure that this loop doesn't
         * go out of bounds of range, map.data(AXIS) - 1 for any axis*/
        int minX = origin.x - 2 * rangef >= 0 ? (int)(origin.x - 2 * rangef) : 0;
        int maxX = origin.x + 2 * rangef < map.dataWidth ? (int)(origin.x + 2 * rangef) : map.dataWidth - 1;

        int minY = origin.y - 2 * rangef >= 0 ? (int)(origin.y - 2 * rangef) : 0;
        int maxY = origin.y + 2 * rangef < map.dataHeight ? (int)(origin.y + 2 * rangef) : map.dataHeight - 1;

        int minZ = origin.z - 2 * rangef >= 0 ? (int)(origin.z - 2 * rangef) : 0;
        int maxZ = origin.z + 2 * rangef < map.dataDepth ? (int)(origin.z + 2 * rangef) : map.dataDepth - 1;

        for (int x = minX; x <= maxX; ++x)
        {
            for (int y = minY; y <= maxY; ++y)
            {
                for (int z = minZ; z <= maxZ; ++z)
                {
                    //if this cell is "empty" and we're in range, set to 1
                    if (map.GetCell(x, y, z) <= -1 && Vector3.Distance(origin, new Vector3(x, y, z)) <= rangef)
                    {
                        map.SetCell(1, x, y, z);
                    }
                }
            }
        }
    }
Ejemplo n.º 3
0
    //This function generates the vertices and triangles based on the given data
    void GenerateChunk(VoxelData data, Vector3 chunkPos, int chunkSize, float cubeSize)
    {
        //Create a new list of vertices
        vertices = new List <Vector3>();
        //Create a new list of triangles
        triangles = new List <int>();

        uvs = new List <Vector2>();

        //Loop trough the x y and z coordinates
        for (int z = 0; z < chunkSize; z++)
        {
            for (int y = 0; y < chunkSize; y++)
            {
                for (int x = 0; x < chunkSize; x++)
                {
                    //If the cell is empty skip this cube
                    if (data.GetCell(chunkPos, x, y, z) == 0)
                    {
                        continue;
                    }
                    //If the cell isn't empty make a cube at this location
                    MakeCube(chunkSize, chunkPos, adjScale, new Vector3((float)x * cubeSize, (float)y * cubeSize, (float)z * cubeSize), x, y, z, data);
                }
            }
        }
    }
Ejemplo n.º 4
0
    public void GenerateChunkMesh(VoxelData data)
    {
        _vertices  = new List <Vector3>();
        _triangles = new List <int>();

        int chunkX = data.ChunkX;
        int chunkZ = data.ChunkZ;

        for (int x = _chunkSize * chunkX; x < _chunkSize * (chunkX + 1); x++)
        {
            for (int y = 0; y < data.Height; y++)
            {
                for (int z = _chunkSize * chunkZ; z < _chunkSize * (chunkZ + 1); z++)
                {
                    if (data.GetCell(x, y, z) == 0)
                    {
                        continue;
                    }

                    int renderOffsetX = chunkX * _chunkSize;
                    int renderOffsetZ = chunkZ * _chunkSize;

                    CreateCube(_adjScale, new Vector3(x * _scale - renderOffsetX, y * _scale, z * _scale - renderOffsetZ), x, y, z, data);
                }
            }
        }

        UpdateMesh();
    }
Ejemplo n.º 5
0
 void MakeCube(float cubeScale, Vector3 cubePos, Vector3Int pos, VoxelData data)
 {
     for (int i = 0; i < 6; i++)
     {
         if (data.GetNeighbor(pos, (Direction)i) == 0)
         {
             MakeFace((Direction)i, cubeScale, cubePos, data.GetCell(pos));
         }
     }
 }
Ejemplo n.º 6
0
 void GenerateVoxelMesh(VoxelData data)
 {
     vertices  = new List <Vector3>();
     triangles = new List <int>();
     for (int z = 0; z < data.Depth; z++)
     {
         for (int x = 0; x < data.Width; x++)
         {
             if (data.GetCell(x, z) == 0)
             {
                 continue;
             }
             MakeCube(adjScale, new Vector3((float)x * scale, 0, (float)z * scale), x, z, data);
         }
     }
 }
Ejemplo n.º 7
0
 void GenerateVoxelMesh(VoxelData data)
 {
     for (int z = 0; z < data.Depth; z++)
     {
         for (int y = 0; y < data.Height; y++)
         {
             for (int x = 0; x < data.Width; x++)
             {
                 if (data.GetCell(x, y, z) == 0)
                 {
                     continue;
                 }
                 MakeCube(adjScale, new Vector3((float)x * scale, (float)y * scale, (float)z * scale), x, y, z, data);
             }
         }
     }
 }
Ejemplo n.º 8
0
    public IEnumerator GenerateVoxelMesh(VoxelData data)
    {
        Debug.Assert(Context != null,
                     "Attempting to generate chunk mesh with no context specified");

        vertices  = new List <Vector3>();
        normals   = new List <Vector3>();
        uv        = new List <Vector2>();
        triangles = new List <int>();

        for (int z = 0; z < Depth; z++)
        {
            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    var index = new Vector3Int(x, y, z);

                    if (data.GetCell(Offset + index) == 0)
                    {
                        continue;
                    }

                    var chunkOrigin = new Vector3(
                        -Width * 0.5f,
                        -Height * 0.5f,
                        -Depth * 0.5f);

                    MakeCube(
                        cubeScale: Context.AdjustedScale,
                        cubePosition: chunkOrigin + new Vector3(
                            x * Context.Scale,
                            y * Context.Scale,
                            z * Context.Scale),
                        index: Offset + index,
                        data: data);
                }
            }
        }

        yield return(null);

        UpdateMesh();

        yield return(null);
    }
Ejemplo n.º 9
0
    //Create a voxel cubewith the correct scale at the given location based on the data
    void MakeCube(int chunkSize, Vector3 chunkPos, float tempScale, Vector3 cubePos, int x, int y, int z, VoxelData data)
    {
        cubePos.x += chunkPos.x * chunkSize;
        cubePos.y += chunkPos.y * chunkSize;
        cubePos.z += chunkPos.z * chunkSize;


        //Check all 6 sides
        for (int i = 0; i < 6; i++)
        {
            //If there is no neighbor in the direction of that size create a cube face
            if (data.GetNeighbor(chunkSize, chunkPos, x, y, z, (Direction)i) == 0)
            {
                //Create a cube face
                MakeFace((Direction)i, tempScale, cubePos, data.GetCell(chunkPos, x, y, z));
            }
        }
    }
Ejemplo n.º 10
0
    private void GenerateVoxelMesh(VoxelData voxelData)
    {
        trianglesList = new List <int>();
        verticesList  = new List <Vector3>();
        float vertexOffSet = cellSize * 0.5f;

        for (int i = 0; i < voxelData.Depth; i++)
        {
            for (int j = 0; j < voxelData.Width; j++)
            {
                if (voxelData.GetCell(j, i) == 0)
                {
                    continue;
                }
                else
                {
                    MakeCubeVoxel((Direction)i, vertexOffSet, new Vector3(j * vertexOffSet, cubeY * vertexOffSet, i * vertexOffSet), j, i, voxelData);
                }
            }
        }
    }
Ejemplo n.º 11
0
    public void GenerateVoxelMesh()
    {
        vertices  = new List <Vector3>();
        triangles = new List <int>();
        UVs       = new List <Vector2>();

        for (int y = 0; y < data.Height; y++)
        {
            for (int z = 0; z < data.Depth; z++)
            {
                for (int x = 0; x < data.Width; x++)
                {
                    if (data.GetCell(new Vector3Int(x, y, z)) == 0)
                    {
                        continue;
                    }
                    MakeCube(adjScale, new Vector3((float)x * scale, (float)y * scale, (float)z * scale), new Vector3Int(x, y, z), data);
                }
            }
        }
    }
Ejemplo n.º 12
0
    public void GenerateVoxelMesh(VoxelData data)
    {
        rendered   = true;
        _vertices  = new List <Vector3>();
        _triangles = new List <int>();

        for (var x = 0; x < chunkSize; x++)
        {
            for (var y = 0; y < chunkSize; y++)
            {
                for (var z = 0; z < chunkSize; z++)
                {
                    //var chunkPos = new Vector3Int(x + chunkX, y + chunkY, z + chunkZ);
                    if (data.GetCell(x, y, z, this) == 0)
                    {
                        continue;
                    }
                    MakeCube(_adjustedScale, new Vector3(x, y, z), x, y, z, data);
                }
            }
        }

        UpdateMesh();
    }
Ejemplo n.º 13
0
    void GenerateVoxelMesh(VoxelData data)
    {
        uvs       = new List <Vector2>();
        vertices  = new List <Vector3>();
        triangles = new List <int>();
        Vector3 offsetFromCenter = new Vector3(-data.Width * 0.5f, -data.Height * 0.5f, -data.Depth * 0.5f);

        for (int z = 0; z < data.Depth; z++)
        {
            for (int y = 0; y < data.Height; y++)
            {
                for (int x = 0; x < data.Width; x++)
                {
                    if (data.GetCell(x, y, z) == 0)
                    {
                        continue;
                    }

                    Vector3 pos = (new Vector3(x, y, z) * scale) + offsetFromCenter;
                    MakeCube(adjScale, pos, x, y, z, data);
                }
            }
        }
    }