Skip to content

Commit

Permalink
Renderer: .setRenderObjectFunction() (mrdoob#27068)
Browse files Browse the repository at this point in the history
* Renderer.setRenderObject()

* Fix .castShadow

* Rename `setRenderObject()` to `setRenderObjectFunction()`

* rename parameter
  • Loading branch information
sunag authored Oct 30, 2023
1 parent 2a50f4b commit 8611778
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
18 changes: 17 additions & 1 deletion examples/jsm/nodes/lighting/AnalyticLightNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,25 @@ class AnalyticLightNode extends LightingNode {

light.shadow.updateMatrices( light );

const currentRenderTarget = renderer.getRenderTarget();
const currentRenderObjectFunction = renderer.getRenderObjectFunction();

renderer.setRenderObjectFunction( ( object, ...params ) => {

if ( object.castShadow === true ) {

renderer.renderObject( object, ...params );

}

} );

renderer.setRenderTarget( rtt );

renderer.render( scene, light.shadow.camera );
renderer.setRenderTarget( null );

renderer.setRenderTarget( currentRenderTarget );
renderer.setRenderObjectFunction( currentRenderObjectFunction );

scene.overrideMaterial = null;

Expand Down
31 changes: 25 additions & 6 deletions examples/jsm/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ class Renderer {
this._activeCubeFace = 0;
this._activeMipmapLevel = 0;

this._renderObjectFunction = null;
this._currentRenderObjectFunction = null;

this._initialized = false;
this._initPromise = null;

Expand Down Expand Up @@ -179,7 +182,8 @@ class Renderer {
const nodeFrame = this._nodes.nodeFrame;

const previousRenderId = nodeFrame.renderId;
const previousRenderState = this._currentRenderContext;
const previousRenderContext = this._currentRenderContext;
const previousRenderObjectFunction = this._currentRenderObjectFunction;

//

Expand All @@ -191,6 +195,7 @@ class Renderer {
const activeMipmapLevel = this._activeMipmapLevel;

this._currentRenderContext = renderContext;
this._currentRenderObjectFunction = this._renderObjectFunction || this.renderObject;

nodeFrame.renderId ++;

Expand Down Expand Up @@ -332,7 +337,9 @@ class Renderer {
// restore render tree

nodeFrame.renderId = previousRenderId;
this._currentRenderContext = previousRenderState;

this._currentRenderContext = previousRenderContext;
this._currentRenderObjectFunction = previousRenderObjectFunction;

this._lastRenderContext = renderContext;

Expand Down Expand Up @@ -642,6 +649,18 @@ class Renderer {

}

setRenderObjectFunction( renderObjectFunction ) {

this._renderObjectFunction = renderObjectFunction;

}

getRenderObjectFunction() {

return this._renderObjectFunction;

}

async compute( computeNodes ) {

if ( this._initialized === false ) await this.init();
Expand Down Expand Up @@ -825,7 +844,7 @@ class Renderer {
const renderItem = renderList[ i ];

// @TODO: Add support for multiple materials per object. This will require to extract
// the material from the renderItem object and pass it with its group data to _renderObject().
// the material from the renderItem object and pass it with its group data to renderObject().

const { object, geometry, material, group } = renderItem;

Expand All @@ -850,23 +869,23 @@ class Renderer {

this.backend.updateViewport( this._currentRenderContext );

this._renderObject( object, scene, camera2, geometry, material, group, lightsNode );
this._currentRenderObjectFunction( object, scene, camera2, geometry, material, group, lightsNode );

}

}

} else {

this._renderObject( object, scene, camera, geometry, material, group, lightsNode );
this._currentRenderObjectFunction( object, scene, camera, geometry, material, group, lightsNode );

}

}

}

_renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
renderObject( object, scene, camera, geometry, material, group, lightsNode ) {

material = scene.overrideMaterial !== null ? scene.overrideMaterial : material;

Expand Down
5 changes: 0 additions & 5 deletions examples/webgpu_compute_particles_rain.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
const { innerWidth, innerHeight } = window;

camera = new THREE.PerspectiveCamera( 60, innerWidth / innerHeight, .1, 110 );
camera.layers.enable( 2 ); // @TODO: Fix .castShadow and remove it
camera.position.set( 40, 8, 0 );
camera.lookAt( 0, 0, 0 );

Expand Down Expand Up @@ -234,8 +233,6 @@
const rainParticles = new THREE.Mesh( new THREE.PlaneGeometry( .1, 2 ), rainMaterial );
rainParticles.isInstancedMesh = true;
rainParticles.count = instanceCount;
rainParticles.layers.disableAll();
rainParticles.layers.enable( 2 );
scene.add( rainParticles );

// ripple
Expand Down Expand Up @@ -276,8 +273,6 @@
const rippleParticles = new THREE.Mesh( rippleGeometry, rippleMaterial );
rippleParticles.isInstancedMesh = true;
rippleParticles.count = instanceCount;
rippleParticles.layers.disableAll();
rippleParticles.layers.enable( 2 );
scene.add( rippleParticles );

// floor geometry
Expand Down

0 comments on commit 8611778

Please sign in to comment.