Skip to content

Commit 976cbf7

Browse files
author
Cory Leach
committed
Simplex value 1D and 2D
1 parent d1c91c3 commit 976cbf7

File tree

4 files changed

+159
-26
lines changed

4 files changed

+159
-26
lines changed

Runtime/Utility/SimplexGradientNoise.cs

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,97 @@ namespace Gameframe.Procgen
44
{
55
public static class SimplexGradientNoise
66
{
7-
public static float Noise1D(float x, uint seed)
7+
private static readonly float SquaresToTriangles = (3f - Mathf.Sqrt(3f)) / 6f;
8+
private static readonly float TrianglesToSquares = (Mathf.Sqrt(3f) - 1f) / 2f;
9+
10+
#region 1D
11+
12+
public static NoiseSample SimplexValue1D(float pointX, uint seed, float frequency)
13+
{
14+
pointX *= frequency;
15+
var ix = Mathf.Floor(pointX);
16+
17+
//Sample left
18+
var sample = _SimplexValue1DPart(pointX, ix, seed);
19+
//Sample right
20+
sample += _SimplexValue1DPart(pointX, ix + 1, seed);
21+
return sample;
22+
}
23+
24+
private static NoiseSample _SimplexValue1DPart(float pointX, float ix, uint seed)
825
{
9-
var x0 = Mathf.FloorToInt(x);
10-
var x1 = x - x0;
26+
var x = pointX - ix;
27+
var f = 1f - x * x;
28+
var f2 = f * f;
29+
var f3 = f * f2;
1130

12-
return 0;
31+
var h = Hash1D((int) ix, seed);
32+
33+
return new NoiseSample
34+
{
35+
value = f3 * h
36+
};
1337
}
38+
39+
private static float Hash1D(int value, uint seed)
40+
{
41+
return SquirrelEiserloh.Get1dNoiseZeroToOne(value, seed);
42+
}
43+
44+
#endregion
45+
46+
#region 2D
47+
48+
public static NoiseSample SimplexValue2D(float x, float y, uint seed, float frequency)
49+
{
50+
x *= frequency;
51+
y *= frequency;
52+
53+
//Skew
54+
var skew = (x + y) * TrianglesToSquares;
55+
var sx = x + skew;
56+
var sy = y + skew;
57+
58+
var ix = Mathf.FloorToInt(sx);
59+
var iy = Mathf.FloorToInt(sy);
60+
61+
var sample = _SimplexValue2DPart(x, y, ix, iy, seed);
62+
sample += _SimplexValue2DPart(x, y, ix + 1, iy + 1, seed);
63+
64+
if (sx - ix >= sy - iy)
65+
{
66+
sample += _SimplexValue2DPart(x, y, ix + 1, iy, seed);
67+
}
68+
else
69+
{
70+
sample += _SimplexValue2DPart(x, y, ix, iy + 1, seed);
71+
}
72+
73+
return sample * 8;
74+
}
75+
76+
private static NoiseSample _SimplexValue2DPart(float x, float y, int ix, int iy, uint seed)
77+
{
78+
var unskew = (ix + iy) * SquaresToTriangles;
79+
80+
var x2 = x - ix + unskew;
81+
var y2 = y - iy + unskew;
82+
var f = 0.5f - x2 * x2 - y2 * y2;
83+
var f2 = f * f;
84+
var f3 = f * f2;
85+
86+
var sample = new NoiseSample
87+
{
88+
value = (f > 0 ? f3 : 0) * Hash2D(ix, iy, seed),
89+
};
90+
return sample;
91+
}
92+
93+
private static float Hash2D(int x, int y, uint seed)
94+
{
95+
return SquirrelEiserloh.Get2dNoiseZeroToOne(x, y, seed);
96+
}
97+
98+
#endregion
1499
}
15-
}
100+
}

Runtime/Visualizers/NoiseVisualizer2D.cs

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace Gameframe.Procgen
77
[ExecuteAlways]
88
public class NoiseVisualizer2D : MonoBehaviour
99
{
10-
[SerializeField]
11-
private Renderer _renderer;
10+
[SerializeField] private Renderer _renderer;
1211

1312
[SerializeField] private int textureResolution = 256;
1413

@@ -22,6 +21,16 @@ public class NoiseVisualizer2D : MonoBehaviour
2221

2322
private Texture2D _texture;
2423

24+
private float minValue = float.MaxValue;
25+
private float maxValue = float.MinValue;
26+
27+
[ContextMenu("ResetMinMax")]
28+
public void ResetMinMax()
29+
{
30+
minValue = float.MaxValue;
31+
maxValue = float.MinValue;
32+
}
33+
2534
public enum Dimension
2635
{
2736
Value1D,
@@ -32,10 +41,14 @@ public enum Dimension
3241
Perlin3D,
3342
SamplePerlin2D,
3443
SamplePerlin3D,
44+
SimplexValue1D,
45+
SimplexValue2D,
3546
}
3647

3748
[SerializeField] private Dimension dimension = Dimension.Value2D;
3849

50+
[SerializeField] private Vector3 offset;
51+
3952
private void OnEnable()
4053
{
4154
Generate();
@@ -48,7 +61,7 @@ private void OnDisable()
4861

4962
private Texture2D CreateTexture()
5063
{
51-
var tex = new Texture2D(textureResolution, textureResolution)
64+
var tex = new Texture2D(textureResolution, textureResolution)
5265
{
5366
filterMode = filterMode
5467
};
@@ -75,10 +88,10 @@ private void Generate()
7588

7689
_texture.filterMode = filterMode;
7790

78-
var point00 = transform.TransformPoint(new Vector3(-0.5f,-0.5f));
79-
var point10 = transform.TransformPoint(new Vector3( 0.5f,-0.5f));
91+
var point00 = transform.TransformPoint(new Vector3(-0.5f, -0.5f));
92+
var point10 = transform.TransformPoint(new Vector3(0.5f, -0.5f));
8093
var point01 = transform.TransformPoint(new Vector3(-0.5f, 0.5f));
81-
var point11 = transform.TransformPoint(new Vector3( 0.5f, 0.5f));
94+
var point11 = transform.TransformPoint(new Vector3(0.5f, 0.5f));
8295

8396
var stepSize = 1f / textureResolution;
8497

@@ -89,43 +102,62 @@ private void Generate()
89102

90103
for (var x = 0; x < textureResolution; x++)
91104
{
92-
var point = Vector3.Lerp(point0, point1, (x + 0.5f) * stepSize);
105+
var point = Vector3.Lerp(point0, point1, (x + 0.5f) * stepSize) + offset;
93106
var v = 0f;
94107

95108
switch (dimension)
96109
{
97110
case Dimension.Value1D:
98-
v = ValueNoise.Fractal1D(point.x * frequency, seed, frequency, octaves, lacunarity, persistence);
111+
v = ValueNoise.Fractal1D(point.x * frequency, seed, frequency, octaves, lacunarity,
112+
persistence);
99113
break;
100114
case Dimension.Value2D:
101-
v = ValueNoise.Fractal2D(point * frequency, seed, frequency, octaves, lacunarity, persistence);
115+
v = ValueNoise.Fractal2D(point * frequency, seed, frequency, octaves, lacunarity,
116+
persistence);
102117
break;
103118
case Dimension.Value3D:
104-
v = ValueNoise.Fractal3D(point * frequency, seed, frequency, octaves, lacunarity, persistence);
119+
v = ValueNoise.Fractal3D(point * frequency, seed, frequency, octaves, lacunarity,
120+
persistence);
105121
break;
106122
case Dimension.Perlin1D:
107-
v = PerlinGradientNoise.Fractal1D(point.x, seed, frequency, octaves, lacunarity, persistence);
123+
v = PerlinGradientNoise.Fractal1D(point.x, seed, frequency, octaves, lacunarity,
124+
persistence);
108125
break;
109126
case Dimension.Perlin2D:
110-
v = PerlinGradientNoise.Fractal2D(point.x, point.y, seed, frequency, octaves, lacunarity, persistence);
127+
v = PerlinGradientNoise.Fractal2D(point.x, point.y, seed, frequency, octaves, lacunarity,
128+
persistence);
111129
break;
112130
case Dimension.Perlin3D:
113-
v = PerlinGradientNoise.Fractal3D(point.x, point.y, point.z, seed, frequency, octaves, lacunarity, persistence);
131+
v = PerlinGradientNoise.Fractal3D(point.x, point.y, point.z, seed, frequency, octaves,
132+
lacunarity, persistence);
114133
break;
115134
case Dimension.SamplePerlin2D:
116-
v = PerlinGradientNoise.FractalSample2D(point.x, point.y, seed, frequency, octaves, lacunarity, persistence).value;
135+
v = PerlinGradientNoise.FractalSample2D(point.x, point.y, seed, frequency, octaves,
136+
lacunarity, persistence).value;
117137
break;
118138
case Dimension.SamplePerlin3D:
119-
v = PerlinGradientNoise.FractalSample3D(point.x, point.y, point.z, seed, frequency, octaves, lacunarity, persistence).value;
139+
v = PerlinGradientNoise.FractalSample3D(point.x, point.y, point.z, seed, frequency, octaves,
140+
lacunarity, persistence).value;
141+
break;
142+
case Dimension.SimplexValue1D:
143+
v = SimplexGradientNoise.SimplexValue1D(point.x, seed, frequency).value;
144+
break;
145+
case Dimension.SimplexValue2D:
146+
v = SimplexGradientNoise.SimplexValue2D(point.x, point.y, seed, frequency).value;
120147
break;
121148
default:
122149
throw new ArgumentOutOfRangeException();
123150
}
124151

125-
_texture.SetPixel(x,y, new Color(v,v,v,1f));
152+
minValue = Mathf.Min(v, minValue);
153+
maxValue = Mathf.Max(v, maxValue);
154+
155+
_texture.SetPixel(x, y, new Color(v, v, v, 1f));
126156
}
127157
}
128158

159+
Debug.Log($"Min: {minValue} Max: {maxValue}");
160+
129161
_texture.Apply();
130162

131163
_renderer.sharedMaterial.mainTexture = _texture;
@@ -139,7 +171,8 @@ private void SaveTexture()
139171
Debug.LogError("Texture does not exist");
140172
return;
141173
}
142-
TextureUtility.SaveTextureAsPNG(_texture,Application.dataPath + "GeneratedTexture.png");
174+
175+
TextureUtility.SaveTextureAsPNG(_texture, Application.dataPath + "GeneratedTexture.png");
143176
}
144177

145178
private void ClearTexture()
@@ -165,6 +198,4 @@ private void OnValidate()
165198
}
166199
}
167200
}
168-
169-
170201
}

Runtime/Visualizers/SurfaceMeshVisualizer.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public enum Dimension
5757
Perlin1D,
5858
Perlin2D,
5959
Perlin3D,
60+
Simplex1D,
6061
}
6162

6263
[SerializeField] private Dimension dimension = Dimension.Value2D;
@@ -177,13 +178,16 @@ private NoiseSample Noise(Vector3 point)
177178
case Dimension.Perlin3D:
178179
sample = PerlinGradientNoise.FractalSample3D(point.x, point.y, point.z, seed, frequency, octaves, lacunarity, persistence);
179180
break;
181+
case Dimension.Simplex1D:
182+
//sample = SimplexGradientNoise.Noise1D(point.x, seed);
183+
break;
180184
}
181-
182185
return sample;
183186
}
184187

185188
private void CreateMesh()
186189
{
190+
Debug.Log("Create Mesh");
187191
currentResolution = resolution;
188192

189193
if (mesh == null)

Runtime/com.gameframe.procgen.asmdef

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
{ "name": "com.gameframe.procgen" }
1+
{
2+
"name": "com.gameframe.procgen",
3+
"rootNamespace": "",
4+
"references": [],
5+
"includePlatforms": [],
6+
"excludePlatforms": [],
7+
"allowUnsafeCode": false,
8+
"overrideReferences": false,
9+
"precompiledReferences": [],
10+
"autoReferenced": true,
11+
"defineConstraints": [],
12+
"versionDefines": [],
13+
"noEngineReferences": false
14+
}

0 commit comments

Comments
 (0)