Skip to content

Commit 95225eb

Browse files
authored
Merge pull request #6 from coryleach/dev
Random Access Simplex and Perlin Noise
2 parents 25f7c70 + 36db891 commit 95225eb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+3144
-95
lines changed

Demo/Demo-URP.unitypackage

1.38 MB
Binary file not shown.

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ Library of utilitities for procedural generation
1717
#### Using UnityPackageManager (for Unity 2019.3 or later)
1818
Open the package manager window (menu: Window > Package Manager)<br/>
1919
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
20-
https://github.com/coryleach/UnityProcgen.git#0.0.6<br/>
20+
https://github.com/coryleach/UnityProcgen.git#0.0.7<br/>
2121

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

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

5858

5959
## Show your support
60-
6160
Give a ⭐️ if this project helped you!
6261

62+
6363
***
6464
_This README was generated with ❤️ by [Gameframe.Packages](https://github.com/coryleach/unitypackages)_

Runtime/ScriptableObjects.meta

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using UnityEngine;
2+
3+
namespace Gameframe.Procgen
4+
{
5+
[CreateAssetMenu(menuName = "Gameframe/Procgen/NoiseGenerator/BasicNoise")]
6+
public class NoiseGenerator : ScriptableObject
7+
{
8+
[SerializeField]
9+
private uint seed;
10+
11+
[SerializeField]
12+
private float frequency = 1.0f;
13+
14+
public uint Seed
15+
{
16+
get => seed;
17+
set => seed = value;
18+
}
19+
20+
public float Frequency
21+
{
22+
get => frequency;
23+
set => frequency = value;
24+
}
25+
26+
public float Value1D(float x)
27+
{
28+
return ValueNoise.Noise1D(x*frequency, seed);
29+
}
30+
31+
public float Value2D(float x, float y)
32+
{
33+
return ValueNoise.Noise2D(x*frequency, y*frequency, seed);
34+
}
35+
36+
public float Value3D(float x, float y, float z)
37+
{
38+
return ValueNoise.Noise3D(x*frequency, y*frequency, z*frequency, seed);
39+
}
40+
41+
public float Value2D(Vector2 v)
42+
{
43+
return Value2D(v.x, v.y);
44+
}
45+
46+
public float Value3D(Vector3 v)
47+
{
48+
return Value3D(v.x, v.y, v.z);
49+
}
50+
51+
}
52+
53+
public class SimplexNoiseGenerator : ScriptableObject
54+
{
55+
56+
/// <summary>
57+
/// Map coordinate to position in a 1d simplex lattice
58+
/// </summary>
59+
/// <param name="x"></param>
60+
/// <returns></returns>
61+
private static float SkewCoordinate1d(float x)
62+
{
63+
const float f = 0.41421356237f; // (Mathf.Sqrt(1 + 1) - 1) / 1;
64+
var skewed = 0f;
65+
skewed = x + x * f;
66+
return skewed;
67+
}
68+
69+
/// <summary>
70+
/// Map coordinate to position in a 2d simplex lattice
71+
/// </summary>
72+
/// <param name="v"></param>
73+
/// <returns></returns>
74+
private static Vector2 SkewCoordinate2d(Vector2 v)
75+
{
76+
const float f = 0.36602540378f; // (Mathf.Sqrt(2 + 1) - 1) / 2;
77+
var skewed = Vector2.zero;
78+
skewed.x = v.x + (v.x + v.y) * f;
79+
skewed.y = v.y + (v.x + v.y) * f;
80+
return skewed;
81+
}
82+
83+
/// <summary>
84+
/// Map coordinate to position in 3d simplex lattice
85+
/// </summary>
86+
/// <param name="v"></param>
87+
/// <returns></returns>
88+
private static Vector3 SkewCoordinate3d(Vector3 v)
89+
{
90+
const float f = 0.33333333333f; // (Mathf.Sqrt(3 + 1) - 1) / 3;
91+
var skewed = Vector3.zero;
92+
var sum = (v.x + v.y + v.z) * f;
93+
skewed.x = v.x + sum;
94+
skewed.y = v.y + sum;
95+
skewed.z = v.z + sum;
96+
return skewed;
97+
}
98+
99+
}
100+
}

Runtime/ScriptableObjects/NoiseGenerator.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using UnityEngine;
2+
3+
namespace Gameframe.Procgen
4+
{
5+
[CreateAssetMenu(menuName = "Gameframe/Procgen/NoiseGenerator/PerlinNoise")]
6+
public class PerlinNoiseGenerator : ScriptableObject
7+
{
8+
[SerializeField]
9+
private uint seed;
10+
11+
[SerializeField]
12+
private float frequency = 1.0f;
13+
14+
public uint Seed
15+
{
16+
get => seed;
17+
set => seed = value;
18+
}
19+
20+
public float Frequency
21+
{
22+
get => frequency;
23+
set => frequency = value;
24+
}
25+
26+
public float Value1D(float x)
27+
{
28+
return ValueNoise.Noise1D(x*frequency, seed);
29+
}
30+
31+
public float Value2D(float x, float y)
32+
{
33+
return ValueNoise.Noise2D(x*frequency, y*frequency, seed);
34+
}
35+
36+
public float Value3D(float x, float y, float z)
37+
{
38+
return ValueNoise.Noise3D(x*frequency, y*frequency, z*frequency, seed);
39+
}
40+
41+
public float Value2D(Vector2 v)
42+
{
43+
return Value2D(v.x, v.y);
44+
}
45+
46+
public float Value3D(Vector3 v)
47+
{
48+
return Value3D(v.x, v.y, v.z);
49+
}
50+
51+
}
52+
}

Runtime/ScriptableObjects/PerlinNoiseGenerator.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Utility/FractalUtility.cs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
3+
namespace Gameframe.Procgen
4+
{
5+
public static class FractalUtility
6+
{
7+
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)
8+
{
9+
var sum = action(x, seed, frequency);
10+
var amplitude = 1f;
11+
var range = 1f;
12+
for (var i = 1; i < octaves; i++)
13+
{
14+
frequency *= lacunarity;
15+
amplitude *= persistence;
16+
range += amplitude;
17+
sum += action(x, seed, frequency) * amplitude;
18+
}
19+
return sum * (1 / range);
20+
}
21+
22+
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)
23+
{
24+
var sum = action(x, seed, frequency);
25+
var amplitude = 1f;
26+
var range = 1f;
27+
for (var i = 1; i < octaves; i++)
28+
{
29+
frequency *= lacunarity;
30+
amplitude *= persistence;
31+
range += amplitude;
32+
sum += action(x, seed, frequency) * amplitude;
33+
}
34+
return sum * (1 / range);
35+
}
36+
37+
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)
38+
{
39+
var sum = action(x, y, seed, frequency);
40+
var amplitude = 1f;
41+
var range = 1f;
42+
for (var i = 1; i < octaves; i++)
43+
{
44+
frequency *= lacunarity;
45+
amplitude *= persistence;
46+
range += amplitude;
47+
sum += action(x, y, seed, frequency) * amplitude;
48+
}
49+
return sum * (1 / range);
50+
}
51+
52+
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)
53+
{
54+
var sum = action(x, y, seed, frequency);
55+
var amplitude = 1f;
56+
var range = 1f;
57+
for (var i = 1; i < octaves; i++)
58+
{
59+
frequency *= lacunarity;
60+
amplitude *= persistence;
61+
range += amplitude;
62+
sum += action(x, y, seed, frequency) * amplitude;
63+
}
64+
return sum * (1 / range);
65+
}
66+
67+
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)
68+
{
69+
var sum = action(x, y, z, seed, frequency);
70+
var amplitude = 1f;
71+
var range = 1f;
72+
for (var i = 1; i < octaves; i++)
73+
{
74+
frequency *= lacunarity;
75+
amplitude *= persistence;
76+
range += amplitude;
77+
sum += action(x, y, z, seed, frequency) * amplitude;
78+
}
79+
return sum * (1 / range);
80+
}
81+
82+
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)
83+
{
84+
var sum = action(x, y, z, seed, frequency);
85+
var amplitude = 1f;
86+
var range = 1f;
87+
for (var i = 1; i < octaves; i++)
88+
{
89+
frequency *= lacunarity;
90+
amplitude *= persistence;
91+
range += amplitude;
92+
sum += action(x, y, z, seed, frequency) * amplitude;
93+
}
94+
return sum * (1 / range);
95+
}
96+
}
97+
}

Runtime/Utility/FractalUtility.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Gameframe.Procgen
2+
{
3+
/// <summary>
4+
/// Random number generator that provides random access to any position in its random number sequence
5+
/// </summary>
6+
public interface IRandomAccessRandomNumberGenerator : IRandomNumberGenerator
7+
{
8+
int Position { get; set; }
9+
}
10+
}

Runtime/Utility/IRandomAccessRandomNumberGenerator.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using UnityEngine;
2+
3+
namespace Gameframe.Procgen
4+
{
5+
/// <summary>
6+
/// Random number generator that provides lots of different convenience methods relevant for game development
7+
/// </summary>
8+
public interface IRandomNumberGenerator
9+
{
10+
uint Seed { get; }
11+
uint NextUint();
12+
ushort NextUshort();
13+
byte NextByte();
14+
int NextInt();
15+
short NextShort();
16+
float NextFloatZeroToOne();
17+
float NextFloatNegOneToOne();
18+
float NextFloatRange(float min, float max);
19+
bool RollChance(float probabilityOfReturningTrue);
20+
Vector2 NextDirection2D();
21+
Vector3 NextDirection3D();
22+
}
23+
}

Runtime/Utility/IRandomNumberGenerator.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)