Example #1
0
        void GenerateBlocksThread(object chunkInstance)
        {
            Chunk chunk = chunkInstance as Chunk;

            ChunkSubspacePosition position;
            BlockSpacePosition    checkPosition;

            for (position.x = 0; position.x < SIZE; position.x++)
            {
                checkPosition.x = worldPosition.x * SIZE + position.x;
                for (position.z = 0; position.z < SIZE; position.z++)
                {
                    checkPosition.z = worldPosition.z * SIZE + position.z;
                    isShorelineCache[position.x, position.z] = true;

                    for (position.y = worldPosition.y * SIZE + SIZE; position.y < Configuration.HEIGHT; position.y++)
                    {
                        checkPosition.y = position.y;
                        Block checkBlock = ChunkRepository.GetBlockAtPosition(checkPosition);
                        if (checkBlock.IsActive() && checkBlock.IsNotTransparent())
                        {
                            isShorelineCache[position.x, position.z] = false;
                            break;
                        }
                    }
                }
            }

            BlockType[,,] blockTypes = worldGenerator.GenerateBlocks(chunk);

            for (position.x = 0; position.x < SIZE; position.x++)
            {
                for (position.y = 0; position.y < SIZE; position.y++)
                {
                    for (position.z = 0; position.z < SIZE; position.z++)
                    {
                        BlockDefinition blockDefinition = BlockDefinition.DefinitionOfType(blockTypes[position.x, position.y, position.z]);
                        chunk.SetBlock(position, blockDefinition, false);

                        if (blockDefinition.IsActive() && blockDefinition.IsNotTransparent() &&
                            blockDefinition.GetBlockType() != BlockType.Sand)
                        {
                            isShorelineCache[position.x, position.z] = false;
                        }

                        if (blockDefinition.IsLightEmitter())
                        {
                            BlockLight light;
                            light.chunk           = this;
                            light.chunkPosition   = position;
                            light.blockDefinition = blockDefinition;
                            lock (chunk.padlock) {
                                chunk.lights.Add(light);
                            }
                        }
                    }
                }
            }

            // Generate and apply the models from this and all nearby chunks
            List <Model> generatedModels = worldGenerator.GenerateModels(chunk);

            lock (generatingModelsLock) {
                for (int i = 0; i < generatedModels.Count; i++)
                {
                    AddModel(generatedModels[i]);
                }
            }

            List <Chunk> lockedChunks = LockNearbyChunkModels();

            ApplyModels();
            UnlockChunkModels(lockedChunks);

            // Cleanup pass
            for (position.x = 0; position.x < SIZE; position.x++)
            {
                for (position.z = 0; position.z < SIZE; position.z++)
                {
                    for (position.y = SIZE - 1; position.y >= 0; position.y--)
                    {
                        chunk.GetBlock(position);

                        // If the block is water, make sure it's surrounded on the bottom and sides
                        if (chunk.GetBlock(position).IsWater())
                        {
                            for (int i = 0; i < 5; i++)
                            {
                                ChunkSubspacePosition solidCheckPosition = position;
                                if (i == 0)
                                {
                                    solidCheckPosition.y -= 1;
                                }
                                else if (i == 1)
                                {
                                    solidCheckPosition.x -= 1;
                                }
                                else if (i == 2)
                                {
                                    solidCheckPosition.x += 1;
                                }
                                else if (i == 3)
                                {
                                    solidCheckPosition.z -= 1;
                                }
                                else if (i == 4)
                                {
                                    solidCheckPosition.z += 1;
                                }

                                if (solidCheckPosition.x >= 0 && solidCheckPosition.x < SIZE &&
                                    solidCheckPosition.y >= 0 && solidCheckPosition.y < SIZE &&
                                    solidCheckPosition.z >= 0 && solidCheckPosition.z < SIZE)
                                {
                                    if (chunk.GetBlock(solidCheckPosition).IsNotActive())
                                    {
                                        chunk.SetBlock(solidCheckPosition, BlockType.Stone, false);
                                    }
                                }
                                else
                                {
                                    BlockSpacePosition checkWorldPosition = solidCheckPosition.GetBlockSpacePosition(chunk);
                                    if (ChunkRepository.GetBlockAtPosition(checkWorldPosition).IsNotActive())
                                    {
                                        ChunkRepository.SetBlockAtPosition(checkWorldPosition, BlockType.Stone, false);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            LoadTransparencyCache();

            chunk.SetLoadState(ChunkLoadState.BlockGenerationComplete);
        }
Example #2
0
        /// <summary>
        /// Get a single face for the mesh setting.
        /// </summary>
        /// <param name='centerPosition'>
        /// Where the center of the block resides.
        /// </param>
        /// <param name='side'>
        /// Which side of the cube to add.
        /// </param>
        /// <param name='block'>
        /// The block that the face belongs to.
        /// </param>
        /// <param name='blockAbove'>
        /// Whether there is a block above the one being represented by the face.
        /// </param>
        /// <param name='blockBelow'>
        /// Whether there is a block below the one being represented by the face.
        /// </param>
        /// <param name='hasSunlight'>
        /// Whether sunlight is reaching the block.
        /// </param>
        private void AddFace(Vector3 centerPosition, CubeSide side, Block block, bool blockAbove, bool blockBelow,
                             List <BlockLight> lightList,
                             bool shadeTopLeft, bool shadeTopRight, bool shadeBottomLeft, bool shadeBottomRight)
        {
            Vector3 vertice;
            Vector3 normalDirection = Vector3.zero;

            Color lightColor;
            ChunkSubspacePosition subspaceSamplePosition;

            subspaceSamplePosition.x = (int)centerPosition.x;
            subspaceSamplePosition.y = (int)centerPosition.y;
            subspaceSamplePosition.z = (int)centerPosition.z;
            BlockSpacePosition worldSamplePosition =
                subspaceSamplePosition.GetBlockSpacePosition(associatedChunkMeshCluster.chunk);

            lightColor = RenderService.SampleLight(lightList, worldSamplePosition, side);

            if (side == CubeSide.Top)
            {
                normalDirection = Vector3.up;

                // 0, 1, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.Bottom)
            {
                normalDirection = Vector3.down;

                // 1, 0, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 0, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.North)
            {
                normalDirection = Vector3.back;

                // 0, 0, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 0, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.South)
            {
                normalDirection = Vector3.forward;

                // 1, 0, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.West)
            {
                normalDirection = Vector3.left;

                // 0, 0, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 1
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 0, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 0, 1, 0
                vertice.x = centerPosition.x - blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);
            }
            else if (side == CubeSide.East)
            {
                normalDirection = Vector3.right;

                // 1, 0, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 0
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z - blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 0, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y - blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);

                // 1, 1, 1
                vertice.x = centerPosition.x + blockMeshHalfSize;
                vertice.y = centerPosition.y + blockMeshHalfSize;
                vertice.z = centerPosition.z + blockMeshHalfSize;
                verticesList.Add(vertice);
            }

            trianglesList.Add((int)(meshArraysIndex * 4 + 0));
            trianglesList.Add((int)(meshArraysIndex * 4 + 1));
            trianglesList.Add((int)(meshArraysIndex * 4 + 2));
            trianglesList.Add((int)(meshArraysIndex * 4 + 1));
            trianglesList.Add((int)(meshArraysIndex * 4 + 3));
            trianglesList.Add((int)(meshArraysIndex * 4 + 2));


            // Determine whether this block is exposed directly to sunlight (nothing at all above it)
            byte sunlightValue = 255;
            BlockSpacePosition checkPosition = worldSamplePosition;

            while (checkPosition.y < Configuration.HEIGHT)
            {
                checkPosition.y++;
                Block checkBlock = ChunkRepository.GetBlockAtPosition(checkPosition);
                if (checkBlock.IsNotTransparent() && checkBlock.IsActive())
                {
                    sunlightValue = 0;
                    break;
                }
            }

            Vector2 textureCoordinates    = block.GetTextureCoordinates(side, blockAbove, blockBelow, sunlightValue);
            Vector2 overallTextureSize    = block.GetOverallTextureSize();
            Vector2 individualTextureSize = block.GetIndividualTextureSize();

            Vector2 lowerUVs, upperUVs;

            lowerUVs.x = textureCoordinates.x / overallTextureSize.x;
            lowerUVs.y = 1.0f - textureCoordinates.y / overallTextureSize.y;
            upperUVs.x = (textureCoordinates.x + individualTextureSize.x) / overallTextureSize.x;
            upperUVs.y = 1.0f - (textureCoordinates.y + individualTextureSize.y) / overallTextureSize.y;

            Vector2 uv;

            uv.x = lowerUVs.x;
            uv.y = upperUVs.y;
            uvList.Add(uv);
            uv.x = lowerUVs.x;
            uv.y = lowerUVs.y;
            uvList.Add(uv);
            uv.x = upperUVs.x;
            uv.y = upperUVs.y;
            uvList.Add(uv);
            uv.x = upperUVs.x;
            uv.y = lowerUVs.y;
            uvList.Add(uv);

            normalsList.Add(normalDirection);
            normalsList.Add(normalDirection);
            normalsList.Add(normalDirection);
            normalsList.Add(normalDirection);

            Color32 color;

            color.a = 255;
            color.r = (byte)(lightColor.r * 255);
            color.g = (byte)(lightColor.g * 255);
            color.b = (byte)(lightColor.b * 255);

            Color32 shadedColor;

            if (shadeTopLeft || shadeTopRight || shadeBottomLeft || shadeBottomRight)
            {
                HSBColor hsbShadedColor = HSBColor.FromColor(lightColor);
                hsbShadedColor.b = Mathf.Max(hsbShadedColor.b - 0.37f, 0.063f);
                shadedColor      = hsbShadedColor.ToColor();
            }
            else
            {
                shadedColor = color;
            }

            colorList.Add(shadeBottomLeft ? shadedColor : color);
            colorList.Add(shadeTopLeft ? shadedColor : color);
            colorList.Add(shadeBottomRight ? shadedColor : color);
            colorList.Add(shadeTopRight ? shadedColor : color);

            meshArraysIndex++;
        }