Skip to content

Commit

Permalink
WebGLClipping: Refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mugen87 committed Aug 16, 2020
1 parent 662e7e1 commit 483196c
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 33 deletions.
29 changes: 13 additions & 16 deletions src/renderers/WebGLRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ function WebGLRenderer( parameters ) {

// clipping

const _clipping = new WebGLClipping();
let _clippingEnabled = false;
let _localClippingEnabled = false;

Expand Down Expand Up @@ -252,7 +251,7 @@ function WebGLRenderer( parameters ) {

let extensions, capabilities, state, info;
let properties, textures, cubemaps, attributes, geometries, objects;
let programCache, materials, renderLists, renderStates;
let programCache, materials, renderLists, renderStates, clipping;

let background, morphtargets, bufferRenderer, indexedBufferRenderer;

Expand Down Expand Up @@ -294,11 +293,11 @@ function WebGLRenderer( parameters ) {
geometries = new WebGLGeometries( _gl, attributes, info, bindingStates );
objects = new WebGLObjects( _gl, geometries, attributes, info );
morphtargets = new WebGLMorphtargets( _gl );
programCache = new WebGLPrograms( _this, cubemaps, extensions, capabilities, bindingStates );
clipping = new WebGLClipping( properties );
programCache = new WebGLPrograms( _this, cubemaps, extensions, capabilities, bindingStates, clipping );
materials = new WebGLMaterials( properties );
renderLists = new WebGLRenderLists( properties );
renderStates = new WebGLRenderStates();

background = new WebGLBackground( _this, cubemaps, state, objects, _premultipliedAlpha );

bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info, capabilities );
Expand Down Expand Up @@ -1012,7 +1011,7 @@ function WebGLRenderer( parameters ) {
_frustum.setFromProjectionMatrix( _projScreenMatrix );

_localClippingEnabled = this.localClippingEnabled;
_clippingEnabled = _clipping.init( this.clippingPlanes, _localClippingEnabled, camera );
_clippingEnabled = clipping.init( this.clippingPlanes, _localClippingEnabled, camera );

currentRenderList = renderLists.get( scene, camera );
currentRenderList.init();
Expand All @@ -1029,15 +1028,15 @@ function WebGLRenderer( parameters ) {

//

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

const shadowsArray = currentRenderState.state.shadowsArray;

shadowMap.render( shadowsArray, scene, camera );

currentRenderState.setupLights( camera );

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

//

Expand Down Expand Up @@ -1306,7 +1305,7 @@ function WebGLRenderer( parameters ) {

const lightsStateVersion = lights.state.version;

const parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, _clipping.numPlanes, _clipping.numIntersection, object );
const parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, object );
const programCacheKey = programCache.getProgramCacheKey( parameters );

let program = materialProperties.program;
Expand Down Expand Up @@ -1394,9 +1393,9 @@ function WebGLRenderer( parameters ) {
! material.isRawShaderMaterial ||
material.clipping === true ) {

materialProperties.numClippingPlanes = _clipping.numPlanes;
materialProperties.numIntersection = _clipping.numIntersection;
uniforms.clippingPlanes = _clipping.uniform;
materialProperties.numClippingPlanes = clipping.numPlanes;
materialProperties.numIntersection = clipping.numIntersection;
uniforms.clippingPlanes = clipping.uniform;

}

Expand Down Expand Up @@ -1468,9 +1467,7 @@ function WebGLRenderer( parameters ) {
// we might want to call this function with some ClippingGroup
// object instead of the material, once it becomes feasible
// (#8465, #8379)
_clipping.setState(
material.clippingPlanes, material.clipIntersection, material.clipShadows,
camera, materialProperties, useCache );
clipping.setState( material, camera, useCache );

}

Expand All @@ -1495,8 +1492,8 @@ function WebGLRenderer( parameters ) {
initMaterial( material, scene, object );

} else if ( materialProperties.numClippingPlanes !== undefined &&
( materialProperties.numClippingPlanes !== _clipping.numPlanes ||
materialProperties.numIntersection !== _clipping.numIntersection ) ) {
( materialProperties.numClippingPlanes !== clipping.numPlanes ||
materialProperties.numIntersection !== clipping.numIntersection ) ) {

initMaterial( material, scene, object );

Expand Down
11 changes: 6 additions & 5 deletions src/renderers/webgl/WebGLClipping.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { Camera } from './../../cameras/Camera';
import { Material } from './../../materials/Material';
import { WebGLProperties } from './WebGLProperties';

export class WebGLClipping {

constructor( properties: WebGLProperties );

uniform: { value: any; needsUpdate: boolean };

/**
Expand All @@ -18,12 +22,9 @@ export class WebGLClipping {
beginShadows(): void;
endShadows(): void;
setState(
planes: any[],
clipIntersection: boolean,
clipShadows: boolean,
material: Material,
camera: Camera,
cache: any,
fromCache: boolean
useCache: boolean
): void;

}
16 changes: 11 additions & 5 deletions src/renderers/webgl/WebGLClipping.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Matrix3 } from '../../math/Matrix3.js';
import { Plane } from '../../math/Plane.js';

function WebGLClipping() {
function WebGLClipping( properties ) {

const scope = this;

Expand Down Expand Up @@ -52,7 +52,13 @@ function WebGLClipping() {

};

this.setState = function ( planes, clipIntersection, clipShadows, camera, cache, fromCache ) {
this.setState = function ( material, camera, useCache ) {

const planes = material.clippingPlanes,
clipIntersection = material.clipIntersection,
clipShadows = material.clipShadows;

const materialProperties = properties.get( material );

if ( ! localClippingEnabled || planes === null || planes.length === 0 || renderingShadows && ! clipShadows ) {

Expand All @@ -75,19 +81,19 @@ function WebGLClipping() {
const nGlobal = renderingShadows ? 0 : numGlobalPlanes,
lGlobal = nGlobal * 4;

let dstArray = cache.clippingState || null;
let dstArray = materialProperties.clippingState || null;

uniform.value = dstArray; // ensure unique state

dstArray = projectPlanes( planes, camera, lGlobal, fromCache );
dstArray = projectPlanes( planes, camera, lGlobal, useCache );

for ( let i = 0; i !== lGlobal; ++ i ) {

dstArray[ i ] = globalState[ i ];

}

cache.clippingState = dstArray;
materialProperties.clippingState = dstArray;
this.numIntersection = clipIntersection ? this.numPlanes : 0;
this.numPlanes += nGlobal;

Expand Down
6 changes: 3 additions & 3 deletions src/renderers/webgl/WebGLPrograms.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { WebGLProgram } from './WebGLProgram';
import { WebGLCapabilities } from './WebGLCapabilities';
import { WebGLCubeMaps } from './WebGLCubeMaps';
import { WebGLExtensions } from './WebGLExtensions';
import { WebGLClipping } from './WebGLClipping';
import { WebGLBindingStates } from './WebGLBindingStates';
import { Material } from './../../materials/Material';
import { Scene } from './../../scenes/Scene';

export class WebGLPrograms {

constructor( renderer: WebGLRenderer, cubemaps: WebGLCubeMaps, extensions: WebGLExtensions, capabilities: WebGLCapabilities );
constructor( renderer: WebGLRenderer, cubemaps: WebGLCubeMaps, extensions: WebGLExtensions, capabilities: WebGLCapabilities, bindingStates: WebGLBindingStates, clipping: WebGLClipping );

programs: WebGLProgram[];

Expand All @@ -17,8 +19,6 @@ export class WebGLPrograms {
lights: any,
shadows: object[],
scene: Scene,
nClipPlanes: number,
nClipIntersection: number,
object: any
): any;
getProgramCacheKey( parameters: any ): string;
Expand Down
8 changes: 4 additions & 4 deletions src/renderers/webgl/WebGLPrograms.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { WebGLProgram } from './WebGLProgram.js';
import { ShaderLib } from '../shaders/ShaderLib.js';
import { UniformsUtils } from '../shaders/UniformsUtils.js';

function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingStates ) {
function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingStates, clipping ) {

const programs = [];

Expand Down Expand Up @@ -108,7 +108,7 @@ function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingSta

}

function getParameters( material, lights, shadows, scene, nClipPlanes, nClipIntersection, object ) {
function getParameters( material, lights, shadows, scene, object ) {

const fog = scene.fog;
const environment = material.isMeshStandardMaterial ? scene.environment : null;
Expand Down Expand Up @@ -240,8 +240,8 @@ function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingSta
numPointLightShadows: lights.pointShadowMap.length,
numSpotLightShadows: lights.spotShadowMap.length,

numClippingPlanes: nClipPlanes,
numClipIntersection: nClipIntersection,
numClippingPlanes: clipping.numPlanes,
numClipIntersection: clipping.numIntersection,

dithering: material.dithering,

Expand Down

0 comments on commit 483196c

Please sign in to comment.