void GenerateNoise2D()
    {
        NoiseUtility.RandomSeed  = mRandomSeed;
        NoiseUtility.Octaves     = mOctaves;
        NoiseUtility.Persistence = mPersistence;
        NoiseUtility.Scale       = mScale;

        NoiseUtility.Noise2D func = null;

        switch (mType)
        {
        case eNoiseType.Perlin:
            NoiseUtility.InitPerlinTable();
            func = NoiseUtility.TiliedPerlinNoise2D;
            break;

        case eNoiseType.Voronoi:
            func = NoiseUtility.TiliedVoronoi2D;
            break;

        case eNoiseType.Cellular:
            func = NoiseUtility.TiliedCellularNoise2D;
            break;
        }

        Texture2D tex2D;

        NoiseUtility.CreateTexture(mTexSize, mTexPeriod, func, mFBM, out tex2D);

        SaveTexture <Texture2D>(tex2D);
    }
Beispiel #2
0
    public override List <GameObject> Process(List <GameObject> input)
    {
        if (null == input || 0 == input.Count)
        {
            return(input);
        }

        var offset    = new UniformRandomNumberGenerator(Randomizer.GenerateSeed()).Range(-1000, 1000);
        var model     = NoiseUtility.Generate(width, height, zoom, lacunarity, persistence, seaLevel, mountainHeight, mountainPeakHeight, offset);
        var heightMap = model.heightMap;

        for (var w = 0; w < width; ++w)
        {
            for (var h = 0; h < height; ++h)
            {
                heightMap[w, h] = Mathf.Floor(heightMap[w, h] * this.elevation);
            }
        }

        var result = new List <GameObject> ();

        for (var w = 0; w < width; ++w)
        {
            for (var h = 0; h < height; ++h)
            {
                if (0 == input.Count)
                {
                    break;
                }

                var go = input[0];
                input.RemoveAt(0);

                go.name = "world_block_" + w + "_" + h;

                var mr      = go.GetComponent <MeshRenderer> ();
                var extends = mr.bounds.size;
                go.transform.position = new Vector3(w * extends.x, heightMap[w, h], h * extends.z);

                var material = new Material(Shader.Find("Standard"));
                material.color    = model.texture.GetPixel(w, h);
                mr.sharedMaterial = material;

                result.Add(go);
            }
        }

        return(result);
    }
    private float _CountPerlinNoise(float xCoord, float yCoord)
    {
        float sample = 0f;

        sample += NoiseUtility.CountRecursivePerlinNoise(
            xCoord + _varietyStatus.XOffset,
            yCoord + _varietyStatus.YOffset,
            0,
            0,
            _para.SCALE,
            _para.FREQ_COUNT_TIMES,
            _para.FREQ_GROW_FACTOR);

        return(sample);
    }
    private IEnumerator _GenerateRandomLocalAreaMap()
    {
        for (int x = 0; x < _width; x++)
        {
            for (int y = 0; y < _height; y++)
            {
                float xCoord = (float)x / _width;
                float yCoord = (float)y / _height;
                int   idx    = MathUtility.MapIndex(x, y, _height);
                var   coord  = new Vector2(xCoord, yCoord);

                float sample = 0;

                for (int i = 0; i < _points.Count; i++)
                {
                    float distance = Vector2.Distance(_points[i], coord);
                    if (distance >= _para.LOCAL_AREA_RADIUS)
                    {
                        continue;
                    }

                    float upDegree = _para.LOCAL_AREA_RADIUS - distance;
                    float upHeight = _para.LOCAL_AREA_SCALE * upDegree;

                    float upNoise = NoiseUtility.CountRecursivePerlinNoise(
                        xCoord,
                        yCoord,
                        _para.LOCAL_XOFFSET,
                        _para.LOCAL_YOFFSET,
                        _para.LOCAL_SCALE,
                        _para.LOCAL_FREQ_COUNT_TIMES,
                        _para.LOCAL_FREQ_GROW_FACTOR);

                    sample += upNoise * upHeight;
                }

                _localAreaMap[idx] = sample;

                if (_sleepCount++ > SettingUtility.MapRestCount)
                {
                    yield return(null);

                    _sleepCount = 0;
                }
            }
        }
    }
Beispiel #5
0
    private IEnumerator _GenerateRandomLocalAreaMap(List <Vector2> points, float radius, float posOrNegfactor)
    {
        for (int x = 0; x < _width; x++)
        {
            for (int y = 0; y < _height; y++)
            {
                float xCoord = (float)x / _width;
                float yCoord = (float)y / _height;
                var   idx    = MathUtility.MapIndex(x, y, _height);
                var   coord  = new Vector2(xCoord, yCoord);

                float sample = 0;

                for (int i = 0; i < points.Count; i++)
                {
                    float distance = Vector2.Distance(points[i], coord);
                    if (distance >= radius)
                    {
                        continue;
                    }

                    float upDegree = radius - distance;

                    float upNoise = NoiseUtility.CountRecursivePerlinNoise(
                        xCoord,
                        yCoord,
                        _randomParam.LOCAL_XOFFSET,
                        _randomParam.LOCAL_YOFFSET,
                        _randomParam.LOCAL_SCALE,
                        _randomParam.LOCAL_FREQ_COUNT_TIMES,
                        _randomParam.LOCAL_FREQ_GROW_FACTOR);

                    sample += upNoise * upDegree * posOrNegfactor;
                }

                _localAreaMap[idx] += sample;

                if (_sleepCount++ > SettingUtility.MapRestCount)
                {
                    yield return(null);

                    _sleepCount = 0;
                }
            }
        }
    }
    private float _CountPerlinNoise(int idx, float xCoord, float yCoord)
    {
        float sample = 0f;

        sample += NoiseUtility.CountRecursivePerlinNoise(
            xCoord,
            yCoord,
            _xOffset,
            _yOffset,
            _para.SCALE,
            _para.FREQ_COUNT_TIMES,
            _para.FREQ_GROW_FACTOR);

        sample += _localAreaMap[idx];

        sample = MathUtility.CountPow(sample, _para.LOW_GROUND_FACTOR);
        sample = 1 - MathUtility.CountPow(1 - sample, _para.HIGH_MOUNTAIN_FACTOR);

        sample *= _CountSurroundDown(new Vector2(xCoord, yCoord));

        return(sample);
    }
Beispiel #7
0
    public static TerrainHeightMap Generate(int width, int height, float zoom, int lacunarity, float persistence, float seaLevel, float mountainHeight, float mountainPeakHeight, int offset)
    {
        var m1 = NoiseUtility.GeneratePerlinNoiseMap(width, height, CalcLayerFrequency(zoom, lacunarity, 0), offset);
        var m2 = NoiseUtility.GeneratePerlinNoiseMap(width, height, CalcLayerFrequency(zoom, lacunarity, 1), offset);
        var m3 = NoiseUtility.GeneratePerlinNoiseMap(width, height, CalcLayerFrequency(zoom, lacunarity, 2), offset);

        var heightMap = new float[width, height];

        var min               = 1f;
        var max               = 0f;
        var waterLevel        = seaLevel;
        var mountainLevel     = (1f - waterLevel) * mountainHeight + waterLevel;
        var mountainPeakLevel = (1f - mountainLevel) * mountainPeakHeight + mountainLevel;
        var c = new Color[width * height];

        for (var w = 0; w < width; ++w)
        {
            for (var h = 0; h < height; ++h)
            {
                var rought = m1[w, h] * Mathf.Pow(persistence, 0);
                var fine   = m2[w, h] * Mathf.Pow(persistence, 1);
                var detail = m3[w, h] * Mathf.Pow(persistence, 2);

                var total = rought + fine + detail;
                heightMap[w, h] = total;

                // used later to normalize the values
                min = total < min ? total : min;
                max = total > max ? total : max;
            }
        }

        // normalize the values and then elevate
        var intervall = max - min;

        for (var w = 0; w < width; ++w)
        {
            for (var h = 0; h < height; ++h)
            {
                var value      = heightMap[w, h];
                var normalized = (value - min) / intervall;

                heightMap[w, h] = normalized;
            }
        }

        // figure out the colors
        for (var w = 0; w < width; ++w)
        {
            for (var h = 0; h < height; ++h)
            {
                var total      = heightMap[w, h];
                var colorIndex = w + h * width;

                c[colorIndex] = CalculateColor(total, waterLevel, mountainLevel, mountainPeakLevel);
            }
        }

        var texture = new Texture2D(width, height);

        texture.SetPixels(c);
        texture.Apply();

        return(new TerrainHeightMap()
        {
            texture = texture,
            heightMap = heightMap
        });
    }