示例#1
0
    public static HeightMap GenerateHeightMap(int p_width, int p_height, HeightMapSettings p_settings, Vector2 p_sampleCentre)
    {
        // initializes the values based on the generated noise map
        float[,] values = Noise.GenerateNoiseMap(p_width, p_height, p_settings.noiseSettings, p_sampleCentre);

        // gets the data from the height map settings script's height curve
        AnimationCurve heightCurve_threadSafe = new AnimationCurve(p_settings.heightCurve.keys);

        // Default min and max value
        float minValue = float.MaxValue;
        float maxValue = float.MinValue;

        // Sets the min and max values based on the height and width of the values variable
        for (int i = 0; i < p_width; i++)
        {
            for (int j = 0; j < p_height; j++)
            {
                // values is set to be multiplied by the heightcuve data which is also multiplied by the height multiplier
                values[i, j] *= heightCurve_threadSafe.Evaluate(values[i, j]) * p_settings.heightMultiplier;
                if (values[i, j] > maxValue)
                {
                    maxValue = values[i, j];
                }
                if (values[i, j] < minValue)
                {
                    minValue = values[i, j];
                }
            }
        }

        // Returns the data
        return(new HeightMap(values, minValue, maxValue));
    }
示例#2
0
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, FalloffMap falloffMap, Vector2 sampleCenter)
    {
        float[,] values = NoiseGenerator.GeneratePerlinNoiseMap(width, height, settings.noiseSettings, sampleCenter);

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

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (settings.useFalloff)
                {
                    values[i, j] *= settings.heightCurve.Evaluate(values[i, j]) * settings.heightMultiplier * falloffMap.values[i, j];
                }
                else
                {
                    values[i, j] *= settings.heightCurve.Evaluate(values[i, j]) * 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));
    }
示例#3
0
    /* Constructors */
    public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings,
                        Transform parent, Transform viewerTransform, Material material)
    {
        this.coord             = coord;
        this.heightMapSettings = heightMapSettings;
        this.meshSettings      = meshSettings;
        viewer = viewerTransform;

        sampleCentre = coord * meshSettings.meshWorldSize / meshSettings.meshScale;
        Vector2 position = coord * meshSettings.meshWorldSize; // Calculate the position of this TerrainChunk

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

        // Instantiate the new object
        meshObject = new GameObject("Terrain Chunk: " + coord.ToString());
        meshObject.SetActive(false);
        GameObject.Instantiate(Resources.Load <GameObject>(Util.PathTo("Lake")), meshObject.transform);
        meshObject.layer = LayerMask.NameToLayer("Terrain");
        meshRenderer     = meshObject.AddComponent <MeshRenderer>();
        meshFilter       = meshObject.AddComponent <MeshFilter>();
        BoxCollider boxCollider = meshObject.AddComponent <BoxCollider>();

        boxCollider.size      = new Vector3(46f, 0.1f, 46f);
        meshRenderer.material = material;
        meshObject.isStatic   = true;

        // Set the GameObjects position and parent
        meshObject.transform.position = new Vector3(position.x, 0, position.y);
        meshObject.transform.parent   = parent;
    }
    /// <summary>
    /// Generates a height map based on the specified settings from a noise map.
    /// </summary>
    /// <param name="width">The width of the height map.</param>
    /// <param name="height">The length of the height map.</param>
    /// <param name="settings">Height settings to specify a height map.</param>
    /// <param name="sampleCenter">The center of the height map.</param>
    /// <returns>The generated height map.</returns>
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCenter)
    {
        // generates a noise map
        float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCenter);

        // gets an animation curve from the settings
        AnimationCurve threadSafeHeightCurve = new AnimationCurve(settings.heightCurve.keys);

        // used to pass the min and max height values into the height map
        float minValue = float.MaxValue;
        float maxValue = float.MinValue;

        // scales each coordinate in the map based on the settings
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                // scales the value based on the height curve and height multiplier
                values[i, j] *= threadSafeHeightCurve.Evaluate(values[i, j]) * settings.heightMultiplier;

                // stores the min and max values
                if (values[i, j] > maxValue)
                {
                    maxValue = values[i, j];
                }
                if (values[i, j] < minValue)
                {
                    minValue = values[i, j];
                }
            }
        }

        // creates and returns a height map from the values
        return(new HeightMap(values, minValue, maxValue));
    }
示例#5
0
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCentre)
    {
        var values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCentre, settings.heightMultiplier);


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

        for (var i = 0; i < width; i++)
        {
            for (var j = 0; j < height; j++)
            {
                if (values [i, j] > maxValue)
                {
                    maxValue = values [i, j];
                }
                if (values [i, j] < minValue)
                {
                    minValue = values [i, j];
                }
            }
        }

        return(new HeightMap(values, minValue, maxValue));
    }
    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;

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                values[i, j] *= heightCurve_threadsafe.Evaluate(values[i, j]) * 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));
    }
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, HeightMapSettings simplexSettings, Vector2 sampleCentre)
    {
        float[,] values  = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCentre);
        float[,] simplex = Noise.GenerateSimplexNoiseMap(width, height, simplexSettings.noiseSettings, sampleCentre);
        //float[,] falloffMap = FalloffGenerator.GenerateFalloffMap(width, settings.noiseSettings.seed);

        AnimationCurve heightCurve_threadSafe        = new AnimationCurve(settings.heightCurve.keys);
        AnimationCurve simplexHeightCurve_threadSafe = new AnimationCurve(simplexSettings.heightCurve.keys);

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

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                values[i, j] *= heightCurve_threadSafe.Evaluate(values[i, j] - simplex[i, j] * 0.5f) * 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));
    }
示例#8
0
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, MeshSettings meshSettings, Vector2 sampleCenter)
    {
        float[,] values     = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCenter);
        float[,] falloffMap = FalloffGenerator.GenerateFallofMap(meshSettings.numVertsPerLine);

        // create a new animation curve for each mesh so that weird spikes dont be spiky (fixes an animation curve 'optamization')
        AnimationCurve heightCurve_threadsafe = new AnimationCurve(settings.heightCurve.keys);

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

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (settings.useFalloff)
                {
                    values[i, j] = Mathf.Clamp01(values[i, j] - falloffMap[i, j]);
                }
                values[i, j] *= heightCurve_threadsafe.Evaluate(values[i, j]) * 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));
    }
示例#9
0
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCenter)
    {
        //get the base perlin noise we generate the heightmap from
        float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCenter);

        //the animation curve has some optimizations that cause it to give incorrect values if it is accessed by multiple threads at once
        //so in order to prevent this from happening, we make a copy
        AnimationCurve heightCurve_threadSafe = new AnimationCurve(settings.heightCurve.keys);

        //used for local normalization
        float minValue = float.MaxValue;
        float maxValue = float.MinValue;

        //evaluating the heightcurve on everything and fincing the min and max values
        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                values[i, j] = heightCurve_threadSafe.Evaluate(values[i, j]) * 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));
    }
示例#10
0
    public WaterChunk(GameObject waterObject, bool isOverWater, Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, WaterSettings waterSettings, LODInfo[] detailLevels, Transform parent, Transform viewer)
    {
        this.coord       = coord;
        this.viewer      = viewer;
        this.waterObject = waterObject;

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

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

        //+Custom+ Initializing the water
        if (waterSettings.hasWater)
        {
            if (isOverWater)
            {
                //water Create
                waterObject.name = "Water Chunk";
                waterObject.transform.position = new Vector3(position.x, waterSettings.waterLevel * heightMapSettings.heightMultiplier, position.y);
            }
            else
            {
                //underwater Create
                waterObject.transform.Rotate(0, 180, 180);
                waterObject.name = "UnderWater Chunk";
                waterObject.transform.position = new Vector3(position.x, (waterSettings.waterLevel * heightMapSettings.heightMultiplier) - 0.34f, position.y);
            }

            waterObject.transform.parent = parent;
        }
        maxViewDst = detailLevels[detailLevels.Length - 1].visibleDstThreshold;
    }
示例#11
0
    public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, int colliderLodIndex, Transform parent, Transform viewer, Material material)
    {
        this.Coord              = coord;
        this._colliderLodIndex  = colliderLodIndex;
        this._heightMapSettings = heightMapSettings;
        this._meshSettings      = meshSettings;
        this._viewer            = viewer;
        this.Biome              = BiomeGenerator.GenerateRelativeBiome(_unnormalizedCenter);

        _unnormalizedCenter = coord * meshSettings.meshWorldSize / meshSettings.meshScale;
        var position = coord * meshSettings.meshWorldSize;

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


        _meshObject = new GameObject("Terrain Chunk");
        _meshObject.transform.localScale = new Vector3(10.2f, 10.2f, 10.2f);

        _meshRenderer           = _meshObject.AddComponent <MeshRenderer>();
        _meshFilter             = _meshObject.AddComponent <MeshFilter>();
        _meshCollider           = _meshObject.AddComponent <MeshCollider>();
        _terrainChunkGameObject = _meshObject.AddComponent <TerrainChunkGameObject>();
        SetNeighboringChunks();
        _meshRenderer.material = material;

        _meshObject.transform.position = new Vector3(position.x, 0, position.y);
        _meshObject.transform.parent   = parent;
        SetVisible(false);
        _maxViewDst = 400f;
    }
示例#12
0
    public void GenerateHeightMap(HeightMapSettings settings, Vector2 sampleCenter)
    {
        this.sampleCenter = sampleCenter;

        GenerateNoise(settings.noiseSettings, sampleCenter);

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

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

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

                if (values[i, j] > maxValue)
                {
                    maxValue = values[i, j];
                }
                if (values[i, j] < minValue)
                {
                    minValue = values[i, j];
                }
            }
        }
    }
示例#13
0
    public TerrainChunk
    (
        Vector2 coord,
        HeightMapSettings heightMapSettings,
        MeshSettings meshSettings,
        LODInfo[] detailLevels,
        int colliderLODIndex,
        Transform parent,
        Transform viewer,
        GameManager gameManager,
        Material material,
        NoiseSettings oreNoiseSettings,         //last 3 are ore related
        OreType[] oreTypes,
        float oreSpawnThreshold
    )
    {
        this.coord             = coord;
        this.detailLevels      = detailLevels;
        this.colliderLODIndex  = colliderLODIndex;
        this.heightMapSettings = heightMapSettings;
        this.meshSettings      = meshSettings;
        this.viewer            = viewer;
        chunkOres = new ChunkOres(oreTypes, oreSpawnThreshold);



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

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



        meshObject            = new GameObject("Terrain Chunk " + coord);
        meshRenderer          = meshObject.AddComponent <MeshRenderer>();
        meshFilter            = meshObject.AddComponent <MeshFilter>();
        meshCollider          = meshObject.AddComponent <MeshCollider>();
        meshObject.layer      = 6;
        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;
        chunkOres.GenerateOres(coord, meshSettings.meshWorldSize, meshSettings.meshScale, oreNoiseSettings);
        gameManager.RunCoroutine(GenerateOreMeshes());
    }
示例#14
0
 public void load(HeightMapSettings settings)
 {
     for (int i = 0; i < biomes.Length; i++)
     {
         biomes[i].textureSettings.applyToMaterial(biomes[i].material);
         biomes[i].textureSettings.updateMeshHeights(biomes[i].material, settings.minHeight, settings.maxHeight);
     }
 }
示例#15
0
 public Region(HeightMapSettings heightMapSettings, Region east, Region west, Vector3 localisation, float power)
 {
     this.heightMapSettings = heightMapSettings;
     this.east         = east;
     this.west         = west;
     this.localisation = localisation;
     this.power        = power;
 }
示例#16
0
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCentre)
    {
        float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCentre);
        HeightMap falloffMap = new HeightMap(FalloffGenerator.GenerateFalloffMap(width), 0, 1);


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

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


        float offset = 200000;

        System.Random rand  = new System.Random((int)(sampleCentre.x + sampleCentre.y + offset));
        System.Random rand2 = new System.Random((int)(sampleCentre.x * (sampleCentre.y + offset)));
        float         val   = Mathf.PerlinNoise(((float)(rand.Next(0, 100)) / 61), ((float)(rand2.Next(0, 100)) / 61));

        if (sampleCentre.x == 0 && sampleCentre.y == 0)
        {
            val = 0f;
        }

        if (val >= settings.islandDensity)
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    values[i, j] = 0;
                }
            }

            maxValue = 0;
            minValue = 0;
        }
        else
        {
            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    values [i, j] *= heightCurve_threadsafe.Evaluate(values [i, j]) * settings.heightMultiplier * (1 - falloffMap.values[i, j]);

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

        return(new HeightMap(values, minValue, maxValue));
    }
示例#17
0
        // TODO: Not needed on Demo.
        //public const bool UseDebug = false;

        public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings,
                                                  Vector2 sampleCentre)
        {
            if (!settings.generate)
            {
                return(new HeightMap(GetEmptyData(width, height), 0, 0));
            }

            throw new NotImplementedException();
        }
    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);
        AnimationCurve falloffCurve_threadsafe = new AnimationCurve(settings.fallOffCurve.keys);

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

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

                if (settings.useFalloff)
                {
                    Vector2 falloffVector = new Vector2(sampleCentre.x + (i - width / 2), sampleCentre.y - (j - height / 2));

                    if (settings.multipleContinents)
                    {
                        falloffVector.x = Mathf.RoundToInt(falloffVector.x / (settings.noiseSettings.scale * 10)) * (settings.noiseSettings.scale * 10);
                        falloffVector.y = Mathf.RoundToInt(falloffVector.y / (settings.noiseSettings.scale * 10)) * (settings.noiseSettings.scale * 10);
                    }
                    else
                    {
                        falloffVector = Vector2.zero;
                    }

                    Vector2 falloffVector2 = new Vector2(sampleCentre.x + (i - width / 2), sampleCentre.y - (j - height / 2));


                    values[i, j] *= falloffCurve_threadsafe.Evaluate(Vector2.Distance(falloffVector2, falloffVector) / (settings.noiseSettings.scale * 4));
                }

                if (values[i, j] < 0)
                {
                    values[i, j] = 0;
                }

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


        return(new HeightMap(values, minValue, maxValue));
    }
示例#19
0
    float[,] CreateHeightMapLayer(HeightMapSettings settings, int seed, int mapWidth, int offsetX, int offsetY)
    {
        float widthFactor = (float)mapWidth / (float)maxWidth;

        float[,] noiseMap = Noise.GenerateNoiseMap(
            settings.noiseType,
            mapWidth,
            mapWidth,
            settings.noiseScale * widthFactor,
            offsetX,
            offsetY,
            settings.noiseOctaves,
            settings.persistence,
            settings.lacunarity,
            settings.noiseRedistributionFactor
            );

        float[,] falloffMap = null;
        if (settings.useFalloff)
        {
            falloffMap = Falloff.GenerateFalloffMap(mapWidth, mapWidth);
        }

        bool useHydraulicErosion = hydraulicErosion.settings.useHydraulicErosion;

        if (useHydraulicErosion && settings.mapDepth > 0)
        {
            noiseMap = hydraulicErosion.ErodeTerrain(noiseMap, seed);
        }

        float[,] heightMap = new float[mapWidth, mapWidth];

        // Determine map depth
        for (int z = 0; z < mapWidth; z++)
        {
            for (int x = 0; x < mapWidth; x++)
            {
                if (settings.useFalloff && settings.mapDepth > 0)
                {
                    noiseMap[x, z] = Mathf.Clamp01(noiseMap[x, z] - falloffMap[x, z]);
                }

                if (settings.mapDepth == 0)
                {
                    heightMap[x, z] = 1f;
                }
                else
                {
                    heightMap[x, z] = 2 * (noiseMap[x, z] * settings.mapDepth);
                }
            }
        }

        return(heightMap);
    }
示例#20
0
    public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int lodIndex, Transform viewer, Transform parent = null, Material material = null, System.Action <TerrainChunk, bool> visibiltyCallback = null)
    {
        this.coord             = coord;
        this.meshSettings      = meshSettings;
        this.heightMapSettings = heightMapSettings;
        this.viewer            = viewer;
        this.detailLevels      = detailLevels;
        heightMapReceived      = false;
        heightMapRequested     = false;

        maxViewDistance = detailLevels[detailLevels.Length - 1].visibleDistanceThreshold;

        sampleCenter = 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>();

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

        if (material != null)
        {
            meshRenderer.material = material;
        }

        LODColliderIndex = lodIndex;
        hasSetCollider   = false;

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

        SetVisible(false);


        if (visibiltyCallback != null)
        {
            OnVisibilityChanged += visibiltyCallback;
            heightMapRequested   = true;
            ThreadedDataRequester.RequestData(() => HeightMapGenerator.GenerateHeightMap(meshSettings.numVerticesPerLine, meshSettings.numVerticesPerLine, heightMapSettings, sampleCenter), OnHeightMapReceived);
        }
    }
 public TerrainGenerator(MeshSettings meshSettings, HeightMapSettings heightMapSettings, Transform viewer,
                         Material terrainMaterial, int maxMapSize)
 {
     _sceneObject           = new GameObject("TerrainGenerator");
     this.meshSettings      = meshSettings;
     this.heightMapSettings = heightMapSettings;
     this.viewer            = viewer;
     this.terrainMaterial   = terrainMaterial;
     this._maxMapSize       = maxMapSize;
     UpdateVisibleChunks();
 }
示例#22
0
        public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings,
                            MeshSettings meshSettings, TreeSettings treeSettings,
                            LevelSettings levelSettings, ClearingSettings clearingSettings, LODInfo[] detailLevels,
                            int colliderLODIndex, Transform parent, Transform viewer, Material material,
                            bool createEnemies)
        {
            this.coord = coord;

            _droidsRequested     = !createEnemies;
            _meshDatSentForTower = !createEnemies;

            _detailLevels      = detailLevels;
            _prevLODIndex      = -1;
            _colliderLODIndex  = colliderLODIndex;
            _heightMapSettings = heightMapSettings;
            _meshSettings      = meshSettings;
            _viewer            = viewer;

            _sampleCenter = coord * meshSettings.meshWorldSize / meshSettings.meshScale;
            var position = coord * meshSettings.meshWorldSize;

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

            _meshObject = new GameObject("Terrain Chunk");
            _meshObject.transform.position = new Vector3(position.x, 0, position.y);
            _meshObject.transform.SetParent(parent);
            _meshObject.layer = 11;
            _meshObject.tag   = TagManager.Terrain;

            _meshRenderer          = _meshObject.AddComponent <MeshRenderer>();
            _meshRenderer.material = material;
            _meshFilter            = _meshObject.AddComponent <MeshFilter>();
            _meshCollider          = _meshObject.AddComponent <MeshCollider>();

            _chunkTrees           = new Trees(position, treeSettings, clearingSettings);
            _terrainInteractibles = new TerrainInteractiblesCreator(position, _meshObject.transform,
                                                                    levelSettings, clearingSettings);

            SetVisible(false);

            _lodMeshes = new LODMesh[detailLevels.Length];
            for (var i = 0; i < detailLevels.Length; i++)
            {
                _lodMeshes[i] = new LODMesh(detailLevels[i].lod);
                _lodMeshes[i].UpdateCallback += UpdateTerrainChunk;
                if (i == _colliderLODIndex)
                {
                    _lodMeshes[i].UpdateCallback += UpdateCollisionMesh;
                }
            }

            _maxViewDistance = detailLevels[detailLevels.Length - 1].visibleDistanceThreshold;
        }
示例#23
0
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCenter)
    {
        float[,] values = Noise.GenerateNoiseMap(width, height, settings.NoiseSettings, sampleCenter);

        float[,] fallOff = new float[0, 0];
        float[,] canyon  = new float[0, 0];

        if (settings.UseFalloffMap)
        {
            fallOff = FallOffGenerator.GenerateFalloffMap(width);
        }

        if (settings.UseCanyonMap)
        {
            canyon = CanyonMapGenerator.GenerateCanyonMap(width);
        }

        AnimationCurve heighCurveThreadSafe = new AnimationCurve(settings.HeightCurve.keys);

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

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (settings.UseFalloffMap)
                {
                    values[i, j] -= fallOff[i, j];
                }

                if (settings.UseCanyonMap)
                {
                    values[i, j] += canyon[i, j];
                }

                values[i, j] *= heighCurveThreadSafe.Evaluate(values[i, j]) * 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));
    }
示例#24
0
    public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int colliderLODIndex, Transform parent, Transform viewer, Material material, bool anotherWorld, TerrainType[] arrayOfTerrainTypes)
    {
        this.isInAnotherWorld  = anotherWorld;
        this.coord             = coord;
        this.detailLevels      = detailLevels;
        this.colliderLODIndex  = colliderLODIndex;
        this.heightMapSettings = heightMapSettings;
        this.meshSettings      = meshSettings;
        this.viewer            = viewer;
        this.terrainType       = arrayOfTerrainTypes[Random.Range(0, arrayOfTerrainTypes.Length)];
        this.chunkType         = this.terrainType.chunkType;

        int chunRandomizer = Random.Range(0, 50);

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

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


        meshObject = new GameObject("Terrain Chunk");
        this.meshObject.transform.tag = "Terrain";
        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;
                lodMeshes[i].updateCallback += UpdateMeshData;
            }
        }

        maxViewDst = detailLevels[detailLevels.Length - 1].visibleDstThreshold;
        this.meshObject.isStatic = true;
    }
示例#25
0
        public HeightMapGenerator(HeightMapSettings settings)
        {
            if (Instance != null)
            {
                throw new System.Exception($"HeightMapGenerator is a singleton.");
            }

            Instance = this;

            this.settings          = settings;
            noise                  = new Noise(settings.noiseSettings);
            heightCurve_threadsafe = new AnimationCurve(settings.heightCurve.keys);
        }
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCentre, float jMultiplier)
    {
        float[,] values = Noise.GenerateNoiseMap(width, height, settings.noiseSettings, sampleCentre);
        // Debug.Log ("Width: "
        AnimationCurve heightCurve_threadsafe = new AnimationCurve(settings.heightCurve.keys);

        float minValue = float.MaxValue;
        float maxValue = float.MinValue;
        float multiplier;
        int   offset = 20;
        float tempi;
        float tempj;

        for (int i = 0; i < width; i++)
        {
            for (int j = 0; j < height; j++)
            {
                if (i > offset && i < width - offset && j > offset && j < height - offset)
                {
                    tempi = i / (width / 2.0f);
                    if (i > width / 2)
                    {
                        tempi = System.Math.Abs(i - width) / (width / 2);
                    }

                    tempj = j / (height / 2.0f);
                    if (tempj > height / 2)
                    {
                        tempj = System.Math.Abs(j - height) / (height / 2);
                    }
                    multiplier = heightCurve_threadsafe.Evaluate(values [i, j]) * settings.heightMultiplier + (jMultiplier * tempi * tempj);
                }
                else
                {
                    multiplier = heightCurve_threadsafe.Evaluate(values [i, j]) * settings.heightMultiplier;
                }
                values [i, j] *= multiplier;

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

        return(new HeightMap(values, minValue, maxValue));
    }
示例#27
0
    public TerrainChunk(Vector2 coord, WorldGenerator worldGenerator, HeightMapSettings heightMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int colliderLODIndex, Transform parent, Transform viewer, Material material)
    {
        this.detailLevels      = detailLevels;
        this.worldGenerator    = worldGenerator;
        this.colliderLODIndex  = colliderLODIndex;
        this.coord             = coord;
        this.heightMapSettings = heightMapSettings;
        this.meshSettings      = meshSettings;
        this.viewer            = viewer;

        sampleCenter = 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;


        int layerIdx = LayerMask.NameToLayer("Terrain");

        if (layerIdx == -1)
        {
            Debug.LogError("OceanDepthCache: Invalid layer specified: \"Terrain\". Please specify valid layers for objects/geometry that provide the ocean depth.");
        }

        meshObject.layer = layerIdx;

        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;
    }
示例#28
0
    public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int colliderLODIndex, Transform parent, Transform viewer, Material material, bool server)
    {
        this.coord              = coord;
        this.detailLevels       = detailLevels;
        this.colliderLODIndex   = colliderLODIndex;
        this.heightMapSettings  = heightMapSettings;
        this.meshSettings       = meshSettings;
        this.viewer             = viewer;
        this.server             = server;
        this.regionSize         = new Vector2(meshSettings.meshWorldSize, meshSettings.meshWorldSize);
        this.meshArrayDimension = (int)meshSettings.meshWorldSize + 1;
        this.meshDimension      = (int)meshSettings.meshWorldSize;

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

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


        meshObject   = new GameObject("Terrain Chunk");
        meshCollider = meshObject.AddComponent <MeshCollider>();

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

        if (!server)
        {
            meshFilter            = meshObject.AddComponent <MeshFilter>();
            meshRenderer          = meshObject.AddComponent <MeshRenderer>();
            meshRenderer.material = material;
            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;
        }
        else
        {
            lodMeshes    = new LODMesh[1];
            lodMeshes[0] = new LODMesh(detailLevels[0].lod);
            lodMeshes[0].updateCallback += ServerUpdateCallback;
        }
    }
示例#29
0
    public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, LODInfo[] detailLevels, int colliderLODindex, Transform parent, Transform viewer, Material meshMaterial, ObjectCreator objectCreator, float collisionGenerationThreshold)
    {
        this.coord            = coord;
        this.detailLevels     = detailLevels;
        this.colliderLODindex = colliderLODindex;
        this.objectCreator    = objectCreator;

        this.heightMapSettings = heightMapSettings;
        this.meshSettings      = meshSettings;
        this.viewer            = viewer;
        this.meshMaterial      = meshMaterial;

        // max view distance should be last detail level
        maxViewDst    = detailLevels[detailLevels.Length - 1].visibleDstThreshold;
        sampleCenter  = coord * meshSettings.meshWorldSize / meshSettings.terrainScale;
        chunkPosition = coord * meshSettings.meshWorldSize;
        bounds        = new Bounds(chunkPosition, Vector2.one * meshSettings.meshWorldSize);

        colliderGenrationThreshold = collisionGenerationThreshold;

        // Create plane
        meshObject     = new GameObject(GameConstants.TerrainChunkPrefix + coord.x + ":" + coord.y);
        meshObject.tag = GameConstants.TerrainChunkTag;
        meshObject.transform.parent   = parent;
        meshObject.transform.position = new Vector3(chunkPosition.x, 0, chunkPosition.y);

        meshFilter   = meshObject.AddComponent <MeshFilter>();
        meshRenderer = meshObject.AddComponent <MeshRenderer>();
        meshCollider = meshObject.AddComponent <MeshCollider>();

        navMeshModifier = meshObject.AddComponent <NavMeshModifierVolume>();
        //navMeshSurface.collectObjects = CollectObjects.Volume;
        //navMeshSurface.useGeometry = NavMeshCollectGeometry.PhysicsColliders;
        navMeshModifier.center = Vector3.zero;
        navMeshModifier.size   = new Vector3(meshSettings.meshWorldSize * 1.1f, heightMapSettings.maxHeight * 2, meshSettings.meshWorldSize * 1.1f);
        SetVisible(false);

        // Create LOD meshes for all levels of detail
        lodMeshes = new LODMesh[detailLevels.Length];
        for (int i = 0; i < detailLevels.Length; i++)
        {
            lodMeshes[i] = new LODMesh(detailLevels[i].lod);
            if (i == colliderLODindex)
            {
                lodMeshes[i].updateCallback += UpdateCollisionMesh;
                lodMeshes[i].updateCallback += UpdateTreeVisibility;
            }
            lodMeshes[i].updateCallback += UpdateTerrainChunk;
        }
    }
示例#30
0
    public TerrainChunk(Vector2 coord, HeightMapSettings heightMapSettings, MeshSettings meshSettings, LevelOfDetailInfo[] detailLevels, int colliderLODIndex, Transform parent, Material material)
    {
        this.coord             = coord;
        this.detailLevels      = detailLevels;
        this.colliderLODIndex  = colliderLODIndex;
        this.heightMapSettings = heightMapSettings;
        this.meshSettings      = meshSettings;

        sampleCenter = coord * meshSettings.meshWorldSize / meshSettings.meshScale;
        Vector2 position = coord * 10000;   // this is a random number, should probably fix it later >.<

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

        //meshObject = GameObject.CreatePrimitive(PrimitiveType.Plane);
        meshObject            = new GameObject("TerrainChunk");
        meshRenderer          = meshObject.AddComponent <MeshRenderer>();
        meshRenderer.material = material;
        meshFilter            = meshObject.AddComponent <MeshFilter>(); // when adding, returns object that was added
        meshCollider          = meshObject.AddComponent <MeshCollider>();

        meshObject.transform.position = new Vector3(position.x, meshYposition, position.y);
        meshObject.layer = 9;
        //meshObject.transform.localScale = Vector3.one * size / 10f; // default scale is 10 units (for planes)
        meshObject.transform.parent = parent;
        SetVisible(true);

        // make a mesh for each level of detail
        levelOfDetailMesh = new LevelOfDetailMesh(colliderLODIndex);
        //for (int i = 0; i < detailLevels.Length; i++)
        //{
        //levelOfDetailMeshes[i] = new LevelOfDetailMesh(detailLevels[i].levelOfDetail);
        //if (detailLevels[i].useForCollider) collisionLODMesh = levelOfDetailMeshes[i];

        // since no longer doing it in constructor
        //levelOfDetailMeshes[i].updateCallback += UpdateTerrainChunk;
        //if (i == colliderLODIndex) levelOfDetailMeshes[i].updateCallback += UpdateCollisionMesh;
        //}

        //maxViewDistance = detailLevels[detailLevels.Length - 1].visibleDstThreshold;

        this.heightMap = HeightMapGenerator.GenerateHeightMap(meshSettings.numVertsPerLine, meshSettings.numVertsPerLine, heightMapSettings, meshSettings, sampleCenter);

        levelOfDetailMesh.RequestMesh(heightMap, meshSettings);
        meshFilter.mesh         = levelOfDetailMesh.mesh;
        meshCollider.sharedMesh = levelOfDetailMesh.mesh;

        //mapGenerator.RequestHeightMap(sampleCenter, OnMapDataRecieved);
    }