Skip to content

Commit a0ae675

Browse files
committed
Some changes to the shader to enable dynamic day/night changes
1 parent 37ee65e commit a0ae675

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

BlueMapCore/src/main/webroot/js/libs/BlueMap.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ import {
3434
FrontSide,
3535
Mesh,
3636
MeshBasicMaterial,
37-
MeshLambertMaterial,
38-
NormalBlending,
3937
NearestFilter,
4038
NearestMipmapLinearFilter,
4139
PerspectiveCamera,
@@ -120,6 +118,8 @@ export default class BlueMap {
120118
z: this.settings[this.map]["startPos"]["z"]
121119
};
122120

121+
this.targetSunLightStrength = 1;
122+
123123
this.controls.setTileSize(this.settings[this.map]['hires']['tileSize']);
124124
this.controls.resetPosition();
125125
this.controls.targetPosition.set(startPos.x, this.controls.targetPosition.y, startPos.z);
@@ -229,13 +229,27 @@ export default class BlueMap {
229229
render = () => {
230230
requestAnimationFrame(this.render);
231231

232+
//update controls
232233
if (this.controls.update()) this.updateFrame = true;
233234

235+
//update lighting
236+
let targetLight = 1;
237+
if (this.camera.position.y < 400){
238+
targetLight = this.targetSunLightStrength;
239+
}
240+
if (Math.abs(targetLight - this.sunLightStrength.value) > 0.01) {
241+
this.sunLightStrength.value += (targetLight - this.sunLightStrength.value) * 0.1;
242+
this.updateFrame = true;
243+
}
244+
245+
//don't render if nothing has changed
234246
if (!this.updateFrame) return;
235247
this.updateFrame = false;
236248

249+
//render event
237250
document.dispatchEvent(new Event('bluemap-update-frame'));
238251

252+
//render
239253
this.skyboxCamera.rotation.copy(this.camera.rotation);
240254
this.skyboxCamera.updateProjectionMatrix();
241255

@@ -291,6 +305,11 @@ export default class BlueMap {
291305
this.updateFrame = true;
292306
this.quality = 1;
293307

308+
this.targetSunLightStrength = 1;
309+
this.sunLightStrength = {
310+
value: this.targetSunLightStrength
311+
};
312+
294313
this.renderer = new WebGLRenderer({
295314
alpha: true,
296315
antialias: true,
@@ -307,23 +326,10 @@ export default class BlueMap {
307326
this.skyboxCamera.updateProjectionMatrix();
308327

309328
this.skyboxScene = new Scene();
310-
this.skyboxScene.ambient = new AmbientLight(0xffffff, 1);
311-
this.skyboxScene.add(this.skyboxScene.ambient);
312329
this.skyboxScene.add(this.createSkybox());
313330

314331
this.lowresScene = new Scene();
315-
this.lowresScene.ambient = new AmbientLight(0xffffff, 0.6);
316-
this.lowresScene.add(this.lowresScene.ambient);
317-
this.lowresScene.sunLight = new DirectionalLight(0xccccbb, 0.7);
318-
this.lowresScene.sunLight.position.set(1, 5, 3);
319-
this.lowresScene.add(this.lowresScene.sunLight);
320-
321332
this.hiresScene = new Scene();
322-
this.hiresScene.ambient = new AmbientLight(0xffffff, 1);
323-
this.hiresScene.add(this.hiresScene.ambient);
324-
this.hiresScene.sunLight = new DirectionalLight(0xccccbb, 0.2);
325-
this.hiresScene.sunLight.position.set(1, 5, 3);
326-
this.hiresScene.add(this.hiresScene.sunLight);
327333

328334
this.element.append(this.renderer.domElement);
329335
this.handleContainerResize();
@@ -391,9 +397,7 @@ export default class BlueMap {
391397
type: 't',
392398
value: texture
393399
},
394-
sunlightStrength: {
395-
value: 1
396-
}
400+
sunlightStrength: this.sunLightStrength
397401
};
398402

399403
let material = new ShaderMaterial({
@@ -419,7 +423,12 @@ export default class BlueMap {
419423
}
420424

421425
async loadLowresMaterial() {
426+
let uniforms = {
427+
sunlightStrength: this.sunLightStrength
428+
};
429+
422430
this.lowresMaterial = new ShaderMaterial({
431+
uniforms: uniforms,
423432
vertexShader: LOWRES_VERTEX_SHADER,
424433
fragmentShader: LOWRES_FRAGMENT_SHADER,
425434
transparent: false,

BlueMapCore/src/main/webroot/js/libs/shaders/LowresFragmentShader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525

2626
const LOWRES_FRAGMENT_SHADER = `
27+
uniform float sunlightStrength;
28+
2729
varying vec3 vPosition;
2830
varying vec3 vNormal;
2931
varying vec2 vUv;
@@ -35,6 +37,8 @@ void main() {
3537
float diff = sqrt(max(dot(vNormal, vec3(0.3637, 0.7274, 0.5819)), 0.0)) * 0.4 + 0.6;
3638
color *= diff;
3739
40+
color *= sunlightStrength;
41+
3842
gl_FragColor = color;
3943
}
4044
`;

0 commit comments

Comments
 (0)