Skip to content

Commit

Permalink
added realistic shading support for image rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ycw committed May 21, 2024
1 parent 2e7e9d4 commit fa63147
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 15 deletions.
72 changes: 58 additions & 14 deletions editor/js/Sidebar.Project.Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as THREE from 'three';

import { UIBreak, UIButton, UIInteger, UIPanel, UIRow, UISelect, UIText } from './libs/ui.js';

// import { ViewportPathtracer } from './Viewport.Pathtracer.js';
import { ViewportPathtracer } from './Viewport.Pathtracer.js';

function SidebarProjectImage( editor ) {

Expand All @@ -19,17 +19,34 @@ function SidebarProjectImage( editor ) {
// Shading

const shadingRow = new UIRow();
// container.add( shadingRow );
container.add( shadingRow );

shadingRow.add( new UIText( strings.getKey( 'sidebar/project/shading' ) ).setClass( 'Label' ) );

const shadingTypeSelect = new UISelect().setOptions( {
0: 'Solid',
1: 'Realistic'
} ).setWidth( '125px' );
shadingTypeSelect.setValue( 0 );
0: 'SOLID / WebGLRenderer',
1: 'REALISTIC / WebGLPathTracer'
} ).setWidth( '170px' ).setTextTransform( 'unset' ).onChange( refreshShadingRow ).setValue( 0 );
shadingRow.add( shadingTypeSelect );

const pathTracerMinSamples = 3;
const pathTracerMaxSamples = 65536;
const samplesNumber = new UIInteger( 16 ).setRange( pathTracerMinSamples, pathTracerMaxSamples );

const samplesRow = new UIRow();
samplesRow.add( new UIText( 'Sample Count' ).setClass( 'Label' ) ); // TODO: l10n
samplesRow.add( samplesNumber );

container.add( samplesRow );

function refreshShadingRow() {

samplesRow.setHidden( shadingTypeSelect.getValue() !== '1' );

}

refreshShadingRow();

// Resolution

const resolutionRow = new UIRow();
Expand All @@ -47,6 +64,8 @@ function SidebarProjectImage( editor ) {

// Render

let pathTracer = null;

const renderButton = new UIButton( strings.getKey( 'sidebar/project/render' ) );
renderButton.setWidth( '170px' );
renderButton.setMarginLeft( '120px' );
Expand Down Expand Up @@ -106,9 +125,10 @@ function SidebarProjectImage( editor ) {

renderer.render( scene, camera );
renderer.dispose();
output.document.body.appendChild( canvas );

break;
/*

case 1: // REALISTIC

const status = document.createElement( 'div' );
Expand All @@ -120,26 +140,50 @@ function SidebarProjectImage( editor ) {
status.style.fontSize = '12px';
output.document.body.appendChild( status );

const pathtracer = new ViewportPathtracer( renderer );
pathtracer.init( scene, camera );
pathtracer.setSize( imageWidth.getValue(), imageHeight.getValue());
if ( pathTracer === null ) {

pathTracer = new ViewportPathtracer( renderer );
pathTracer.init( scene, camera );

} else {

pathTracer.setScene( scene, camera );

}

pathTracer.setSize( imageWidth.getValue(), imageHeight.getValue() );

const maxSamples = Math.max( pathTracerMinSamples, Math.min( pathTracerMaxSamples, samplesNumber.getValue() ) );

function animate() {

if ( output.closed === true ) return;

requestAnimationFrame( animate );
const samples = Math.floor( pathTracer.getSamples() ) + 1;

if ( samples < maxSamples ) {

requestAnimationFrame( animate );

}

pathTracer.update();

const progress = Math.floor( samples / maxSamples * 100 );

status.textContent = `${ samples } / ${ maxSamples } ( ${ progress }% )`;

if ( progress === 100 ) {

pathtracer.update();
status.textContent += ' ✓';

// status.textContent = Math.floor( samples );
}

}

animate();

break;
*/

}

Expand Down
11 changes: 10 additions & 1 deletion editor/js/Viewport.Pathtracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,23 @@ function ViewportPathtracer( renderer ) {

}

function getSamples() {

if ( pathTracer === null ) return;

return pathTracer.samples;

}

return {
init: init,
setSize: setSize,
setBackground: setBackground,
setEnvironment: setEnvironment,
updateMaterials: updateMaterials,
update: update,
reset: reset
reset: reset,
getSamples: getSamples
};

}
Expand Down

0 comments on commit fa63147

Please sign in to comment.