Parcourir la source

Fix constant texture indexing for points

Daniel Salvadori il y a 6 ans
Parent
commit
bf87e32ba5
2 fichiers modifiés avec 71 ajouts et 57 suppressions
  1. 35 28
      renderer/shaders/point_fragment.glsl
  2. 36 29
      renderer/shaders/sources.go

+ 35 - 28
renderer/shaders/point_fragment.glsl

@@ -2,24 +2,6 @@ precision highp float;
 
 #include <material>
 
-// GLSL 3.30 does not allow indexing texture sampler with non constant values.
-// This macro is used to mix the texture with the specified index with the material color.
-// It should be called for each texture index.
-#if MAT_TEXTURES > 0
-vec4 MIX_POINT_TEXTURE(vec4 texMixed, mat2 rotation, int i) {                                                           \
-    if (MatTexVisible(i)) {                                                                                      \
-        vec2 pt = gl_PointCoord - vec2(0.5);                                                                     \
-        vec4 texColor = texture(MatTexture[i], (rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
-        if (i == 0) {                                                                                            \
-            texMixed = texColor;                                                                                 \
-        } else {                                                                                                 \
-            texMixed = mix(texMixed, texColor, texColor.a);                                                      \
-        }                                                                                                        \
-    }
-    return texMixed;
-}
-#endif
-
 // Inputs from vertex shader
 in vec3 Color;
 flat in mat2 Rotation;
@@ -29,17 +11,42 @@ out vec4 FragColor;
 
 void main() {
 
-    // Mix material color with textures colors
+    // Compute final texture color
     vec4 texMixed = vec4(1);
-    #if MAT_TEXTURES==1
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 0);
-    #elif MAT_TEXTURES==2
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 0);
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 1);
-    #elif MAT_TEXTURES==3
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 0);
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 1);
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 2);
+    #if MAT_TEXTURES > 0
+        vec2 pointCoord = Rotation * gl_PointCoord - vec2(0.5) + vec2(0.5);
+        bool firstTex = true;
+        if (MatTexVisible(0)) {
+            vec4 texColor = texture(MatTexture[0], pointCoord * MatTexRepeat(0) + MatTexOffset(0));
+            if (firstTex) {
+                texMixed = texColor;
+                firstTex = false;
+            } else {
+                texMixed = Blend(texMixed, texColor);
+            }
+        }
+        #if MAT_TEXTURES > 1
+            if (MatTexVisible(1)) {
+                vec4 texColor = texture(MatTexture[1], pointCoord * MatTexRepeat(1) + MatTexOffset(1));
+                if (firstTex) {
+                    texMixed = texColor;
+                    firstTex = false;
+                } else {
+                    texMixed = Blend(texMixed, texColor);
+                }
+            }
+            #if MAT_TEXTURES > 2
+                if (MatTexVisible(2)) {
+                    vec4 texColor = texture(MatTexture[2], pointCoord * MatTexRepeat(2) + MatTexOffset(2));
+                    if (firstTex) {
+                        texMixed = texColor;
+                        firstTex = false;
+                    } else {
+                        texMixed = Blend(texMixed, texColor);
+                    }
+                }
+            #endif
+        #endif
     #endif
 
     // Generates final color

+ 36 - 29
renderer/shaders/sources.go

@@ -205,7 +205,7 @@ void phongModel(vec4 position, vec3 normal, vec3 camDir, vec3 matAmbient, vec3 m
     // Directional lights
     for (int i = 0; i < DIR_LIGHTS; ++i) {
         vec3 lightDirection = normalize(DirLightPosition(i)); // Vector from fragment to light source
-        float dotNormal = dot(lightDirection, normal); // Dot product between light direction and fragment normal // TODO can remove the max here
+        float dotNormal = dot(lightDirection, normal); // Dot product between light direction and fragment normal
         if (dotNormal > EPS) { // If the fragment is lit
             diffuseTotal += DirLightColor(i) * matDiffuse * dotNormal;
             specularTotal += DirLightColor(i) * MatSpecularColor * pow(max(dot(reflect(-lightDirection, normal), camDir), 0.0), MatShininess);
@@ -898,24 +898,6 @@ const point_fragment_source = `precision highp float;
 
 #include <material>
 
-// GLSL 3.30 does not allow indexing texture sampler with non constant values.
-// This macro is used to mix the texture with the specified index with the material color.
-// It should be called for each texture index.
-#if MAT_TEXTURES > 0
-vec4 MIX_POINT_TEXTURE(vec4 texMixed, mat2 rotation, int i) {                                                           \
-    if (MatTexVisible(i)) {                                                                                      \
-        vec2 pt = gl_PointCoord - vec2(0.5);                                                                     \
-        vec4 texColor = texture(MatTexture[i], (rotation * pt + vec2(0.5)) * MatTexRepeat(i) + MatTexOffset(i)); \
-        if (i == 0) {                                                                                            \
-            texMixed = texColor;                                                                                 \
-        } else {                                                                                                 \
-            texMixed = mix(texMixed, texColor, texColor.a);                                                      \
-        }                                                                                                        \
-    }
-    return texMixed;
-}
-#endif
-
 // Inputs from vertex shader
 in vec3 Color;
 flat in mat2 Rotation;
@@ -925,17 +907,42 @@ out vec4 FragColor;
 
 void main() {
 
-    // Mix material color with textures colors
+    // Compute final texture color
     vec4 texMixed = vec4(1);
-    #if MAT_TEXTURES==1
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 0);
-    #elif MAT_TEXTURES==2
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 0);
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 1);
-    #elif MAT_TEXTURES==3
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 0);
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 1);
-        texMixed = MIX_POINT_TEXTURE(texMixed, Rotation, 2);
+    #if MAT_TEXTURES > 0
+        vec2 pointCoord = Rotation * gl_PointCoord - vec2(0.5) + vec2(0.5);
+        bool firstTex = true;
+        if (MatTexVisible(0)) {
+            vec4 texColor = texture(MatTexture[0], pointCoord * MatTexRepeat(0) + MatTexOffset(0));
+            if (firstTex) {
+                texMixed = texColor;
+                firstTex = false;
+            } else {
+                texMixed = Blend(texMixed, texColor);
+            }
+        }
+        #if MAT_TEXTURES > 1
+            if (MatTexVisible(1)) {
+                vec4 texColor = texture(MatTexture[1], pointCoord * MatTexRepeat(1) + MatTexOffset(1));
+                if (firstTex) {
+                    texMixed = texColor;
+                    firstTex = false;
+                } else {
+                    texMixed = Blend(texMixed, texColor);
+                }
+            }
+            #if MAT_TEXTURES > 2
+                if (MatTexVisible(2)) {
+                    vec4 texColor = texture(MatTexture[2], pointCoord * MatTexRepeat(2) + MatTexOffset(2));
+                    if (firstTex) {
+                        texMixed = texColor;
+                        firstTex = false;
+                    } else {
+                        texMixed = Blend(texMixed, texColor);
+                    }
+                }
+            #endif
+        #endif
     #endif
 
     // Generates final color