Example #1
0
    /// <summary>
    /// Build the mesh data
    /// </summary>
    public override void Execute()
    {
        mMeshData.Clear();
        byte[] endodedData = new byte[4];


        for (int x = 0; x < VoxelChunk.ChunkWidth; ++x)
        {
            for (int y = 0; y < VoxelChunk.ChunkHeight; ++y)
            {
                for (int z = 0; z < VoxelChunk.ChunkWidth; ++z)
                {
                    if (bIsAborted)
                    {
                        return;
                    }

                    VoxelInstance voxel = mChunk.Get(x, y, z);
                    VoxelInstance check = null;

                    // Ignore if collision disabled
                    if (voxel.bDisableCollision)
                    {
                        continue;
                    }


                    // Check all sides
                    // Up
                    if ((check = mChunk.Get(x, y + 1, z)) == null || check.bDisableCollision)
                    {
                        mMeshData.AddTopPanel(x, y, z, endodedData, true);
                    }
                    // Bottom
                    if ((check = mChunk.Get(x, y - 1, z)) != null && check.bDisableCollision)
                    {
                        mMeshData.AddBottomPanel(x, y, z, endodedData, true);
                    }

                    // Left
                    if ((check = mChunk.Get(x - 1, y, z)) != null && check.bDisableCollision)
                    {
                        mMeshData.AddLeftPanel(x, y, z, endodedData, true);
                    }
                    // Right
                    if ((check = mChunk.Get(x + 1, y, z)) != null && check.bDisableCollision)
                    {
                        mMeshData.AddRightPanel(x, y, z, endodedData, true);
                    }

                    // Back
                    if ((check = mChunk.Get(x, y, z + 1)) != null && check.bDisableCollision)
                    {
                        mMeshData.AddBackPanel(x, y, z, endodedData, true);
                    }
                    // Front
                    if ((check = mChunk.Get(x, y, z - 1)) != null && check.bDisableCollision)
                    {
                        mMeshData.AddFrontPanel(x, y, z, endodedData, true);
                    }
                }
            }
        }

        mMeshData.Cook();
    }
Example #2
0
    /// <summary>
    /// Build the mesh data
    /// </summary>
    public override void Execute()
    {
        mMeshData.Clear();

        // First 2 bits are the tile id
        // Second bit is the tile's exposure (How close is it to air 0-4; 0:Next to air, 4:No air within 3 voxels)
        byte[] endodedData = new byte[4];


        for (int x = 0; x < VoxelChunk.ChunkWidth; ++x)
        {
            for (int y = 0; y < VoxelChunk.ChunkHeight; ++y)
            {
                for (int z = 0; z < VoxelChunk.ChunkWidth; ++z)
                {
                    if (bIsAborted)
                    {
                        return;
                    }

                    VoxelInstance voxel       = mChunk.Get(x, y, z);
                    VoxelInstance check       = null;
                    bool          transparent = voxel.bIsTransparent;

                    // Ignore if not part of terrain
                    if (voxel.mType != VoxelType.Terrain)
                    {
                        continue;
                    }

                    byte[] tileBits;

                    // Use main tile for top tile
                    tileBits       = System.BitConverter.GetBytes(voxel.mMainTileID);
                    endodedData[0] = tileBits[0];
                    endodedData[1] = tileBits[1];
                    endodedData[2] = 0;


                    // Check all sides
                    // Up
                    if ((check = mChunk.Get(x, y + 1, z)) == null || check.mType != VoxelType.Terrain || transparent != check.bIsTransparent)
                    {
                        mMeshData.AddTopPanel(x, y, z, endodedData);
                    }

                    // Include top tile and work out how exposed this tile is (Will be later culled in rendering, if neccessary)
                    else
                    {
                        byte airExposure = 0;

                        // Work out the air exposure for this exposure voxel
                        if ((check = mChunk.Get(x, y + 1, z)) == null || check.mType == VoxelType.None || transparent != check.bIsTransparent)
                        {
                            airExposure = 0;
                        }
                        else if ((check = mChunk.Get(x, y + 2, z)) == null || check.mType == VoxelType.None || transparent != check.bIsTransparent)
                        {
                            airExposure = 1;
                        }
                        else if ((check = mChunk.Get(x, y + 3, z)) == null || check.mType == VoxelType.None || transparent != check.bIsTransparent)
                        {
                            airExposure = 2;
                        }
                        else if ((check = mChunk.Get(x, y + 4, z)) == null || check.mType == VoxelType.None || transparent != check.bIsTransparent)
                        {
                            airExposure = 3;
                        }
                        else
                        {
                            airExposure = 4;
                        }

                        // Check adjacent tiles
                        if (airExposure == 4)
                        {
                            for (byte c = 1; c <= 3; ++c)
                            {
                                if ((check = mChunk.Get(x + c, y, z)) != null && (check.mType == VoxelType.None || transparent != check.bIsTransparent))
                                {
                                    airExposure = c;
                                    break;
                                }
                                if ((check = mChunk.Get(x - c, y, z)) != null && (check.mType == VoxelType.None || transparent != check.bIsTransparent))
                                {
                                    airExposure = c;
                                    break;
                                }
                                if ((check = mChunk.Get(x, y, z + c)) != null && (check.mType == VoxelType.None || transparent != check.bIsTransparent))
                                {
                                    airExposure = c;
                                    break;
                                }
                                if ((check = mChunk.Get(x, y, z - c)) != null && (check.mType == VoxelType.None || transparent != check.bIsTransparent))
                                {
                                    airExposure = c;
                                    break;
                                }
                            }
                        }

                        endodedData[2] = airExposure;
                        mMeshData.AddTopPanel(x, y, z, endodedData);
                    }


                    // Switch to secondary tile
                    tileBits       = System.BitConverter.GetBytes(voxel.mSecondaryTileID);
                    endodedData[0] = tileBits[0];
                    endodedData[1] = tileBits[1];
                    endodedData[2] = 0;


                    // Left
                    if ((check = mChunk.Get(x - 1, y, z)) != null && (check.mType != VoxelType.Terrain || transparent != check.bIsTransparent))
                    {
                        mMeshData.AddLeftPanel(x, y, z, endodedData);
                    }
                    // Right
                    if ((check = mChunk.Get(x + 1, y, z)) != null && (check.mType != VoxelType.Terrain || transparent != check.bIsTransparent))
                    {
                        mMeshData.AddRightPanel(x, y, z, endodedData);
                    }

                    // Back
                    if ((check = mChunk.Get(x, y, z + 1)) != null && (check.mType != VoxelType.Terrain || transparent != check.bIsTransparent))
                    {
                        mMeshData.AddBackPanel(x, y, z, endodedData);
                    }
                    // Front
                    if ((check = mChunk.Get(x, y, z - 1)) != null && (check.mType != VoxelType.Terrain || transparent != check.bIsTransparent))
                    {
                        mMeshData.AddFrontPanel(x, y, z, endodedData);
                    }
                }
            }
        }

        mMeshData.Cook();
    }