コード例 #1
0
ファイル: VisualChunk.cs プロジェクト: muzzybear/VoxelCitadel
    public void RebuildMesh()
    {
        ChunkLightmap lightmap = _world.GetLightmap(_chunk.Key);

        var appearances = new System.Collections.Generic.Dictionary <BlockType, BlockAppearance>();

        // air
        appearances.Add(BlockType.Empty, new BlockAppearance());
        // stone
        BlockAppearance tmp = new BlockAppearance();

        for (int i = 0; i < 6; i++)
        {
            tmp.UVRects[i] = _atlas.Rects[0];
            tmp.Colors[i]  = new Color(0.4f, 0.4f, 0.4f);
        }
        appearances.Add(new BlockType(2), tmp);
        // dirt
        tmp = new BlockAppearance();
        for (int i = 0; i < 6; i++)
        {
            if (i != (int)BlockFace.Top)
            {
                tmp.UVRects[i] = _atlas.Rects[1];
            }
            else
            {
                tmp.UVRects[i] = _atlas.Rects[3];
            }
        }
        appearances.Add(new BlockType(1), tmp);
        // grass
        tmp = new BlockAppearance();
        for (int i = 0; i < 6; i++)
        {
            tmp.UVRects[i] = _atlas.Rects[2];
        }
        tmp.UVRects[(int)BlockFace.Top]    = _atlas.Rects[4];
        tmp.UVRects[(int)BlockFace.Bottom] = _atlas.Rects[1];

        appearances.Add(new BlockType(3), tmp);

        Profiler.BeginSample("RebuildMesh");
        for (int x = 0; x < 16; x++)
        {
            for (int y = 0; y < 16; y++)
            {
                for (int z = 0; z < 16; z++)
                {
                    BlockType block = _chunk.GetBlock(x, y, z);
                    if (block.isTransparent() == false && _chunk.IsBlockVisible(x, y, z))
                    {
                        BlockAppearance appearance;
                        if (!appearances.TryGetValue(block, out appearance))
                        {
                            appearance = new BlockAppearance();
                        }

                        CubeBuilder.buildCube(_meshBuilder, new Vector3i(x, y, z), _chunk, lightmap, appearance);
                    }
                }
            }
        }

        _mesh.Clear();
        _meshBuilder.BuildMesh(_mesh);
        _meshBuilder.Clear();
        _dirty = false;
        Profiler.EndSample();
    }