Example #1
0
        protected override void MeshForCoord(ref MeshDetails details, int x, int y, int z, Chunk chunk)
        {
            ChunkPiece curr = null;
            ChunkPiece checking;

            try
            {
                curr = chunk.GetPieceRollover(x, y, z);
            }
            catch (NoChunkException nce)
            {
            }

            // if the current block is empty, skip it
            if (curr == null)
            {
                return;
            }

            // check each direction
            for (int i = 0; i < s_DirOffsets.GetLength(0); i++)
            {
                try
                {
                    checking = chunk.GetPieceRollover(
                        x + s_DirOffsets[i, 0],
                        y + s_DirOffsets[i, 1],
                        z + s_DirOffsets[i, 2]
                        );
                }
                catch (NoChunkException nce)
                {
                    if (!DrawEdgeChunkWalls)
                    {
                        // if there is no chunk there, don't draw the edge
                        continue;
                    }

                    checking = null;
                }

                // if the checking block is empty
                if (checking == null)
                {
                    // generate the desired face
                    Face(ref details, x, y, z, i, chunk.ChunkScale, curr);
                }
            }
        }
        private ChunkPiece GetNonNullVertex(ChunkPiece[] mvCorners, int edge)
        {
            ChunkPiece mvOut = null;

            // check both corners
            for (int i = 0; i < s_CubeEdgeConnection.GetLength(1); i++)
            {
                // if the assigned value is not null (ie, something was there)
                if ((mvOut = mvCorners[s_CubeEdgeConnection[edge, i]]) != null)
                {
                    // don't check other other vertex
                    break;
                }
            }

            return(mvOut);
        }
Example #3
0
        /// <summary>
        /// Sets the piece, with rollover.
        /// </summary>
        /// <returns>The piece rollover.</returns>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="z">The z coordinate.</param>
        /// <param name="val">Value.</param>
        public void SetPieceRollover(int x, int y, int z, ChunkPiece val)
        {
            try
            {
                this[x, y, z] = val;

                MakeDirty();
            }
            catch (OutOfChunkException ooce)
            {
                Vector3Int   worldCoord = ConvertChunkToWorldCoord(x, y, z);
                Vector3Int[] coordPair  = Parent.ConvertWorldCoordToGroupAndChunk(worldCoord);

                Chunk chunk = Parent[coordPair[0]];
                chunk[coordPair[1]] = val;
            }
        }
Example #4
0
        private void Face(ref MeshDetails details, int x, int y, int z, int arrIndex, float scale, ChunkPiece cp)
        {
            int i;

            // vertices
            for (i = 0; i < s_FaceCorners.GetLength(1); i++)
            {
                // the face array is used to access the given cube corner index
                Vector3 point = new Vector3(
                    x + s_CubeCorners[s_FaceCorners[arrIndex, i], 0],
                    y + s_CubeCorners[s_FaceCorners[arrIndex, i], 1],
                    z + s_CubeCorners[s_FaceCorners[arrIndex, i], 2]
                    ) * scale;

                details.AddPoint(point);
                details.AddTri(details.Tris.Count);

                details.AddUV(cp.GetUV(s_UVLookup[s_UVCorners[arrIndex, i]]));
            }

            // UVs

            // Normals?
        }
        private void MarchCube(ref MeshDetails md, int x, int y, int z, Chunk chunk)
        {
            // corners of the currently marching cube
            ChunkPiece[] mvCorners = new ChunkPiece[8];

            // store each corner in a local array
            for (int vertex = 0; vertex < s_CubeVertexOffset.GetLength(0) /* 8 */; vertex++)
            {
                try
                {
                    mvCorners[vertex] = chunk.GetPieceRollover(
                        x + s_CubeVertexOffset[vertex, 0],
                        y + s_CubeVertexOffset[vertex, 1],
                        z + s_CubeVertexOffset[vertex, 2]
                        );
                }
                catch (NoChunkException nce)
                {
                    if (!DrawEdgeChunkWalls)
                    {
                        return;
                    }

                    mvCorners[vertex] = null;
                }
            }

            // find which corners are/aren't inside the surface
            int flagIndex = 0;

            for (int vertexTest = 0; vertexTest < s_CubeVertexOffset.GetLength(0) /* 8 */; vertexTest++)
            {
                // if there is something there, set the bit
                if (mvCorners[vertexTest] == null)
                {
                    flagIndex |= 1 << vertexTest;
                }
            }

            // Find which edges intersect the surface
            int edgeFlags = s_CubeEdgeFlags[flagIndex];

            // if fully in/out of the surface, don't draw it
            if (edgeFlags == 0)
            {
                return;
            }

            Vector3[] edgeVerticies = new Vector3[12];
            Vector3[] edgeNorms     = new Vector3[12];
            Vector2[] edgeUVs       = new Vector2[12];
            Color[]   edgeCols      = new Color[12];
            float     offset;

            // find the edge intersection point along each relevant edge
            for (int edge = 0; edge < s_CubeEdgeConnection.GetLength(0) /* 8 */; edge++)
            {
                // if there was not an intersection
                if ((edgeFlags & (1 << edge)) == 0)
                {
                    // skip it
                    continue;
                }

                /*
                 * // some complicated thing to get it looking less blocky ...
                 * offset = GetOffset(
                 *  mvCorners[s_CubeEdgeConnection[edge, 0]] != null ? 1 : 0,
                 *  mvCorners[s_CubeEdgeConnection[edge, 1]] != null ? 1 : 0,
                 *  fTargetValue
                 * );
                 */
                offset = 0.5f;

                Vector3 vec = new Vector3(x, y, z);
                for (int i = 0; i < 3; i++)
                {
                    edgeVerticies[edge][i] = vec[i] +
                                             (s_CubeVertexOffset[s_CubeEdgeConnection[edge, 0], i]
                                              + offset * s_EdgeDirection[edge, i] * chunk.ChunkScale);
                }

                int        cnrIndex = GetNonNullVertexIndex(mvCorners, edge);
                ChunkPiece cp       = mvCorners[cnrIndex];

                // check again, just in case something has gone horribly wrong
                if (cp != null)
                {
                    edgeCols[edge] = cp.PieceColour;
                }

                // edgeNorms[edge] = GetNormal(edgeVerticies[edge].x, edgeVerticies[edge].y, edgeVerticies[edge].z);
            }

            Vector3 vertexOffset = Vector3.one * chunk.ChunkScale * 0.5f;

            // draw the relevant triangles. Up to 5 per cube.
            for (int tri = 0; tri < 5; tri++)
            {
                // no more triangles if this is true
                if (s_TriangleConnectionTable[flagIndex, 3 * tri] < 0)
                {
                    break;
                }

                int baseN = md.Points.Count;

                for (int cnr = 0; cnr < 3; cnr++)
                {
                    int vertIndex = s_TriangleConnectionTable[flagIndex, 3 * tri + cnr];

                    md.AddTri(baseN + cnr);
                    md.AddPoint(edgeVerticies[vertIndex] + vertexOffset);
                    md.AddUV(edgeUVs[vertIndex]);
                    md.AddColour(edgeCols[vertIndex]);
                }
            }
        }
Example #6
0
 /// <summary>
 /// Sets the piece, with rollover.
 /// </summary>
 /// <returns>The piece rollover.</returns>
 /// <param name="chunkCoord">Chunk coordinate.</param>
 /// <param name="val">Value.</param>
 public void SetPieceRollover(Vector3Int chunkCoord, ChunkPiece val)
 {
     SetPieceRollover(chunkCoord.x, chunkCoord.y, chunkCoord.z, val);
 }