3 namespace droid.Runtime.Utilities.Sampling {
10 const int _n = 0x1000;
12 int[] _p =
new int[_b + _b + 2];
13 float[,] _g3 =
new float[_b + _b + 2, 3];
14 float[,] _g2 =
new float[_b + _b + 2, 2];
15 float[] _g1 =
new float[_b + _b + 2];
17 float s_curve(
float t) {
return t * t * (3.0F - 2.0F * t); }
19 float Lerp(
float t,
float a,
float b) {
return a + t * (b - a); }
21 void Setup(
float value, out
int b0, out
int b1, out
float r0, out
float r1) {
29 float At2(
float rx,
float ry,
float x,
float y) {
return rx * x + ry * y; }
30 float At3(
float rx,
float ry,
float rz,
float x,
float y,
float z) {
return rx * x + ry * y + rz * z; }
34 this.Setup(arg, out var bx0, out var bx1, out var rx0, out var rx1);
36 sx = this.s_curve(rx0);
37 u = rx0 * this._g1[this._p[bx0]];
38 v = rx1 * this._g1[this._p[bx1]];
40 return (this.Lerp(sx, u, v));
43 public float Noise(
float x,
float y) {
44 int b00, b10, b01, b11;
45 float sx, sy, a, b, u, v;
48 this.Setup(x, out var bx0, out var bx1, out var rx0, out var rx1);
49 this.Setup(y, out var by0, out var by1, out var ry0, out var ry1);
54 b00 = this._p[i + by0];
55 b10 = this._p[j + by0];
56 b01 = this._p[i + by1];
57 b11 = this._p[j + by1];
59 sx = this.s_curve(rx0);
60 sy = this.s_curve(ry0);
62 u = this.At2(rx0, ry0, this._g2[b00, 0], this._g2[b00, 1]);
63 v = this.At2(rx1, ry0, this._g2[b10, 0], this._g2[b10, 1]);
64 a = this.Lerp(sx, u, v);
66 u = this.At2(rx0, ry1, this._g2[b01, 0], this._g2[b01, 1]);
67 v = this.At2(rx1, ry1, this._g2[b11, 0], this._g2[b11, 1]);
68 b = this.Lerp(sx, u, v);
70 return this.Lerp(sy, a, b);
73 public float Noise(
float x,
float y,
float z) {
74 int b00, b10, b01, b11;
75 float sy, sz, a, b, c, d, t, u, v;
78 this.Setup(x, out var bx0, out var bx1, out var rx0, out var rx1);
79 this.Setup(y, out var by0, out var by1, out var ry0, out var ry1);
80 this.Setup(z, out var bz0, out var bz1, out var rz0, out var rz1);
85 b00 = this._p[i + by0];
86 b10 = this._p[j + by0];
87 b01 = this._p[i + by1];
88 b11 = this._p[j + by1];
90 t = this.s_curve(rx0);
91 sy = this.s_curve(ry0);
92 sz = this.s_curve(rz0);
94 u = this.At3(rx0, ry0, rz0, this._g3[b00 + bz0, 0], this._g3[b00 + bz0, 1], this._g3[b00 + bz0, 2]);
95 v = this.At3(rx1, ry0, rz0, this._g3[b10 + bz0, 0], this._g3[b10 + bz0, 1], this._g3[b10 + bz0, 2]);
96 a = this.Lerp(t, u, v);
98 u = this.At3(rx0, ry1, rz0, this._g3[b01 + bz0, 0], this._g3[b01 + bz0, 1], this._g3[b01 + bz0, 2]);
99 v = this.At3(rx1, ry1, rz0, this._g3[b11 + bz0, 0], this._g3[b11 + bz0, 1], this._g3[b11 + bz0, 2]);
100 b = this.Lerp(t, u, v);
102 c = this.Lerp(sy, a, b);
104 u = this.At3(rx0, ry0, rz1, this._g3[b00 + bz1, 0], this._g3[b00 + bz1, 2], this._g3[b00 + bz1, 2]);
105 v = this.At3(rx1, ry0, rz1, this._g3[b10 + bz1, 0], this._g3[b10 + bz1, 1], this._g3[b10 + bz1, 2]);
106 a = this.Lerp(t, u, v);
108 u = this.At3(rx0, ry1, rz1, this._g3[b01 + bz1, 0], this._g3[b01 + bz1, 1], this._g3[b01 + bz1, 2]);
109 v = this.At3(rx1, ry1, rz1, this._g3[b11 + bz1, 0], this._g3[b11 + bz1, 1], this._g3[b11 + bz1, 2]);
110 b = this.Lerp(t, u, v);
112 d = this.Lerp(sy, a, b);
114 return this.Lerp(sz, c, d);
117 static void Normalize2(ref
float x, ref
float y) {
120 s = (float)Math.Sqrt(x * x + y * y);
125 void Normalize3(ref
float x, ref
float y, ref
float z) {
127 s = (float)Math.Sqrt(x * x + y * y + z * z);
137 var rnd =
new Random(seed);
139 for (i = 0; i < _b; i++) {
141 this._g1[i] = (float)(rnd.Next(_b + _b) - _b) / _b;
143 for (j = 0; j < 2; j++) {
144 this._g2[i, j] = (float)(rnd.Next(_b + _b) - _b) / _b;
147 Normalize2(ref this._g2[i, 0], ref this._g2[i, 1]);
149 for (j = 0; j < 3; j++) {
150 this._g3[i, j] = (float)(rnd.Next(_b + _b) - _b) / _b;
153 this.Normalize3(ref this._g3[i, 0], ref this._g3[i, 1], ref this._g3[i, 2]);
158 this._p[i] = this._p[j = rnd.Next(_b)];
162 for (i = 0; i < _b + 2; i++) {
163 this._p[_b + i] = this._p[i];
164 this._g1[_b + i] = this._g1[i];
165 for (j = 0; j < 2; j++) {
166 this._g2[_b + i, j] = this._g2[i, j];
169 for (j = 0; j < 3; j++) {
170 this._g3[_b + i, j] = this._g3[i, j];
UnityEngine.Random Random
float Noise(float x, float y, float z)
float Noise(float x, float y)