@@ -4,12 +4,12 @@ namespace Gameframe.Procgen
4
4
{
5
5
public static class NoiseGradients
6
6
{
7
- public const int GradientsMask1D = 1 ;
8
- public static readonly float [ ] Gradients1D = new [ ] { 1f , - 1f } ;
7
+ private const int GradientsMask1D = 1 ;
8
+ private static readonly float [ ] Gradients1D = new [ ] { 1f , - 1f } ;
9
9
10
- public const int GradientsMask2D = 7 ;
10
+ private const int GradientsMask2D = 7 ;
11
11
12
- public static readonly Vector2 [ ] Gradients2D =
12
+ private static readonly Vector2 [ ] Gradients2D =
13
13
{
14
14
new Vector2 ( 1f , 0f ) ,
15
15
new Vector2 ( - 1f , 0f ) ,
@@ -21,9 +21,10 @@ public static class NoiseGradients
21
21
new Vector2 ( - 1f , - 1f ) . normalized
22
22
} ;
23
23
24
- public const int GradientsMask3D = 15 ;
24
+ private const int GradientsMask3D = 15 ;
25
+ private const int SimplexGradientsMask3D = 31 ;
25
26
26
- public static readonly Vector3 [ ] Gradients3D =
27
+ private static readonly Vector3 [ ] Gradients3D =
27
28
{
28
29
new Vector3 ( 1f , 1f , 0f ) ,
29
30
new Vector3 ( - 1f , 1f , 0f ) ,
@@ -44,6 +45,43 @@ public static class NoiseGradients
44
45
new Vector3 ( 0f , - 1f , - 1f )
45
46
} ;
46
47
48
+ private static readonly Vector3 [ ] SimplexGradients3D = {
49
+ new Vector3 ( 1f , 1f , 0f ) . normalized ,
50
+ new Vector3 ( - 1f , 1f , 0f ) . normalized ,
51
+ new Vector3 ( 1f , - 1f , 0f ) . normalized ,
52
+ new Vector3 ( - 1f , - 1f , 0f ) . normalized ,
53
+ new Vector3 ( 1f , 0f , 1f ) . normalized ,
54
+ new Vector3 ( - 1f , 0f , 1f ) . normalized ,
55
+ new Vector3 ( 1f , 0f , - 1f ) . normalized ,
56
+ new Vector3 ( - 1f , 0f , - 1f ) . normalized ,
57
+ new Vector3 ( 0f , 1f , 1f ) . normalized ,
58
+ new Vector3 ( 0f , - 1f , 1f ) . normalized ,
59
+ new Vector3 ( 0f , 1f , - 1f ) . normalized ,
60
+ new Vector3 ( 0f , - 1f , - 1f ) . normalized ,
61
+
62
+ new Vector3 ( 1f , 1f , 0f ) . normalized ,
63
+ new Vector3 ( - 1f , 1f , 0f ) . normalized ,
64
+ new Vector3 ( 1f , - 1f , 0f ) . normalized ,
65
+ new Vector3 ( - 1f , - 1f , 0f ) . normalized ,
66
+ new Vector3 ( 1f , 0f , 1f ) . normalized ,
67
+ new Vector3 ( - 1f , 0f , 1f ) . normalized ,
68
+ new Vector3 ( 1f , 0f , - 1f ) . normalized ,
69
+ new Vector3 ( - 1f , 0f , - 1f ) . normalized ,
70
+ new Vector3 ( 0f , 1f , 1f ) . normalized ,
71
+ new Vector3 ( 0f , - 1f , 1f ) . normalized ,
72
+ new Vector3 ( 0f , 1f , - 1f ) . normalized ,
73
+ new Vector3 ( 0f , - 1f , - 1f ) . normalized ,
74
+
75
+ new Vector3 ( 1f , 1f , 1f ) . normalized ,
76
+ new Vector3 ( - 1f , 1f , 1f ) . normalized ,
77
+ new Vector3 ( 1f , - 1f , 1f ) . normalized ,
78
+ new Vector3 ( - 1f , - 1f , 1f ) . normalized ,
79
+ new Vector3 ( 1f , 1f , - 1f ) . normalized ,
80
+ new Vector3 ( - 1f , 1f , - 1f ) . normalized ,
81
+ new Vector3 ( 1f , - 1f , - 1f ) . normalized ,
82
+ new Vector3 ( - 1f , - 1f , - 1f ) . normalized
83
+ } ;
84
+
47
85
private static float HashToGradient1D ( uint value )
48
86
{
49
87
return Gradients1D [ value & GradientsMask1D ] ;
@@ -54,7 +92,7 @@ public static float HashToGradient1D(int x, uint seed)
54
92
return HashToGradient1D ( Hash1D ( x , seed ) ) ;
55
93
}
56
94
57
- public static Vector2 HashToGradient2D ( uint value )
95
+ private static Vector2 HashToGradient2D ( uint value )
58
96
{
59
97
return Gradients2D [ value & GradientsMask2D ] ;
60
98
}
@@ -69,11 +107,21 @@ private static Vector3 HashToGradient3D(uint value)
69
107
return Gradients3D [ value & GradientsMask3D ] ;
70
108
}
71
109
110
+ private static Vector3 HashToSimplexGradient3D ( uint value )
111
+ {
112
+ return SimplexGradients3D [ value & SimplexGradientsMask3D ] ;
113
+ }
114
+
72
115
public static Vector3 HashToGradient3D ( int x , int y , int z , uint seed )
73
116
{
74
117
return HashToGradient3D ( Hash3D ( x , y , z , seed ) ) ;
75
118
}
76
119
120
+ public static Vector3 HashToSimplexGradient3D ( int x , int y , int z , uint seed )
121
+ {
122
+ return HashToSimplexGradient3D ( Hash3D ( x , y , z , seed ) ) ;
123
+ }
124
+
77
125
public static uint Hash1D ( int value , uint seed )
78
126
{
79
127
return SquirrelEiserloh . Get1dNoiseUint ( value , seed ) ;
@@ -276,39 +324,37 @@ public static NoiseSample SampleGradient2D(float x, float y, uint seed, float fr
276
324
}
277
325
278
326
sample . derivative *= frequency ;
279
- //sample.value *= (SimplexScale2D / 43.71107f);
280
- //sample.value = (sample.value + 1) * 0.5f;
281
327
282
- //sample.value *= SimplexScale2D; //32.99077
283
- //Debug.Log($"Scale2D : {SimplexScale2D}");
328
+ sample . value *= SimplexScale2D ;
329
+ sample . value += 1 ;
330
+ sample . value *= 0.5f ;
284
331
285
332
return sample ;
286
333
}
287
334
288
- private static NoiseSample _Gradient2DPart ( float x , float y , int ix , int iy , uint seed )
335
+ private static NoiseSample _Gradient2DPart ( float pointX , float pointY , int ix , int iy , uint seed )
289
336
{
290
337
var unskew = ( ix + iy ) * SquaresToTriangles ;
291
338
292
- var x2 = x - ix + unskew ;
293
- var y2 = y - iy + unskew ;
339
+ var x2 = pointX - ix + unskew ;
340
+ var y2 = pointY - iy + unskew ;
294
341
var f = 0.5f - x2 * x2 - y2 * y2 ;
295
- var f2 = f * f ;
296
- var f3 = f * f2 ;
297
-
298
342
299
343
if ( f > 0 )
300
344
{
345
+ var f2 = f * f ;
346
+ var f3 = f * f2 ;
301
347
var g = NoiseGradients . HashToGradient2D ( ix , iy , seed ) ;
302
- var v = NoiseGradients . Dot2D ( g , x , y ) ;
348
+ var v = NoiseGradients . Dot2D ( g , x2 , y2 ) ;
303
349
var v6f2 = - 6f * v * f2 ;
304
350
305
351
return new NoiseSample
306
352
{
307
353
value = f3 * v ,
308
354
derivative = new Vector3
309
355
{
310
- x = g . x * f3 + v6f2 * x ,
311
- y = g . y * f3 + v6f2 * y ,
356
+ x = g . x * f3 + v6f2 * pointX ,
357
+ y = g . y * f3 + v6f2 * pointY ,
312
358
}
313
359
} ;
314
360
}
0 commit comments