Skip to content

Commit 4ee7352

Browse files
authored
Src: Correct the blending formulas (#31246)
* Correct the blending formulas * Update examples that use MultiplyBlending
1 parent 651306b commit 4ee7352

File tree

7 files changed

+17
-15
lines changed

7 files changed

+17
-15
lines changed

examples/misc_exporter_usdz.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145

146146
const geometry = new THREE.PlaneGeometry();
147147
const material = new THREE.MeshBasicMaterial( {
148-
map: shadowTexture, blending: THREE.MultiplyBlending, toneMapped: false
148+
map: shadowTexture, blending: THREE.MultiplyBlending, toneMapped: false, premultipliedAlpha: true
149149
} );
150150

151151
const mesh = new THREE.Mesh( geometry, material );

examples/webgl_materials_blending.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
material.transparent = true;
103103
material.blending = blending.constant;
104104

105+
material.premultipliedAlpha = true;
106+
105107
const x = ( i - blendings.length / 2 ) * 110;
106108
const z = 0;
107109

examples/webgl_materials_car.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@
171171
const mesh = new THREE.Mesh(
172172
new THREE.PlaneGeometry( 0.655 * 4, 1.3 * 4 ),
173173
new THREE.MeshBasicMaterial( {
174-
map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true
174+
map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true, premultipliedAlpha: true
175175
} )
176176
);
177177
mesh.rotation.x = - Math.PI / 2;

examples/webgl_materials_envmaps_groundprojected.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
const mesh = new THREE.Mesh(
113113
new THREE.PlaneGeometry( 0.655 * 4, 1.3 * 4 ),
114114
new THREE.MeshBasicMaterial( {
115-
map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true
115+
map: shadow, blending: THREE.MultiplyBlending, toneMapped: false, transparent: true, premultipliedAlpha: true
116116
} )
117117
);
118118
mesh.rotation.x = - Math.PI / 2;

src/renderers/webgl-fallback/utils/WebGLState.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ class WebGLState {
339339
break;
340340

341341
case MultiplyBlending:
342-
gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
342+
gl.blendFuncSeparate( gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE );
343343
break;
344344

345345
default:
@@ -357,15 +357,15 @@ class WebGLState {
357357
break;
358358

359359
case AdditiveBlending:
360-
gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
360+
gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE, gl.ONE, gl.ONE );
361361
break;
362362

363363
case SubtractiveBlending:
364-
gl.blendFuncSeparate( gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ZERO, gl.ONE );
364+
console.error( 'THREE.WebGLState: SubtractiveBlending requires material.premultipliedAlpha = true' );
365365
break;
366366

367367
case MultiplyBlending:
368-
gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
368+
console.error( 'THREE.WebGLState: MultiplyBlending requires material.premultipliedAlpha = true' );
369369
break;
370370

371371
default:

src/renderers/webgl/WebGLState.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ function WebGLState( gl, extensions ) {
667667
break;
668668

669669
case MultiplyBlending:
670-
gl.blendFuncSeparate( gl.ZERO, gl.SRC_COLOR, gl.ZERO, gl.SRC_ALPHA );
670+
gl.blendFuncSeparate( gl.DST_COLOR, gl.ONE_MINUS_SRC_ALPHA, gl.ZERO, gl.ONE );
671671
break;
672672

673673
default:
@@ -685,15 +685,15 @@ function WebGLState( gl, extensions ) {
685685
break;
686686

687687
case AdditiveBlending:
688-
gl.blendFunc( gl.SRC_ALPHA, gl.ONE );
688+
gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE, gl.ONE, gl.ONE );
689689
break;
690690

691691
case SubtractiveBlending:
692-
gl.blendFuncSeparate( gl.ZERO, gl.ONE_MINUS_SRC_COLOR, gl.ZERO, gl.ONE );
692+
console.error( 'THREE.WebGLState: SubtractiveBlending requires material.premultipliedAlpha = true' );
693693
break;
694694

695695
case MultiplyBlending:
696-
gl.blendFunc( gl.ZERO, gl.SRC_COLOR );
696+
console.error( 'THREE.WebGLState: MultiplyBlending requires material.premultipliedAlpha = true' );
697697
break;
698698

699699
default:

src/renderers/webgpu/utils/WebGPUPipelineUtils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ class WebGPUPipelineUtils {
389389
break;
390390

391391
case MultiplyBlending:
392-
setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.SrcAlpha );
392+
setBlend( GPUBlendFactor.Dst, GPUBlendFactor.OneMinusSrcAlpha, GPUBlendFactor.Zero, GPUBlendFactor.One );
393393
break;
394394

395395
}
@@ -403,15 +403,15 @@ class WebGPUPipelineUtils {
403403
break;
404404

405405
case AdditiveBlending:
406-
setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.SrcAlpha, GPUBlendFactor.One );
406+
setBlend( GPUBlendFactor.SrcAlpha, GPUBlendFactor.One, GPUBlendFactor.One, GPUBlendFactor.One );
407407
break;
408408

409409
case SubtractiveBlending:
410-
setBlend( GPUBlendFactor.Zero, GPUBlendFactor.OneMinusSrc, GPUBlendFactor.Zero, GPUBlendFactor.One );
410+
console.error( 'THREE.WebGPURenderer: SubtractiveBlending requires material.premultipliedAlpha = true' );
411411
break;
412412

413413
case MultiplyBlending:
414-
setBlend( GPUBlendFactor.Zero, GPUBlendFactor.Src, GPUBlendFactor.Zero, GPUBlendFactor.Src );
414+
console.error( 'THREE.WebGPURenderer: MultiplyBlending requires material.premultipliedAlpha = true' );
415415
break;
416416

417417
}

0 commit comments

Comments
 (0)