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

WebGLRenderer: Support custom painter sort functions. #16909

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions docs/api/en/renderers/WebGLRenderer.html
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,16 @@ <h3>[method:null setScissorTest]( [param:Boolean boolean] )</h3>
scissor area will be affected by further renderer actions.
</p>

<h3>[method:null setOpaqueSort]( [param:Function method] )</h3>
<p>
Sets the costum opaque sort function for the WebGLRenderLists. Pass null to use the default painterSortStable function.
WestLangley marked this conversation as resolved.
Show resolved Hide resolved
</p>

<h3>[method:null setTransparentSort]( [param:Function method] )</h3>
<p>
Sets the costum transparent sort function for the WebGLRenderLists. Pass null to use the default reversePainterSortStable function. In some cases the transparent materials sort may get rendered in the wrong order. This issue may cause materials to rendere behind other materials instead of the front of them and vice versa.
</p>

<h3>[method:null setSize]( [param:Integer width], [param:Integer height], [param:Boolean updateStyle] )</h3>
<p>
Resizes the output canvas to (width, height) with device pixel ratio taken into account,
Expand Down
10 changes: 10 additions & 0 deletions src/renderers/WebGLRenderer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ export class WebGLRenderer implements Renderer {
*/
setScissorTest( enable: boolean ): void;

/**
* Sets the costum opaque sort function for the WebGLRenderLists. Pass null to use the default painterSortStable function.
*/
setOpaqueSort( method: Function ): void;

/**
* Sets the costum transparent sort function for the WebGLRenderLists. Pass null to use the default reversePainterSortStable function.
*/
setTransparentSort( method: Function ): void;

/**
* Returns a THREE.Color instance with the current clear color.
*/
Expand Down
16 changes: 15 additions & 1 deletion src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ function WebGLRenderer( parameters ) {

var background, morphtargets, bufferRenderer, indexedBufferRenderer;

var opaqueSort, transparentSort;
WestLangley marked this conversation as resolved.
Show resolved Hide resolved

var utils;

function initGLContext() {
Expand Down Expand Up @@ -505,6 +507,18 @@ function WebGLRenderer( parameters ) {

};

this.setOpaqueSort = function ( method ) {

opaqueSort = typeof method === 'function' ? method : null;
WestLangley marked this conversation as resolved.
Show resolved Hide resolved

};

this.setTransparentSort = function ( method ) {

transparentSort = typeof method === 'function' ? method : null;

};

// Clearing

this.getClearColor = function () {
Expand Down Expand Up @@ -1154,7 +1168,7 @@ function WebGLRenderer( parameters ) {

if ( _this.sortObjects === true ) {

currentRenderList.sort();
currentRenderList.sort( opaqueSort, transparentSort );

}

Expand Down
6 changes: 3 additions & 3 deletions src/renderers/webgl/WebGLRenderLists.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ function WebGLRenderList() {

}

function sort() {
function sort( costumOpaqueSort, costumTransparentSort ) {

if ( opaque.length > 1 ) opaque.sort( painterSortStable );
if ( transparent.length > 1 ) transparent.sort( reversePainterSortStable );
if ( opaque.length > 1 ) opaque.sort( costumOpaqueSort || painterSortStable );
if ( transparent.length > 1 ) transparent.sort( costumTransparentSort || reversePainterSortStable );

}

Expand Down