From f5ff13ffe40524c575839a6037adaae664c6ac68 Mon Sep 17 00:00:00 2001
From: Alexander Rose <alexander.rose@weirdbyte.de>
Date: Mon, 12 Sep 2022 22:38:26 -0700
Subject: [PATCH] dpoit, fix transparent background

---
 src/mol-gl/shader/chunks/apply-fog.glsl.ts   | 17 ++++++++++++++---
 src/mol-gl/shader/chunks/dpoit-write.glsl.ts |  4 ++--
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/mol-gl/shader/chunks/apply-fog.glsl.ts b/src/mol-gl/shader/chunks/apply-fog.glsl.ts
index de2f323ed..f1700d95b 100644
--- a/src/mol-gl/shader/chunks/apply-fog.glsl.ts
+++ b/src/mol-gl/shader/chunks/apply-fog.glsl.ts
@@ -12,8 +12,19 @@ if (!uTransparentBackground) {
         gl_FragColor.rgb = mix(gl_FragColor.rgb, uFogColor, fogFactor);
     }
 } else {
-    // pre-multiplied alpha expected for transparent background
-    gl_FragColor.rgb *= fogAlpha;
-    gl_FragColor.a = fogAlpha;
+    #if defined(dRenderVariant_colorDpoit)
+        if (gl_FragColor.a < 1.0) {
+            // transparent objects are blended with background color
+            gl_FragColor.a = fogAlpha;
+        } else {
+            // opaque objects need to be pre-multiplied alpha
+            gl_FragColor.rgb *= fogAlpha;
+            gl_FragColor.a = fogAlpha;
+        }
+    #else
+        // pre-multiplied alpha expected for transparent background
+        gl_FragColor.rgb *= fogAlpha;
+        gl_FragColor.a = fogAlpha;
+    #endif
 }
 `;
\ No newline at end of file
diff --git a/src/mol-gl/shader/chunks/dpoit-write.glsl.ts b/src/mol-gl/shader/chunks/dpoit-write.glsl.ts
index 145981192..289e49487 100644
--- a/src/mol-gl/shader/chunks/dpoit-write.glsl.ts
+++ b/src/mol-gl/shader/chunks/dpoit-write.glsl.ts
@@ -59,9 +59,9 @@ export const dpoit_write = `
 
             if (fragmentDepth == nearestDepth) {
                 gl_FragColor.rgb += fragColor.rgb * fragColor.a * alphaMultiplier;
-                gl_FragColor.a = (1.0 - alphaMultiplier * (1.0 - fragColor.a)) * (uTransparentBackground ? fragColor.a : 1.0);
+                gl_FragColor.a = 1.0 - alphaMultiplier * (1.0 - fragColor.a);
             } else {
-                gl_FragData[1] += vec4(fragColor.rgb, fragColor.a * (uTransparentBackground ? fragColor.a : 1.0));
+                gl_FragData[1] += fragColor;
             }
 
         } else {
-- 
GitLab