From 30f50aa9eeb5e33a9f0533d089a21c3fb3e26eb6 Mon Sep 17 00:00:00 2001 From: Noeri Huisman <8823461+mrxz@users.noreply.github.com> Date: Fri, 25 Jul 2025 03:26:29 +0200 Subject: [PATCH] Use `material.premultipliedAlpha` to adjust blending function (#136) --- src/SparkRenderer.ts | 24 +++--------------------- src/shaders/splatFragment.glsl | 7 +++---- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/src/SparkRenderer.ts b/src/SparkRenderer.ts index c3e84d7..2f185ae 100644 --- a/src/SparkRenderer.ts +++ b/src/SparkRenderer.ts @@ -294,14 +294,8 @@ export class SparkRenderer extends THREE.Mesh { vertexShader: shaders.splatVertex, fragmentShader: shaders.splatFragment, uniforms, + premultipliedAlpha, transparent: true, - blending: premultipliedAlpha - ? THREE.CustomBlending - : THREE.NormalBlending, - blendSrc: premultipliedAlpha ? THREE.OneFactor : THREE.SrcAlphaFactor, - blendDst: premultipliedAlpha - ? THREE.OneMinusSrcAlphaFactor - : THREE.OneFactor, depthTest: true, depthWrite: false, side: THREE.DoubleSide, @@ -435,8 +429,6 @@ export class SparkRenderer extends THREE.Mesh { time: { value: 0 }, // Delta time in seconds since last frame deltaTime: { value: 0 }, - // Whether to use premultiplied alpha when accumulating splat RGB - premultipliedAlpha: { value: true }, // Whether to encode Gsplat with linear RGB (for environment mapping) encodeLinear: { value: false }, // Debug flag that alternates each frame @@ -531,20 +523,10 @@ export class SparkRenderer extends THREE.Mesh { if (isNewFrame) { // Keep these uniforms the same for both eyes if in WebXR - const blending = this.premultipliedAlpha - ? THREE.CustomBlending - : THREE.NormalBlending; - if (blending !== this.material.blending) { - this.material.blending = blending; - this.material.blendSrc = this.premultipliedAlpha - ? THREE.OneFactor - : THREE.SrcAlphaFactor; - this.material.blendDst = this.premultipliedAlpha - ? THREE.OneMinusSrcAlphaFactor - : THREE.OneFactor; + if (this.material.premultipliedAlpha !== this.premultipliedAlpha) { + this.material.premultipliedAlpha = this.premultipliedAlpha; this.material.needsUpdate = true; } - this.uniforms.premultipliedAlpha.value = this.premultipliedAlpha; this.uniforms.time.value = time; this.uniforms.deltaTime.value = deltaTime; // Alternating debug flag that can aid in visual debugging diff --git a/src/shaders/splatFragment.glsl b/src/shaders/splatFragment.glsl index e51be13..07f3b98 100644 --- a/src/shaders/splatFragment.glsl +++ b/src/shaders/splatFragment.glsl @@ -6,7 +6,6 @@ precision highp int; uniform float near; uniform float far; -uniform bool premultipliedAlpha; uniform bool encodeLinear; uniform float maxStdDev; uniform float minAlpha; @@ -69,9 +68,9 @@ void main() { rgba.rgb = srgbToLinear(rgba.rgb); } - if (premultipliedAlpha) { + #ifdef PREMULTIPLIED_ALPHA fragColor = vec4(rgba.rgb * rgba.a, rgba.a); - } else { + #else fragColor = rgba; - } + #endif }