예제 #1
0
    public static HeightMap GenerateHeightMap(int width, int height, HeightMapSettings settings, Vector2 sampleCenter)
    {
        float[,] values = Noise.GenerateNoiseMap(width, height, 1, settings.NoiseSettingsData.NoiseSettingsDataMerge, sampleCenter);
        values          = Noise.Clamp(values, settings.NoiseSettingsData);

        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));
    }
예제 #2
0
    public float GetAndSetPointInNoise(Vector2 samplePoint)
    {
        float[,] noise = Noise.GenerateNoiseMap(1, 1, 1, _noiseSettings.NoiseSettingsDataMerge, samplePoint);
        noise          = Noise.Clamp(noise, _noiseSettings);
        _value         = noise[0, 0] > _noiseStartPoint ? noise[0, 0] : 0;

        return(_value);
    }
예제 #3
0
    public void DrawMapInEditor()
    {
        //Used to store prefab objects in edit mode and delete them when changes are made (is a bit buggy)
        if (_biomeContainer != null)
        {
            DestroyImmediate(_biomeContainer.gameObject);
        }
        _biomeContainer = new GameObject("DELETE ME IF MANY OF ME").transform;

        //Apply material to mesh
        _textureData.ApplyToMaterial(_terrainMaterial);
        _textureData.UpdateMeshHeights(_terrainMaterial, _heightMapSettings.MinHeight, _heightMapSettings.MaxHeight);

        //Generate the heightmap for the chunk at origin
        HeightMap heightMap = HeightMapGenerator.GenerateHeightMap(_meshSettings.NumVertsPerLine, _meshSettings.NumVertsPerLine, _heightMapSettings, _chunkCoord);

        Vector2 sampleCenter = _chunkCoord * _meshSettings.MeshWorldSize / _meshSettings.MeshScale;

        float[,] noise1 = Noise.GenerateNoiseMap(_meshSettings.NumVertsPerLine * _noiseViewSize, _meshSettings.NumVertsPerLine * _noiseViewSize, 1, _noiseSettingsData_1.NoiseSettingsDataMerge, _chunkCoord);
        noise1          = Noise.Clamp(noise1, _noiseSettingsData_1);
        float[,] noise2 = Noise.GenerateNoiseMap(_meshSettings.NumVertsPerLine * _noiseViewSize, _meshSettings.NumVertsPerLine * _noiseViewSize, 1, _noiseSettingsData_2.NoiseSettingsDataMerge, _chunkCoord);
        noise2          = Noise.Clamp(noise2, _noiseSettingsData_2);

        float[,] noise = _noiseMergeType == NoiseMergeType.ONLY_FIRST ? noise1 : noise2;



        if (_drawMode == DrawMode.NOISE_MAP)
        {
            DrawTexture(TextureGenerator.TextureFromNoise(noise));
        }
        else if (_drawMode == DrawMode.MESH)
        {
            DrawMesh(MeshGenerator.GenerateTerrainMesh(heightMap.heightMap, _meshSettings, _editorPreviewLevelOfDetail));
        }
        else if (_drawMode == DrawMode.FALL_OF_MAP)
        {
            float[,] fallOf = (new HeightMap(FallofGenerator.GenerateFallofMap(_meshSettings.NumVertsPerLine), 1, 1).heightMap);
            DrawTexture(TextureGenerator.TextureFromNoise(fallOf));
        }
        else if (_drawMode == DrawMode.BIOME)
        {
            MeshData meshData = MeshGenerator.GenerateTerrainMesh(heightMap.heightMap, _meshSettings, _editorPreviewLevelOfDetail);
            DrawMesh(meshData);
            PrefabSpawner    prefabSpawner = new PrefabSpawner();
            List <SpawnInfo> spawnInfo     = prefabSpawner.SpawnOnChunk(2, 0, _biome, heightMap, meshData, _meshSettings, new Vector2(sampleCenter.x, -sampleCenter.y), _chunkCoord);
            prefabSpawner.SpawnSpawnInfo(spawnInfo, _biomeContainer, true);
        }
        else if (_drawMode == DrawMode.MAP)
        {
            TextureChunkData data = TextureGenerator.DrawMap(_meshSettings.NumVertsPerLine * _noiseViewSize, _mapSettings, new Vector2(sampleCenter.x, -sampleCenter.y), _chunkCoord);
            DrawTexture(TextureGenerator.TextureFromColorMap(data.colorMap, data.width, data.height));
        }
    }
예제 #4
0
    public static TextureChunkData DrawMap(int size, MapSettings mapSettings, Vector2 center, Vector2 chunkCoord, int noiseViewSize = 1)
    {
        float[,] heightNoise = Noise.GenerateNoiseMap(size * noiseViewSize, size * noiseViewSize, mapSettings.DetailLevel, mapSettings.HeightRegion.NoiseData.NoiseSettingsDataMerge, center);
        heightNoise          = Noise.Clamp(heightNoise, mapSettings.HeightRegion.NoiseData);

        float[][,] noises = new float[mapSettings.MapRegions.Length][, ];

        for (int i = 0; i < mapSettings.MapRegions.Length; i++)
        {
            noises[i] = Noise.GenerateNoiseMap(size * noiseViewSize, size * noiseViewSize, mapSettings.DetailLevel, mapSettings.MapRegions[i].NoiseData.NoiseSettingsDataMerge, center);
            noises[i] = Noise.Clamp(noises[i], mapSettings.MapRegions[i].NoiseData);
        }

        int width  = noises[0].GetLength(0);
        int height = noises[0].GetLength(0);

        Color[] colorMap = new Color[width * height];

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                float currentHeight = heightNoise[x, y];

                for (int h = 0; h < mapSettings.HeightRegion.Regions.Length; h++)
                {
                    if (currentHeight <= mapSettings.HeightRegion.Regions[h].height)
                    {
                        Color finalColor = mapSettings.HeightRegion.Regions[h].color;

                        for (int i = 0; i < noises.Length; i++)
                        {
                            if (noises[i][x, y] >= mapSettings.MapRegions[i].NoiseStartPoint && currentHeight > mapSettings.MapRegions[i].MinHeightStart)
                            {
                                finalColor   = finalColor.grayscale * mapSettings.MapRegions[i].Color;
                                finalColor.a = mapSettings.MapRegions[i].Color.a;
                                break;
                            }
                        }

                        colorMap[x * width + y] = finalColor;

                        break;
                    }
                }
            }
        }

        return(new TextureChunkData(chunkCoord, colorMap, width, height));
    }
예제 #5
0
    // Used to calculate all the different noises for every spawable
    public void Setup(float[,] parentNoise, int chunkSize, NoiseSettingsData offsetNoiseSettings, Vector2 center, Vector2 offsetNoiseOffset)
    {
        if (parentNoise != null)
        {
            _noise = Noise.MergeNoise(chunkSize, chunkSize, 1, _noiseSettingsData.NoiseSettingsDataMerge, parentNoise, _noiseMergeType, new Vector2(center.x, -center.y));
        }
        else
        {
            _noise = Noise.GenerateNoiseMap(chunkSize, chunkSize, 1, _noiseSettingsData.NoiseSettingsDataMerge, new Vector2(center.x, -center.y));
        }

        _noise = Noise.Clamp(_noise, _noiseSettingsData);

        _offsetNoise = Noise.GenerateNoiseMap(chunkSize, chunkSize, 1, offsetNoiseSettings.NoiseSettingsDataMerge, new Vector2(center.x, -center.y) + offsetNoiseOffset);
        _spreadNoise = Noise.GenerateNoiseMap(chunkSize, chunkSize, 1, offsetNoiseSettings.NoiseSettingsDataMerge, new Vector2(center.x, -center.y) + offsetNoiseOffset * 2);

        _prefabMaxProbability = SpawnablePrefab.GetMaxSize(_prefabs);

        for (int i = 0; i < _subSpawners.Length; i++)
        {
            _subSpawners[i].Setup(_noise, chunkSize, offsetNoiseSettings, new Vector2(center.x, -center.y), offsetNoiseOffset * 2);
        }
    }
예제 #6
0
    public Material MakeMaterial(int size, Vector2 pos, MeshRenderer renderer)
    {
        Material  _material = new Material(_shader);
        Texture2D noiseTex  = new Texture2D(size, size, TextureFormat.RGBA32, false);

        noiseTex.wrapMode = TextureWrapMode.Clamp;
        var colour = noiseTex.GetRawTextureData();

        Color[] colorMap = new Color[1];
        Task.Run(() =>
        {
            try
            {
                colorMap          = new Color[size * size];
                float[][,] noises = new float[LAYER_COUNT][, ];
                for (int i = 0; i < LAYER_COUNT; i++)
                {
                    if (_layers[i].GetNoise != null)
                    {
                        noises[i] = Noise.GenerateNoiseMap(size, size, _noiseDetailLevel, _layers[i].GetNoise.NoiseSettingsDataMerge, pos);
                        noises[i] = Noise.Clamp(noises[i], _layers[i].GetNoise);
                    }
                    else
                    {
                        noises[i] = new float[size, size];
                    }
                }
                float f = 0;
                int a   = 0, b = 0;
                int aa  = 0;
                int bb  = 0;
                //byte[] noiseByte = new byte[size * size];
                for (int y = 0; y < size; y++)
                {
                    for (int x = 0; x < size; x++)
                    {
                        float large = Math.Max(noises[0][x, y], noises[1][x, y]);
                        large       = Math.Max(large, noises[2][x, y]);
                        if (large > f)
                        {
                            f  = large;
                            aa = x;
                            bb = y;
                        }
                        //ThreadedDataRequester.AddToCallbackQueue(() => { Debug.Log(noises[1][x, y]); });
                        // colour[y * size + x] = (byte)(255 * noise[x, y]);
                        colorMap[y * size + x].r = noises[0][x, y];
                        colorMap[y * size + x].g = noises[1][x, y];
                        colorMap[y * size + x].b = noises[2][x, y];
                        colorMap[y * size + x].a = noises[3][x, y];
                    }
                }
                //colour = noiseByte;
                //Debug.Log(noiseByte[1]);
                ThreadedDataRequester.AddToCallbackQueue(() =>
                {
                    //noiseTex.LoadRawTextureData(colour);
                    noiseTex.SetPixels(colorMap);
                    noiseTex.Apply();
                    //MaterialPropertyBlock prop = new MaterialPropertyBlock();
                    //prop.SetTexture("_NoiseTextures", noiseTex);
                    //renderer.SetPropertyBlock(prop);
                    _material.SetTexture("_NoiseTexture", noiseTex);
                });
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
        });
        _material.SetTexture("_AltTex0", _layers[0].GetTexture);
        _material.SetTexture("_AltTex1", _layers[1].GetTexture);
        _material.SetTexture("_AltTex2", _layers[2].GetTexture);
        _material.SetTexture("_AltTex3", _layers[3].GetTexture);
        _material.SetTexture("_MainTex", _mainTex);
        _material.SetTexture("_Emission", _emission);
        _material.SetFloat("baseTextureScale", _mainTexScale);
        _material.SetFloatArray("startStep", _layers.Select(x => x.GetStepValues.startStep).ToArray());
        _material.SetFloatArray("endStep", _layers.Select(x => x.GetStepValues.endStep).ToArray());
        _material.SetFloatArray("altTextureScale", _layers.Select(x => x.GetTextureScale).ToArray());
        _material.SetColorArray("altTextureColour", _layers.Select(x => x.GetColour).ToArray());
        _material.SetFloat("mainTexStop", _mainTexStop);
        _material.SetFloat("mainTexStart", _mainTexStart);

        return(_material);
    }