From 4209409f6d2aebff54f20cedef0746d224541b7f Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Sun, 17 Mar 2024 00:41:54 +0900 Subject: [PATCH 1/3] Add dynamic low res render fallback option --- src/core/WebGLPathTracer.js | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/core/WebGLPathTracer.js b/src/core/WebGLPathTracer.js index c42f476a2..e79f6f05f 100644 --- a/src/core/WebGLPathTracer.js +++ b/src/core/WebGLPathTracer.js @@ -88,6 +88,7 @@ export class WebGLPathTracer { this._renderer = renderer; this._generator = null; this._pathTracer = new PathTracingRenderer( renderer ); + this._lowResPathTracer = new PathTracingRenderer( renderer ); this._quad = new FullScreenQuad( new MeshBasicMaterial( { map: null, blending: CustomBlending, @@ -96,6 +97,8 @@ export class WebGLPathTracer { this.renderScale = 1; this.synchronizeRenderSize = true; + this.dynamicLowRes = true; + this.lowResScale = 0.15; this.rasterizeScene = true; this.renderToCanvas = true; this.textureSize = new Vector2( 1024, 1024 ); @@ -135,6 +138,7 @@ export class WebGLPathTracer { const renderer = this._renderer; const pathTracer = this._pathTracer; + const lowResPathTracer = this._lowResPathTracer; const material = pathTracer.material; // TODO: adjust this so we don't have to create a new tracer every time and the @@ -222,6 +226,8 @@ export class WebGLPathTracer { // camera update pathTracer.camera = camera; + lowResPathTracer.camera = camera; + lowResPathTracer.material = pathTracer.material; // save previously used items this._previousScene = scene; @@ -237,19 +243,35 @@ export class WebGLPathTracer { this._updateScale(); + const lowResPathTracer = this._lowResPathTracer; const pathTracer = this._pathTracer; pathTracer.update(); if ( this.renderToCanvas ) { - if ( this.samples < 1 && this.rasterizeScene ) { + const renderer = this._renderer; + const quad = this._quad; + if ( this.dynamicLowRes ) { + + if ( lowResPathTracer.samples < 1 ) { + + lowResPathTracer.update(); + + } + + quad.material.map = lowResPathTracer.target.texture; + + const autoClear = renderer.autoClear; + renderer.autoClear = false; + quad.render( renderer ); + renderer.autoClear = autoClear; + + } else if ( this.samples < 1 && this.rasterizeScene ) { this.rasterizeSceneCallback(); } - const renderer = this._renderer; - const quad = this._quad; quad.material.map = pathTracer.target.texture; const autoClear = renderer.autoClear; @@ -264,6 +286,7 @@ export class WebGLPathTracer { reset() { this._pathTracer.reset(); + this._lowResPathTracer.reset(); } @@ -293,7 +316,9 @@ export class WebGLPathTracer { this._pathTracer.getSize( _resolution ); if ( _resolution.x !== w || _resolution.y !== h ) { + const lowResScale = this.lowResScale; this._pathTracer.setSize( w, h ); + this._lowResPathTracer.setSize( Math.floor( w * lowResScale ), Math.floor( h * lowResScale ) ); } From 128235d120774b4c869c80df1902a7533d376548 Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Sun, 17 Mar 2024 00:47:41 +0900 Subject: [PATCH 2/3] Rearrange --- src/core/WebGLPathTracer.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/core/WebGLPathTracer.js b/src/core/WebGLPathTracer.js index e79f6f05f..0e2b08a09 100644 --- a/src/core/WebGLPathTracer.js +++ b/src/core/WebGLPathTracer.js @@ -95,10 +95,11 @@ export class WebGLPathTracer { premultipliedAlpha: renderer.getContextAttributes().premultipliedAlpha, } ) ); + // options + this.dynamicLowRes = false; + this.lowResScale = 0.15; this.renderScale = 1; this.synchronizeRenderSize = true; - this.dynamicLowRes = true; - this.lowResScale = 0.15; this.rasterizeScene = true; this.renderToCanvas = true; this.textureSize = new Vector2( 1024, 1024 ); @@ -251,6 +252,7 @@ export class WebGLPathTracer { const renderer = this._renderer; const quad = this._quad; + const autoClear = renderer.autoClear; if ( this.dynamicLowRes ) { if ( lowResPathTracer.samples < 1 ) { @@ -259,12 +261,9 @@ export class WebGLPathTracer { } - quad.material.map = lowResPathTracer.target.texture; - - const autoClear = renderer.autoClear; renderer.autoClear = false; + quad.material.map = lowResPathTracer.target.texture; quad.render( renderer ); - renderer.autoClear = autoClear; } else if ( this.samples < 1 && this.rasterizeScene ) { @@ -272,10 +271,8 @@ export class WebGLPathTracer { } - quad.material.map = pathTracer.target.texture; - - const autoClear = renderer.autoClear; renderer.autoClear = false; + quad.material.map = pathTracer.target.texture; quad.render( renderer ); renderer.autoClear = autoClear; From 77586fac0529600152294178e6e3c9b19fbce50b Mon Sep 17 00:00:00 2001 From: Garrett Johnson Date: Sun, 17 Mar 2024 09:32:30 +0900 Subject: [PATCH 3/3] Updates --- example/primitives.js | 1 - src/core/PathTracingRenderer.js | 1 + src/core/WebGLPathTracer.js | 30 +++++++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/example/primitives.js b/example/primitives.js index 977791b9e..d730f2a07 100644 --- a/example/primitives.js +++ b/example/primitives.js @@ -71,7 +71,6 @@ function animate() { requestAnimationFrame( animate ); // update the camera and render one sample - camera.updateMatrixWorld(); renderer.renderSample(); } diff --git a/src/core/PathTracingRenderer.js b/src/core/PathTracingRenderer.js index 338ce6c02..fee24eefb 100644 --- a/src/core/PathTracingRenderer.js +++ b/src/core/PathTracingRenderer.js @@ -67,6 +67,7 @@ function* renderTask() { material.cameraWorldMatrix.copy( camera.matrixWorld ); material.invProjectionMatrix.copy( camera.projectionMatrixInverse ); + material.physicalCamera.updateFrom( camera ); // Perspective camera (default) let cameraType = 0; diff --git a/src/core/WebGLPathTracer.js b/src/core/WebGLPathTracer.js index 0e2b08a09..41c11f03f 100644 --- a/src/core/WebGLPathTracer.js +++ b/src/core/WebGLPathTracer.js @@ -113,23 +113,45 @@ export class WebGLPathTracer { [ 'getPixelRatio', 'setPixelRatio', + 'setDrawingBufferSize', + 'getDrawingBufferSize', 'getSize', 'setSize', + ].forEach( key => { + + this[ key ] = ( ...args ) => { + + this._renderer[ key ]( ...args ); + if ( this.renderToCanvas ) { + + this.reset(); + + } + + }; + + } ); + + // Functions that require always resetting the render + [ 'setViewport', 'getViewport', 'getScissor', 'setScissor', 'getScissorTest', 'setScissorTest', - 'setDrawingBufferSize', - 'getDrawingBufferSize', 'getClearAlpha', 'setClearAlpha', 'getClearColor', 'setClearColor', ].forEach( key => { - this[ key ] = ( ...args ) => this._renderer[ key ]( ...args ); + this[ key ] = ( ...args ) => { + + this._renderer[ key ]( ...args ); + this.reset(); + + }; } ); @@ -226,6 +248,8 @@ export class WebGLPathTracer { } // camera update + // TODO: these cameras should only be set once so we don't depend on movement + camera.updateMatrixWorld(); pathTracer.camera = camera; lowResPathTracer.camera = camera; lowResPathTracer.material = pathTracer.material;