Line data Source code
1 : // Utility function used in both noises as an ease curve
2 : function easeCurve(t)
3 : {
4 0 : return t*t*t*(t*(t*6-15)+10);
5 : }
6 :
7 : // Find mod of number but only positive values
8 : function modPos(num, m)
9 : {
10 0 : let p = num % m;
11 0 : if (p < 0)
12 0 : p += m;
13 :
14 0 : return p;
15 : }
16 :
17 : /**
18 : * @class Noise2D
19 : * @brief Class representing 2D noise with a given base frequency
20 : *
21 : * @param {number} freq - The base frequency (grid resolution) of the noise.
22 : * Determines the number of gradient vectors along each axis.
23 : */
24 : function Noise2D(freq)
25 : {
26 0 : freq = Math.floor(freq);
27 0 : this.freq = freq;
28 0 : this.grads = [];
29 :
30 0 : for (let i=0; i < freq; ++i)
31 : {
32 0 : this.grads[i] = [];
33 0 : for (let j=0; j < freq; ++j)
34 : {
35 0 : const a = randomAngle();
36 0 : this.grads[i][j] = new Vector2D(Math.cos(a), Math.sin(a));
37 : }
38 : }
39 : }
40 :
41 7 : Noise2D.prototype.get = function(x, y)
42 : {
43 0 : x *= this.freq;
44 0 : y *= this.freq;
45 :
46 0 : const ix = modPos(Math.floor(x), this.freq);
47 0 : const iy = modPos(Math.floor(y), this.freq);
48 :
49 0 : const fx = x - ix;
50 0 : const fy = y - iy;
51 :
52 0 : const ix1 = (ix+1) % this.freq;
53 0 : const iy1 = (iy+1) % this.freq;
54 :
55 0 : const s = this.grads[ix][iy].dot(new Vector2D(fx, fy));
56 0 : const t = this.grads[ix1][iy].dot(new Vector2D(fx-1, fy));
57 0 : const u = this.grads[ix][iy1].dot(new Vector2D(fx, fy-1));
58 0 : const v = this.grads[ix1][iy1].dot(new Vector2D(fx-1, fy-1));
59 :
60 0 : const ex = easeCurve(fx);
61 0 : const ey = easeCurve(fy);
62 0 : const a = s + ex*(t-s);
63 0 : const b = u + ex*(v-u);
64 0 : return (a + ey*(b-a)) * 0.5 + 0.5;
65 : };
66 :
67 : /**
68 : * @class Noise3D
69 : * @brief Class representing 3D noise with given base frequencies
70 : *
71 : * @param {number} freq - The base frequency (grid resolution) of the noise.
72 : * Determines the number of gradient vectors along the first to axis.
73 : * @param {number} vfreq - The base frequency for the third dimension.
74 : */
75 : function Noise3D(freq, vfreq)
76 : {
77 0 : freq = Math.floor(freq);
78 0 : vfreq = Math.floor(vfreq);
79 0 : this.freq = freq;
80 0 : this.vfreq = vfreq;
81 0 : this.grads = [];
82 :
83 0 : for (let i=0; i < freq; ++i)
84 : {
85 0 : this.grads[i] = [];
86 0 : for (let j=0; j < freq; ++j)
87 : {
88 0 : this.grads[i][j] = [];
89 0 : for (let k=0; k < vfreq; ++k)
90 : {
91 0 : const v = new Vector3D();
92 0 : do
93 : {
94 0 : v.set(randFloat(-1, 1), randFloat(-1, 1), randFloat(-1, 1));
95 : }
96 : while (v.lengthSquared() > 1 || v.lengthSquared() < 0.1);
97 :
98 0 : v.normalize();
99 :
100 0 : this.grads[i][j][k] = v;
101 : }
102 : }
103 : }
104 : }
105 :
106 7 : Noise3D.prototype.get = function(x, y, z)
107 : {
108 0 : x *= this.freq;
109 0 : y *= this.freq;
110 0 : z *= this.vfreq;
111 :
112 0 : const ix =modPos(Math.floor(x), this.freq);
113 0 : const iy = modPos(Math.floor(y), this.freq);
114 0 : const iz = modPos(Math.floor(z), this.vfreq);
115 :
116 0 : const fx = x - ix;
117 0 : const fy = y - iy;
118 0 : const fz = z - iz;
119 :
120 0 : const ix1 = (ix+1) % this.freq;
121 0 : const iy1 = (iy+1) % this.freq;
122 0 : const iz1 = (iz+1) % this.vfreq;
123 :
124 0 : const s0 = this.grads[ix][iy][iz].dot(new Vector3D(fx, fy, fz));
125 0 : const t0 = this.grads[ix1][iy][iz].dot(new Vector3D(fx-1, fy, fz));
126 0 : const u0 = this.grads[ix][iy1][iz].dot(new Vector3D(fx, fy-1, fz));
127 0 : const v0 = this.grads[ix1][iy1][iz].dot(new Vector3D(fx-1, fy-1, fz));
128 :
129 0 : const s1 = this.grads[ix][iy][iz1].dot(new Vector3D(fx, fy, fz-1));
130 0 : const t1 = this.grads[ix1][iy][iz1].dot(new Vector3D(fx-1, fy, fz-1));
131 0 : const u1 = this.grads[ix][iy1][iz1].dot(new Vector3D(fx, fy-1, fz-1));
132 0 : const v1 = this.grads[ix1][iy1][iz1].dot(new Vector3D(fx-1, fy-1, fz-1));
133 :
134 0 : const ex = easeCurve(fx);
135 0 : const ey = easeCurve(fy);
136 0 : const ez = easeCurve(fz);
137 :
138 0 : const a0 = s0 + ex*(t0-s0);
139 0 : const b0 = u0 + ex*(v0-u0);
140 0 : const c0 = a0 + ey*(b0-a0);
141 :
142 0 : const a1 = s1 + ex*(t1-s1);
143 0 : const b1 = u1 + ex*(v1-u1);
144 0 : const c1 = a1 + ey*(b1-a1);
145 :
146 0 : return (c0 + ez*(c1-c0)) * 0.5 + 0.5;
147 : };
|