Skip to content

Commit

Permalink
Editor: Improved pathtracer performance and robustness.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Jan 30, 2024
1 parent 85cd3cb commit 1b35d64
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
22 changes: 20 additions & 2 deletions editor/js/Viewport.Pathtracer.js
Expand Up @@ -18,13 +18,17 @@ function buildColorTexture( color ) {

function ViewportPathtracer( renderer ) {

let generator = null;
let pathtracer = null;
let quad = null;
let hdr = null;

function init( scene, camera ) {

if ( pathtracer === null ) {

generator = new PathTracingSceneGenerator();

pathtracer = new PathTracingRenderer( renderer );
pathtracer.setSize( renderer.domElement.offsetWidth, renderer.domElement.offsetHeight );
pathtracer.alpha = true;
Expand All @@ -42,7 +46,14 @@ function ViewportPathtracer( renderer ) {
pathtracer.material.backgroundBlur = scene.backgroundBlurriness;
pathtracer.reset();

const generator = new PathTracingSceneGenerator();
// TOFIX: If the scene is empty the generator crashes so we render a tiny cube (:

if ( scene.children.length === 0 ) {

scene = new THREE.Mesh( new THREE.BoxGeometry( 0.0001, 0.0001, 0.0001 ) );

}

const { bvh, textures, materials, lights } = generator.generate( scene );

const ptGeometry = bvh.geometry;
Expand Down Expand Up @@ -88,7 +99,14 @@ function ViewportPathtracer( renderer ) {

if ( environment && environment.isTexture === true ) {

ptMaterial.envMapInfo.updateFrom( scene.environment );
// Avoid calling envMapInfo() with the same hdr

if ( scene.environment !== hdr ) {

ptMaterial.envMapInfo.updateFrom( scene.environment );
hdr = scene.environment;

}

} else {

Expand Down
14 changes: 11 additions & 3 deletions editor/js/Viewport.js
Expand Up @@ -92,7 +92,7 @@ function Viewport( editor ) {

}

render();
render( true );

} );
transformControls.addEventListener( 'mouseDown', function () {
Expand Down Expand Up @@ -302,6 +302,8 @@ function Viewport( editor ) {
signals.editorCleared.add( function () {

controls.center.set( 0, 0, 0 );
pathtracer.reset();

render();

} );
Expand Down Expand Up @@ -697,7 +699,7 @@ function Viewport( editor ) {
sceneHelpers.visible = value;
transformControls.enabled = value;

render();
render( true );

} );

Expand Down Expand Up @@ -766,7 +768,13 @@ function Viewport( editor ) {
let startTime = 0;
let endTime = 0;

function render() {
function render( isHelper = false ) {

if ( editor.viewportShading === 'realistic' && isHelper === false ) {

pathtracer.init( scene, camera );

}

startTime = performance.now();

Expand Down

0 comments on commit 1b35d64

Please sign in to comment.