Skip to content

Commit

Permalink
WebGLRenderer: Made render loop code simpler and more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdoob committed Jun 8, 2020
1 parent cfa0429 commit 5288ed2
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ function WebGLRenderer( parameters ) {

_vector3 = new Vector3();

const _emptyScene = { background: null, fog: null, environment: null, overrideMaterial: null, isScene: true };

function getTargetPixelRatio() {

return _currentRenderTarget === null ? _pixelRatio : 1;
Expand Down Expand Up @@ -713,11 +715,9 @@ function WebGLRenderer( parameters ) {

};

const tempScene = new Scene();

this.renderBufferDirect = function ( camera, scene, geometry, material, object, group ) {

if ( scene === null ) scene = tempScene; // renderBufferDirect second parameter used to be fog (could be null)
if ( scene === null ) scene = _emptyScene; // renderBufferDirect second parameter used to be fog (could be null)

const frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 );

Expand Down Expand Up @@ -1139,14 +1139,14 @@ function WebGLRenderer( parameters ) {

}

if ( ! ( camera && camera.isCamera ) ) {
if ( camera !== undefined && camera.isCamera !== true ) {

console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
return;

}

if ( _isContextLost ) return;
if ( _isContextLost === true ) return;

// reset caching for this frame

Expand All @@ -1164,14 +1164,14 @@ function WebGLRenderer( parameters ) {

if ( camera.parent === null ) camera.updateMatrixWorld();

if ( xr.enabled && xr.isPresenting ) {
if ( xr.enabled === true && xr.isPresenting === true ) {

camera = xr.getCamera( camera );

}

//
if ( scene.isScene ) scene.onBeforeRender( _this, scene, camera, renderTarget || _currentRenderTarget );
if ( scene.isScene === true ) scene.onBeforeRender( _this, scene, camera, renderTarget || _currentRenderTarget );

currentRenderState = renderStates.get( scene, camera );
currentRenderState.init();
Expand All @@ -1197,19 +1197,19 @@ function WebGLRenderer( parameters ) {

//

if ( _clippingEnabled ) _clipping.beginShadows();
if ( _clippingEnabled === true ) _clipping.beginShadows();

const shadowsArray = currentRenderState.state.shadowsArray;

shadowMap.render( shadowsArray, scene, camera );

currentRenderState.setupLights( camera );

if ( _clippingEnabled ) _clipping.endShadows();
if ( _clippingEnabled === true ) _clipping.endShadows();

//

if ( this.info.autoReset ) this.info.reset();
if ( this.info.autoReset === true ) this.info.reset();

if ( renderTarget !== undefined ) {

Expand All @@ -1226,28 +1226,12 @@ function WebGLRenderer( parameters ) {
const opaqueObjects = currentRenderList.opaque;
const transparentObjects = currentRenderList.transparent;

if ( scene.overrideMaterial ) {

const overrideMaterial = scene.overrideMaterial;

if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera, overrideMaterial );
if ( transparentObjects.length ) renderObjects( transparentObjects, scene, camera, overrideMaterial );

} else {

// opaque pass (front-to-back order)

if ( opaqueObjects.length ) renderObjects( opaqueObjects, scene, camera );

// transparent pass (back-to-front order)

if ( transparentObjects.length ) renderObjects( transparentObjects, scene, camera );

}
if ( opaqueObjects.length > 0 ) renderObjects( opaqueObjects, scene, camera );
if ( transparentObjects.length > 0 ) renderObjects( transparentObjects, scene, camera );

//

if ( scene.isScene ) scene.onAfterRender( _this, scene, camera );
if ( scene.isScene === true ) scene.onAfterRender( _this, scene, camera );

//

Expand Down Expand Up @@ -1403,15 +1387,17 @@ function WebGLRenderer( parameters ) {

}

function renderObjects( renderList, scene, camera, overrideMaterial ) {
function renderObjects( renderList, scene, camera ) {

const overrideMaterial = scene.isScene === true ? scene.overrideMaterial : null;

for ( let i = 0, l = renderList.length; i < l; i ++ ) {

const renderItem = renderList[ i ];

const object = renderItem.object;
const geometry = renderItem.geometry;
const material = overrideMaterial === undefined ? renderItem.material : overrideMaterial;
const material = overrideMaterial === null ? renderItem.material : overrideMaterial;
const group = renderItem.group;

if ( camera.isArrayCamera ) {
Expand Down Expand Up @@ -1481,6 +1467,8 @@ function WebGLRenderer( parameters ) {

function initMaterial( material, scene, object ) {

if ( scene.isScene !== true ) scene = _emptyScene; // scene could be a Mesh, Line, Points, ...

const materialProperties = properties.get( material );

const lights = currentRenderState.state.lights;
Expand Down Expand Up @@ -1622,6 +1610,8 @@ function WebGLRenderer( parameters ) {

function setProgram( camera, scene, material, object ) {

if ( scene.isScene !== true ) scene = _emptyScene; // scene could be a Mesh, Line, Points, ...

textures.resetTextureUnits();

const fog = scene.fog;
Expand All @@ -1631,9 +1621,9 @@ function WebGLRenderer( parameters ) {
const materialProperties = properties.get( material );
const lights = currentRenderState.state.lights;

if ( _clippingEnabled ) {
if ( _clippingEnabled === true ) {

if ( _localClippingEnabled || camera !== _currentCamera ) {
if ( _localClippingEnabled === true || camera !== _currentCamera ) {

const useCache =
camera === _currentCamera &&
Expand Down

0 comments on commit 5288ed2

Please sign in to comment.