Skip to content
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

Editor: Added pathtracer to viewport #27643

Merged
merged 1 commit into from
Jan 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion editor/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "../examples/jsm/"
"three/addons/": "../examples/jsm/",

"three/examples/": "../examples/",
"three-gpu-pathtracer": "https://unpkg.com/three-gpu-pathtracer@0.0.17/build/index.module.js",
"three-mesh-bvh": "https://unpkg.com/three-mesh-bvh@0.7.0/build/index.module.js"
}
}
</script>
Expand Down
4 changes: 2 additions & 2 deletions editor/js/Viewport.Controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ function ViewportControls( editor ) {
// shading

const shadingSelect = new UISelect();
shadingSelect.setOptions( { 'default': 'default', 'normals': 'normals', 'wireframe': 'wireframe' } );
shadingSelect.setValue( 'default' );
shadingSelect.setOptions( { 'realistic': 'realistic', 'solid': 'solid', 'normals': 'normals', 'wireframe': 'wireframe' } );
shadingSelect.setValue( 'solid' );
shadingSelect.onChange( function () {

editor.setViewportShading( this.getValue() );
Expand Down
98 changes: 98 additions & 0 deletions editor/js/Viewport.Pathtracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { FullScreenQuad } from 'three/examples/jsm/postprocessing/Pass.js';
import {
PathTracingSceneGenerator,
PathTracingRenderer,
PhysicalPathTracingMaterial
} from 'three-gpu-pathtracer';

function ViewportPathtracer( renderer ) {

let pathtracer = null;
let quad = null;

function init( scene, camera ) {

if ( pathtracer === null ) {

pathtracer = new PathTracingRenderer( renderer );
pathtracer.setSize( renderer.domElement.offsetWidth, renderer.domElement.offsetHeight );
pathtracer.alpha = true;
pathtracer.camera = camera;
pathtracer.material = new PhysicalPathTracingMaterial();
pathtracer.tiles.set( 3, 4 );

quad = new FullScreenQuad( new THREE.MeshBasicMaterial( {
map: pathtracer.target.texture,
blending: 5 // THREE.CustomBlending
} ) );

}

pathtracer.material.backgroundBlur = scene.backgroundBlurriness;
pathtracer.reset();

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

const ptGeometry = bvh.geometry;
const ptMaterial = pathtracer.material;

ptMaterial.bvh.updateFrom( bvh );
ptMaterial.attributesArray.updateFrom(
ptGeometry.attributes.normal,
ptGeometry.attributes.tangent,
ptGeometry.attributes.uv,
ptGeometry.attributes.color,
);
ptMaterial.materialIndexAttribute.updateFrom( ptGeometry.attributes.materialIndex );
ptMaterial.textures.setTextures( renderer, 2048, 2048, textures );
ptMaterial.materials.updateFrom( materials, textures );
ptMaterial.lights.updateFrom( lights );

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

ptMaterial.envMapInfo.updateFrom( scene.environment );

}

}

function setSize( width, height ) {

if ( pathtracer === null ) return;

pathtracer.setSize( width, height );
pathtracer.reset();

}

function update() {

if ( pathtracer === null ) return;

pathtracer.update();

renderer.autoClear = false;
quad.render( renderer );
renderer.autoClear = true;

}

function reset() {

if ( pathtracer === null ) return;

pathtracer.reset();

}

return {
init: init,
setSize: setSize,
update: update,
reset: reset
};

}

export { ViewportPathtracer };
19 changes: 18 additions & 1 deletion editor/js/Viewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { SetRotationCommand } from './commands/SetRotationCommand.js';
import { SetScaleCommand } from './commands/SetScaleCommand.js';

import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
import { ViewportPathtracer } from './Viewport.Pathtracer.js';

function Viewport( editor ) {

Expand All @@ -33,6 +34,7 @@ function Viewport( editor ) {

let renderer = null;
let pmremGenerator = null;
let pathtracer = null;

const camera = editor.camera;
const scene = editor.scene;
Expand Down Expand Up @@ -378,6 +380,8 @@ function Viewport( editor ) {
pmremGenerator = new THREE.PMREMGenerator( renderer );
pmremGenerator.compileEquirectangularShader();

pathtracer = new ViewportPathtracer( renderer );

container.dom.appendChild( renderer.domElement );

render();
Expand All @@ -398,6 +402,8 @@ function Viewport( editor ) {

signals.cameraChanged.add( function () {

pathtracer.reset();

render();

} );
Expand Down Expand Up @@ -641,7 +647,11 @@ function Viewport( editor ) {

switch ( viewportShading ) {

case 'default':
case 'realistic':
pathtracer.init( scene, camera );
break;

case 'solid':
scene.overrideMaterial = null;
break;

Expand All @@ -668,6 +678,7 @@ function Viewport( editor ) {
updateAspectRatio();

renderer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );
pathtracer.setSize( container.dom.offsetWidth, container.dom.offsetHeight );

render();

Expand Down Expand Up @@ -742,6 +753,12 @@ function Viewport( editor ) {

if ( needsUpdate === true ) render();

if ( editor.viewportShading === 'realistic' ) {

pathtracer.update();

}

}

//
Expand Down