-
Notifications
You must be signed in to change notification settings - Fork 3
/
Perlin Noise.js
184 lines (156 loc) · 9.52 KB
/
Perlin Noise.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
(function (Scratch) {
'use strict';
const icon = 'data:image/svg+xml;base64,PHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHdpZHRoPSIyMjUuMzU0ODEiIGhlaWdodD0iMjI1LjM1NDgiIHZpZXdCb3g9IjAsMCwyMjUuMzU0ODEsMjI1LjM1NDgiPjxnIHRyYW5zZm9ybT0idHJhbnNsYXRlKC0xMjcuMzIyODUsLTY3LjMyMjYpIj48ZyBkYXRhLXBhcGVyLWRhdGE9InsmcXVvdDtpc1BhaW50aW5nTGF5ZXImcXVvdDs6dHJ1ZX0iIGZpbGwtcnVsZT0ibm9uemVybyIgc3Ryb2tlLXdpZHRoPSIwIiBzdHJva2UtbGluZWNhcD0iYnV0dCIgc3Ryb2tlLWxpbmVqb2luPSJtaXRlciIgc3Ryb2tlLW1pdGVybGltaXQ9IjEwIiBzdHJva2UtZGFzaGFycmF5PSIiIHN0cm9rZS1kYXNob2Zmc2V0PSIwIiBzdHlsZT0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AuMDAyNDMgMC4xMTc4NSwtMC4wMTcyMSAwLjE3OTIzLC0wLjAxNzIxYzIuODA2NSwwIDQuNzAyNDksMS45OTA0MiA0Ljk1ODczLDQuNzk0MjdjMC4yNTYyNCwyLjgwMzg1IDAsNDkuNDYyMjcgMCw0OS40NjIyN2MwLDAgNDAuMzQyMTcsLTIzLjM3ODYyIDQyLjc3NTMxLC0yNC42ODU3NWMyLjQzMzE0LC0xLjMwNzEzIDQuODQyOTksLTAuNDExMDIgNi41NjkwNywxLjc1NzY4YzEuNzI2MDcsMi4xNjg3MSAxMy40OTE2OSwyMy40MzYyMSAxMy41MjM2MiwyMy40ODc3M2MwLjAzMTkzLDAuMDUxNTIgMC4wNzM2NywwLjA5MzMgMC4xMDMxMiwwLjE0NDgyYzEuNDA0NDksMi40MzM0MSAwLjYyNzksNS4wNzI2MiAtMS42NzQxOSw2LjY5OTIxYy0yLjMwMjA5LDEuNjI2NiAtNDIuNjg5MzgsMjQuNjQ2NDMgLTQyLjY4OTM4LDI0LjY0NjQzbDQyLjQwOTUsMjQuNjc4MzZjMC4wMjQ1OSwwLjAxNDcyIDAuMDU0MDQsMC4wMjIwNyAwLjA3ODU4LDAuMDM2NzljMi4xMTM1OSwxLjIyMjUxIDIuOTc1MiwzLjc2MDggMi4xNzAwNiw1Ljk3NzUxYy0wLjgwNTE0LDIuMjE2NzEgLTEyLjM0ODM0LDIxLjU4NjkyIC0xMy45NjA1OCwyNC4zMjQ4OXoiIGZpbGw9IiNmZmZmZmYiIHN0cm9rZT0iI2I4MjAyMiIvPjwvZz48L2c+PC9zdmc+PCEtLXJvdGF0aW9uQ2VudGVyOjExMi42NzcxNDU6MTEyLjY3NzQwNS0tPg==';
class PerlinNoise {
constructor() {
this.seed = Math.random();
}
getInfo() {
return {
id: 'perlinnoise',
name: 'Perlin Noise',
color1: '#f2c911',
color2: '#b69507',
menuIconURI: icon,
blocks: [
{
opcode: 'setSeed',
blockType: Scratch.BlockType.COMMAND,
text: 'Set Perlin noise seed to [SEED]',
arguments: {
SEED: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0',
},
},
},
{
opcode: 'getNoise1D',
blockType: Scratch.BlockType.REPORTER,
text: 'Perlin noise 1D at [X]',
arguments: {
X: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0',
},
},
},
{
opcode: 'getNoise2D',
blockType: Scratch.BlockType.REPORTER,
text: 'Perlin noise 2D at x:[X] y:[Y]',
arguments: {
X: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0',
},
Y: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0',
},
},
},
{
opcode: 'getNoise3D',
blockType: Scratch.BlockType.REPORTER,
text: 'Perlin noise 3D at x:[X] y:[Y] z:[Z]',
arguments: {
X: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0',
},
Y: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0',
},
Z: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: '0',
},
},
},
],
};
}
setSeed(args) {
this.seed = args.SEED;
}
getNoise1D(args) {
const x = args.X;
return this.perlinNoise1D(x);
}
getNoise2D(args) {
const x = args.X;
const y = args.Y;
return this.perlinNoise2D(x, y);
}
getNoise3D(args) {
const x = args.X;
const y = args.Y;
const z = args.Z;
return this.perlinNoise3D(x, y, z);
}
perlinNoise1D(x) {
const floorX = Math.floor(x);
const t = x - floorX;
const tRemapSmoothstep = t * t * (3 - 2 * t);
const xMin = floorX;
const xMax = xMin + 1;
const y = this.lerp(this.random(xMin), this.random(xMax), tRemapSmoothstep);
return y;
}
perlinNoise2D(x, y) {
const floorX = Math.floor(x);
const floorY = Math.floor(y);
const tX = x - floorX;
const tY = y - floorY;
const tRemapSmoothstepX = tX * tX * (3 - 2 * tX);
const tRemapSmoothstepY = tY * tY * (3 - 2 * tY);
const xMin = floorX;
const xMax = xMin + 1;
const yMin = floorY;
const yMax = yMin + 1;
const sx = this.lerp(this.random(xMin, yMin), this.random(xMax, yMin), tRemapSmoothstepX);
const sy = this.lerp(this.random(xMin, yMax), this.random(xMax, yMax), tRemapSmoothstepX);
const value = this.lerp(sx, sy, tRemapSmoothstepY);
return value;
}
perlinNoise3D(x, y, z) {
const floorX = Math.floor(x);
const floorY = Math.floor(y);
const floorZ = Math.floor(z);
const tX = x - floorX;
const tY = y - floorY;
const tZ = z - floorZ;
const tRemapSmoothstepX = tX * tX * (3 - 2 * tX);
const tRemapSmoothstepY = tY * tY * (3 - 2 * tY);
const tRemapSmoothstepZ = tZ * tZ * (3 - 2 * tZ);
const xMin = floorX;
const xMax = xMin + 1;
const yMin = floorY;
const yMax = yMin + 1;
const zMin = floorZ;
const zMax = zMin + 1;
const sx = this.lerp(this.random(xMin, yMin, zMin), this.random(xMax, yMin, zMin), tRemapSmoothstepX);
const sy = this.lerp(this.random(xMin, yMax, zMin), this.random(xMax, yMax, zMin), tRemapSmoothstepX);
const sz = this.lerp(this.random(xMin, yMin, zMax), this.random(xMax, yMin, zMax), tRemapSmoothstepX);
const sxy = this.lerp(sx, sy, tRemapSmoothstepY);
const szw = this.lerp(sz, this.random(xMin, yMax, zMax), tRemapSmoothstepY);
const value = this.lerp(sxy, szw, tRemapSmoothstepZ);
return value;
}
random(...args) {
let value = 0;
for (const arg of args) {
value += arg * 10000;
}
value += this.seed * 10000;
value = Math.sin(value) * 10000;
return value - Math.floor(value);
}
lerp(a, b, t) {
return a + t * (b - a);
}
}
Scratch.extensions.register(new PerlinNoise());
})(window.Scratch = window.Scratch || {});