예제 #1
0
    bool GenerateTiles(MapDataStore data, out CPUMesh tiles, out CPUMesh stencilTiles, out CPUMesh transparentTiles, out CPUMesh topTiles, out CPUMesh topStencilTiles, out CPUMesh topTransparentTiles, out CPUMesh collisionTiles, TempBuffers temp)
    {
        int block_x                = data.SliceOrigin.x / GameMap.blockSize;
        int block_y                = data.SliceOrigin.y / GameMap.blockSize;
        int block_z                = data.SliceOrigin.z;
        int bufferIndex            = 0;
        int stencilBufferIndex     = 0;
        int transparentBufferIndex = 0;
        int collisionIndex         = 0;

        for (int xx = (block_x * GameMap.blockSize); xx < (block_x + 1) * GameMap.blockSize; xx++)
        {
            for (int yy = (block_y * GameMap.blockSize); yy < (block_y + 1) * GameMap.blockSize; yy++)
            {
                if (!data.InSliceBounds(new DFCoord(xx, yy, block_z)))
                {
                    throw new UnityException("OOB");
                }
                if (data[xx, yy, block_z] == null)
                {
                    continue;
                }

                for (int i = 0; i < (int)MeshLayer.Count; i++)
                {
                    if (i < (int)MeshLayer.StaticCutout)
                    {
                        FillMeshBuffer(out temp.meshBuffer[bufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        bufferIndex++;
                    }
                    else if (i < (int)MeshLayer.StaticTransparent)
                    {
                        FillMeshBuffer(out temp.stencilMeshBuffer[stencilBufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        stencilBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.Collision)
                    {
                        FillMeshBuffer(out temp.transparentMeshBuffer[transparentBufferIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        transparentBufferIndex++;
                    }
                    else
                    {
                        FillMeshBuffer(out temp.collisionMeshBuffer[collisionIndex], (MeshLayer)i, data[xx, yy, block_z], GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize), yy - (block_y * GameMap.blockSize), -GameMap.MapZOffset));
                        collisionIndex++;
                    }
                }
            }
        }
        bool dontCare, success;

        stencilTiles        = MeshCombineUtility.ColorCombine(temp.stencilMeshBuffer, out dontCare, false);
        topStencilTiles     = MeshCombineUtility.ColorCombine(temp.stencilMeshBuffer, out dontCare, true);
        transparentTiles    = MeshCombineUtility.ColorCombine(temp.transparentMeshBuffer, out dontCare, false);
        topTransparentTiles = MeshCombineUtility.ColorCombine(temp.transparentMeshBuffer, out dontCare, true);
        topTiles            = MeshCombineUtility.ColorCombine(temp.meshBuffer, out dontCare, true);
        tiles          = MeshCombineUtility.ColorCombine(temp.meshBuffer, out success, false);
        collisionTiles = MeshCombineUtility.ColorCombine(temp.collisionMeshBuffer, out dontCare, false);

        return(success);
    }
예제 #2
0
    bool GenerateTiles(
        MapDataStore data,
        out CPUMesh tiles,
        out CPUMesh stencilTiles,
        out CPUMesh transparentTiles,
        out CPUMesh topTiles,
        out CPUMesh topStencilTiles,
        out CPUMesh topTransparentTiles,
        out CPUMesh collisionTiles,
        out CPUMesh terrainTiles,
        out CPUMesh topTerrainTiles)
    {
        int block_x                = data.SliceOrigin.x / GameMap.blockSize;
        int block_y                = data.SliceOrigin.y / GameMap.blockSize;
        int block_z                = data.SliceOrigin.z;
        int bufferIndex            = 0;
        int stencilBufferIndex     = 0;
        int transparentBufferIndex = 0;
        int collisionIndex         = 0;
        int terrainIndex           = 0;

        GameMap.BeginSample("Fill Mesh Buffer");
        for (int i = 0; i < (int)MeshLayer.Count; i++)
        {
            for (int xx = (block_x * GameMap.blockSize); xx < (block_x + 1) * GameMap.blockSize; xx++)
            {
                for (int yy = (block_y * GameMap.blockSize); yy < (block_y + 1) * GameMap.blockSize; yy++)
                {
                    if (!data.InSliceBounds(new DFCoord(xx, yy, block_z)))
                    {
                        throw new UnityException("OOB");
                    }
                    if (data[xx, yy, block_z] == null)
                    {
                        continue;
                    }

                    if (i < (int)MeshLayer.StaticCutout)
                    {
                        FillMeshBuffer(
                            out meshBuffer[bufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        bufferIndex++;
                    }
                    else if (i < (int)MeshLayer.StaticTransparent)
                    {
                        FillMeshBuffer(
                            out stencilMeshBuffer[stencilBufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        stencilBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.Collision)
                    {
                        FillMeshBuffer(
                            out transparentMeshBuffer[transparentBufferIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        transparentBufferIndex++;
                    }
                    else if (i < (int)MeshLayer.NaturalTerrain)
                    {
                        FillMeshBuffer(
                            out collisionMeshBuffer[collisionIndex],
                            (MeshLayer)i,
                            data[xx, yy, block_z],
                            GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                   yy - (block_y * GameMap.blockSize),
                                                   -GameMap.MapZOffset));
                        collisionIndex++;
                    }
                    else if (i == (int)MeshLayer.NaturalTerrain)
                    {
                        FillMeshBuffer(out terrainMeshBuffer[terrainIndex],
                                       (MeshLayer)i,
                                       data[xx, yy, block_z],
                                       GameMap.DFtoUnityCoord(xx - (block_x * GameMap.blockSize),
                                                              yy - (block_y * GameMap.blockSize),
                                                              -GameMap.MapZOffset));
                        terrainIndex++;
                    }
                }
            }
        }
        GameMap.EndSample();
        bool dontCare, success;

        GameMap.BeginSample("Generate Voxels");
        VoxelGenerator voxelGen = new VoxelGenerator();

        if (block_z == 0)
        {
            voxelGen.bottomless = true;
        }
        var naturalTerrain = voxelGen.Triangulate(data);

        GameMap.EndSample();
        GameMap.BeginSample("Combine Meshes");
        terrainTiles        = MeshCombineUtility.ColorCombine(terrainMeshBuffer, out dontCare, false, naturalTerrain);
        topTerrainTiles     = MeshCombineUtility.ColorCombine(terrainMeshBuffer, out dontCare, true);
        stencilTiles        = MeshCombineUtility.ColorCombine(stencilMeshBuffer, out dontCare, false);
        topStencilTiles     = MeshCombineUtility.ColorCombine(stencilMeshBuffer, out dontCare, true);
        transparentTiles    = MeshCombineUtility.ColorCombine(transparentMeshBuffer, out dontCare, false);
        topTransparentTiles = MeshCombineUtility.ColorCombine(transparentMeshBuffer, out dontCare, true);
        topTiles            = MeshCombineUtility.ColorCombine(meshBuffer, out dontCare, true);
        tiles          = MeshCombineUtility.ColorCombine(meshBuffer, out success, false);
        collisionTiles = MeshCombineUtility.ColorCombine(collisionMeshBuffer, out dontCare, false, naturalTerrain);
        GameMap.EndSample();

        return(success);
    }
예제 #3
0
    bool GenerateTiles(int block_x, int block_y, int block_z)
    {
        if (!blockDirtyBits[block_x, block_y, block_z])
        {
            return(true);
        }
        blockDirtyBits[block_x, block_y, block_z] = false;
        int bufferIndex        = 0;
        int stencilBufferIndex = 0;

        for (int xx = (block_x * blockSize); xx < (block_x + 1) * blockSize; xx++)
        {
            for (int yy = (block_y * blockSize); yy < (block_y + 1) * blockSize; yy++)
            {
                ////do lights first
                //if (tiles[xx, yy, block_z] != null)
                //{
                //    //do magma lights
                //    if ((xx % 1 == 0) && (yy % 1 == 0) && (block_z % 1 == 0))
                //        if (tiles[xx, yy, block_z].magma > 0 && magmaGlow[xx, yy, block_z] == null)
                //        {
                //            magmaGlow[xx, yy, block_z] = (Light)Instantiate(magmaGlowPrefab);
                //            magmaGlow[xx, yy, block_z].gameObject.SetActive(true);
                //            magmaGlow[xx, yy, block_z].transform.parent = this.transform;
                //            magmaGlow[xx, yy, block_z].transform.position = DFtoUnityCoord(xx, yy, block_z + 1);
                //        }
                //}

                if (meshBuffer == null)
                {
                    meshBuffer = new MeshCombineUtility.MeshInstance[blockSize * blockSize * (int)MeshLayer.StaticCutout];
                }
                if (stencilMeshBuffer == null)
                {
                    stencilMeshBuffer = new MeshCombineUtility.MeshInstance[blockSize * blockSize * ((int)MeshLayer.Count - (int)MeshLayer.StaticCutout)];
                }

                for (int i = 0; i < (int)MeshLayer.Count; i++)
                {
                    MeshLayer layer = (MeshLayer)i;
                    switch (layer)
                    {
                    case MeshLayer.StaticMaterial:
                    case MeshLayer.BaseMaterial:
                    case MeshLayer.LayerMaterial:
                    case MeshLayer.VeinMaterial:
                    case MeshLayer.NoMaterial:
                        FillMeshBuffer(out meshBuffer[bufferIndex], layer, tiles[xx, yy, block_z]);
                        bufferIndex++;
                        break;

                    case MeshLayer.StaticCutout:
                    case MeshLayer.BaseCutout:
                    case MeshLayer.LayerCutout:
                    case MeshLayer.VeinCutout:
                    case MeshLayer.Growth0Cutout:
                    case MeshLayer.Growth1Cutout:
                    case MeshLayer.Growth2Cutout:
                    case MeshLayer.Growth3Cutout:
                    case MeshLayer.NoMaterialCutout:
                        FillMeshBuffer(out stencilMeshBuffer[stencilBufferIndex], layer, tiles[xx, yy, block_z]);
                        stencilBufferIndex++;
                        break;

                    default:
                        break;
                    }
                }
            }
        }
        if (blocks[block_x, block_y, block_z] == null)
        {
            GameObject block = Instantiate(defaultMapBlock) as GameObject;
            block.SetActive(true);
            block.transform.parent            = this.transform;
            block.name                        = "terrain(" + block_x + ", " + block_y + ", " + block_z + ")";
            blocks[block_x, block_y, block_z] = block.GetComponent <MeshFilter>();
        }
        MeshFilter mf = blocks[block_x, block_y, block_z];

        if (mf == null)
        {
            Debug.LogError("MF is null");
        }
        if (mf.mesh == null)
        {
            mf.mesh = new Mesh();
        }
        mf.mesh.Clear();
        //mf.mesh.CombineMeshes(meshBuffer);
        if (stencilBlocks[block_x, block_y, block_z] == null)
        {
            GameObject stencilBlock = Instantiate(defaultStencilMapBLock) as GameObject;
            stencilBlock.SetActive(true);
            stencilBlock.transform.parent            = this.transform;
            stencilBlock.name                        = "foliage(" + block_x + ", " + block_y + ", " + block_z + ")";
            stencilBlocks[block_x, block_y, block_z] = stencilBlock.GetComponent <MeshFilter>();
        }
        MeshFilter mfs = stencilBlocks[block_x, block_y, block_z];

        if (mfs == null)
        {
            Debug.LogError("MFS is null");
        }
        if (mfs.mesh == null)
        {
            mfs.mesh = new Mesh();
        }
        mfs.mesh.Clear();
        MeshCombineUtility.ColorCombine(mfs.mesh, stencilMeshBuffer);
        return(MeshCombineUtility.ColorCombine(mf.mesh, meshBuffer));
        //Debug.Log("Generated a mesh with " + (mf.mesh.triangles.Length / 3) + " tris");
    }
예제 #4
0
    bool GenerateTiles(MapDataStore data, out MeshData tiles, out MeshData stencilTiles, TempBuffers temp)
    {
        int block_x            = data.SliceOrigin.x / GameMap.blockSize;
        int block_y            = data.SliceOrigin.y / GameMap.blockSize;
        int block_z            = data.SliceOrigin.z;
        int bufferIndex        = 0;
        int stencilBufferIndex = 0;

        for (int xx = (block_x * GameMap.blockSize); xx < (block_x + 1) * GameMap.blockSize; xx++)
        {
            for (int yy = (block_y * GameMap.blockSize); yy < (block_y + 1) * GameMap.blockSize; yy++)
            {
                if (!data.InSliceBounds(new DFCoord(xx, yy, block_z)))
                {
                    throw new UnityException("OOB");
                }
                if (data[xx, yy, block_z] == null)
                {
                    continue;
                }

                for (int i = 0; i < (int)MeshLayer.Count; i++)
                {
                    MeshLayer layer = (MeshLayer)i;
                    switch (layer)
                    {
                    case MeshLayer.StaticMaterial:
                    case MeshLayer.BaseMaterial:
                    case MeshLayer.LayerMaterial:
                    case MeshLayer.VeinMaterial:
                    case MeshLayer.NoMaterial:
                        FillMeshBuffer(out temp.meshBuffer[bufferIndex], layer, data[xx, yy, block_z].Value);
                        bufferIndex++;
                        break;

                    case MeshLayer.StaticCutout:
                    case MeshLayer.BaseCutout:
                    case MeshLayer.LayerCutout:
                    case MeshLayer.VeinCutout:
                    case MeshLayer.Growth0Cutout:
                    case MeshLayer.Growth1Cutout:
                    case MeshLayer.Growth2Cutout:
                    case MeshLayer.Growth3Cutout:
                    case MeshLayer.NoMaterialCutout:
                        FillMeshBuffer(out temp.stencilMeshBuffer[stencilBufferIndex], layer, data[xx, yy, block_z].Value);
                        stencilBufferIndex++;
                        break;

                    default:
                        break;
                    }
                }
            }
        }
        bool dontCare, success;

        stencilTiles = MeshCombineUtility.ColorCombine(temp.stencilMeshBuffer, out dontCare);
        tiles        = MeshCombineUtility.ColorCombine(temp.meshBuffer, out success);

        return(success);
    }