Пример #1
0
 private void DrawSettingsEditors()
 {
     if (terrainGenerator.worldSettings != null)
     {
         WorldSettings settings = terrainGenerator.worldSettings;
         DrawSettingsEditor(settings, ref terrainGenerator.worldFoldout, ref worldEditor);
         DrawSettingsEditor(settings.globalHeightMapSettings, ref settings.globalHeightMapSettingsFoldout, ref globalHeightMapEditor);
         DrawSettingsEditor(settings.meshSettings, ref settings.meshSettingsFoldout, ref meshSettingsEditor);
     }
 }
        /// <summary>
        /// Gets a 3x3 square surrounded by a border so a blur kernel can be applied correctly
        /// The center 'pixel' will represent the biome value for this whole chunk
        /// </summary>
        /// <param name="coord"></param>
        /// <param name="worldSettings"></param>
        /// <param name="extraBorderSize"></param>
        /// <returns></returns>
        private static Region GetBiomeMap(Vector2 coord, WorldSettings worldSettings, out int extraBorderSize)
        {
            // Based on max edge-smoothing kernel width, determine how many extra points we need to grab outside of this area to calculate smoothing correctly at edge of chunk
            float worldSmoothing = worldSettings.globalBiomeSettings.heightMapEdgeSmoothing;
            float maxSmoothing   = worldSmoothing;

            foreach (BiomeSettings biome in worldSettings.biomes)
            {
                float biomeSmoothing = worldSmoothing * biome.heightMapEdgeSmoothingModifier;
                if (biomeSmoothing > maxSmoothing)
                {
                    maxSmoothing = biomeSmoothing;
                }
            }
            float[] maxBlurKernel = StandardDeviation.GetKernel(maxSmoothing);
            extraBorderSize = maxBlurKernel.Length;// / 2;

            int     biomeRegionSize       = 3 + (2 * extraBorderSize);
            Vector2 biomeRegionStartPoint = new Vector2(coord.x - 1 - extraBorderSize, coord.y - 1 - extraBorderSize);

            return(worldSettings.biomeMapSettings == null ? new Region(new float[biomeRegionSize, biomeRegionSize], 0, 0) : RegionGenerator.GenerateRegion(biomeRegionSize, biomeRegionSize, worldSettings.biomeMapSettings, biomeRegionStartPoint));
        }
Пример #3
0
        public TerrainChunk(Vector2 coord, WorldSettings worldSettings, LODInfo[] detailLevels, int colliderLODIndex, Transform parent, Transform viewer)
        {
            this.coord            = coord;
            this.detailLevels     = detailLevels;
            this.colliderLODIndex = colliderLODIndex;
            this.worldSettings    = worldSettings;

            this.viewer = viewer;

            Vector2 position = coord * worldSettings.meshSettings.meshSizeInWorld;

            startPoint = position / worldSettings.meshSettings.meshScale;

            meshObject   = new GameObject("Terrain Chunk " + coord);
            meshRenderer = meshObject.AddComponent <MeshRenderer>();
            meshFilter   = meshObject.AddComponent <MeshFilter>();
            meshCollider = meshObject.AddComponent <MeshCollider>();

            meshObject.transform.position = new Vector3(position.x, 0, position.y);
            meshObject.transform.parent   = parent;
            SetVisible(false);

            lodMeshes = new LODMesh[detailLevels.Length];
            for (int lodIndex = 0; lodIndex < detailLevels.Length; lodIndex++)
            {
                lodMeshes[lodIndex] = new LODMesh(lodIndex, detailLevels[lodIndex].lod);
                lodMeshes[lodIndex].OnMeshDataUpdated += SetLodMesh;
                if (lodIndex == colliderLODIndex)
                {
                    lodMeshes[lodIndex].OnMeshDataUpdated += SetCollisionMesh;
                }
            }

            maxViewDst    = detailLevels[detailLevels.Length - 1].visibleDstThreshold;
            sqrMaxViewDst = (maxViewDst * maxViewDst);
        }
        public static ChunkData GenerateChunkData(Vector2 startPoint, Vector2 chunkCoord, WorldSettings worldSettings)
        {
            int    width  = worldSettings.meshSettings.numVertsPerLine;
            int    height = worldSettings.meshSettings.numVertsPerLine;
            int    extraBorderSize;
            Region biomeMap       = GetBiomeMap(chunkCoord, worldSettings, out extraBorderSize);
            Region worldHeightMap = worldSettings.globalHeightMapSettings == null ? new Region(new float[width, height], 0, 0) : RegionGenerator.GenerateRegion(width, height, worldSettings.globalHeightMapSettings, startPoint);

            float[,] combinedHeightMap = new float[width, height];
            List <int>  activeBiomes       = new List <int>();
            MinMaxFloat minMax             = new MinMaxFloat();
            Material    material           = null;
            float       worldEdgeSmoothing = worldSettings.globalBiomeSettings.heightMapEdgeSmoothing;

            // Add global world height
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    combinedHeightMap[x, y] += worldHeightMap.values[x, y];
                }
            }
            //string debug = "";

            // Add biome height
            foreach (BiomeSettings biome in worldSettings.biomes)
            {
                // @TODO: apply masking to materials/texturing, too (one material/shader for entire world?)
                if (material == null)
                {
                    material = biome.TerrainMaterial;
                }

                Region heightMap = RegionGenerator.GenerateRegion(width, height, biome.heightSettings, startPoint);

                /*
                 * if (Mathf.Approximately(biome.worldMapBiomeValue, 0f) && startPoint.x >= -150 && startPoint.x <= -50 && startPoint.y >= -150 && startPoint.y <= -50)
                 * {
                 *  debug = "chunkPosition" + startPoint + " ";
                 * }
                 */

                float[,] biomeBlurMask = CalculateBlurredBiomeMask(biomeMap.values, width, height, biome.worldMapBiomeValue, worldEdgeSmoothing * biome.heightMapEdgeSmoothingModifier);//, debug);


                minMax.AddValue(heightMap.minValue + worldHeightMap.minValue);
                minMax.AddValue(heightMap.maxValue + worldHeightMap.maxValue);
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        float biomeBlurValue = biomeBlurMask[x, y];
                        if (biomeBlurValue <= 0)
                        {
                            continue;
                        }
                        activeBiomes.Add(biome.id);
                        combinedHeightMap[x, y] += heightMap.values[x, y] * biomeBlurValue;
                    }
                }
            }

            return(new ChunkData
            {
                heightMap = new Region(combinedHeightMap, minMax.Min, minMax.Max),
                material = material
            });
        }