Exemple #1
0
        public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int colliderLODIndex, Transform parent, Transform viewer, Material material)
        {
            this.coord             = coord;
            this.detailLevels      = detailLevels;
            this.colliderLODIndex  = colliderLODIndex;
            this.heightMapSettings = heightMapSettings;
            this.meshSettings      = meshSettings;
            this.viewer            = viewer;

            sampleCentre = coord * meshSettings.meshWorldSize / meshSettings.meshScale;
            Vector2 position = coord * meshSettings.meshWorldSize;

            bounds = new Bounds(position, Vector2.one * meshSettings.meshWorldSize);


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

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

            lodMeshes = new LODMesh[detailLevels.Length];
            for (int i = 0; i < detailLevels.Length; i++)
            {
                lodMeshes[i] = new LODMesh(detailLevels[i].lod);
                lodMeshes[i].updateCallback += UpdateTerrainChunk;
                if (i == colliderLODIndex)
                {
                    lodMeshes[i].updateCallback += UpdateCollisionMesh;
                }
            }

            maxViewDst = detailLevels[detailLevels.Length - 1].visibleDstThreshold;
        }
        public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCentre)
        {
            float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCentre);

            AnimationCurve heightCurve_threadsafe = new AnimationCurve(settings.heightCurve.keys);

            float minValue = float.MaxValue;
            float maxValue = float.MinValue;

            if (settings.useFalloff)
            {
                if (falloffMap == null)
                {
                    falloffMap = FalloffGenerator.GenerateFalloffMap(width);
                }
            }

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    values [i, j] *= heightCurve_threadsafe.Evaluate(values [i, j] - (settings.useFalloff ? falloffMap[i, j] : 0)) * settings.heightMultiplier;

                    if (values[i, j] > maxValue)
                    {
                        maxValue = values[i, j];
                    }
                    if (values[i, j] < minValue)
                    {
                        minValue = values[i, j];
                    }
                }
            }

            return(new HeightMap(values, minValue, maxValue));
        }