GeneratePerlinNoise() public static method

public static GeneratePerlinNoise ( int seed, int octaveCount, float persistence, int noiseWidth, int noiseHeight ) : ].float[
seed int
octaveCount int
persistence float
noiseWidth int
noiseHeight int
return ].float[
Example #1
0
    void GenerateCloudNoise(ref Texture2D tex, int noiseWidth, int noiseHeight, int seed, int scale)
    {
        float[,] perlinNoise = PerlinNoise.GeneratePerlinNoise(seed, octaves, persistence, noiseWidth, noiseHeight);
        float noiseValue;

        for (int y = 0; y < noiseWidth; y++)
        {
            for (int x = 0; x < noiseHeight; x++)
            {
                noiseValue  = perlinNoise[x, y];
                noiseValue *= SimplexNoise.SeamlessNoise((float)x / (float)_texWidth, (float)y / (float)_texHeight, scale, scale, 0f);

                noiseValue = Mathf.Clamp(noiseValue, contrastLow, contrastHigh + contrastLow) - contrastLow;
                noiseValue = Mathf.Clamp(noiseValue, 0f, 1f);

                var   brightOff = Random.Range(-0.01f, 0.01f);
                float r         = Mathf.Clamp(CloudColor.r + brightOff, 0f, 1f);
                float g         = Mathf.Clamp(CloudColor.g + brightOff, 0f, 1f);
                float b         = Mathf.Clamp(CloudColor.b + brightOff, 0f, 1f);

                tex.SetPixel(x, y, new Color(r, g, b, noiseValue));
                tex.SetPixel(511 - x, y, new Color(r, g, b, noiseValue));
                tex.SetPixel(x, 511 - y, new Color(r, g, b, noiseValue));
                tex.SetPixel(511 - x, 511 - y, new Color(r, g, b, noiseValue));
            }
        }

        tex.Apply();
    }
    void InitializeBuffers()
    {
        VertCount = 10 * 10 * 10 * cubeMultiplier * cubeMultiplier * cubeMultiplier;

        // Set output buffer size.
        outputBuffer = new ComputeBuffer(VertCount, (sizeof(float) * 3) + (sizeof(int) * 6));
        mapBuffer    = new ComputeBuffer(VertCount, sizeof(int));

        int width  = 10 * cubeMultiplier;
        int height = 10 * cubeMultiplier;
        int depth  = 10 * cubeMultiplier;

        PerlinNoise.Seed = Seed;
        float[][] fmap = PerlinNoise.GeneratePerlinNoise(width, height, 8);
        //fmap = PerlinNoise.GeneratePerlinNoise(fmap, 8);

        int[] map = new int[VertCount];

        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                for (int z = 0; z < depth; z++)
                {
                    int idx = x + (y * 10 * cubeMultiplier) + (z * 10 * cubeMultiplier * 10 * cubeMultiplier);

                    if (fmap[x][z] >= y / (float)height)
                    {
                        map[idx] = 1;
                    }
                    else
                    {
                        map[idx] = 0;
                    }
                }
            }
        }

        mapBuffer.SetData(map);

        computeShader.SetBuffer(CSKernel, "outputBuffer", outputBuffer);
        computeShader.SetBuffer(CSKernel, "mapBuffer", mapBuffer);

        computeShader.SetVector("group_size", new Vector3(cubeMultiplier, cubeMultiplier, cubeMultiplier));

        if (DebugRender)
        {
            PointMaterial.SetBuffer("buf_Points", outputBuffer);
        }

        transform.position -= (Vector3.one * 10 * cubeMultiplier) * .5f;
    }
Example #3
0
    public float[,] GenerateClimateNoiseMap(int width, int height, Vector2Int chunkPos)
    {
        float[,] climateMap = PerlinNoise.GeneratePerlinNoise(width, height, seed, scale, 6, new Vector2(chunkPos.x, chunkPos.y) * world.chunkSize, persistance, lacunarity, step, PerlinNoise.NormalizeMode.Global);

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                climateMap[x, y] = Mathf.Clamp(climateMap[x, y] + averageTemperatureScaler, 0, 1);
            }
        }

        return(climateMap);
    }
Example #4
0
 // Use this for initialization
 void Start()
 {
     if (Instance != null)
     {
         Debug.LogError("There should never be two world controllers.");
     }
     Instance = this;
     World    = new World(width, height);
     float[][] perlinNoise = PerlinNoise.GeneratePerlinNoise(width, height, octave, frequency, persistance);
     //filterNoise = Filters.meanFilter (perlinNoise, width, height);
     //filterNoise = Filters.meanFilter (filterNoise, width, height);
     filterNoise = perlinNoise;
     placeTiles();
 }
Example #5
0
    // Randomly fill the chunk
    void FillChunk()
    {
        int maxHeight   = _CurChunk.sizeY;
        int octaveCount = 5;
        int _curHeight;

        float[][] perlinNoiseArray = new float[_CurChunk.sizeX][];

        // Calculate the perlinNoiseArray
        perlinNoiseArray = PerlinNoise.GeneratePerlinNoise(_CurChunk.sizeX, _CurChunk.sizeZ, octaveCount);

        for (int i = 0; i < _CurChunk.sizeX; i++)
        {
            for (int k = 0; k < _CurChunk.sizeZ; k++)
            {
                _curHeight = (int)(maxHeight * Mathf.Clamp01(perlinNoiseArray[i][k]));

                // Specify the type of block according to its height
                for (int j = 0; j < _curHeight; j++)
                {
                    if (j == _curHeight - 1)
                    {
                        _CurChunk.blocksArray[i, j, k] = 1; // Grass
                    }
                    else if (j == _curHeight - 2)
                    {
                        _CurChunk.blocksArray[i, j, k] = 3; // Sand
                    }
                    else
                    {
                        _CurChunk.blocksArray[i, j, k] = 2; // Rock
                    }
                    if (j > 8)
                    {
                        _CurChunk.blocksArray[i, j, k] = 2; // Grass
                    }
                }
            }
        }
    }
Example #6
0
    public float[,] GenerateHeightMap(int width, int height, Vector2Int chunkPos)
    {
        float[,] noiseMap = PerlinNoise.GeneratePerlinNoise(width, height, seed, scale, 6, new Vector2(chunkPos.x, chunkPos.y) * world.chunkSize, persistance, lacunarity, step, PerlinNoise.NormalizeMode.Global);

        // The below code basically adds a falloff to the height map, preventing the land from bordering the edges of the world
        int        falloffEdgeDistance   = 50;
        Vector2Int falloffCentreDistance = new Vector2Int(Mathf.RoundToInt(world.width / 2) - falloffEdgeDistance, Mathf.RoundToInt(world.height / 2) - falloffEdgeDistance);         // The distance from the centre of the world at which the islands height starts to fall off to 0

        chunkPos = chunkPos - new Vector2Int(Mathf.RoundToInt(world.width / 2), Mathf.RoundToInt(world.height / 2));
        chunkPos = new Vector2Int(Mathf.Abs(chunkPos.x), Mathf.Abs(chunkPos.y)) - new Vector2Int(falloffCentreDistance.x, falloffCentreDistance.y);

        Vector2Int maxValue            = new Vector2Int(Mathf.RoundToInt(world.width / 2), Mathf.RoundToInt(world.height / 2)) - new Vector2Int(falloffCentreDistance.x, falloffCentreDistance.y);
        Vector2    edgeClosenessVector = new Vector2((float)chunkPos.x / (float)maxValue.x, (float)chunkPos.y / (float)maxValue.y);      // Convert it to a value between 0 and 1

        if (edgeClosenessVector.x < 0)
        {
            edgeClosenessVector = new Vector2(0, edgeClosenessVector.y);
        }
        if (edgeClosenessVector.y < 0)
        {
            edgeClosenessVector = new Vector2(edgeClosenessVector.x, 0);
        }

        // Finding the length of a diagonal line
        // FORMULAE = A^2 = B^2 + C^2
        float edgeCloseness = (edgeClosenessVector.x * edgeClosenessVector.x) + (edgeClosenessVector.y * edgeClosenessVector.y);

        edgeCloseness = Mathf.Sqrt(edgeCloseness);

        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                noiseMap[x, y] = Mathf.Clamp01(noiseMap[x, y] - edgeCloseness);
            }
        }

        return(noiseMap);
    }
Example #7
0
    public void GenerateChunkFeatures(GameObject chunk, Vector2 chunkPos)
    { // Generates all of the resources and environmental features
        float[,] noise = PerlinNoise.GeneratePerlinNoise(CHUNK_SIZE, CHUNK_SIZE, 309742, 10, 6, chunkPos * CHUNK_SIZE, 0.5f, 2, 0, PerlinNoise.NormalizeMode.Global);

        for (int x = -CHUNK_SIZE / 2; x < CHUNK_SIZE / 2; x++)
        {
            for (int y = -CHUNK_SIZE / 2; y < CHUNK_SIZE / 2; y++)
            {
                foreach (GameObject feature in features)
                {
                    if (noise[x + CHUNK_SIZE / 2, y + CHUNK_SIZE / 2] > 0.6f)
                    {
                        GameObject newFeature = Instantiate(feature, Vector3.zero, Quaternion.identity);
                        newFeature.transform.SetParent(chunk.transform);
                        newFeature.transform.localPosition = new Vector2(x + 0.5f, y); // +0.5f to center the object in the grid cell.
                        float size = (float)rnd.Next(700, 1200) / 1000;
                        newFeature.transform.localScale = new Vector3(size, size, 1);
                        break;
                    }
                }
            }
        }
    }
Example #8
0
 public float[,] GenerateObjectNutrientsMap(int width, int height, Vector2Int chPunkPos)
 {
     return(PerlinNoise.GeneratePerlinNoise(width, height, seed, scale, 6, new Vector2(chunkPos.x, chunkPos.y) * world.chunkSize, persistance, lacunarity, step, PerlinNoise.NormalizeMode.Global));
 }
Example #9
0
        public void Generate()
        {
            DebugGame.Log("Zone", "Generate", "Génération de la zone");

            //Perlin
            int width       = Settings.ZoneWidth;
            int height      = Settings.ZoneHeight;
            int octaveCount = 5;

            float[][] perlinNoise = PerlinNoise.GeneratePerlinNoise(width, height, octaveCount);

            //var simplex = new OpenSimplexNoise();

            var terrainGrass = new Terrain(TypeTerrain.Grass);
            var terrainRock  = new Terrain(TypeTerrain.Rock);
            var terrainDirt  = new Terrain(TypeTerrain.Dirt);

            for (var line = 0; line < Height; line++)
            {
                for (var col = 0; col < Width; col++)
                {
                    //var altitude = (simplex.Evaluate(line, col))* 4f;
                    var altitude = (perlinNoise[line][col] - 0.5f) * 50f;

                    //if (altitude < -6)
                    //{
                    //    layer.Tiles[line, col] = new Tile()
                    //    {
                    //        X  = col,
                    //        Y = line,
                    //        Terrain = terrainDirt,
                    //        Water = 3,
                    //        Altitude = (int)altitude
                    //    };
                    //}
                    //else if (altitude < -4)
                    //{
                    //    layer.Tiles[line, col] = new Tile()
                    //    {
                    //        X = col,
                    //        Y = line,
                    //        Terrain = terrainDirt,
                    //        Water = 2,
                    //        Altitude = (int)altitude
                    //    };
                    //}
                    //else if (altitude < -2)
                    //{
                    //    layer.Tiles[line, col] = new Tile()
                    //    {
                    //        X = col,
                    //        Y = line,
                    //        Terrain = terrainDirt,
                    //        Water = 1,
                    //        Altitude = (int)altitude
                    //    };
                    //}
                    //else
                    if (altitude > 5)
                    {
                        LayerFloor.Tiles[line, col] = new Tile()
                        {
                            X        = col,
                            Y        = line,
                            Terrain  = terrainRock,
                            Water    = 0,
                            Altitude = (int)altitude
                        };
                    }
                    else if (altitude > 3)
                    {
                        LayerFloor.Tiles[line, col] = new Tile()
                        {
                            X        = col,
                            Y        = line,
                            Terrain  = terrainDirt,
                            Water    = 0,
                            Altitude = (int)altitude
                        };
                    }
                    else
                    {
                        LayerFloor.Tiles[line, col] = new Tile()
                        {
                            X        = col,
                            Y        = line,
                            Terrain  = terrainGrass,
                            Water    = 0,
                            Altitude = (int)altitude
                        };
                    }
                }
            }
        }