Ejemplo n.º 1
0
 private int getLight(int worldX, int worldY, int worldZ)
 {
     return(region.getLight(worldX - this.orginX, worldY - this.orginY, worldZ - this.orginZ));
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Bakes the block meshes and light levels into the chunk.
        /// </summary>
        public void renderChunk()
        {
            CachedChunk3x3 cachedRegion = CachedChunk3x3.getNewRegion(this.world, this);

            if (!cachedRegion.allChunksLoaded())
            {
                // Waiting for the lazy chunk loading to finish...
                return;
            }

            this.isDirty = false;

            MeshBuilder meshBuilder = RenderManager.getMeshBuilder();

            Block currentBlock, neighborBlock;
            bool  isSolid;

            Block[]   surroundingBlocks = new Block[6];
            BlockPos  dirPos;
            int       x, y, z, i, facesCulled, x1, y1, z1, renderFaceMask, x2, y2, z2;
            Direction direction;

            // Bake blocks into mesh.
            for (x = 0; x < Chunk.SIZE; x++)
            {
                for (y = 0; y < Chunk.SIZE; y++)
                {
                    for (z = 0; z < Chunk.SIZE; z++)
                    {
                        currentBlock = this.getBlock(x, y, z);
                        if (currentBlock.renderer != null && currentBlock.renderer.bakeIntoChunks)
                        {
                            renderFaceMask = 0;

                            // Find the surrounding blocks and faces to cull.
                            facesCulled = 0;

                            for (i = 0; i < 6; i++)
                            {
                                direction = Direction.all[i];
                                dirPos    = direction.blockPos;
                                x1        = x + dirPos.x;
                                y1        = y + dirPos.y;
                                z1        = z + dirPos.z;

                                if (x1 < 0 || y1 < 0 || z1 < 0 || x1 >= Chunk.SIZE || y1 >= Chunk.SIZE || z1 >= Chunk.SIZE)
                                {
                                    neighborBlock = cachedRegion.getBlock(x1, y1, z1);
                                }
                                else
                                {
                                    neighborBlock = this.getBlock(x1, y1, z1);
                                }

                                isSolid = neighborBlock.isSolid;
                                if (!isSolid)
                                {
                                    renderFaceMask |= direction.renderMask;
                                }

                                if (currentBlock.renderer.lookupAdjacentBlocks)
                                {
                                    surroundingBlocks[i] = neighborBlock;
                                }

                                if (isSolid)
                                {
                                    facesCulled++;
                                }
                            }

                            // If at least one face is visible, render the block.
                            if (facesCulled != 6)
                            {
                                // Populate the meshData with light levels.
                                if (currentBlock.renderer.lookupAdjacentLight)
                                {
                                    for (x2 = -1; x2 <= 1; x2++)
                                    {
                                        for (y2 = -1; y2 <= 1; y2++)
                                        {
                                            for (z2 = -1; z2 <= 1; z2++)
                                            {
                                                x1 = x + x2;
                                                y1 = y + y2;
                                                z1 = z + z2;
                                                meshBuilder.setLightLevel(x2, y2, z2, x1 < 0 || y1 < 0 || z1 < 0 || x1 >= Chunk.SIZE || y1 >= Chunk.SIZE || z1 >= Chunk.SIZE ? cachedRegion.getLight(x1, y1, z1) : this.getLight(x1, y1, z1));
                                            }
                                        }
                                    }
                                }
                                else
                                {
                                    meshBuilder.setLightLevel(0, 0, 0, this.getLight(x, y, z));
                                }
                                // Render the block.
                                currentBlock.renderer.renderBlock(currentBlock, this.getMeta(x, y, z), meshBuilder, x, y, z, renderFaceMask, surroundingBlocks);
                            }
                        }
                    }
                }
            }

            // Set light UVs for tile entities that don't bake into the chunk.
            Material[] materials;
            Color      lightColor;

            foreach (TileEntityBase te in this.tileEntityDict.Values)
            {
                if (te is TileEntityGameObject)
                {
                    x          = te.posX - this.worldPos.x;
                    y          = te.posY - this.worldPos.y;
                    z          = te.posZ - this.worldPos.z;
                    materials  = ((TileEntityGameObject)te).modelMaterials;
                    lightColor = RenderManager.instance.lightColors.getColorFromBrightness(this.getLight(x, y, z));
                    for (i = 0; i < materials.Length; i++)
                    {
                        materials[i].SetColor(LightColors.SERIALIZED_LightColor, lightColor);
                    }
                }
            }

            this.filter.mesh = meshBuilder.getGraphicMesh();
            this.blockCollider.sharedMesh = meshBuilder.getColliderMesh();
        }