Skip to content

Commit

Permalink
Merge pull request #6 from coryleach/dev
Browse files Browse the repository at this point in the history
Random Access Simplex and Perlin Noise
  • Loading branch information
coryleach authored Feb 25, 2023
2 parents 25f7c70 + 36db891 commit 95225eb
Show file tree
Hide file tree
Showing 44 changed files with 3,144 additions and 95 deletions.
Binary file modified Demo/Demo-URP.unitypackage
Binary file not shown.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ Library of utilitities for procedural generation
#### Using UnityPackageManager (for Unity 2019.3 or later)
Open the package manager window (menu: Window > Package Manager)<br/>
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
https://github.com/coryleach/UnityProcgen.git#0.0.6<br/>
https://github.com/coryleach/UnityProcgen.git#0.0.7<br/>

#### Using UnityPackageManager (for Unity 2019.1 or later)

Find the manifest.json file in the Packages folder of your project and edit it to look like this:
```js
{
"dependencies": {
"com.gameframe.procgen": "https://github.com/coryleach/UnityProcgen.git#0.0.6",
"com.gameframe.procgen": "https://github.com/coryleach/UnityProcgen.git#0.0.7",
...
},
}
Expand Down Expand Up @@ -57,8 +57,8 @@ The demo may require 2019.3


## Show your support

Give a ⭐️ if this project helped you!


***
_This README was generated with ❤️ by [Gameframe.Packages](https://github.com/coryleach/unitypackages)_
8 changes: 8 additions & 0 deletions Runtime/ScriptableObjects.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 100 additions & 0 deletions Runtime/ScriptableObjects/NoiseGenerator.cs
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;
}

}
}
11 changes: 11 additions & 0 deletions Runtime/ScriptableObjects/NoiseGenerator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 52 additions & 0 deletions Runtime/ScriptableObjects/PerlinNoiseGenerator.cs
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);
}

}
}
3 changes: 3 additions & 0 deletions Runtime/ScriptableObjects/PerlinNoiseGenerator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions Runtime/Utility/FractalUtility.cs
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);
}
}
}
3 changes: 3 additions & 0 deletions Runtime/Utility/FractalUtility.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Runtime/Utility/IRandomAccessRandomNumberGenerator.cs
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; }
}
}
3 changes: 3 additions & 0 deletions Runtime/Utility/IRandomAccessRandomNumberGenerator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Runtime/Utility/IRandomNumberGenerator.cs
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();
}
}
3 changes: 3 additions & 0 deletions Runtime/Utility/IRandomNumberGenerator.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 95225eb

Please sign in to comment.