-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
Random Access Simplex and Perlin Noise
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
using UnityEngine; | ||
|
||
namespace Gameframe.Procgen | ||
{ | ||
[CreateAssetMenu(menuName = "Gameframe/Procgen/NoiseGenerator/BasicNoise")] | ||
public class NoiseGenerator : ScriptableObject | ||
{ | ||
[SerializeField] | ||
private uint seed; | ||
|
||
[SerializeField] | ||
private float frequency = 1.0f; | ||
|
||
public uint Seed | ||
{ | ||
get => seed; | ||
set => seed = value; | ||
} | ||
|
||
public float Frequency | ||
{ | ||
get => frequency; | ||
set => frequency = value; | ||
} | ||
|
||
public float Value1D(float x) | ||
{ | ||
return ValueNoise.Noise1D(x*frequency, seed); | ||
} | ||
|
||
public float Value2D(float x, float y) | ||
{ | ||
return ValueNoise.Noise2D(x*frequency, y*frequency, seed); | ||
} | ||
|
||
public float Value3D(float x, float y, float z) | ||
{ | ||
return ValueNoise.Noise3D(x*frequency, y*frequency, z*frequency, seed); | ||
} | ||
|
||
public float Value2D(Vector2 v) | ||
{ | ||
return Value2D(v.x, v.y); | ||
} | ||
|
||
public float Value3D(Vector3 v) | ||
{ | ||
return Value3D(v.x, v.y, v.z); | ||
} | ||
|
||
} | ||
|
||
public class SimplexNoiseGenerator : ScriptableObject | ||
{ | ||
|
||
/// <summary> | ||
/// Map coordinate to position in a 1d simplex lattice | ||
/// </summary> | ||
/// <param name="x"></param> | ||
/// <returns></returns> | ||
private static float SkewCoordinate1d(float x) | ||
{ | ||
const float f = 0.41421356237f; // (Mathf.Sqrt(1 + 1) - 1) / 1; | ||
var skewed = 0f; | ||
skewed = x + x * f; | ||
return skewed; | ||
} | ||
|
||
/// <summary> | ||
/// Map coordinate to position in a 2d simplex lattice | ||
/// </summary> | ||
/// <param name="v"></param> | ||
/// <returns></returns> | ||
private static Vector2 SkewCoordinate2d(Vector2 v) | ||
{ | ||
const float f = 0.36602540378f; // (Mathf.Sqrt(2 + 1) - 1) / 2; | ||
var skewed = Vector2.zero; | ||
skewed.x = v.x + (v.x + v.y) * f; | ||
skewed.y = v.y + (v.x + v.y) * f; | ||
return skewed; | ||
} | ||
|
||
/// <summary> | ||
/// Map coordinate to position in 3d simplex lattice | ||
/// </summary> | ||
/// <param name="v"></param> | ||
/// <returns></returns> | ||
private static Vector3 SkewCoordinate3d(Vector3 v) | ||
{ | ||
const float f = 0.33333333333f; // (Mathf.Sqrt(3 + 1) - 1) / 3; | ||
var skewed = Vector3.zero; | ||
var sum = (v.x + v.y + v.z) * f; | ||
skewed.x = v.x + sum; | ||
skewed.y = v.y + sum; | ||
skewed.z = v.z + sum; | ||
return skewed; | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using UnityEngine; | ||
|
||
namespace Gameframe.Procgen | ||
{ | ||
[CreateAssetMenu(menuName = "Gameframe/Procgen/NoiseGenerator/PerlinNoise")] | ||
public class PerlinNoiseGenerator : ScriptableObject | ||
{ | ||
[SerializeField] | ||
private uint seed; | ||
|
||
[SerializeField] | ||
private float frequency = 1.0f; | ||
|
||
public uint Seed | ||
{ | ||
get => seed; | ||
set => seed = value; | ||
} | ||
|
||
public float Frequency | ||
{ | ||
get => frequency; | ||
set => frequency = value; | ||
} | ||
|
||
public float Value1D(float x) | ||
{ | ||
return ValueNoise.Noise1D(x*frequency, seed); | ||
} | ||
|
||
public float Value2D(float x, float y) | ||
{ | ||
return ValueNoise.Noise2D(x*frequency, y*frequency, seed); | ||
} | ||
|
||
public float Value3D(float x, float y, float z) | ||
{ | ||
return ValueNoise.Noise3D(x*frequency, y*frequency, z*frequency, seed); | ||
} | ||
|
||
public float Value2D(Vector2 v) | ||
{ | ||
return Value2D(v.x, v.y); | ||
} | ||
|
||
public float Value3D(Vector3 v) | ||
{ | ||
return Value3D(v.x, v.y, v.z); | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using System; | ||
|
||
namespace Gameframe.Procgen | ||
{ | ||
public static class FractalUtility | ||
{ | ||
public static NoiseSample Sample1D(Func<float, uint, float, NoiseSample> action, float x, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f) | ||
{ | ||
var sum = action(x, seed, frequency); | ||
var amplitude = 1f; | ||
var range = 1f; | ||
for (var i = 1; i < octaves; i++) | ||
{ | ||
frequency *= lacunarity; | ||
amplitude *= persistence; | ||
range += amplitude; | ||
sum += action(x, seed, frequency) * amplitude; | ||
} | ||
return sum * (1 / range); | ||
} | ||
|
||
public static float Float1D(Func<float, uint, float, float> action, float x, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f) | ||
{ | ||
var sum = action(x, seed, frequency); | ||
var amplitude = 1f; | ||
var range = 1f; | ||
for (var i = 1; i < octaves; i++) | ||
{ | ||
frequency *= lacunarity; | ||
amplitude *= persistence; | ||
range += amplitude; | ||
sum += action(x, seed, frequency) * amplitude; | ||
} | ||
return sum * (1 / range); | ||
} | ||
|
||
public static NoiseSample Sample2D(Func<float, float, uint, float, NoiseSample> action, float x, float y, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f) | ||
{ | ||
var sum = action(x, y, seed, frequency); | ||
var amplitude = 1f; | ||
var range = 1f; | ||
for (var i = 1; i < octaves; i++) | ||
{ | ||
frequency *= lacunarity; | ||
amplitude *= persistence; | ||
range += amplitude; | ||
sum += action(x, y, seed, frequency) * amplitude; | ||
} | ||
return sum * (1 / range); | ||
} | ||
|
||
public static float Float2D(Func<float, float, uint, float, float> action, float x, float y, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f) | ||
{ | ||
var sum = action(x, y, seed, frequency); | ||
var amplitude = 1f; | ||
var range = 1f; | ||
for (var i = 1; i < octaves; i++) | ||
{ | ||
frequency *= lacunarity; | ||
amplitude *= persistence; | ||
range += amplitude; | ||
sum += action(x, y, seed, frequency) * amplitude; | ||
} | ||
return sum * (1 / range); | ||
} | ||
|
||
public static NoiseSample Sample3D(Func<float, float, float, uint, float, NoiseSample> action, float x, float y, float z, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f) | ||
{ | ||
var sum = action(x, y, z, seed, frequency); | ||
var amplitude = 1f; | ||
var range = 1f; | ||
for (var i = 1; i < octaves; i++) | ||
{ | ||
frequency *= lacunarity; | ||
amplitude *= persistence; | ||
range += amplitude; | ||
sum += action(x, y, z, seed, frequency) * amplitude; | ||
} | ||
return sum * (1 / range); | ||
} | ||
|
||
public static float Float3D(Func<float, float, float, uint, float, float> action, float x, float y, float z, uint seed, float frequency, int octaves, float lacunarity = 2f, float persistence = 0.5f) | ||
{ | ||
var sum = action(x, y, z, seed, frequency); | ||
var amplitude = 1f; | ||
var range = 1f; | ||
for (var i = 1; i < octaves; i++) | ||
{ | ||
frequency *= lacunarity; | ||
amplitude *= persistence; | ||
range += amplitude; | ||
sum += action(x, y, z, seed, frequency) * amplitude; | ||
} | ||
return sum * (1 / range); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Gameframe.Procgen | ||
{ | ||
/// <summary> | ||
/// Random number generator that provides random access to any position in its random number sequence | ||
/// </summary> | ||
public interface IRandomAccessRandomNumberGenerator : IRandomNumberGenerator | ||
{ | ||
int Position { get; set; } | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using UnityEngine; | ||
|
||
namespace Gameframe.Procgen | ||
{ | ||
/// <summary> | ||
/// Random number generator that provides lots of different convenience methods relevant for game development | ||
/// </summary> | ||
public interface IRandomNumberGenerator | ||
{ | ||
uint Seed { get; } | ||
uint NextUint(); | ||
ushort NextUshort(); | ||
byte NextByte(); | ||
int NextInt(); | ||
short NextShort(); | ||
float NextFloatZeroToOne(); | ||
float NextFloatNegOneToOne(); | ||
float NextFloatRange(float min, float max); | ||
bool RollChance(float probabilityOfReturningTrue); | ||
Vector2 NextDirection2D(); | ||
Vector3 NextDirection3D(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.