Skip to content

WebGLPathTracer: Add support for dynamic low res render #565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion example/primitives.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function animate() {
requestAnimationFrame( animate );

// update the camera and render one sample
camera.updateMatrixWorld();
renderer.renderSample();

}
Expand Down
1 change: 1 addition & 0 deletions src/core/PathTracingRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
64 changes: 55 additions & 9 deletions src/core/WebGLPathTracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,16 @@ 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,
premultipliedAlpha: renderer.getContextAttributes().premultipliedAlpha,
} ) );

// options
this.dynamicLowRes = false;
this.lowResScale = 0.15;
this.renderScale = 1;
this.synchronizeRenderSize = true;
this.rasterizeScene = true;
Expand All @@ -109,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();

};

} );

Expand All @@ -135,6 +161,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
Expand Down Expand Up @@ -221,7 +248,11 @@ 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;

// save previously used items
this._previousScene = scene;
Expand All @@ -237,23 +268,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;
const autoClear = renderer.autoClear;
if ( this.dynamicLowRes ) {

if ( lowResPathTracer.samples < 1 ) {

lowResPathTracer.update();

}

renderer.autoClear = false;
quad.material.map = lowResPathTracer.target.texture;
quad.render( renderer );

} 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;
renderer.autoClear = false;
quad.material.map = pathTracer.target.texture;
quad.render( renderer );
renderer.autoClear = autoClear;

Expand All @@ -264,6 +307,7 @@ export class WebGLPathTracer {
reset() {

this._pathTracer.reset();
this._lowResPathTracer.reset();

}

Expand Down Expand Up @@ -293,7 +337,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 ) );

}

Expand Down