示例#1
0
    public byte GenBlockData(Vector3 globalPos)
    {
        int yPos = Mathf.FloorToInt(globalPos.y);

        /* IMMUTABLE PASS */

        // If outside world, return air.
        if (!worldData.IsVoxelInWorld(globalPos))
        {
            return(0);
        }

        // If bottom block of chunk, return bedrock.
        if (yPos == 0)
        {
            return(1);
        }

        /* BIOME SELECTION PASS*/

        int   solidGroundHeight   = 42;
        float sumOfHeights        = 0f;
        int   count               = 0;
        float strongestWeight     = 0f;
        int   strongestBiomeIndex = 0;

        for (int i = 0; i < GameAssets.Instance.biomes.Length; i++)
        {
            float weight = Noise.Get2DPerlin(new Vector2(globalPos.x, globalPos.z), GameAssets.Instance.biomes[i].GetTerrainNoiseOffset(), GameAssets.Instance.biomes[i].GetTerrainNoiseScale());

            // Keep track of which weight is strongest.
            if (weight > strongestWeight)
            {
                strongestWeight     = weight;
                strongestBiomeIndex = i;
            }

            // Get the height of the terrain (for the current biome) and multiply it by its weight.
            float height = GameAssets.Instance.biomes[i].GetTerrainHeight() * Noise.Get2DPerlin(new Vector2(globalPos.x, globalPos.z), 0, GameAssets.Instance.biomes[i].GetTerrainScale()) * weight;

            // If the height value is greater 0 add it to the sum of heights.
            if (height > 0)
            {
                sumOfHeights += height;
                count++;
            }
        }

        // Set biome to the one with the strongest weight.
        BiomeData biome = GameAssets.Instance.biomes[strongestBiomeIndex];

        // Get the average of the heights.
        sumOfHeights /= count;

        int terrainHeight = Mathf.FloorToInt(sumOfHeights + solidGroundHeight);

        /* BASIC TERRAIN PASS */

        byte voxelID;

        if (yPos == terrainHeight)
        {
            voxelID = biome.GetSurfaceBlock();
        }
        else if (yPos < terrainHeight && yPos > terrainHeight - 4)
        {
            voxelID = biome.GetSubSurfaceBlock();
        }
        else if (yPos > terrainHeight)
        {
            return(0);
        }
        else
        {
            voxelID = 2;
        }

        /* SECOND PASS */

        if (voxelID == 2)
        {
            foreach (DepositData deposit in biome.GetDeposits())
            {
                if (yPos > deposit.GetHeight().x&& yPos < deposit.GetHeight().y)
                {
                    if (Noise.Get3DPerlin(globalPos, deposit.GetOffset(), deposit.GetScale(), deposit.GetThreshold()))
                    {
                        voxelID = deposit.GetDepositId();
                    }
                }
            }
        }

        /* TREE PASS */

        //if ( yPos == terrainHeight && biome.placeMajorFlora ) {
        //	if ( Noise.Get2DPerlin( new Vector2( globalPos.x, globalPos.z ), 0, biome.majorFloraZoneScale ) > biome.majorFloraZoneThreshold ) {
        //		if ( Noise.Get2DPerlin( new Vector2( globalPos.x, globalPos.z ), 0, biome.majorFloraPlacementScale ) > biome.majorFloraPlacementThreshold ) {
        //			modifications.Enqueue( Structures.GenerateMajorFlora( biome.majorFloraIndex, globalPos, biome.height.x, biome.height.y ) );
        //		}
        //	}
        //}

        return(voxelID);
    }