Browse Source

* Fixed issue with spot light shader not taking into account alpha
* Reduced number of varyings by 2 in lighting shader

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7909 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

sha..rd 14 years ago
parent
commit
7308cdfca6

+ 28 - 25
engine/src/core-data/Common/MatDefs/Light/Lighting.frag

@@ -7,14 +7,16 @@ varying vec2 texCoord;
   varying vec2 texCoord2;
 #endif
 
-varying vec4 AmbientSum;
+varying vec3 AmbientSum;
 varying vec4 DiffuseSum;
-varying vec4 SpecularSum;
+varying vec3 SpecularSum;
 
 #ifndef VERTEX_LIGHTING
   varying vec3 vPosition;
   varying vec3 vViewDir;
   varying vec4 vLightDir;
+#else
+  varying vec2 vertexLightValues;
 #endif
 
 #ifdef DIFFUSEMAP
@@ -128,7 +130,7 @@ vec2 computeLighting(in vec3 wvPos, in vec3 wvNorm, in vec3 wvViewDir, in vec3 w
 void main(){
     vec2 newTexCoord;
      
-    #if defined(PARALLAXMAP) || defined(NORMALMAP_PARALLAX)
+    #if (defined(PARALLAXMAP) || defined(NORMALMAP_PARALLAX)) && !defined(VERTEX_LIGHTING)
        float h;
        #ifdef PARALLAXMAP
           h = texture2D(m_ParallaxMap, texCoord).r;
@@ -149,31 +151,33 @@ void main(){
     #else
       vec4 diffuseColor = vec4(1.0);
     #endif
+
+    float alpha = DiffuseSum.a * diffuseColor.a;
+    #ifdef ALPHAMAP
+       alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;
+    #endif
+    if(alpha < m_AlphaDiscardThreshold){
+        discard;
+    }
+
     #ifndef VERTEX_LIGHTING
         float spotFallOff = 1.0;
-        if(spotVec.w!=0.0){
-              vec3 L=normalize(lightVec.xyz);
+        if(spotVec.w != 0.0){
+              vec3 L       = normalize(lightVec.xyz);
               vec3 spotdir = normalize(spotVec.xyz);
               float curAngleCos = dot(-L, spotdir);             
               float innerAngleCos = spotVec.w;
               float outerAngleCos = lightVec.w;
               float innerMinusOuter = innerAngleCos - outerAngleCos;
               spotFallOff = clamp((curAngleCos - outerAngleCos) / innerMinusOuter, 0.0, 1.0);
-              if(spotFallOff<=0.0){
-                  gl_FragColor =  AmbientSum * diffuseColor;
+              if(spotFallOff <= 0.0){
+                  gl_FragColor.rgb = AmbientSum * diffuseColor.rgb;
+                  gl_FragColor.a   = alpha;
                   return;
               }
         }
      #endif
  
-    float alpha = DiffuseSum.a * diffuseColor.a;
-    #ifdef ALPHAMAP
-       alpha = alpha * texture2D(m_AlphaMap, newTexCoord).r;
-    #endif
-    if(alpha < m_AlphaDiscardThreshold){
-        discard;
-    }
-
     // ***********************
     // Read from textures
     // ***********************
@@ -209,15 +213,14 @@ void main(){
     #endif
 
     #ifdef VERTEX_LIGHTING
-       vec2 light = vec2(AmbientSum.a, SpecularSum.a);
        #ifdef COLORRAMP
-           light.x = texture2D(m_ColorRamp, vec2(light.x, 0.0)).r;
-           light.y = texture2D(m_ColorRamp, vec2(light.y, 0.0)).r;
+           light.x = texture2D(m_ColorRamp, vec2(vertexLightValues.x, 0.0)).r;
+           light.y = texture2D(m_ColorRamp, vec2(vertexLightValues.y, 0.0)).r;
        #endif
 
-       gl_FragColor =  AmbientSum * diffuseColor + 
-                       DiffuseSum * diffuseColor  * light.x +
-                       SpecularSum * specularColor * light.y;
+       gl_FragColor.rgb =  AmbientSum     * diffuseColor.rgb + 
+                           DiffuseSum.rgb * diffuseColor.rgb  * vec3(vertexLightValues.x) +
+                           SpecularSum    * specularColor.rgb * vec3(vertexLightValues.y);
     #else
        vec4 lightDir = vLightDir;
        lightDir.xyz = normalize(lightDir.xyz);
@@ -229,7 +232,7 @@ void main(){
        #endif
 
        // Workaround, since it is not possible to modify varying variables
-       vec4 SpecularSum2 = SpecularSum;
+       vec4 SpecularSum2 = vec4(SpecularSum, 1.0);
        #ifdef USE_REFLECTION
             vec4 refColor = Optics_GetEnvColor(m_EnvMap, refVec.xyz);
 
@@ -241,9 +244,9 @@ void main(){
             light.y = 1.0;
        #endif
 
-       gl_FragColor =  AmbientSum * diffuseColor +
-                       DiffuseSum * diffuseColor  * light.x +
-                       SpecularSum2 * specularColor * light.y;
+       gl_FragColor.rgb =  AmbientSum       * diffuseColor.rgb  +
+                           DiffuseSum.rgb   * diffuseColor.rgb  * vec3(light.x) +
+                           SpecularSum2.rgb * specularColor.rgb * vec3(light.y);
     #endif
     gl_FragColor.a = alpha;
 }

+ 15 - 16
engine/src/core-data/Common/MatDefs/Light/Lighting.vert

@@ -22,9 +22,9 @@ varying vec2 texCoord;
   attribute vec2 inTexCoord2;
 #endif
 
-varying vec4 AmbientSum;
+varying vec3 AmbientSum;
 varying vec4 DiffuseSum;
-varying vec4 SpecularSum;
+varying vec3 SpecularSum;
 
 attribute vec3 inPosition;
 attribute vec2 inTexCoord;
@@ -45,7 +45,9 @@ varying vec4 spotVec;
   #endif
   varying vec3 vPosition;
   varying vec3 vViewDir;
-  varying vec4 vLightDir;  
+  varying vec4 vLightDir;
+#else
+  varying vec2 vertexLightValues;
 #endif
 
 #ifdef USE_REFLECTION
@@ -112,7 +114,7 @@ vec2 computeLighting(in vec3 wvPos, in vec3 wvNorm, in vec3 wvViewDir, in vec4 w
      vec4 lightDir;
      lightComputeDir(wvPos, g_LightColor, wvLightPos, lightDir);
      float spotFallOff = 1.0;
-     if(spotVec.w!=0.0){
+     if(spotVec.w != 0.0){
           vec3 L=normalize(lightVec.xyz);
           vec3 spotdir = normalize(spotVec.xyz);
           float curAngleCos = dot(-L, spotdir);             
@@ -174,31 +176,28 @@ void main(){
    #endif
 
    //computing spot direction in view space and unpacking spotlight cos
-   spotVec=(g_ViewMatrix *vec4(g_LightDirection.xyz,0.0) );
-   spotVec.w=floor(g_LightDirection.w)*0.001;
+   spotVec = (g_ViewMatrix * vec4(g_LightDirection.xyz, 0.0) );
+   spotVec.w  = floor(g_LightDirection.w) * 0.001;
    lightVec.w = fract(g_LightDirection.w);
 
    lightColor.w = 1.0;
    #ifdef MATERIAL_COLORS
-      AmbientSum  = m_Ambient  * g_AmbientLightColor;
-      DiffuseSum  = m_Diffuse  * lightColor;
-      SpecularSum = m_Specular * lightColor;
+      AmbientSum  = (m_Ambient  * g_AmbientLightColor).rgb;
+      DiffuseSum  =  m_Diffuse  * lightColor;
+      SpecularSum = (m_Specular * lightColor).rgb;
     #else
-      AmbientSum  = vec4(0.2, 0.2, 0.2, 1.0) * g_AmbientLightColor; // Default: ambient color is dark gray
+      AmbientSum  = vec3(0.2, 0.2, 0.2) * g_AmbientLightColor.rgb; // Default: ambient color is dark gray
       DiffuseSum  = lightColor;
-      SpecularSum = lightColor;
+      SpecularSum = lightColor.rgb;
     #endif
 
     #ifdef VERTEX_COLOR
-      AmbientSum *= inColor;
+      AmbientSum *= inColor.rgb;
       DiffuseSum *= inColor;
     #endif
 
     #ifdef VERTEX_LIGHTING
-       vec2 light = computeLighting(wvPosition, wvNormal, viewDir, wvLightPos);
-
-       AmbientSum.a  = light.x;
-       SpecularSum.a = light.y;
+       vertexLightValues = computeLighting(wvPosition, wvNormal, viewDir, wvLightPos);
     #endif
 
     #ifdef USE_REFLECTION