-
Notifications
You must be signed in to change notification settings - Fork 207
/
heightfield-mapper.js
374 lines (328 loc) · 11.9 KB
/
heightfield-mapper.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import * as THREE from 'three';
import {getRenderer, camera} from './renderer.js';
import {WebaverseShaderMaterial} from './materials.js';
//
const maxAnisotropy = 16;
/* const fullScreenQuadGeometry = new THREE.PlaneBufferGeometry(2, 2);
const fullscreenVertexShader = `\
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4(position.xy, 0., 1.0);
}
`; */
//
const localVector = new THREE.Vector3();
// const localVector2 = new THREE.Vector3();
// const localVector2D = new THREE.Vector2();
// const localQuaternion = new THREE.Quaternion();
// const localMatrix = new THREE.Matrix4();
//
const _makeHeightfieldRenderTarget = (w, h) => new THREE.WebGLRenderTarget(w, h, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
// minFilter: THREE.NearestFilter,
// magFilter: THREE.NearestFilter,
format: THREE.RedFormat,
type: THREE.FloatType,
// wrapS: THREE.RepeatWrapping,
// wrapT: THREE.RepeatWrapping,
wrapS: THREE.ClampToEdgeWrapping,
wrapT: THREE.ClampToEdgeWrapping,
stencilBuffer: false,
anisotropy: maxAnisotropy,
// flipY: false,
});
export class HeightfieldMapper /* extends EventTarget */ {
constructor({
procGenInstance,
size,
debug = false,
}) {
// super();
this.procGenInstance = procGenInstance;
this.size = size;
// this.debug = debug;
this.lastUpdateCoord = new THREE.Vector3(NaN, NaN, NaN);
this.queued = false;
this.queuedPosition = new THREE.Vector3();
this.heightfieldRenderTarget = _makeHeightfieldRenderTarget(size, size);
this.heightfieldFourTapRenderTarget = _makeHeightfieldRenderTarget(size, size);
this.heightfieldTexture = new THREE.DataTexture(
new Float32Array(size * size),
size,
size,
THREE.RedFormat,
THREE.FloatType,
THREE.UVMapping,
THREE.ClampToEdgeWrapping,
THREE.ClampToEdgeWrapping,
THREE.LinearFilter,
THREE.LinearFilter,
);
this.heightfieldTexture.flipY = true;
this.heightfieldMinPosition = new THREE.Vector2();
// this.blankChunkData = new Float32Array(chunkSize * chunkSize);
/* this.heightfieldScene = (() => {
const chunkPlaneGeometry = new THREE.PlaneBufferGeometry(1, 1)
.rotateX(-Math.PI / 2)
.translate(0.5, 0, 0.5)
// .scale(this.chunkSize, 1, this.chunkSize);
const fullscreenMatrixVertexShader = `\
uniform float uHeightfieldSize;
varying vec2 vUv;
void main() {
vUv = uv;
vUv.y = 1. - vUv.y;
// vUv += 0.5 / uHeightfieldSize;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
`;
const fullscreenFragmentShader3 = `\
uniform sampler2D uHeightfieldDrawTexture;
uniform float uHeightfieldSize;
varying vec2 vUv;
void main() {
vec2 uv = vUv;
// uv.x += 0.5 / uHeightfieldSize;
// uv.y -= 0.5 / uHeightfieldSize;
// uv += 0.5 / uHeightfieldSize;
float heightValue = texture2D(uHeightfieldDrawTexture, uv).r;
gl_FragColor.rgb = vec3(heightValue);
gl_FragColor.a = 1.;
}
`;
const heightfieldDrawTexture = new THREE.DataTexture(
new Float32Array(chunkSize * chunkSize),
chunkSize,
chunkSize,
THREE.RedFormat,
THREE.FloatType
);
heightfieldDrawTexture.minFilter = THREE.LinearFilter;
heightfieldDrawTexture.magFilter = THREE.LinearFilter;
const fullscreenMaterial3 = new WebaverseShaderMaterial({
uniforms: {
uHeightfieldDrawTexture: {
value: heightfieldDrawTexture,
needsUpdate: true,
},
uHeightfieldSize: {
value: size,
needsUpdate: true,
},
},
vertexShader: fullscreenMatrixVertexShader,
fragmentShader: fullscreenFragmentShader3,
// side: THREE.DoubleSide,
});
const fullscreenQuadMesh3 = new THREE.Mesh(chunkPlaneGeometry, fullscreenMaterial3);
fullscreenQuadMesh3.frustumCulled = false;
const scene3 = new THREE.Scene();
scene3.add(fullscreenQuadMesh3);
scene3.update = (heightfieldLocalWritePosition, heightfield) => {
fullscreenQuadMesh3.position.copy(heightfieldLocalWritePosition);
fullscreenQuadMesh3.updateMatrixWorld();
heightfieldDrawTexture.image.data.set(heightfield);
heightfieldDrawTexture.needsUpdate = true;
fullscreenMaterial3.uniforms.uHeightfieldDrawTexture.value = heightfieldDrawTexture;
fullscreenMaterial3.uniforms.uHeightfieldDrawTexture.needsUpdate = true;
// console.log('got data', fullscreenQuadMesh3.position.x, fullscreenQuadMesh3.position.z, heightfieldDrawTexture.image.data);
// console.log('render heightfield', heightfieldLocalWritePosition.x, heightfieldLocalWritePosition.y, heightfield);
};
scene3.camera = new THREE.OrthographicCamera(0, size, 0, -size, -1000, 1000);
scene3.camera.quaternion.setFromAxisAngle(new THREE.Vector3(1, 0, 0), -Math.PI / 2);
scene3.camera.updateMatrixWorld();
return scene3;
})();
this.heightfieldFourTapScene = (() => {
const fullscreenFragmentShader = `\
uniform sampler2D uHeightfield;
uniform vec2 uHeightfieldBase;
uniform vec2 uHeightfieldMinPosition;
uniform float uHeightfieldSize;
varying vec2 vUv;
void main() {
vec2 pos2D = vUv;
vec2 posDiff = pos2D - 0.5 - (uHeightfieldMinPosition) / uHeightfieldSize;
vec2 uvHeightfield = posDiff;
uvHeightfield = mod(uvHeightfield, 1.);
gl_FragColor = texture2D(uHeightfield, uvHeightfield);
}
`;
const fourTapFullscreenMaterial = new WebaverseShaderMaterial({
uniforms: {
uHeightfield: {
value: this.heightfieldRenderTarget.texture,
needsUpdate: true,
},
uHeightfieldSize: {
value: size,
needsUpdate: true,
},
uHeightfieldMinPosition: {
value: new THREE.Vector2(),
needsUpdate: true,
}
},
vertexShader: fullscreenVertexShader,
fragmentShader: fullscreenFragmentShader,
});
const fourTapQuadMesh = new THREE.Mesh(fullScreenQuadGeometry, fourTapFullscreenMaterial);
fourTapQuadMesh.frustumCulled = false;
const scene = new THREE.Scene();
scene.add(fourTapQuadMesh);
scene.mesh = fourTapQuadMesh;
return scene;
})(); */
this.debugMesh = null;
if (debug) {
this.debugMesh = (() => {
const planeGeometry = new THREE.PlaneBufferGeometry(
size - 1,
size - 1,
size - 1,
size - 1
)
.translate(
-0.5,
0.5,
0
)
.rotateX(-Math.PI / 2);
const fullscreenVertexShader = `\
uniform sampler2D uHeightfield;
varying vec2 vUv;
void main() {
vUv = uv;
vec3 p = position;
float textureHeight = texture2D(uHeightfield, vUv).r;
p.y += textureHeight;
gl_Position = projectionMatrix * modelViewMatrix * vec4(p, 1.0);
}
`;
const fullscreenFragmentShader = `\
/* uniform sampler2D uHeightfield;
uniform vec2 uHeightfieldBase;
uniform vec2 uHeightfieldMinPosition;
uniform float uHeightfieldSize; */
varying vec2 vUv;
void main() {
/* vec2 pos2D = vUv;
vec2 posDiff = pos2D - 0.5 - (uHeightfieldMinPosition) / uHeightfieldSize;
vec2 uvHeightfield = posDiff;
uvHeightfield = mod(uvHeightfield, 1.); */
// gl_FragColor = vec4(vUv.x, 0., vUv.y, 0.5);
gl_FragColor = vec4(vUv.x, 0., vUv.y, 1.);
}
`;
const debugMaterial = new WebaverseShaderMaterial({
uniforms: {
uHeightfield: {
value: this.heightfieldTexture,
needsUpdate: true,
},
},
vertexShader: fullscreenVertexShader,
fragmentShader: fullscreenFragmentShader,
side: THREE.DoubleSide,
// transparent: true,
});
const debugMesh = new THREE.Mesh(planeGeometry, debugMaterial);
debugMesh.frustumCulled = false;
return debugMesh;
})();
}
}
update(position) {
if (!this.updating) {
(async () => {
this.updating = true;
const coord = localVector.copy(position);
coord.x = Math.floor(coord.x);
coord.y = 0;
coord.z = Math.floor(coord.z);
if (!this.lastUpdateCoord.equals(coord)) {
this.lastUpdateCoord.copy(coord);
if (this.debugMesh) {
const lod = 1;
const heightfield = await this.procGenInstance.dcWorkerManager.getHeightfieldRange(
coord.x - this.size / 2,
coord.z - this.size / 2,
this.size,
this.size,
lod
);
this.heightfieldTexture.image.data.set(heightfield);
this.heightfieldTexture.needsUpdate = true;
this.debugMesh.position.copy(coord);
this.debugMesh.updateMatrixWorld();
}
}
this.updating = false;
if (this.queued) {
this.queued = false;
this.update(this.queuedPosition);
}
})();
} else {
this.queued = true;
this.queuedPosition.copy(position);
}
}
renderHeightfieldUpdate(worldModPosition, heightfield) {
const renderer = getRenderer();
const context = renderer.getContext();
{
// update
this.heightfieldScene.update(worldModPosition, heightfield);
// push state
const oldRenderTarget = renderer.getRenderTarget();
context.disable(context.SAMPLE_ALPHA_TO_COVERAGE);
// render
renderer.setRenderTarget(this.heightfieldRenderTarget);
// renderer.clear();
renderer.render(this.heightfieldScene, this.heightfieldScene.camera);
// pop state
renderer.setRenderTarget(oldRenderTarget);
context.enable(context.SAMPLE_ALPHA_TO_COVERAGE);
}
}
/* clearHeightfieldChunk(worldModPosition) {
const renderer = getRenderer();
const context = renderer.getContext();
// const camera = useCamera();
{
// update
this.heightfieldScene.update(worldModPosition, this.blankChunkData);
// push state
const oldRenderTarget = renderer.getRenderTarget();
context.disable(context.SAMPLE_ALPHA_TO_COVERAGE);
// render
renderer.setRenderTarget(this.heightfieldRenderTarget);
// renderer.clear();
renderer.render(this.heightfieldScene, this.heightfieldScene.camera);
// pop state
renderer.setRenderTarget(oldRenderTarget);
context.enable(context.SAMPLE_ALPHA_TO_COVERAGE);
}
} */
/* updateFourTapHeightfield() {
const renderer = getRenderer();
const context = renderer.getContext();
{
// update
// heightfieldFourTapScene.update();
// push state
const oldRenderTarget = renderer.getRenderTarget();
context.disable(context.SAMPLE_ALPHA_TO_COVERAGE);
// render
renderer.setRenderTarget(this.heightfieldFourTapRenderTarget);
// renderer.clear();
renderer.render(this.heightfieldFourTapScene, camera);
// pop state
renderer.setRenderTarget(oldRenderTarget);
context.enable(context.SAMPLE_ALPHA_TO_COVERAGE);
}
} */
}