Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Draw the water mirror without local lights and leave the cascade redraw to the lit frame (part of #316)

Nguyen Xuan Ana0041e0730b9
diff --git a/src/renderer.js b/src/renderer.js
index c7175fe1..148940e9 100644
--- a/src/renderer.js
+++ b/src/renderer.js
@@ -2098,7 +2098,13 @@ export class Renderer67 {
mat4.invert(this.inverseViewProjection, this.viewProjection);
extractFrustumPlanes(this.viewProjection, this.frustumPlanes);
this.viewForward = [-this.view[2], -this.view[6], -this.view[10]];
- this.shadowCascades?.update(this, casterChanged);
+ // The water mirror is a cheap draw: it reuses the cascades of the last lit frame and leaves their redraw to the lit frame that follows.
+ if (frame.reflectionTarget && this.shadowCascades?.texture) {
+ this.cascadeCastersChanged ||= casterChanged;
+ } else {
+ this.shadowCascades?.update(this, casterChanged || Boolean(this.cascadeCastersChanged));
+ this.cascadeCastersChanged = false;
+ }
// The water reflection addon draws the scene a second time into its own smaller target (frame.reflectionTarget).
const mirrorTarget = frame.reflectionTarget;
context.bindFramebuffer(context.FRAMEBUFFER, mirrorTarget ? mirrorTarget.framebuffer : passMode ? this.passSupport.framebuffer : this.benchmark ? null : this.multisampleFramebuffer);
@@ -2128,11 +2134,11 @@ export class Renderer67 {
context.uniform1f(this.uniforms.exposure, exposure);
context.uniform1f(this.uniforms.inspection, inspection ? 1 : 0);
context.uniform1f(this.uniforms.hdrEnabled, this.hdr ? 1 : 0);
- context.uniform1i(this.uniforms.pointLightCount, this.lighting.pointCount);
+ context.uniform1i(this.uniforms.pointLightCount, frame.reflectionTarget ? 0 : this.lighting.pointCount);
context.uniform4fv(this.uniforms['pointLightPositions[0]'], this.lighting.positions);
context.uniform4fv(this.uniforms['pointLightColors[0]'], this.lighting.colors);
context.uniform1fv(this.uniforms['pointLightRadii[0]'], this.lighting.radii);
- this.applyLocalLightUniforms();
+ this.applyLocalLightUniforms(Boolean(frame.reflectionTarget));
context.uniform3fv(this.uniforms.keyColor, this.lighting.keyColor);
context.uniform1f(this.uniforms.fillIntensity, this.lighting.fillIntensity);
context.uniform1f(this.uniforms.ambientIntensity, this.lighting.ambientIntensity);
@@ -2196,17 +2202,18 @@ export class Renderer67 {
this.gpuMilliseconds = this.gpuTimer.collect();
}
- applyLocalLightUniforms() {
+ // The water mirror draws without local lights: it is blurred and rippled, and the sun, sky and fog colour carry the picture.
+ applyLocalLightUniforms(withoutLights = false) {
const context = this.context;
const lighting = this.lighting;
const uniforms = this.uniforms;
context.uniform1fv(uniforms['pointLightShadowTiles[0]'], lighting.pointShadows);
- context.uniform1i(uniforms.spotLightCount, lighting.spotCount);
+ context.uniform1i(uniforms.spotLightCount, withoutLights ? 0 : lighting.spotCount);
context.uniform4fv(uniforms['spotLightPositions[0]'], lighting.spotPositions);
context.uniform4fv(uniforms['spotLightColors[0]'], lighting.spotColors);
context.uniform4fv(uniforms['spotLightDirections[0]'], lighting.spotDirections);
context.uniform4fv(uniforms['spotLightParameters[0]'], lighting.spotParameters);
- context.uniform1i(uniforms.areaLightCount, lighting.areaCount);
+ context.uniform1i(uniforms.areaLightCount, withoutLights ? 0 : lighting.areaCount);
context.uniform4fv(uniforms['areaLightPositions[0]'], lighting.areaPositions);
context.uniform4fv(uniforms['areaLightColors[0]'], lighting.areaColors);
context.uniform4fv(uniforms['areaLightNormals[0]'], lighting.areaNormals);
diff --git a/tests/water-reflection.test.js b/tests/water-reflection.test.js
index c0d2b3fe..52e19f57 100644
--- a/tests/water-reflection.test.js
+++ b/tests/water-reflection.test.js
@@ -3,6 +3,7 @@ import assert from 'node:assert/strict';
import { box, sphere } from '../src/geometry.js';
import { applyWaterReflection, attachWaterReflection, findWaterSurface } from '../src/water-reflection.js';
import { createFragmentShader } from '../src/shaders.js';
+import { Renderer67 } from '../src/renderer.js';
const cube = box(0, 1);
@@ -128,3 +129,15 @@ test('a render pass frame skips the mirror image and the next lit frame draws it
assert.equal(mirrorDraws(), 1);
detach();
});
+
+test('the mirror draw leaves out spot and area lights and the lit frame sends them again', () => {
+ const counts = {};
+ const context = new Proxy({}, { get: (_, name) => (name === 'uniform1i' ? (location, value) => { counts[location] = value; } : () => {}) });
+ const renderer = Object.create(Renderer67.prototype);
+ const lighting = { spotCount: 3, areaCount: 2, shadowCasters: [], pointShadows: [], spotPositions: [], spotColors: [], spotDirections: [], spotParameters: [], areaPositions: [], areaColors: [], areaNormals: [], areaTangents: [] };
+ Object.assign(renderer, { context, lighting, uniforms: { spotLightCount: 'spot', areaLightCount: 'area' } });
+ renderer.applyLocalLightUniforms(true);
+ assert.deepEqual(counts, { spot: 0, area: 0 });
+ renderer.applyLocalLightUniforms();
+ assert.deepEqual(counts, { spot: 3, area: 2 });
+});