Beispiel #1
0
    protected void Render(Chunk chunk)
    {
        Mesh mesh = ChunkCubesMesher.Mesh(chunk, this);

        MeshFilter cMesh = Instantiate <MeshFilter>(chunkMeshPrefab, transform);

        cMesh.mesh = mesh;

        chunkMeshes[chunk.indexX, chunk.indexY, chunk.indexZ] = cMesh;
    }
Beispiel #2
0
    public int ApplyRadialDamage(Chunk chunk, int radius, int voxX, int voxY, int voxZ)
    {
        // figure out bottom-left-back corner and top-right-forward corner indices
        // iterate triple-nested for-loop all chunks and destroy voxels within radius

        Vector3 center    = new Vector3(voxX, voxY, voxZ);
        Vector3 radialBox = Vector3.one * radius;

        Vector3 botLeftBackCorner     = center - radialBox;
        Vector3 topRightForwardCorner = center + radialBox;

        int xSize = chunkSizeXYZ[0];
        int ySize = chunkSizeXYZ[1];
        int zSize = chunkSizeXYZ[2];

        int chunksLeft = Mathf.FloorToInt(botLeftBackCorner.x / xSize);
        int chunksDown = Mathf.FloorToInt(botLeftBackCorner.y / ySize);
        int chunksBack = Mathf.FloorToInt(botLeftBackCorner.z / zSize);

        int botLeftBackCornerChunkX = Math.Min(chunk.indexX - chunksLeft, 0);
        int botLeftBackCornerChunkY = Math.Min(chunk.indexY - chunksDown, 0);
        int botLeftBackCornerChunkZ = Math.Min(chunk.indexZ - chunksBack, 0);

        int chunksRight   = Mathf.FloorToInt(topRightForwardCorner.x / xSize);
        int chunksUp      = Mathf.FloorToInt(topRightForwardCorner.y / ySize);
        int chunksForward = Mathf.FloorToInt(topRightForwardCorner.z / zSize);

        int topRightForwardCornerChunkX = Math.Min(chunk.indexX + chunksRight, chunks.GetLength(0) - 1);
        int topRightForwardCornerChunkY = Math.Min(chunk.indexY + chunksUp, chunks.GetLength(1) - 1);
        int topRightForwardCornerChunkZ = Math.Min(chunk.indexZ + chunksForward, chunks.GetLength(2) - 1);

        // debug box corner indices
        //Vector3 blb = new Vector3(botLeftBackCornerChunkX, botLeftBackCornerChunkY, botLeftBackCornerChunkZ);
        //Vector3 trf = new Vector3(topRightForwardCornerChunkX, topRightForwardCornerChunkY, topRightForwardCornerChunkZ);
        //Debug.Log(blb + "  " + trf);

        Vector3 centerVec = new Vector3(
            chunk.indexX * xSize + voxX,
            chunk.indexY * ySize + voxY,
            chunk.indexZ * zSize + voxZ);

        int amtDestroyed = 0;

        for (int x = botLeftBackCornerChunkX; x <= topRightForwardCornerChunkX; x++)
        {
            for (int y = botLeftBackCornerChunkY; y <= topRightForwardCornerChunkY; y++)
            {
                for (int z = botLeftBackCornerChunkZ; z <= topRightForwardCornerChunkZ; z++)
                {
                    Chunk currChunk = chunks[x, y, z];

                    for (int vx = 0; vx < xSize; vx++)
                    {
                        for (int vy = 0; vy < ySize; vy++)
                        {
                            for (int vz = 0; vz < zSize; vz++)
                            {
                                Vector3 voxVec = new Vector3(
                                    x * xSize + vx,
                                    y * ySize + vy,
                                    z * zSize + vz);

                                if (Vector3.Distance(centerVec, voxVec) <= radius && ChunkVoxelPresent(currChunk, vx, vy, vz))
                                {
                                    currChunk.typeGrid[vx, vy, vz] = 0;
                                    amtDestroyed++;
                                }
                            }
                        }
                    }

                    Mesh mesh = ChunkCubesMesher.Mesh(currChunk, this);
                    chunkMeshes[x, y, z].mesh = mesh;
                }
            }
        }

        return(amtDestroyed);
    }