@@ -4,12 +4,12 @@ namespace Gameframe.Procgen
44{
55 public static class NoiseGradients
66 {
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 } ;
99
10- public const int GradientsMask2D = 7 ;
10+ private const int GradientsMask2D = 7 ;
1111
12- public static readonly Vector2 [ ] Gradients2D =
12+ private static readonly Vector2 [ ] Gradients2D =
1313 {
1414 new Vector2 ( 1f , 0f ) ,
1515 new Vector2 ( - 1f , 0f ) ,
@@ -21,9 +21,10 @@ public static class NoiseGradients
2121 new Vector2 ( - 1f , - 1f ) . normalized
2222 } ;
2323
24- public const int GradientsMask3D = 15 ;
24+ private const int GradientsMask3D = 15 ;
25+ private const int SimplexGradientsMask3D = 31 ;
2526
26- public static readonly Vector3 [ ] Gradients3D =
27+ private static readonly Vector3 [ ] Gradients3D =
2728 {
2829 new Vector3 ( 1f , 1f , 0f ) ,
2930 new Vector3 ( - 1f , 1f , 0f ) ,
@@ -44,6 +45,43 @@ public static class NoiseGradients
4445 new Vector3 ( 0f , - 1f , - 1f )
4546 } ;
4647
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+
4785 private static float HashToGradient1D ( uint value )
4886 {
4987 return Gradients1D [ value & GradientsMask1D ] ;
@@ -54,7 +92,7 @@ public static float HashToGradient1D(int x, uint seed)
5492 return HashToGradient1D ( Hash1D ( x , seed ) ) ;
5593 }
5694
57- public static Vector2 HashToGradient2D ( uint value )
95+ private static Vector2 HashToGradient2D ( uint value )
5896 {
5997 return Gradients2D [ value & GradientsMask2D ] ;
6098 }
@@ -69,11 +107,21 @@ private static Vector3 HashToGradient3D(uint value)
69107 return Gradients3D [ value & GradientsMask3D ] ;
70108 }
71109
110+ private static Vector3 HashToSimplexGradient3D ( uint value )
111+ {
112+ return SimplexGradients3D [ value & SimplexGradientsMask3D ] ;
113+ }
114+
72115 public static Vector3 HashToGradient3D ( int x , int y , int z , uint seed )
73116 {
74117 return HashToGradient3D ( Hash3D ( x , y , z , seed ) ) ;
75118 }
76119
120+ public static Vector3 HashToSimplexGradient3D ( int x , int y , int z , uint seed )
121+ {
122+ return HashToSimplexGradient3D ( Hash3D ( x , y , z , seed ) ) ;
123+ }
124+
77125 public static uint Hash1D ( int value , uint seed )
78126 {
79127 return SquirrelEiserloh . Get1dNoiseUint ( value , seed ) ;
@@ -276,39 +324,37 @@ public static NoiseSample SampleGradient2D(float x, float y, uint seed, float fr
276324 }
277325
278326 sample . derivative *= frequency ;
279- //sample.value *= (SimplexScale2D / 43.71107f);
280- //sample.value = (sample.value + 1) * 0.5f;
281327
282- //sample.value *= SimplexScale2D; //32.99077
283- //Debug.Log($"Scale2D : {SimplexScale2D}");
328+ sample . value *= SimplexScale2D ;
329+ sample . value += 1 ;
330+ sample . value *= 0.5f ;
284331
285332 return sample ;
286333 }
287334
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 )
289336 {
290337 var unskew = ( ix + iy ) * SquaresToTriangles ;
291338
292- var x2 = x - ix + unskew ;
293- var y2 = y - iy + unskew ;
339+ var x2 = pointX - ix + unskew ;
340+ var y2 = pointY - iy + unskew ;
294341 var f = 0.5f - x2 * x2 - y2 * y2 ;
295- var f2 = f * f ;
296- var f3 = f * f2 ;
297-
298342
299343 if ( f > 0 )
300344 {
345+ var f2 = f * f ;
346+ var f3 = f * f2 ;
301347 var g = NoiseGradients . HashToGradient2D ( ix , iy , seed ) ;
302- var v = NoiseGradients . Dot2D ( g , x , y ) ;
348+ var v = NoiseGradients . Dot2D ( g , x2 , y2 ) ;
303349 var v6f2 = - 6f * v * f2 ;
304350
305351 return new NoiseSample
306352 {
307353 value = f3 * v ,
308354 derivative = new Vector3
309355 {
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 ,
312358 }
313359 } ;
314360 }
0 commit comments