Skip to content

Commit 30f50aa

Browse files
authored
Use material.premultipliedAlpha to adjust blending function (#136)
1 parent 3445167 commit 30f50aa

File tree

2 files changed

+6
-25
lines changed

2 files changed

+6
-25
lines changed

src/SparkRenderer.ts

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,8 @@ export class SparkRenderer extends THREE.Mesh {
294294
vertexShader: shaders.splatVertex,
295295
fragmentShader: shaders.splatFragment,
296296
uniforms,
297+
premultipliedAlpha,
297298
transparent: true,
298-
blending: premultipliedAlpha
299-
? THREE.CustomBlending
300-
: THREE.NormalBlending,
301-
blendSrc: premultipliedAlpha ? THREE.OneFactor : THREE.SrcAlphaFactor,
302-
blendDst: premultipliedAlpha
303-
? THREE.OneMinusSrcAlphaFactor
304-
: THREE.OneFactor,
305299
depthTest: true,
306300
depthWrite: false,
307301
side: THREE.DoubleSide,
@@ -435,8 +429,6 @@ export class SparkRenderer extends THREE.Mesh {
435429
time: { value: 0 },
436430
// Delta time in seconds since last frame
437431
deltaTime: { value: 0 },
438-
// Whether to use premultiplied alpha when accumulating splat RGB
439-
premultipliedAlpha: { value: true },
440432
// Whether to encode Gsplat with linear RGB (for environment mapping)
441433
encodeLinear: { value: false },
442434
// Debug flag that alternates each frame
@@ -531,20 +523,10 @@ export class SparkRenderer extends THREE.Mesh {
531523

532524
if (isNewFrame) {
533525
// Keep these uniforms the same for both eyes if in WebXR
534-
const blending = this.premultipliedAlpha
535-
? THREE.CustomBlending
536-
: THREE.NormalBlending;
537-
if (blending !== this.material.blending) {
538-
this.material.blending = blending;
539-
this.material.blendSrc = this.premultipliedAlpha
540-
? THREE.OneFactor
541-
: THREE.SrcAlphaFactor;
542-
this.material.blendDst = this.premultipliedAlpha
543-
? THREE.OneMinusSrcAlphaFactor
544-
: THREE.OneFactor;
526+
if (this.material.premultipliedAlpha !== this.premultipliedAlpha) {
527+
this.material.premultipliedAlpha = this.premultipliedAlpha;
545528
this.material.needsUpdate = true;
546529
}
547-
this.uniforms.premultipliedAlpha.value = this.premultipliedAlpha;
548530
this.uniforms.time.value = time;
549531
this.uniforms.deltaTime.value = deltaTime;
550532
// Alternating debug flag that can aid in visual debugging

src/shaders/splatFragment.glsl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ precision highp int;
66

77
uniform float near;
88
uniform float far;
9-
uniform bool premultipliedAlpha;
109
uniform bool encodeLinear;
1110
uniform float maxStdDev;
1211
uniform float minAlpha;
@@ -69,9 +68,9 @@ void main() {
6968
rgba.rgb = srgbToLinear(rgba.rgb);
7069
}
7170

72-
if (premultipliedAlpha) {
71+
#ifdef PREMULTIPLIED_ALPHA
7372
fragColor = vec4(rgba.rgb * rgba.a, rgba.a);
74-
} else {
73+
#else
7574
fragColor = rgba;
76-
}
75+
#endif
7776
}

0 commit comments

Comments
 (0)