Answers for "unity 2d perlin noise"

C#
9

perlin noise unity

// HOW TO IMPLEMENT A PERLIN NOISE FONCTION TO GET A TERRAIN HEIGHT IN UNITY
using UnityEngine;

public static class NoiseCreator
{  
  /// scale : The scale of the "perlin noise" view
  /// heightMultiplier : The maximum height of the terrain
  /// octaves : Number of iterations (the more there is, the more detailed the terrain will be)
  /// persistance : The higher it is, the rougher the terrain will be (this value should be between 0 and 1 excluded)
  /// lacunarity : The higher it is, the more "feature" the terrain will have (should be strictly positive)
  public static float GetNoiseAt(int x, int z, float scale, float heightMultiplier, int octaves, float persistance, float lacunarity)
  {
      float PerlinValue = 0f;
      float amplitude = 1f;
      float frequency = 1f;

      for(int i = 0; i < octaves; i++)
      {
      	 // Get the perlin value at that octave and add it to the sum
		 PerlinValue += Mathf.PerlinNoise(x * frequency, z * frequency) * amplitude;
         
         // Decrease the amplitude and the frequency
         amplitude *= persistance;
         frequency *= lacunarity;
      }
      
      // Return the noise value
      return PerlinValue * heightMultiplier;
  }
}
Posted by: Guest on November-28-2020
2

perlin noise unity

Mathf.PerlinNoise(x, y); //Return value is between 0.0 and 1.0
Posted by: Guest on May-21-2020
0

unity 2d perlin noise

public static float[] GenerateNoiseMap(int mapWidth, int mapHeight, int seed, float scale, int octaves, float persistance, float lacunarity, Vector2 offset)
{
    float[] noiseMap = new float[mapWidth * mapHeight];
 
    var random = new System.Random(seed);
 
    // We need atleast one octave
    if (octaves < 1)
    {
        octaves = 1;
    }
 
    Vector2[] octaveOffsets = new Vector2[octaves];
    for (int i = 0; i < octaves; i++)
    {
        float offsetX = random.Next(-100000, 100000) + offset.x;
        float offsetY = random.Next(-100000, 100000) + offset.y;
        octaveOffsets[i] = new Vector2(offsetX, offsetY);
    }
 
    if (scale <= 0f)
    {
        scale = 0.0001f;
    }
 
    float maxNoiseHeight = float.MinValue;
    float minNoiseHeight = float.MaxValue;
 
    // When changing noise scale, it zooms from top-right corner
    // This will make it zoom from the center
    float halfWidth = mapWidth / 2f;
    float halfHeight = mapHeight / 2f;
 
    for (int x = 0, y; x < mapWidth; x++)
    {
        for (y = 0; y < mapHeight; y++)
        {
            float amplitude = 1;
            float frequency = 1;
            float noiseHeight = 0;
            for (int i = 0; i < octaves; i++)
            {
                float sampleX = (x - halfWidth) / scale * frequency + octaveOffsets[i].x;
                float sampleY = (y - halfHeight) / scale * frequency + octaveOffsets[i].y;
 
                // Use unity's implementation of perlin noise
                float perlinValue = Mathf.PerlinNoise(sampleX, sampleY) * 2 - 1;
 
                noiseHeight += perlinValue * amplitude;
                amplitude *= persistance;
                frequency *= lacunarity;
            }
 
            if (noiseHeight > maxNoiseHeight)
                maxNoiseHeight = noiseHeight;
            else if (noiseHeight < minNoiseHeight)
                minNoiseHeight = noiseHeight;
 
            noiseMap[y * mapWidth + x] = noiseHeight;
        }
    }
 
    for (int x = 0, y; x < mapWidth; x++)
    {
        for (y = 0; y < mapHeight; y++)
        {
            // Returns a value between 0f and 1f based on noiseMap value
            // minNoiseHeight being 0f, and maxNoiseHeight being 1f
            noiseMap[y * mapWidth + x] = Mathf.InverseLerp(minNoiseHeight, maxNoiseHeight, noiseMap[y * mapWidth + x]);
        }
    }
    return noiseMap;
}
Posted by: Guest on June-27-2021

C# Answers by Framework

Browse Popular Code Answers by Language