Ejemplo n.º 1
0
    public static float[,] GenerateNoise(SettingsVariation noiseSettings, int seed, Vector2 offset)
    {
        Init();
        float halfX = noiseSettings.mapChunkSize - 1;
        float halfY = noiseSettings.mapChunkSize - 1;

        noiseLite.SetSeed(seed);
        noiseLite.SetNoiseType(noiseSettings.noiseType);
        noiseLite.SetFractalType(noiseSettings.fractalType);
        noiseLite.SetFractalLacunarity(noiseSettings.lacunarity);
        noiseLite.SetFractalOctaves(noiseSettings.octives);
        float[,] map = new float[noiseSettings.mapChunkSize, noiseSettings.mapChunkSize];
        for (int x = 0; x < noiseSettings.mapChunkSize; x++)
        {
            for (int y = 0; y < noiseSettings.mapChunkSize; y++)
            {
                float sampleX = (float)(x - halfX) / noiseSettings.scale + offset.x;
                float sampleY = (float)(y - halfY) / noiseSettings.scale + offset.y;
                map[x, y] = (noiseLite.GetNoise((float)(x + offset.x) / noiseSettings.scale, (float)(y - offset.y) / noiseSettings.scale) + 1) * 0.5f;
            }
        }



        return(map);
    }
Ejemplo n.º 2
0
    public MeshMatData GetGeneratedData(Vector2 centre, Vector2 coord)
    {
        heightTracker = new MinMaxTracker();

        float[,] map = new float[noiseSettings.mapChunkSize, noiseSettings.mapChunkSize];
        for (int i = 0; i < noiseSettings.SettingsVariations.Length; i++)
        {
            float[,] firstLayerValue = new float[noiseSettings.mapChunkSize, noiseSettings.mapChunkSize];
            if (noiseSettings.SettingsVariations.Length > 0)
            {
                firstLayerValue = NoiseGenerator.GenerateNoise(noiseSettings.SettingsVariations[0], seed, centre);
            }

            SettingsVariation sv = noiseSettings.SettingsVariations[i];
            if (sv.enabled)
            {
                float[,] newMap = NoiseGenerator.GenerateNoise(sv, seed, centre);
                for (int y = 0; y < newMap.GetLength(0); y++)
                {
                    for (int x = 0; x < newMap.GetLength(1); x++)
                    {
                        if (sv.noiseMode == NoiseMode.Add)
                        {
                            float mask = 1;

                            if (sv.useLayerAsMask)
                            {
                                mask = firstLayerValue[x, y];
                                if (sv.InverseLayer)
                                {
                                    mask = 1 - mask;
                                    if (mask <= sv.sampleBelow)
                                    {
                                        mask = 0;
                                    }
                                }
                                else
                                {
                                    if (mask <= sv.sampleBelow)
                                    {
                                        mask = 0;
                                    }
                                }
                            }
                            newMap[x, y] = Mathf.Max(0, newMap[x, y] - sv.minValue);
                            map[x, y]   += newMap[x, y] * sv.strength * mask;
                        }
                        else
                        if (sv.noiseMode == NoiseMode.Multiply)
                        {
                            map[x, y] *= newMap[x, y];
                        }
                        else
                        if (sv.noiseMode == NoiseMode.Subtract)
                        {
                            float mask = 1;

                            if (sv.useLayerAsMask)
                            {
                                mask = firstLayerValue[x, y];
                                if (sv.InverseLayer)
                                {
                                    mask = 1 - mask;
                                    if (mask <= sv.sampleBelow)
                                    {
                                        mask = 0;
                                    }
                                }
                            }
                            newMap[x, y] += Mathf.Min(0, -newMap[x, y] * sv.minValue);
                            map[x, y]    += newMap[x, y] * sv.strength * mask;
                        }
                    }
                }
            }
        }

        Debug.Log(centre);


        for (int x = 0; x < map.GetLength(0); x++)
        {
            for (int y = 0; y < map.GetLength(1); y++)
            {
                heightTracker.AddValue(map[x, y]);
            }
        }

        if (noiseSettings.mapType != MapType.None)
        {
            for (int x = 0; x < map.GetLength(0); x++)
            {
                for (int y = 0; y < map.GetLength(1); y++)
                {
                    float m1     = (map.GetLength(0) - 1) * 3f;
                    float m2     = (map.GetLength(1) - 1) * 3f;
                    float pointX = ((float)x + (centre.x)) / m1;
                    float pointY = ((float)y + (-centre.y)) / m2;


                    map[x, y] = map[x, y] * GradientMap.sampleGradient(pointY, pointX);
                }
            }
        }

        //SmoothenTerrain(map,1,1);
        map = SmoothenTerrain(map);


        colorGen.UpdateSettings(colorSettings);
        colorGen.UpdateHeight(heightTracker);
        colorGen.UpdateColors();
        return(new MeshMatData(map, colorSettings.terrainMaterial));
    }