Skip to content

Commit 3be85ba

Browse files
authored
Merge pull request #565 from gkjohnson/low-res-render
WebGLPathTracer: Add support for dynamic low res render
2 parents 8a2d693 + 77586fa commit 3be85ba

File tree

3 files changed

+56
-10
lines changed

3 files changed

+56
-10
lines changed

example/primitives.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ function animate() {
7171
requestAnimationFrame( animate );
7272

7373
// update the camera and render one sample
74-
camera.updateMatrixWorld();
7574
renderer.renderSample();
7675

7776
}

src/core/PathTracingRenderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ function* renderTask() {
6767

6868
material.cameraWorldMatrix.copy( camera.matrixWorld );
6969
material.invProjectionMatrix.copy( camera.projectionMatrixInverse );
70+
material.physicalCamera.updateFrom( camera );
7071

7172
// Perspective camera (default)
7273
let cameraType = 0;

src/core/WebGLPathTracer.js

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,16 @@ export class WebGLPathTracer {
8888
this._renderer = renderer;
8989
this._generator = null;
9090
this._pathTracer = new PathTracingRenderer( renderer );
91+
this._lowResPathTracer = new PathTracingRenderer( renderer );
9192
this._quad = new FullScreenQuad( new MeshBasicMaterial( {
9293
map: null,
9394
blending: CustomBlending,
9495
premultipliedAlpha: renderer.getContextAttributes().premultipliedAlpha,
9596
} ) );
9697

98+
// options
99+
this.dynamicLowRes = false;
100+
this.lowResScale = 0.15;
97101
this.renderScale = 1;
98102
this.synchronizeRenderSize = true;
99103
this.rasterizeScene = true;
@@ -109,23 +113,45 @@ export class WebGLPathTracer {
109113
[
110114
'getPixelRatio',
111115
'setPixelRatio',
116+
'setDrawingBufferSize',
117+
'getDrawingBufferSize',
112118
'getSize',
113119
'setSize',
120+
].forEach( key => {
121+
122+
this[ key ] = ( ...args ) => {
123+
124+
this._renderer[ key ]( ...args );
125+
if ( this.renderToCanvas ) {
126+
127+
this.reset();
128+
129+
}
130+
131+
};
132+
133+
} );
134+
135+
// Functions that require always resetting the render
136+
[
114137
'setViewport',
115138
'getViewport',
116139
'getScissor',
117140
'setScissor',
118141
'getScissorTest',
119142
'setScissorTest',
120-
'setDrawingBufferSize',
121-
'getDrawingBufferSize',
122143
'getClearAlpha',
123144
'setClearAlpha',
124145
'getClearColor',
125146
'setClearColor',
126147
].forEach( key => {
127148

128-
this[ key ] = ( ...args ) => this._renderer[ key ]( ...args );
149+
this[ key ] = ( ...args ) => {
150+
151+
this._renderer[ key ]( ...args );
152+
this.reset();
153+
154+
};
129155

130156
} );
131157

@@ -135,6 +161,7 @@ export class WebGLPathTracer {
135161

136162
const renderer = this._renderer;
137163
const pathTracer = this._pathTracer;
164+
const lowResPathTracer = this._lowResPathTracer;
138165
const material = pathTracer.material;
139166

140167
// TODO: adjust this so we don't have to create a new tracer every time and the
@@ -221,7 +248,11 @@ export class WebGLPathTracer {
221248
}
222249

223250
// camera update
251+
// TODO: these cameras should only be set once so we don't depend on movement
252+
camera.updateMatrixWorld();
224253
pathTracer.camera = camera;
254+
lowResPathTracer.camera = camera;
255+
lowResPathTracer.material = pathTracer.material;
225256

226257
// save previously used items
227258
this._previousScene = scene;
@@ -237,23 +268,35 @@ export class WebGLPathTracer {
237268

238269
this._updateScale();
239270

271+
const lowResPathTracer = this._lowResPathTracer;
240272
const pathTracer = this._pathTracer;
241273
pathTracer.update();
242274

243275
if ( this.renderToCanvas ) {
244276

245-
if ( this.samples < 1 && this.rasterizeScene ) {
277+
const renderer = this._renderer;
278+
const quad = this._quad;
279+
const autoClear = renderer.autoClear;
280+
if ( this.dynamicLowRes ) {
281+
282+
if ( lowResPathTracer.samples < 1 ) {
283+
284+
lowResPathTracer.update();
285+
286+
}
287+
288+
renderer.autoClear = false;
289+
quad.material.map = lowResPathTracer.target.texture;
290+
quad.render( renderer );
291+
292+
} else if ( this.samples < 1 && this.rasterizeScene ) {
246293

247294
this.rasterizeSceneCallback();
248295

249296
}
250297

251-
const renderer = this._renderer;
252-
const quad = this._quad;
253-
quad.material.map = pathTracer.target.texture;
254-
255-
const autoClear = renderer.autoClear;
256298
renderer.autoClear = false;
299+
quad.material.map = pathTracer.target.texture;
257300
quad.render( renderer );
258301
renderer.autoClear = autoClear;
259302

@@ -264,6 +307,7 @@ export class WebGLPathTracer {
264307
reset() {
265308

266309
this._pathTracer.reset();
310+
this._lowResPathTracer.reset();
267311

268312
}
269313

@@ -293,7 +337,9 @@ export class WebGLPathTracer {
293337
this._pathTracer.getSize( _resolution );
294338
if ( _resolution.x !== w || _resolution.y !== h ) {
295339

340+
const lowResScale = this.lowResScale;
296341
this._pathTracer.setSize( w, h );
342+
this._lowResPathTracer.setSize( Math.floor( w * lowResScale ), Math.floor( h * lowResScale ) );
297343

298344
}
299345

0 commit comments

Comments
 (0)