how to use perlin noise in unity code example
Example 1: perlin noise unity
using UnityEngine;
public static class NoiseCreator
{
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++)
{
PerlinValue += Mathf.PerlinNoise(x * frequency, z * frequency) * amplitude;
amplitude *= persistance;
frequency *= lacunarity;
}
return PerlinValue * heightMultiplier;
}
}
Example 2: 3d perlin noise unity
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;
}