Browse Source

workaround for buggy gl_FrontFacing

- e.g. on some integrated Intel GPUs
Alexander Rose 5 years ago
parent
commit
7536abe96c

+ 1 - 9
src/mol-gl/shader/chunks/assign-normal.glsl.ts

@@ -1,12 +1,4 @@
 export default `
-// inputs
-// - vViewPosition (if dFlatShaded)
-// - vNormal (if NOT dFlatShaded)
-
-// outputs
-// - normal
-
-// surface normal
 #if defined(dFlatShaded) && defined(enabledStandardDerivatives)
     vec3 fdx = dFdx(vViewPosition);
     vec3 fdy = dFdy(vViewPosition);
@@ -14,7 +6,7 @@ export default `
 #else
     vec3 normal = -normalize(vNormal);
     #ifdef dDoubleSided
-        normal = normal * (float(gl_FrontFacing) * 2.0 - 1.0);
+        normal = normal * (float(frontFacing) * 2.0 - 1.0);
     #endif
 #endif
 `

+ 1 - 3
src/mol-gl/shader/chunks/normal-frag-params.glsl.ts

@@ -1,5 +1,3 @@
 export default `
-#if !defined(dFlatShaded) || !defined(enabledStandardDerivatives)
-    varying vec3 vNormal;
-#endif
+varying vec3 vNormal;
 `

+ 13 - 2
src/mol-gl/shader/mesh.frag.ts

@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 2018-2019 mol* contributors, licensed under MIT, See LICENSE file for more info.
+ * Copyright (c) 2018-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  *
  * @author Alexander Rose <alexander.rose@weirdbyte.de>
  */
@@ -15,7 +15,18 @@ precision highp int;
 #include normal_frag_params
 
 void main() {
-    interior = !gl_FrontFacing; // TODO take dFlipSided into account
+    // Workaround for buggy gl_FrontFacing (e.g. on some integrated Intel GPUs)
+    #if defined(enabledStandardDerivatives)
+        vec3 fdx = dFdx(vViewPosition);
+        vec3 fdy = dFdy(vViewPosition);
+        vec3 faceNormal = normalize(cross(fdx,fdy));
+        bool frontFacing = dot(vNormal, faceNormal) > 0.0;
+    #else
+        bool frontFacing = dot(vNormal, vViewPosition) < 0.0;
+    #endif
+
+    interior = !frontFacing; // TODO take dFlipSided into account
+
     #include assign_material_color
 
     #if defined(dColorType_objectPicking) || defined(dColorType_instancePicking) || defined(dColorType_groupPicking)

+ 15 - 19
src/mol-gl/shader/mesh.vert.ts

@@ -23,14 +23,12 @@ attribute mat4 aTransform;
 attribute float aInstance;
 attribute float aGroup;
 
-#if !defined(dFlatShaded) || !defined(enabledStandardDerivatives) || defined(dIgnoreLight)
-    #ifdef dGeoTexture
-        uniform sampler2D tNormal;
-    #else
-        attribute vec3 aNormal;
-    #endif
-    varying vec3 vNormal;
+#ifdef dGeoTexture
+    uniform sampler2D tNormal;
+#else
+    attribute vec3 aNormal;
 #endif
+varying vec3 vNormal;
 
 void main(){
     #include assign_group
@@ -38,18 +36,16 @@ void main(){
     #include assign_marker_varying
     #include assign_position
 
-    #if !defined(dFlatShaded) || !defined(enabledStandardDerivatives) || defined(dIgnoreLight)
-        #ifdef dGeoTexture
-            vec3 normal = readFromTexture(tNormal, aGroup, uGeoTexDim).xyz;
-        #else
-            vec3 normal = aNormal;
-        #endif
-        mat3 normalMatrix = transpose3(inverse3(mat3(modelView)));
-        vec3 transformedNormal = normalize(normalMatrix * normalize(normal));
-        #if defined(dFlipSided) && !defined(dDoubleSided) // TODO checking dDoubleSided should not be required, ASR
-            transformedNormal = -transformedNormal;
-        #endif
-        vNormal = transformedNormal;
+    #ifdef dGeoTexture
+        vec3 normal = readFromTexture(tNormal, aGroup, uGeoTexDim).xyz;
+    #else
+        vec3 normal = aNormal;
+    #endif
+    mat3 normalMatrix = transpose3(inverse3(mat3(modelView)));
+    vec3 transformedNormal = normalize(normalMatrix * normalize(normal));
+    #if defined(dFlipSided) && !defined(dDoubleSided) // TODO checking dDoubleSided should not be required, ASR
+        transformedNormal = -transformedNormal;
     #endif
+    vNormal = transformedNormal;
 }
 `