Skip to content
Merged
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
151 changes: 82 additions & 69 deletions src/renderers/webgl/WebGLState.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @author mrdoob / http://mrdoob.com/
*/

import { NotEqualDepth, GreaterDepth, GreaterEqualDepth, EqualDepth, LessEqualDepth, LessDepth, AlwaysDepth, NeverDepth, CullFaceFront, CullFaceBack, CullFaceNone, CustomBlending, MultiplyBlending, SubtractiveBlending, AdditiveBlending, NoBlending, NormalBlending, DoubleSide, BackSide } from '../../constants.js';
import { NotEqualDepth, GreaterDepth, GreaterEqualDepth, EqualDepth, LessEqualDepth, LessDepth, AlwaysDepth, NeverDepth, CullFaceFront, CullFaceBack, CullFaceNone, CustomBlending, MultiplyBlending, SubtractiveBlending, AdditiveBlending, NoBlending, NormalBlending, AddEquation, DoubleSide, BackSide } from '../../constants.js';
import { Vector4 } from '../../math/Vector4.js';

function WebGLState( gl, extensions, utils ) {
Expand Down Expand Up @@ -323,6 +323,7 @@ function WebGLState( gl, extensions, utils ) {

var currentProgram = null;

var currentBlendingEnabled = null;
var currentBlending = null;
var currentBlendEquation = null;
var currentBlendSrc = null;
Expand Down Expand Up @@ -400,8 +401,7 @@ function WebGLState( gl, extensions, utils ) {
setCullFace( CullFaceBack );
enable( gl.CULL_FACE );

enable( gl.BLEND );
setBlending( NormalBlending );
setBlending( NoBlending );

//

Expand Down Expand Up @@ -525,122 +525,135 @@ function WebGLState( gl, extensions, utils ) {

function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) {

if ( blending !== NoBlending ) {
if ( blending === NoBlending ) {
Copy link
Collaborator

@Mugen87 Mugen87 Aug 1, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write the first block like this. It's a bit easier to read since all operations related to NoBlending are in a single if/else block.

if ( blending === NoBlending ) {

	if ( currentBlending !== NoBlending ) {

		disable( gl.BLEND );
		currentBlending = NoBlending;

	}

        return;

} else {

	if ( currentBlending === NoBlending ) {

		enable( gl.BLEND );

	}

}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undone. It's all different now...


enable( gl.BLEND );
if ( currentBlendingEnabled ) {

} else {
disable( gl.BLEND );
currentBlendingEnabled = false;

disable( gl.BLEND );
}

return;

}

if ( blending !== CustomBlending ) {
if ( ! currentBlendingEnabled ) {

if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {
enable( gl.BLEND );
currentBlendingEnabled = true;

switch ( blending ) {
}

case AdditiveBlending:
if ( blending !== CustomBlending ) {

if ( premultipliedAlpha ) {
if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) {

gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
gl.blendFuncSeparate( gl.ONE, gl.ONE, gl.ONE, gl.ONE );
if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) {

} else {
gl.blendEquation( gl.FUNC_ADD );

gl.blendEquation( gl.FUNC_ADD );
gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
currentBlendEquation = AddEquation;
currentBlendEquationAlpha = AddEquation;

}
break;
}

case SubtractiveBlending:
if ( premultipliedAlpha ) {

if ( premultipliedAlpha ) {
switch ( blending ) {

gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
gl.blendFuncSeparate( gl.ZERO, gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ONE_MINUS_SRC_ALPHA );
case NormalBlending:
gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
break;

} else {
case AdditiveBlending:
gl.blendFunc( gl.ONE, gl.ONE );
break;

gl.blendEquation( gl.FUNC_ADD );
gl.blendFunc( gl.ZERO, gl.ONE_MINUS_SRC_COLOR );
case SubtractiveBlending:
gl.blendFuncSeparate( gl.ZERO, gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ONE_MINUS_SRC_ALPHA );
break;

}
break;
case MultiplyBlending:
gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
break;

case MultiplyBlending:
default:
console.error( 'THREE.WebGLState: Invalid blending: ', blending );
break;

if ( premultipliedAlpha ) {
}

gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
} else {

} else {
switch ( blending ) {

gl.blendEquation( gl.FUNC_ADD );
gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
case NormalBlending:
gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
break;

}
break;
case AdditiveBlending:
gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
break;

default:
case SubtractiveBlending:
gl.blendFunc( gl.ZERO, gl.ONE_MINUS_SRC_COLOR );
break;

if ( premultipliedAlpha ) {
case MultiplyBlending:
gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
break;

gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
gl.blendFuncSeparate( gl.ONE, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
default:
console.error( 'THREE.WebGLState: Invalid blending: ', blending );
break;

} else {
}

gl.blendEquationSeparate( gl.FUNC_ADD, gl.FUNC_ADD );
gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA );
}

}
currentBlendSrc = null;
currentBlendDst = null;
currentBlendSrcAlpha = null;
currentBlendDstAlpha = null;

}
currentBlending = blending;
currentPremultipledAlpha = premultipliedAlpha;

}

currentBlendEquation = null;
currentBlendSrc = null;
currentBlendDst = null;
currentBlendEquationAlpha = null;
currentBlendSrcAlpha = null;
currentBlendDstAlpha = null;
return;

} else {
}

blendEquationAlpha = blendEquationAlpha || blendEquation;
blendSrcAlpha = blendSrcAlpha || blendSrc;
blendDstAlpha = blendDstAlpha || blendDst;
// custom blending

if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {
blendEquationAlpha = blendEquationAlpha || blendEquation;
blendSrcAlpha = blendSrcAlpha || blendSrc;
blendDstAlpha = blendDstAlpha || blendDst;

gl.blendEquationSeparate( utils.convert( blendEquation ), utils.convert( blendEquationAlpha ) );
if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) {

currentBlendEquation = blendEquation;
currentBlendEquationAlpha = blendEquationAlpha;
gl.blendEquationSeparate( utils.convert( blendEquation ), utils.convert( blendEquationAlpha ) );

}
currentBlendEquation = blendEquation;
currentBlendEquationAlpha = blendEquationAlpha;

if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {
}

gl.blendFuncSeparate( utils.convert( blendSrc ), utils.convert( blendDst ), utils.convert( blendSrcAlpha ), utils.convert( blendDstAlpha ) );
if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) {

currentBlendSrc = blendSrc;
currentBlendDst = blendDst;
currentBlendSrcAlpha = blendSrcAlpha;
currentBlendDstAlpha = blendDstAlpha;
gl.blendFuncSeparate( utils.convert( blendSrc ), utils.convert( blendDst ), utils.convert( blendSrcAlpha ), utils.convert( blendDstAlpha ) );

}
currentBlendSrc = blendSrc;
currentBlendDst = blendDst;
currentBlendSrcAlpha = blendSrcAlpha;
currentBlendDstAlpha = blendDstAlpha;

}

currentBlending = blending;
currentPremultipledAlpha = premultipliedAlpha;
currentPremultipledAlpha = null;

}

Expand Down