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

WebGPURenderer: Added double-side transmission 2/2 #29718

Merged
merged 2 commits into from
Oct 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/renderers/common/RenderList.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { DoubleSide } from '../../constants.js';

function painterSortStable( a, b ) {

if ( a.groupOrder !== b.groupOrder ) {
Expand Down Expand Up @@ -46,6 +48,14 @@ function reversePainterSortStable( a, b ) {

}

function needsDoublePass( material ) {

const hasTransmission = material.transmission > 0 || material.transmissionNode;

return hasTransmission && material.side === DoubleSide && material.forceSinglePass === false;

}

class RenderList {

constructor( lighting, scene, camera ) {
Expand All @@ -54,6 +64,7 @@ class RenderList {
this.renderItemsIndex = 0;

this.opaque = [];
this.transparentDoublePass = [];
this.transparent = [];
this.bundles = [];

Expand All @@ -72,6 +83,7 @@ class RenderList {
this.renderItemsIndex = 0;

this.opaque.length = 0;
this.transparentDoublePass.length = 0;
this.transparent.length = 0;
this.bundles.length = 0;

Expand Down Expand Up @@ -127,15 +139,35 @@ class RenderList {

if ( object.occlusionTest === true ) this.occlusionQueryCount ++;

( material.transparent === true || material.transmission > 0 ? this.transparent : this.opaque ).push( renderItem );
if ( material.transparent === true || material.transmission > 0 ) {

if ( needsDoublePass( material ) ) this.transparentDoublePass.push( renderItem );

this.transparent.push( renderItem );

} else {

this.opaque.push( renderItem );

}

}

unshift( object, geometry, material, groupOrder, z, group ) {

const renderItem = this.getNextRenderItem( object, geometry, material, groupOrder, z, group );

( material.transparent === true ? this.transparent : this.opaque ).unshift( renderItem );
if ( material.transparent === true || material.transmission > 0 ) {

if ( needsDoublePass( material ) ) this.transparentDoublePass.unshift( renderItem );

this.transparent.unshift( renderItem );

} else {

this.opaque.unshift( renderItem );

}

}

Expand All @@ -154,6 +186,7 @@ class RenderList {
sort( customOpaqueSort, customTransparentSort ) {

if ( this.opaque.length > 1 ) this.opaque.sort( customOpaqueSort || painterSortStable );
if ( this.transparentDoublePass.length > 1 ) this.transparentDoublePass.sort( customTransparentSort || reversePainterSortStable );
if ( this.transparent.length > 1 ) this.transparent.sort( customTransparentSort || reversePainterSortStable );

}
Expand Down
59 changes: 50 additions & 9 deletions src/renderers/common/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ class Renderer {
const lightsNode = renderList.lightsNode;

if ( this.opaque === true && opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
if ( this.transparent === true && transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
if ( this.transparent === true && transparentObjects.length > 0 ) this._renderTransparents( transparentObjects, camera, sceneRef, lightsNode );

// restore render tree

Expand Down Expand Up @@ -452,7 +452,7 @@ class Renderer {

const opaqueObjects = renderList.opaque;

if ( opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
if ( this.opaque === true && opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );

this._currentRenderBundle = null;

Expand Down Expand Up @@ -728,13 +728,14 @@ class Renderer {
const {
bundles,
lightsNode,
transparentDoublePass: transparentDoublePassObjects,
transparent: transparentObjects,
opaque: opaqueObjects
} = renderList;

if ( bundles.length > 0 ) this._renderBundles( bundles, sceneRef, lightsNode );
if ( this.opaque === true && opaqueObjects.length > 0 ) this._renderObjects( opaqueObjects, camera, sceneRef, lightsNode );
if ( this.transparent === true && transparentObjects.length > 0 ) this._renderObjects( transparentObjects, camera, sceneRef, lightsNode );
if ( this.transparent === true && transparentObjects.length > 0 ) this._renderTransparents( transparentObjects, transparentDoublePassObjects, camera, sceneRef, lightsNode );

// finish render pass

Expand Down Expand Up @@ -1438,7 +1439,47 @@ class Renderer {

}

_renderObjects( renderList, camera, scene, lightsNode ) {
_renderTransparents( renderList, doublePassList, camera, scene, lightsNode ) {

if ( doublePassList.length > 0 ) {

// render back side

for ( const { material } of doublePassList ) {

material.side = BackSide;

}

this._renderObjects( doublePassList, camera, scene, lightsNode, 'backSide' );

// render front side

for ( const { material } of doublePassList ) {

material.side = FrontSide;

}

this._renderObjects( renderList, camera, scene, lightsNode );

// restore

for ( const { material } of doublePassList ) {

material.side = DoubleSide;

}

} else {

this._renderObjects( renderList, camera, scene, lightsNode );

}

}

_renderObjects( renderList, camera, scene, lightsNode, passId = null ) {

// process renderable objects

Expand Down Expand Up @@ -1472,23 +1513,23 @@ class Renderer {

this.backend.updateViewport( this._currentRenderContext );

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

}

}

} else {

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

}

}

}

renderObject( object, scene, camera, geometry, material, group, lightsNode ) {
renderObject( object, scene, camera, geometry, material, group, lightsNode, passId = null ) {

let overridePositionNode;
let overrideFragmentNode;
Expand Down Expand Up @@ -1570,13 +1611,13 @@ class Renderer {
this._handleObjectFunction( object, material, scene, camera, lightsNode, group, 'backSide' ); // create backSide pass id

material.side = FrontSide;
this._handleObjectFunction( object, material, scene, camera, lightsNode, group ); // use default pass id
this._handleObjectFunction( object, material, scene, camera, lightsNode, group, passId ); // use default pass id

material.side = DoubleSide;

} else {

this._handleObjectFunction( object, material, scene, camera, lightsNode, group );
this._handleObjectFunction( object, material, scene, camera, lightsNode, group, passId );

}

Expand Down