3d 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;
}
}
4.33
3
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;
}
Thank you!
3
0
4.1
10
public static float Perlin3D(float x, float y, float z, float density, float scale){
float XY = Mathf.PerlinNoise(x, y);
float YZ = Mathf.PerlinNoise(y, z);
float ZX = Mathf.PerlinNoise(z, x);
float YX = Mathf.PerlinNoise(y, z);
float ZY = Mathf.PerlinNoise(z, y);
float XZ = Mathf.PerlinNoise(x, z);
float val = (XY + YZ + ZX + YX + ZY + XZ)/6f;
return val * scale;
}
Thank you!
10
0
3.9
10
Mathf.PerlinNoise(x, y); // Returns a value between 0.0 and 1.0
Thank you!
10
0
5
1
Mathf.PerlinNoise(x, y); //Return value is between 0.0 and 1.0
Thank you!
1
0
Are there any code examples left?
New code examples in category C#
-
C# 2022-03-27 22:40:23 c# movement script
-
C# 2022-03-27 22:15:17 Delete last modification on EntityFramework Core
-
C# 2022-03-27 22:15:02 file.create folder c#
-
C# 2022-03-27 19:25:04 Error inflating class android.support.constraint.ConstraintLayout
-
C# 2022-03-27 19:15:02 top down shooting in unity 2D
-
C# 2022-03-27 18:50:02 c# remove invalid directory characters
-
C# 2022-03-27 18:30:07 how to use K2 games Games parallax background
-
C# 2022-03-27 18:25:10 c# replace crlf
-
C# 2022-03-27 18:25:09 keycode for left mouse click unity