Explorar el Código

Removed all xxx15.frag shaders and used GLSLCompat to have the same shader whatever version it's used for

Nehon hace 8 años
padre
commit
f7181c38dc
Se han modificado 56 ficheros con 793 adiciones y 2025 borrados
  1. 0 1
      jme3-core/src/main/resources/Common/ShaderLib/MultiSample.glsllib
  2. 2 1
      jme3-effects/src/main/java/com/jme3/post/filters/GammaCorrectionFilter.java
  3. 2 15
      jme3-effects/src/main/resources/Common/MatDefs/Post/BloomExtract.j3md
  4. 2 9
      jme3-effects/src/main/resources/Common/MatDefs/Post/BloomFinal.j3md
  5. 7 4
      jme3-effects/src/main/resources/Common/MatDefs/Post/CartoonEdge.frag
  6. 2 12
      jme3-effects/src/main/resources/Common/MatDefs/Post/CartoonEdge.j3md
  7. 0 57
      jme3-effects/src/main/resources/Common/MatDefs/Post/CartoonEdge15.frag
  8. 7 4
      jme3-effects/src/main/resources/Common/MatDefs/Post/Compose.frag
  9. 2 10
      jme3-effects/src/main/resources/Common/MatDefs/Post/Compose.j3md
  10. 0 14
      jme3-effects/src/main/resources/Common/MatDefs/Post/Compose15.frag
  11. 5 2
      jme3-effects/src/main/resources/Common/MatDefs/Post/CrossHatch.frag
  12. 3 11
      jme3-effects/src/main/resources/Common/MatDefs/Post/CrossHatch.j3md
  13. 0 55
      jme3-effects/src/main/resources/Common/MatDefs/Post/CrossHatch15.frag
  14. 7 4
      jme3-effects/src/main/resources/Common/MatDefs/Post/DepthOfField.frag
  15. 2 9
      jme3-effects/src/main/resources/Common/MatDefs/Post/DepthOfField.j3md
  16. 0 93
      jme3-effects/src/main/resources/Common/MatDefs/Post/DepthOfField15.frag
  17. 5 2
      jme3-effects/src/main/resources/Common/MatDefs/Post/Fade.frag
  18. 2 9
      jme3-effects/src/main/resources/Common/MatDefs/Post/Fade.j3md
  19. 0 12
      jme3-effects/src/main/resources/Common/MatDefs/Post/Fade15.frag
  20. 8 5
      jme3-effects/src/main/resources/Common/MatDefs/Post/Fog.frag
  21. 2 9
      jme3-effects/src/main/resources/Common/MatDefs/Post/Fog.j3md
  22. 0 25
      jme3-effects/src/main/resources/Common/MatDefs/Post/Fog15.frag
  23. 0 16
      jme3-effects/src/main/resources/Common/MatDefs/Post/GammaCorrection.frag
  24. 0 31
      jme3-effects/src/main/resources/Common/MatDefs/Post/GammaCorrection.j3md
  25. 0 20
      jme3-effects/src/main/resources/Common/MatDefs/Post/GammaCorrection15.frag
  26. 17 16
      jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering.frag
  27. 3 10
      jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering.j3md
  28. 0 41
      jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering15.frag
  29. 0 16
      jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering15.vert
  30. 5 2
      jme3-effects/src/main/resources/Common/MatDefs/Post/Overlay.frag
  31. 2 10
      jme3-effects/src/main/resources/Common/MatDefs/Post/Overlay.j3md
  32. 0 12
      jme3-effects/src/main/resources/Common/MatDefs/Post/Overlay15.frag
  33. 7 3
      jme3-effects/src/main/resources/Common/MatDefs/Post/Posterization.frag
  34. 7 9
      jme3-effects/src/main/resources/Common/MatDefs/Post/Posterization.j3md
  35. 0 21
      jme3-effects/src/main/resources/Common/MatDefs/Post/Posterization15.frag
  36. 5 2
      jme3-effects/src/main/resources/Common/MatDefs/Post/bloomExtract.frag
  37. 0 33
      jme3-effects/src/main/resources/Common/MatDefs/Post/bloomExtract15.frag
  38. 5 2
      jme3-effects/src/main/resources/Common/MatDefs/Post/bloomFinal.frag
  39. 0 16
      jme3-effects/src/main/resources/Common/MatDefs/Post/bloomFinal15.frag
  40. 10 10
      jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssao.frag
  41. 2 15
      jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssao.j3md
  42. 0 116
      jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssao15.frag
  43. 106 138
      jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssaoBlur.frag
  44. 2 18
      jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssaoBlur.j3md
  45. 0 133
      jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssaoBlur15.frag
  46. 446 417
      jme3-effects/src/main/resources/Common/MatDefs/Water/Water.frag
  47. 2 20
      jme3-effects/src/main/resources/Common/MatDefs/Water/Water.j3md
  48. 0 439
      jme3-effects/src/main/resources/Common/MatDefs/Water/Water15.frag
  49. 9 7
      jme3-examples/src/main/java/jme3test/post/LightScatteringUI.java
  50. 64 22
      jme3-examples/src/main/java/jme3test/post/TestFog.java
  51. 4 20
      jme3-examples/src/main/java/jme3test/post/TestLightScattering.java
  52. 3 3
      jme3-examples/src/main/java/jme3test/post/TestPostFilters.java
  53. 19 14
      jme3-examples/src/main/java/jme3test/post/TestPostFiltersCompositing.java
  54. 11 28
      jme3-examples/src/main/java/jme3test/post/TestPosterization.java
  55. 1 0
      jme3-examples/src/main/java/jme3test/post/TestSSAO2.java
  56. 5 2
      jme3-examples/src/main/java/jme3test/water/TestPostWater.java

+ 0 - 1
jme3-core/src/main/resources/Common/ShaderLib/MultiSample.glsllib

@@ -1,5 +1,4 @@
 #extension GL_ARB_texture_multisample : enable
-#import "Common/ShaderLib/GLSLCompat.glsllib"
 
 uniform int m_NumSamples;
 uniform int m_NumSamplesDepth;

+ 2 - 1
jme3-effects/src/main/java/com/jme3/post/filters/GammaCorrectionFilter.java

@@ -41,8 +41,9 @@ import com.jme3.renderer.ViewPort;
  *
  * @author Phate666
  * @version 1.0 initial version
- * 
+ * @deprecated use the Gama Correction setting instead.
  */
+@Deprecated
 public class GammaCorrectionFilter extends Filter {
 
     private float gamma = 2.2f;   

+ 2 - 15
jme3-effects/src/main/resources/Common/MatDefs/Post/BloomExtract.j3md

@@ -10,8 +10,8 @@ MaterialDef Bloom {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/bloomExtract15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/bloomExtract.frag
 
         WorldParameters {
         }
@@ -22,17 +22,4 @@ MaterialDef Bloom {
             RESOLVE_MS : NumSamples
         }
     }
-
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/bloomExtract.frag
-
-        WorldParameters {
-        }
-
-        Defines {
-            HAS_GLOWMAP : GlowMap
-            DO_EXTRACT : Extract
-        }
-    }
 }

+ 2 - 9
jme3-effects/src/main/resources/Common/MatDefs/Post/BloomFinal.j3md

@@ -8,8 +8,8 @@ MaterialDef Bloom Final {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/bloomFinal15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/bloomFinal.frag
 
         WorldParameters {
         }
@@ -19,11 +19,4 @@ MaterialDef Bloom Final {
         }
     }
 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/bloomFinal.frag
-
-        WorldParameters {
-        }
-    }
 }

+ 7 - 4
jme3-effects/src/main/resources/Common/MatDefs/Post/CartoonEdge.frag

@@ -1,3 +1,6 @@
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
 uniform vec4 m_EdgeColor;
 
 uniform float m_EdgeWidth;
@@ -11,21 +14,21 @@ uniform float m_DepthSensitivity;
 
 varying vec2 texCoord;
 
-uniform sampler2D m_Texture;
+uniform COLORTEXTURE m_Texture;
 uniform sampler2D m_NormalsTexture;
-uniform sampler2D m_DepthTexture;
+uniform DEPTHTEXTURE m_DepthTexture;
 
 uniform vec2 g_ResolutionInverse;
 
 vec4 fetchNormalDepth(vec2 tc){
     vec4 nd;
     nd.xyz = texture2D(m_NormalsTexture, tc).rgb;
-    nd.w   = texture2D(m_DepthTexture,   tc).r;
+    nd.w   = fetchTextureSample(m_DepthTexture,   tc, 0).r;
     return nd;
 }
 
 void main(){
-    vec3 color = texture2D(m_Texture, texCoord).rgb;
+    vec3 color = getColor(m_Texture, texCoord).rgb;
 
     vec2 edgeOffset = vec2(m_EdgeWidth) * g_ResolutionInverse;
 

+ 2 - 12
jme3-effects/src/main/resources/Common/MatDefs/Post/CartoonEdge.j3md

@@ -16,8 +16,8 @@ MaterialDef Cartoon Edge {
     }
 
      Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/CartoonEdge15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/CartoonEdge.frag
 
         WorldParameters {
             WorldViewMatrix
@@ -30,14 +30,4 @@ MaterialDef Cartoon Edge {
         }
     }
 
-   
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/CartoonEdge.frag
-
-        WorldParameters {
-            WorldViewMatrix
-            ResolutionInverse
-        }
-    }
 }

+ 0 - 57
jme3-effects/src/main/resources/Common/MatDefs/Post/CartoonEdge15.frag

@@ -1,57 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform DEPTHTEXTURE m_DepthTexture;
-
-uniform sampler2D m_NormalsTexture;
-uniform vec2 g_ResolutionInverse;
-
-uniform vec4 m_EdgeColor;
-
-uniform float m_EdgeWidth;
-uniform float m_EdgeIntensity;
-
-uniform float m_NormalThreshold;
-uniform float m_DepthThreshold;
-
-uniform float m_NormalSensitivity;
-uniform float m_DepthSensitivity;
-
-in vec2 texCoord;
-out vec4 outFragColor;
-
-vec4 fetchNormalDepth(vec2 tc){
-    vec4 nd;
-    nd.xyz = texture2D(m_NormalsTexture, tc).rgb;
-    nd.w   = fetchTextureSample(m_DepthTexture,   tc,0).r;
-    return nd;
-}
-
-void main(){
-    vec3 color = getColor(m_Texture, texCoord).rgb;
-
-    vec2 edgeOffset = vec2(m_EdgeWidth) * g_ResolutionInverse;
-    vec4 n1 = fetchNormalDepth(texCoord + vec2(-1.0, -1.0) * edgeOffset);
-    vec4 n2 = fetchNormalDepth(texCoord + vec2( 1.0,  1.0) * edgeOffset);
-    vec4 n3 = fetchNormalDepth(texCoord + vec2(-1.0,  1.0) * edgeOffset);
-    vec4 n4 = fetchNormalDepth(texCoord + vec2( 1.0, -1.0) * edgeOffset);
-
-    // Work out how much the normal and depth values are changing.
-    vec4 diagonalDelta = abs(n1 - n2) + abs(n3 - n4);
-
-    float normalDelta = dot(diagonalDelta.xyz, vec3(1.0));
-    float depthDelta = diagonalDelta.w;
-
-    // Filter out very small changes, in order to produce nice clean results.
-    normalDelta = clamp((normalDelta - m_NormalThreshold) * m_NormalSensitivity, 0.0, 1.0);
-    depthDelta  = clamp((depthDelta - m_DepthThreshold) * m_DepthSensitivity,    0.0, 1.0);
-
-    // Does this pixel lie on an edge?
-    float edgeAmount = clamp(normalDelta + depthDelta, 0.0, 1.0) * m_EdgeIntensity;
-
-    // Apply the edge detection result to the main scene color.
-    //color *= (1.0 - edgeAmount);
-    color = mix (color,m_EdgeColor.rgb,edgeAmount);
-
-    outFragColor = vec4(color, 1.0);
-}

+ 7 - 4
jme3-effects/src/main/resources/Common/MatDefs/Post/Compose.frag

@@ -1,10 +1,13 @@
-uniform sampler2D m_Texture;
-uniform sampler2D m_CompositeTexture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
+uniform COLORTEXTURE m_CompositeTexture;
 varying vec2 texCoord;
 
 void main() {
-      vec4 texVal = texture2D(m_Texture, texCoord);
-      vec4 compositeVal = texture2D(m_CompositeTexture, texCoord);
+      vec4 texVal = getColor(m_Texture, texCoord);
+      vec4 compositeVal = getColor(m_CompositeTexture, texCoord);
       gl_FragColor = mix(compositeVal,texVal,texVal.a);
 }
 

+ 2 - 10
jme3-effects/src/main/resources/Common/MatDefs/Post/Compose.j3md

@@ -8,8 +8,8 @@ MaterialDef Default GUI {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/Compose15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/Compose.frag
 
         WorldParameters {
         }
@@ -20,12 +20,4 @@ MaterialDef Default GUI {
 
     }
 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/Compose.frag
-
-        WorldParameters {
-        }
-
-    }
 }

+ 0 - 14
jme3-effects/src/main/resources/Common/MatDefs/Post/Compose15.frag

@@ -1,14 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform COLORTEXTURE m_CompositeTexture;
-in vec2 texCoord;
-
-out vec4 finalColor;
-
-void main() {
-      vec4 texVal = getColor(m_Texture, texCoord);
-      vec4 compositeVal = getColor(m_CompositeTexture, texCoord);
-      finalColor = mix(compositeVal,texVal,texVal.a);      
-}
-

+ 5 - 2
jme3-effects/src/main/resources/Common/MatDefs/Post/CrossHatch.frag

@@ -1,4 +1,7 @@
-uniform sampler2D m_Texture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
 varying vec2 texCoord;
  
 uniform vec4 m_LineColor;
@@ -17,7 +20,7 @@ uniform float m_LineDistance;
 uniform float m_LineThickness;
  
 void main() {
-    vec4 texVal = texture2D(m_Texture, texCoord);
+    vec4 texVal = getColor(m_Texture, texCoord);
     float linePixel = 0.0;
  
     float lum = texVal.r*0.2126 + texVal.g*0.7152 + texVal.b*0.0722;

+ 3 - 11
jme3-effects/src/main/resources/Common/MatDefs/Post/CrossHatch.j3md

@@ -18,19 +18,11 @@ MaterialDef CrossHatch {
     }
  
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/CrossHatch15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/CrossHatch.frag
  
         WorldParameters {
         }
     }
- 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/CrossHatch.frag
- 
-        WorldParameters {
-            WorldViewProjectionMatrix
-        }
-    }
+
 }

+ 0 - 55
jme3-effects/src/main/resources/Common/MatDefs/Post/CrossHatch15.frag

@@ -1,55 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
- 
-uniform COLORTEXTURE m_Texture;
-in vec2 texCoord;
- 
-uniform vec4 m_LineColor;
-uniform vec4 m_PaperColor;
-uniform float m_ColorInfluenceLine;
-uniform float m_ColorInfluencePaper;
- 
-uniform float m_FillValue;
-uniform float m_Luminance1;
-uniform float m_Luminance2;
-uniform float m_Luminance3;
-uniform float m_Luminance4;
-uniform float m_Luminance5;
- 
-uniform float m_LineDistance;
-uniform float m_LineThickness;
-
-out vec4 fragColor;
-
-void main() {
-    vec4 texVal = getColor(m_Texture, texCoord);
-    float linePixel = 0;
- 
-    float lum = texVal.r*0.2126 + texVal.g*0.7152 + texVal.b*0.0722;
- 
-    if (lum < m_Luminance1){
-        if (mod(gl_FragCoord.x + gl_FragCoord.y, m_LineDistance * 2.0) < m_LineThickness)
-            linePixel = 1;
-    }
-    if (lum < m_Luminance2){
-        if (mod(gl_FragCoord.x - gl_FragCoord.y, m_LineDistance * 2.0) < m_LineThickness)
-            linePixel = 1;
-    }
-    if (lum < m_Luminance3){
-        if (mod(gl_FragCoord.x + gl_FragCoord.y - m_LineDistance, m_LineDistance) < m_LineThickness)
-            linePixel = 1;
-    }
-    if (lum < m_Luminance4){
-        if (mod(gl_FragCoord.x - gl_FragCoord.y - m_LineDistance, m_LineDistance) < m_LineThickness)
-            linePixel = 1;
-    }
-    if (lum < m_Luminance5){ // No line, make a blob instead
-        linePixel = m_FillValue;
-    }
- 
-    // Mix line color with existing color information
-    vec4 lineColor = mix(m_LineColor, texVal, m_ColorInfluenceLine);
-    // Mix paper color with existing color information
-    vec4 paperColor = mix(m_PaperColor, texVal, m_ColorInfluencePaper);
- 
-    fragColor = mix(paperColor, lineColor, linePixel);
-}

+ 7 - 4
jme3-effects/src/main/resources/Common/MatDefs/Post/DepthOfField.frag

@@ -1,5 +1,8 @@
-uniform sampler2D m_Texture;
-uniform sampler2D m_DepthTexture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
+uniform DEPTHTEXTURE m_DepthTexture;
 varying vec2 texCoord;
 
 uniform float m_FocusRange;
@@ -11,9 +14,9 @@ vec2 m_NearFar = vec2( 0.1, 1000.0 );
 
 void main() {
 
-    vec4 texVal = texture2D( m_Texture, texCoord );
+    vec4 texVal = getColor( m_Texture, texCoord );
 
-    float zBuffer = texture2D( m_DepthTexture, texCoord ).r;
+    float zBuffer = getDepth( m_DepthTexture, texCoord ).r;
 
     //
     // z_buffer_value = a + b / z;

+ 2 - 9
jme3-effects/src/main/resources/Common/MatDefs/Post/DepthOfField.j3md

@@ -12,8 +12,8 @@ MaterialDef Depth Of Field {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/DepthOfField15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/DepthOfField.frag
 
         WorldParameters {
         }
@@ -24,11 +24,4 @@ MaterialDef Depth Of Field {
         }
     }
 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/DepthOfField.frag
-
-        WorldParameters {
-        }
-    }
 }

+ 0 - 93
jme3-effects/src/main/resources/Common/MatDefs/Post/DepthOfField15.frag

@@ -1,93 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform DEPTHTEXTURE m_DepthTexture;
-varying vec2 texCoord;
-
-uniform float m_FocusRange;
-uniform float m_FocusDistance;
-uniform float m_XScale;
-uniform float m_YScale;
-
-vec2 m_NearFar = vec2( 0.1, 1000.0 );
-
-out vec4 fragColor;
-
-void main() {
-
-    vec4 texVal = getColor( m_Texture, texCoord );
-
-    float zBuffer = getDepth( m_DepthTexture, texCoord ).r;
-
-    //
-    // z_buffer_value = a + b / z;
-    //
-    // Where:
-    //  a = zFar / ( zFar - zNear )
-    //  b = zFar * zNear / ( zNear - zFar )
-    //  z = distance from the eye to the object
-    //
-    // Which means:
-    // zb - a = b / z;
-    // z * (zb - a) = b
-    // z = b / (zb - a)
-    //
-    float a = m_NearFar.y / (m_NearFar.y - m_NearFar.x);
-    float b = m_NearFar.y * m_NearFar.x / (m_NearFar.x - m_NearFar.y);
-    float z = b / (zBuffer - a);
-
-    // Above could be the same for any depth-based filter
-
-    // We want to be purely focused right at
-    // m_FocusDistance and be purely unfocused
-    // at +/- m_FocusRange to either side of that.
-    float unfocus = min( 1.0, abs( z - m_FocusDistance ) / m_FocusRange );
-
-    if( unfocus < 0.2 ) {
-        // If we are mostly in focus then don't bother with the
-        // convolution filter
-        fragColor = texVal;
-    } else {
-    // Perform a wide convolution filter and we scatter it
-    // a bit to avoid some texture look-ups.  Instead of
-    // a full 5x5 (25-1 lookups) we'll skip every other one
-    // to only perform 12.
-    // 1  0  1  0  1
-    // 0  1  0  1  0
-    // 1  0  x  0  1
-    // 0  1  0  1  0
-    // 1  0  1  0  1
-    //
-    // You can get away with 8 just around the outside but
-    // it looks more jittery to me.
-
-    vec4 sum = vec4(0.0);
-
-    float x = texCoord.x;
-    float y = texCoord.y;
-
-    float xScale = m_XScale;
-    float yScale = m_YScale;
-
-    // In order from lower left to right, depending on how you look at it
-    sum += getColor( m_Texture, vec2(x - 2.0 * xScale, y - 2.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x - 0.0 * xScale, y - 2.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x + 2.0 * xScale, y - 2.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x - 1.0 * xScale, y - 1.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x + 1.0 * xScale, y - 1.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x - 2.0 * xScale, y - 0.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x + 2.0 * xScale, y - 0.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x - 1.0 * xScale, y + 1.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x + 1.0 * xScale, y + 1.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x - 2.0 * xScale, y + 2.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x - 0.0 * xScale, y + 2.0 * yScale) );
-    sum += getColor( m_Texture, vec2(x + 2.0 * xScale, y + 2.0 * yScale) );
-
-    sum = sum / 12.0;
-
-    fragColor = mix( texVal, sum, unfocus );
-
-    // I used this for debugging the range
-    // gl_FragColor.r = unfocus;
-    }
-}

+ 5 - 2
jme3-effects/src/main/resources/Common/MatDefs/Post/Fade.frag

@@ -1,10 +1,13 @@
-uniform sampler2D m_Texture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
 varying vec2 texCoord;
 
 uniform float m_Value;
 
 void main() {
-       vec4 texVal = texture2D(m_Texture, texCoord);
+       vec4 texVal = getColor(m_Texture, texCoord);
 
        gl_FragColor = texVal * m_Value;
 

+ 2 - 9
jme3-effects/src/main/resources/Common/MatDefs/Post/Fade.j3md

@@ -7,8 +7,8 @@ MaterialDef Fade {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/Fade15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/Fade.frag
 
         WorldParameters {
         }
@@ -18,11 +18,4 @@ MaterialDef Fade {
         }
     }
 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/Fade.frag
-
-        WorldParameters {
-        }
-    }
 }

+ 0 - 12
jme3-effects/src/main/resources/Common/MatDefs/Post/Fade15.frag

@@ -1,12 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform float m_Value;
-
-in vec2 texCoord;
-out vec4 fragColor;
-
-void main() {
-    vec4 texVal = getColor(m_Texture, texCoord);
-    fragColor = texVal * m_Value;
-}

+ 8 - 5
jme3-effects/src/main/resources/Common/MatDefs/Post/Fog.frag

@@ -1,5 +1,8 @@
-uniform sampler2D m_Texture;
-uniform sampler2D m_DepthTexture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
+uniform DEPTHTEXTURE m_DepthTexture;
 varying vec2 texCoord;
 
 uniform vec4 m_FogColor;
@@ -10,12 +13,12 @@ const float LOG2 = 1.442695;
 
 void main() {
        vec2 m_FrustumNearFar=vec2(1.0,m_FogDistance);
-       vec4 texVal = texture2D(m_Texture, texCoord);      
-       float fogVal =texture2D(m_DepthTexture,texCoord).r;
+       vec4 texVal = getColor(m_Texture, texCoord);
+       float fogVal = getDepth(m_DepthTexture,texCoord).r;
        float depth= (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - fogVal* (m_FrustumNearFar.y-m_FrustumNearFar.x));
 
        float fogFactor = exp2( -m_FogDensity * m_FogDensity * depth *  depth * LOG2 );
        fogFactor = clamp(fogFactor, 0.0, 1.0);
-       gl_FragColor =mix(m_FogColor,texVal,fogFactor);
+       gl_FragColor = mix(m_FogColor,texVal,fogFactor);
 
 }

+ 2 - 9
jme3-effects/src/main/resources/Common/MatDefs/Post/Fog.j3md

@@ -11,8 +11,8 @@ MaterialDef Fade {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/Fog15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/Fog.frag
 
         WorldParameters {
         }
@@ -23,11 +23,4 @@ MaterialDef Fade {
         }
     }
 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/Fog.frag
-
-        WorldParameters {
-        }
-    }
 }

+ 0 - 25
jme3-effects/src/main/resources/Common/MatDefs/Post/Fog15.frag

@@ -1,25 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform DEPTHTEXTURE m_DepthTexture;
-
-uniform vec4 m_FogColor;
-uniform float m_FogDensity;
-uniform float m_FogDistance;
-
-in vec2 texCoord;
-out vec4 fragColor;
-
-const float LOG2 = 1.442695;
-
-void main() {
-       vec2 m_FrustumNearFar=vec2(1.0,m_FogDistance);
-       vec4 texVal = getColor(m_Texture, texCoord);
-       float fogVal = getDepth(m_DepthTexture,texCoord).r;
-       float depth= (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - fogVal* (m_FrustumNearFar.y-m_FrustumNearFar.x));
-
-       float fogFactor = exp2( -m_FogDensity * m_FogDensity * depth *  depth * LOG2 );
-       fogFactor = clamp(fogFactor, 0.0, 1.0);
-       fragColor =mix(m_FogColor,texVal,fogFactor);
-
-}

+ 0 - 16
jme3-effects/src/main/resources/Common/MatDefs/Post/GammaCorrection.frag

@@ -1,16 +0,0 @@
-uniform sampler2D m_Texture;
-varying vec2 texCoord;
-
-uniform float m_InvGamma;
-
-vec3 gamma(vec3 L,float invGamma){
-	return pow(L, vec3(invGamma));
-}
-
-void main() {
-    vec4 texVal = texture2D(m_Texture, texCoord);
-
-    texVal.rgb = gamma(texVal.rgb , m_InvGamma);
- 	
-    gl_FragColor = texVal;
-}

+ 0 - 31
jme3-effects/src/main/resources/Common/MatDefs/Post/GammaCorrection.j3md

@@ -1,31 +0,0 @@
-MaterialDef GammaCorrection {
- 
-    MaterialParameters {
-        Int NumSamples
-        Texture2D Texture
-        Float InvGamma       
-    }
- 
-    Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/GammaCorrection15.frag
- 
-        WorldParameters {
-        }
-        
-        Defines { 
-            RESOLVE_MS : NumSamples
-        }
-    }
- 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/GammaCorrection.frag
- 
-        WorldParameters {
-        }
-        
-         Defines {            
-        }
-    }
-}

+ 0 - 20
jme3-effects/src/main/resources/Common/MatDefs/Post/GammaCorrection15.frag

@@ -1,20 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
- 
-uniform COLORTEXTURE m_Texture;
-in vec2 texCoord;
- 
-uniform float m_InvGamma;
-
-vec3 gamma(vec3 L,float invGamma){
-	return pow(L, vec3(invGamma));
-}
-
-out vec4 fragColor;
- 
-void main() {
-    vec4 texVal = getColor(m_Texture, texCoord);
-    
-    texVal.rgb = gamma(texVal.rgb , m_InvGamma);
- 
-    fragColor = texVal;
-}

+ 17 - 16
jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering.frag

@@ -1,37 +1,38 @@
-uniform sampler2D m_Texture;
-uniform sampler2D m_DepthTexture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
+uniform DEPTHTEXTURE m_DepthTexture;
 uniform int m_NbSamples;
 uniform float m_BlurStart;
 uniform float m_BlurWidth;
 uniform float m_LightDensity;
-uniform bool m_Display;
 uniform vec3 m_LightPosition;
 
 varying vec2 texCoord;
 
-void main(void)
-{
-   if(m_Display){
+void main(void) {
+   #ifdef DISPLAY
 
-       vec4 colorRes= texture2D(m_Texture,texCoord);
-       float factor=(m_BlurWidth/(float(m_NbSamples)-1.0));
+       vec4 colorRes = getColor(m_Texture, texCoord);
+       float factor = (m_BlurWidth/(float(m_NbSamples) - 1.0));
        float scale;
-       vec2 texCoo=texCoord - m_LightPosition.xy;
+       vec2 texCoo = texCoord - m_LightPosition.xy;
        vec2 scaledCoord;
        vec4 res = vec4(0.0);
        for(int i=0; i<m_NbSamples; i++) {
             scale = float(i) * factor + m_BlurStart ;
-            scaledCoord=texCoo*scale + m_LightPosition.xy;
-            if(texture2D(m_DepthTexture,scaledCoord).r==1.0){
-                res += texture2D(m_Texture,scaledCoord);
+            scaledCoord = texCoo*scale + m_LightPosition.xy;
+            if(fetchTextureSample(m_DepthTexture, scaledCoord, 0).r == 1.0){
+                res += fetchTextureSample(m_Texture, scaledCoord, 0);
             }
         }
         res /= float(m_NbSamples);
 
         //Blend the original color with the averaged pixels
         float mean = (res.r + res.g + res.b)/3.0;
-        gl_FragColor =mix(colorRes ,mix( colorRes, res, m_LightDensity),mean);   
-    }else{
-        gl_FragColor= texture2D(m_Texture,texCoord);
-    }
+        gl_FragColor = mix(colorRes, mix( colorRes, res, m_LightDensity), mean);
+    #else
+        gl_FragColor = getColor(m_Texture, texCoord);
+    #endif
 }

+ 3 - 10
jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering.j3md

@@ -14,8 +14,8 @@ MaterialDef Light Scattering {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/LightScattering15.frag
+        VertexShader GLSL150 GLSL120:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL120: Common/MatDefs/Post/LightScattering.frag
 
         WorldParameters {          
         }
@@ -23,14 +23,7 @@ MaterialDef Light Scattering {
         Defines {
             RESOLVE_MS : NumSamples
             RESOLVE_DEPTH_MS : NumSamplesDepth
-        }
-    }
-
-    Technique {
-        VertexShader GLSL120:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL120: Common/MatDefs/Post/LightScattering.frag
-
-        WorldParameters { 
+            DISPLAY: Display
         }
     }
 }

+ 0 - 41
jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering15.frag

@@ -1,41 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform DEPTHTEXTURE m_DepthTexture;
-
-uniform int m_NbSamples;
-uniform float m_BlurStart;
-uniform float m_BlurWidth;
-uniform float m_LightDensity;
-uniform bool m_Display;
-uniform vec3 m_LightPosition;
-
-in vec2 texCoord;
-out vec4 fragColor;
-
-void main(void)
-{
-   if(m_Display){
-
-       vec4 colorRes= getColor(m_Texture,texCoord);
-       float factor=(m_BlurWidth/(float(m_NbSamples)-1.0));
-       float scale;
-       vec2 texCoo=texCoord - m_LightPosition.xy;
-       vec2 scaledCoord;
-       vec4 res = vec4(0.0);
-       for(int i=0; i<m_NbSamples; i++) {
-            scale = float(i) * factor + m_BlurStart ;
-            scaledCoord=texCoo*scale + m_LightPosition.xy;            
-            if(fetchTextureSample(m_DepthTexture, scaledCoord,0).r==1.0){
-                res += fetchTextureSample(m_Texture,scaledCoord,0);
-                }
-            }
-        res /= float(m_NbSamples);
-        
-        //Blend the original color with the averaged pixels
-        float mean = (res.r + res.g + res.b)/3.0;
-        fragColor =mix(colorRes ,mix( colorRes, res, m_LightDensity),mean);        
-    }else{
-        fragColor = getColor(m_Texture,texCoord);
-    }
-}

+ 0 - 16
jme3-effects/src/main/resources/Common/MatDefs/Post/LightScattering15.vert

@@ -1,16 +0,0 @@
-//uniform mat4 g_WorldViewProjectionMatrix;
-uniform vec2 g_Resolution;
-uniform vec4 g_ViewPort;
-uniform vec3 m_LightPosition;
-
-in vec4 inPosition;
-in vec2 inTexCoord;
-out vec2 texCoord;
-out vec2 lightPos;
-
-void main() {
-    vec2 pos = inPosition.xy* 2.0 - 1.0;   
-    gl_Position = vec4(pos, 0.0, 1.0);
-    lightPos=m_LightPosition.xy;
-    texCoord = inTexCoord;
-}

+ 5 - 2
jme3-effects/src/main/resources/Common/MatDefs/Post/Overlay.frag

@@ -1,9 +1,12 @@
-uniform sampler2D m_Texture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
 uniform vec4 m_Color;
 varying vec2 texCoord;
 
 void main() {
-      vec4 texVal = texture2D(m_Texture, texCoord);
+      vec4 texVal = getColor(m_Texture, texCoord);
       gl_FragColor = texVal * m_Color;
 }
 

+ 2 - 10
jme3-effects/src/main/resources/Common/MatDefs/Post/Overlay.j3md

@@ -8,8 +8,8 @@ MaterialDef Default GUI {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/Overlay15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/Overlay.frag
 
         WorldParameters {
         }
@@ -20,12 +20,4 @@ MaterialDef Default GUI {
 
     }
 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/Overlay.frag
-
-        WorldParameters {  
-        }
-
-    }
 }

+ 0 - 12
jme3-effects/src/main/resources/Common/MatDefs/Post/Overlay15.frag

@@ -1,12 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform vec4 m_Color;
-in vec2 texCoord;
-out vec4 fragColor;
-
-void main() {
-      vec4 texVal = getColor(m_Texture, texCoord);
-      fragColor = texVal * m_Color;
-}
-

+ 7 - 3
jme3-effects/src/main/resources/Common/MatDefs/Post/Posterization.frag

@@ -1,4 +1,7 @@
-uniform sampler2D m_Texture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
 varying vec2 texCoord;
  
 uniform int m_NumColors;
@@ -6,7 +9,8 @@ uniform float m_Gamma;
 uniform float m_Strength;
  
 void main() {
-    vec4 texVal = texture2D(m_Texture, texCoord);
+    vec4 color = getColor(m_Texture, texCoord);
+    vec4 texVal = vec4(color);
  
     texVal = pow(texVal, vec4(m_Gamma));
     texVal = texVal * vec4(m_NumColors);
@@ -14,5 +18,5 @@ void main() {
     texVal = texVal / vec4(m_NumColors);
     texVal = pow(texVal, vec4(1.0/m_Gamma));
  
-    gl_FragColor = mix(texture2D(m_Texture, texCoord), texVal, m_Strength);
+    gl_FragColor = mix(color, texVal, m_Strength);
 }

+ 7 - 9
jme3-effects/src/main/resources/Common/MatDefs/Post/Posterization.j3md

@@ -2,6 +2,7 @@ MaterialDef Posterization {
  
     MaterialParameters {
         Int NumSamples
+        Int NumSamplesDepth
         Texture2D Texture;
         Int NumColors;
         Float Gamma;
@@ -9,18 +10,15 @@ MaterialDef Posterization {
     }
  
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/Post/Posterization15.frag
+        VertexShader GLSL150 GLSL100:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL100: Common/MatDefs/Post/Posterization.frag
  
         WorldParameters {
         }
-    }
- 
-    Technique {
-        VertexShader GLSL100:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL100: Common/MatDefs/Post/Posterization.frag
- 
-        WorldParameters {   
+
+        Defines {
+           RESOLVE_MS : NumSamples
         }
+
     }
 }

+ 0 - 21
jme3-effects/src/main/resources/Common/MatDefs/Post/Posterization15.frag

@@ -1,21 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
- 
-uniform COLORTEXTURE m_Texture;
-in vec2 texCoord;
-out vec4 fragColor;
-
-uniform int m_NumColors;
-uniform float m_Gamma;
-uniform float m_Strength;
- 
-void main() {
-    vec4 texVal = getColor(m_Texture, texCoord);
- 
-    texVal = pow(texVal, vec4(m_Gamma));
-    texVal = texVal * m_NumColors;
-    texVal = floor(texVal);
-    texVal = texVal / m_NumColors;
-    texVal = pow(texVal, vec4(1.0/m_Gamma));
- 
-    fragColor = mix(getColor(m_Texture, texCoord), texVal, m_Strength);
-}

+ 5 - 2
jme3-effects/src/main/resources/Common/MatDefs/Post/bloomExtract.frag

@@ -1,6 +1,9 @@
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
 uniform float m_ExposurePow;
 uniform float m_ExposureCutoff;
-uniform sampler2D m_Texture;
+uniform COLORTEXTURE m_Texture;
 
 varying vec2 texCoord;
 
@@ -11,7 +14,7 @@ varying vec2 texCoord;
 void main(){ 
    vec4 color = vec4(0.0);
    #ifdef DO_EXTRACT
-    color = texture2D( m_Texture, texCoord );
+    color = getColorSingle( m_Texture, texCoord );
     if ( (color.r+color.g+color.b)/3.0 < m_ExposureCutoff ) {
           color = vec4(0.0);
     }else{

+ 0 - 33
jme3-effects/src/main/resources/Common/MatDefs/Post/bloomExtract15.frag

@@ -1,33 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-
-uniform float m_ExposurePow;
-uniform float m_ExposureCutoff;
-
-in vec2 texCoord;
-out vec4 outFragColor;
-
-#ifdef HAS_GLOWMAP
-  uniform sampler2D m_GlowMap;
-#endif
-
-void main(){
-   vec4 color = vec4(0.0);
-   #ifdef DO_EXTRACT
-     color = getColorSingle(m_Texture, texCoord);
-     if ( (color.r + color.g + color.b) / 3.0 >= m_ExposureCutoff ) {
-         color = pow(color, vec4(m_ExposurePow));
-     }else{
-         color = vec4(0.0);
-     }
-   #endif
-
-   #ifdef HAS_GLOWMAP
-        vec4 glowColor = texture( m_GlowMap, texCoord );
-        glowColor = pow(glowColor, vec4(m_ExposurePow));
-        color += glowColor;
-   #endif
-   
-   outFragColor = color;
-}

+ 5 - 2
jme3-effects/src/main/resources/Common/MatDefs/Post/bloomFinal.frag

@@ -1,11 +1,14 @@
-uniform sampler2D m_Texture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
 uniform sampler2D m_BloomTex;
 uniform float m_BloomIntensity;
 
 varying vec2 texCoord;
 
 void main(){
-   vec4 colorRes = texture2D(m_Texture, texCoord);
+   vec4 colorRes = getColor(m_Texture, texCoord);
    vec4 bloom = texture2D(m_BloomTex, texCoord);
    gl_FragColor = bloom * m_BloomIntensity + colorRes;
 }

+ 0 - 16
jme3-effects/src/main/resources/Common/MatDefs/Post/bloomFinal15.frag

@@ -1,16 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-
-uniform sampler2D m_BloomTex;
-uniform float m_BloomIntensity;
-
-in vec2 texCoord;
-out vec4 fragColor;
-
-void main(){
-  vec4 colorRes = getColor(m_Texture,texCoord);
-  vec4 bloom = texture(m_BloomTex, texCoord);
-  fragColor = bloom * m_BloomIntensity + colorRes;
-}
-

+ 10 - 10
jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssao.frag

@@ -1,10 +1,13 @@
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
 uniform vec2 g_Resolution;
 uniform vec2 g_ResolutionInverse;
 uniform vec2 m_FrustumNearFar;
-uniform sampler2D m_Texture;
+uniform COLORTEXTURE m_Texture;
 uniform sampler2D m_Normals;
 uniform sampler2D m_RandomMap;
-uniform sampler2D m_DepthTexture;
+uniform DEPTHTEXTURE m_DepthTexture;
 uniform vec3 m_FrustumCorner;
 uniform float m_SampleRadius;
 uniform float m_Intensity;
@@ -24,7 +27,7 @@ vec3 getPosition(float depthv, in vec2 uv){
   float x = mix(-m_FrustumCorner.x, m_FrustumCorner.x, uv.x);
   float y = mix(-m_FrustumCorner.y, m_FrustumCorner.y, uv.y);
 
-  return depth* vec3(x, y, m_FrustumCorner.z);
+  return depth * vec3(x, y, m_FrustumCorner.z);
 }
 
 vec3 approximateNormal(in vec3 pos,in vec2 texCoord){
@@ -45,12 +48,12 @@ vec3 getNormal(in vec2 uv){
 }
 
 vec2 getRandom(in vec2 uv){  
-   vec4 rand=texture2D(m_RandomMap,g_Resolution * uv / 128.0 * 3.0)*2.0 -1.0;
+   vec4 rand = texture2D(m_RandomMap,g_Resolution * uv / 128.0 * 3.0)*2.0 -1.0;
    return normalize(rand.xy);
 }
 
 float doAmbientOcclusion(in vec2 tc, in vec3 pos, in vec3 norm){
-   float depthv = texture2D(m_DepthTexture, tc).r;
+   float depthv = getDepth(m_DepthTexture, tc).r;
    vec3 diff = getPosition(depthv, tc)- pos;
    vec3 v = normalize(diff);
    float d = length(diff) * m_Scale;
@@ -60,18 +63,15 @@ float doAmbientOcclusion(in vec2 tc, in vec3 pos, in vec3 norm){
 
 vec2 reflection(in vec2 v1,in vec2 v2){
     vec2 result= 2.0 * dot(v2, v1) * v2;
-    result=v1-result;
+    result = v1-result;
     return result;
 }
 
-
-//const vec2 vec[4] = vec2[4](vec2(1.0,0.0), vec2(-1.0,0.0), vec2(0.0,1.0), vec2(0.0,-1.0));
 void main(){
 
    float result;
 
-
-   float depthv = texture2D(m_DepthTexture, texCoord).r;
+   float depthv = getDepth(m_DepthTexture, texCoord).r;
    //optimization, do not calculate AO if depth is 1
    if(depthv == 1.0){
            gl_FragColor = vec4(1.0);

+ 2 - 15
jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssao.j3md

@@ -18,8 +18,8 @@ MaterialDef SSAO {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/SSAO/ssao15.frag
+        VertexShader GLSL150 GLSL120:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL120: Common/MatDefs/SSAO/ssao.frag
 
         WorldParameters {
             WorldViewMatrix
@@ -34,17 +34,4 @@ MaterialDef SSAO {
         }
     }
 
-    Technique {
-        VertexShader GLSL120:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL120: Common/MatDefs/SSAO/ssao.frag
-
-        WorldParameters {
-            WorldViewMatrix
-            Resolution
-            ResolutionInverse
-        }
-        Defines {
-            APPROXIMATE_NORMALS : ApproximateNormals
-        }
-    }
 }

+ 0 - 116
jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssao15.frag

@@ -1,116 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform DEPTHTEXTURE m_DepthTexture;
-
-uniform vec2 g_ResolutionInverse;
-uniform vec2 g_Resolution;
-uniform vec2 m_FrustumNearFar;
-uniform sampler2D m_Normals;
-uniform sampler2D m_RandomMap;
-uniform vec3 m_FrustumCorner;
-uniform float m_SampleRadius;
-uniform float m_Intensity;
-uniform float m_Scale;
-uniform float m_Bias;
-uniform vec2[4] m_Samples;
-
-in vec2 texCoord;
-
-out vec4 fragColor;
-
-
-
-vec3 getPosition(float depthv, in vec2 uv){
-  //Reconstruction from depth
-  float depth = (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - depthv * (m_FrustumNearFar.y-m_FrustumNearFar.x));
-
-  //one frustum corner method
-  float x = mix(-m_FrustumCorner.x, m_FrustumCorner.x, uv.x);
-  float y = mix(-m_FrustumCorner.y, m_FrustumCorner.y, uv.y);
-
-  return depth* vec3(x, y, m_FrustumCorner.z);
-}
-
-vec3 getNormal(in vec2 uv){
-  return normalize(texture2D(m_Normals, uv).xyz * 2.0 - 1.0);
-}
-
-vec3 approximateNormal(in vec3 pos,in vec2 texCoord){
-    float step = g_ResolutionInverse.x ;
-    float stepy = g_ResolutionInverse.y ;
-    float depth2 = getDepth(m_DepthTexture,texCoord + vec2(step,-stepy)).r;
-    float depth3 = getDepth(m_DepthTexture,texCoord + vec2(-step,-stepy)).r;
-    vec3 pos2 = vec3(getPosition(depth2,texCoord + vec2(step,-stepy)));
-    vec3 pos3 = vec3(getPosition(depth3,texCoord + vec2(-step,-stepy)));
-
-    vec3 v1 = (pos - pos2).xyz;
-    vec3 v2 = (pos3 - pos2).xyz;
-    return normalize(cross(-v1, v2));
-}
-
-vec2 getRandom(in vec2 uv){
-   //float rand=(fract(uv.x*(g_Resolution.x/2.0))*0.25)+(fract(uv.y*(g_Resolution.y/2.0))*0.5);
-   vec4 rand=texture2D(m_RandomMap, g_Resolution * uv / 128.0 * 3.0)*2.0 -1.0;
-
-   return normalize(rand.xy);
-}
-
-float doAmbientOcclusion(in vec2 tc, in vec3 pos, in vec3 norm){
-   float depthv = getDepth(m_DepthTexture, tc).r;
-   vec3 diff = getPosition(depthv, tc)- pos;
-   vec3 v = normalize(diff);
-   float d = length(diff) * m_Scale;
-
-   return max(0.0, dot(norm, v) - m_Bias) * ( 1.0/(1.0 + d) ) * m_Intensity;
-}
-
-
-vec2 reflection(in vec2 v1,in vec2 v2){
-    vec2 result= 2.0 * dot(v2, v1) * v2;
-    result=v1-result;
-    return result;
-}
-
-
-void main(){
-
-   float result;
-
-   float depthv = getDepth(m_DepthTexture, texCoord).r;
-   //optimization, do not calculate AO if depth is 1
-   if(depthv == 1.0){
-           fragColor = vec4(1.0);
-           return;
-   }
-   vec3 position = getPosition(depthv, texCoord);
-
-   #ifdef APPROXIMATE_NORMALS
-        vec3 normal = approximateNormal(position, texCoord);
-   #else
-        vec3 normal = getNormal(texCoord);
-   #endif
-
-   vec2 rand = getRandom(texCoord);
-
-   float ao = 0.0;
-   float rad =m_SampleRadius / position.z;
-
-
-   int iterations = 4;
-   for (int j = 0; j < iterations; ++j){
-      vec2 coord1 = reflection(vec2(m_Samples[j]), rand) * vec2(rad,rad);
-      vec2 coord2 = vec2(coord1.x* 0.707 - coord1.y* 0.707, coord1.x* 0.707 + coord1.y* 0.707) ;
-
-      ao += doAmbientOcclusion(texCoord + coord1.xy * 0.25, position, normal);
-      ao += doAmbientOcclusion(texCoord + coord2 * 0.50, position, normal);
-      ao += doAmbientOcclusion(texCoord + coord1.xy * 0.75, position, normal);
-      ao += doAmbientOcclusion(texCoord + coord2 * 1.00, position, normal);
-
-   }
-   ao /= float(iterations) * 4.0;
-   result = 1.0-ao;
-
-   fragColor = vec4(result,result,result, 1.0);
-
-}

+ 106 - 138
jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssaoBlur.frag

@@ -1,5 +1,8 @@
-uniform sampler2D m_Texture;
-uniform sampler2D m_DepthTexture;
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+
+uniform COLORTEXTURE m_Texture;
+uniform DEPTHTEXTURE m_DepthTexture;
 uniform sampler2D m_SSAOMap;
 uniform vec2 g_Resolution;
 uniform bool m_UseOnlyAo;
@@ -10,150 +13,115 @@ uniform vec2 m_FrustumNearFar;
 
 varying vec2 texCoord;
 
+const float epsilon = 0.005;
+
+float readDepth(in vec2 uv){
+    float depthv = fetchTextureSample(m_DepthTexture, uv, 0).r;
+    return (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - depthv* (m_FrustumNearFar.y-m_FrustumNearFar.x));
+}
+
+vec4 convolutionFilter(){
+    vec4 sum = vec4(0.0);
+
+    float x = texCoord.x;
+    float y = texCoord.y;
+
+    float xScale = m_XScale;
+    float yScale = m_YScale;
+
+    float zsum = 1.0;
+    float Zp =readDepth(texCoord);
+
+    vec2 sample = vec2(x - 2.0 * xScale, y - 2.0 * yScale);
+    float zTmp = readDepth(sample);
+    float coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x - 0.0 * xScale, y - 2.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x + 2.0 * xScale, y - 2.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x - 1.0 * xScale, y - 1.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x + 1.0 * xScale, y - 1.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x - 2.0 * xScale, y - 0.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x + 2.0 * xScale, y - 0.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x - 1.0 * xScale, y + 1.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x + 1.0 * xScale, y + 1.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x - 2.0 * xScale, y + 2.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x - 0.0 * xScale, y + 2.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+    sample = vec2(x + 2.0 * xScale, y + 2.0 * yScale);
+    zTmp =readDepth(sample);
+    coefZ = 1.0 / (epsilon + abs(Zp - zTmp));
+    zsum += coefZ;
+    sum += coefZ* texture2D( m_SSAOMap, sample);
+
+
+    return  sum / zsum;
+}
+
 vec4 getColor(vec4 color){
 
-    
+
     #ifdef USE_ONLY_AO
         return color;
     #endif
     #ifdef USE_AO
-        return texture2D(m_Texture,texCoord)* color;
+        return getColor(m_Texture,texCoord) * color;
     #endif
-    
-    return texture2D(m_Texture,texCoord);
 
-}
+    return getColor(m_Texture,texCoord);
 
-float readDepth(in vec2 uv){
-    float depthv =texture2D(m_DepthTexture,uv).r;
-    return (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - depthv* (m_FrustumNearFar.y-m_FrustumNearFar.x));
 }
 
- const float epsilon = 0.005;
-
-
-/*
-    const int kernelSize=7;
-   
-    vec4 bilateralFilter() {
-        vec4 color = vec4(0.0);
-
-        vec2 sample;
-        float sum = 0.0;
-        float coefZ;
-        float Zp = readDepth(texCoord);
-
-        for(int i = -(kernelSize-1); i <= (kernelSize-1); i+=2) {
-            for(int j = -(kernelSize-1); j <= (kernelSize-1); j+=2) {
-                  sample = texCoord + vec2(i,j) / g_Resolution;           
-                float zTmp =readDepth(sample);
-                coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-                sum += coefZ;
-
-                color += coefZ * texture2D(m_SSAOMap,sample);
-             
-            }
-        }
-
-        return color / sum;
-    }
-*/
-
-    vec4 convolutionFilter(){
-           vec4 sum = vec4(0.0);
-
-            float x = texCoord.x;
-            float y = texCoord.y;
-
-            float xScale = m_XScale;
-            float yScale = m_YScale;
-       
-            float zsum = 1.0;
-        float Zp =readDepth(texCoord);
-
-
-        vec2 sample = vec2(x - 2.0 * xScale, y - 2.0 * yScale);           
-        float zTmp =readDepth(sample);
-        float coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 0.0 * xScale, y - 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 2.0 * xScale, y - 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 1.0 * xScale, y - 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 1.0 * xScale, y - 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-  
-        sample = vec2(x - 2.0 * xScale, y - 0.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 2.0 * xScale, y - 0.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 1.0 * xScale, y + 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-   
-        sample = vec2(x + 1.0 * xScale, y + 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 2.0 * xScale, y + 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-  
-        sample = vec2(x - 0.0 * xScale, y + 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 2.0 * xScale, y + 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-
-        return  sum / zsum;
-    }
-
-
-    void main(){
-        //  float depth =texture2D(m_DepthTexture,uv).r;
-
-       gl_FragColor=getColor(convolutionFilter());
-      // gl_FragColor=getColor(bilateralFilter());
-      //  gl_FragColor=texture2D(m_SSAOMap,texCoord);
-
-    }
+void main(){
+   gl_FragColor = getColor(convolutionFilter());
+}

+ 2 - 18
jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssaoBlur.j3md

@@ -14,8 +14,8 @@ MaterialDef SSAOBlur {
     }
 
     Technique {
-        VertexShader GLSL150:   Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150: Common/MatDefs/SSAO/ssaoBlur15.frag
+        VertexShader GLSL150 GLSL120:   Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL120: Common/MatDefs/SSAO/ssaoBlur.frag
 
         WorldParameters {
             WorldViewMatrix
@@ -28,20 +28,4 @@ MaterialDef SSAOBlur {
             RESOLVE_DEPTH_MS : NumSamplesDepth
         }
     }
-
-    Technique {
-        VertexShader GLSL120:   Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL120: Common/MatDefs/SSAO/ssaoBlur.frag
-
-        WorldParameters {            
-            WorldViewMatrix
-        }
-        
-        Defines {
-            USE_AO : UseAo
-            USE_ONLY_AO : UseOnlyAo
-            RESOLVE_MS : NumSamples
-            RESOLVE_DEPTH_MS : NumSamplesDepth
-        }
-    }
 }

+ 0 - 133
jme3-effects/src/main/resources/Common/MatDefs/SSAO/ssaoBlur15.frag

@@ -1,133 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-
-uniform COLORTEXTURE m_Texture;
-uniform DEPTHTEXTURE m_DepthTexture;
-uniform sampler2D m_SSAOMap;
-uniform bool m_UseOnlyAo;
-uniform bool m_UseAo;
-uniform float m_XScale;
-uniform float m_YScale;
-uniform vec2 m_FrustumNearFar;
-
-in vec2 texCoord;
-out vec4 fragColor;
-
-vec4 getResult(vec4 color){
- 
-    #ifdef USE_ONLY_AO
-        return color;
-    #endif
-    #ifdef USE_AO
-        return getColor(m_Texture,texCoord)* color;
-    #endif
-    
-    return getColor(m_Texture,texCoord);
-
-}
-
-float readDepth(in vec2 uv){
-    float depthv =fetchTextureSample(m_DepthTexture,uv,0).r;
-    return (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - depthv* (m_FrustumNearFar.y-m_FrustumNearFar.x));
-}
-
- const float epsilon = 0.005;
-
-
-    vec4 convolutionFilter(){
-           vec4 sum = vec4(0.0);
-
-            float x = texCoord.x;
-            float y = texCoord.y;
-
-            float xScale = m_XScale;
-            float yScale = m_YScale;
-       
-            float zsum = 1.0;
-        float Zp =readDepth(texCoord);
-
-
-        vec2 sample = vec2(x - 2.0 * xScale, y - 2.0 * yScale);           
-        float zTmp =readDepth(sample);
-        float coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 0.0 * xScale, y - 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 2.0 * xScale, y - 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 1.0 * xScale, y - 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 1.0 * xScale, y - 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-  
-        sample = vec2(x - 2.0 * xScale, y - 0.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 2.0 * xScale, y - 0.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 1.0 * xScale, y + 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-   
-        sample = vec2(x + 1.0 * xScale, y + 1.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x - 2.0 * xScale, y + 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-  
-        sample = vec2(x - 0.0 * xScale, y + 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-        sample = vec2(x + 2.0 * xScale, y + 2.0 * yScale);           
-        zTmp =readDepth(sample);
-        coefZ = 1.0 / (epsilon + abs(Zp - zTmp));               
-        zsum += coefZ;
-        sum += coefZ* texture2D( m_SSAOMap, sample);
-
-
-        return  sum / zsum;
-    }
-
-
-    void main(){
-        //  float depth =texture2D(m_DepthTexture,uv).r;
-
-       fragColor=getResult(convolutionFilter());
-      // gl_FragColor=getResult(bilateralFilter());
-      //  gl_FragColor=getColor(m_SSAOMap,texCoord);
-
-    }

+ 446 - 417
jme3-effects/src/main/resources/Common/MatDefs/Water/Water.frag

@@ -1,418 +1,447 @@
-#import "Common/ShaderLib/WaterUtil.glsllib"
-// Water pixel shader
-// Copyright (C) JMonkeyEngine 3.0
-// by Remy Bouquet (nehon) for JMonkeyEngine 3.0
-// original HLSL version by Wojciech Toman 2009
-
-uniform sampler2D m_HeightMap;
-uniform sampler2D m_Texture;
-uniform sampler2D m_DepthTexture;
-uniform sampler2D m_NormalMap;
-uniform sampler2D m_FoamMap;
-uniform sampler2D m_CausticsMap;
-uniform sampler2D m_ReflectionMap;
-
-uniform mat4 g_ViewProjectionMatrixInverse;
-uniform mat4 m_TextureProjMatrix;
-uniform vec3 m_CameraPosition;
-
-uniform float m_WaterHeight;
-uniform float m_Time;
-uniform float m_WaterTransparency;
-uniform float m_NormalScale;
-uniform float m_R0;
-uniform float m_MaxAmplitude;
-uniform vec3 m_LightDir;
-uniform vec4 m_LightColor;
-uniform float m_ShoreHardness;
-uniform float m_FoamHardness;
-uniform float m_RefractionStrength;
-uniform vec3 m_FoamExistence;
-uniform vec3 m_ColorExtinction;
-uniform float m_Shininess;
-uniform vec4 m_WaterColor;
-uniform vec4 m_DeepWaterColor;
-uniform vec2 m_WindDirection;
-uniform float m_SunScale;
-uniform float m_WaveScale;
-uniform float m_UnderWaterFogDistance;
-uniform float m_CausticsIntensity;
-#ifdef ENABLE_AREA
-uniform vec3 m_Center;
-uniform float m_Radius;
-#endif
-
-vec2 scale = vec2(m_WaveScale, m_WaveScale);
-float refractionScale = m_WaveScale;
-
-// Modifies 4 sampled normals. Increase first values to have more
-// smaller "waves" or last to have more bigger "waves"
-const vec4 normalModifier = vec4(3.0, 2.0, 4.0, 10.0);
-// Strength of displacement along normal.
-// Strength of displacement along normal.
-uniform float m_ReflectionDisplace;
-// Water transparency along eye vector.
-const float visibility = 3.0;
-// foam intensity
-uniform float m_FoamIntensity ;
-
-varying vec2 texCoord;
-
-mat3 MatrixInverse(in mat3 inMatrix){  
-    float det = dot(cross(inMatrix[0], inMatrix[1]), inMatrix[2]);
-    mat3 T = transpose(inMatrix);
-    return mat3(cross(T[1], T[2]),
-        cross(T[2], T[0]),
-        cross(T[0], T[1])) / det;
-}
-
-
-mat3 computeTangentFrame(in vec3 N, in vec3 P, in vec2 UV) {
-    vec3 dp1 = dFdx(P);
-    vec3 dp2 = dFdy(P);
-    vec2 duv1 = dFdx(UV);
-    vec2 duv2 = dFdy(UV);
-
-    // solve the linear system
-    mat3 M = mat3(dp1, dp2, cross(dp1, dp2));
-    //vec3 dp1xdp2 = cross(dp1, dp2);
-    mat3 inverseM = MatrixInverse(M);
-    //mat2x3 inverseM = mat2x3(cross(dp2, dp1xdp2), cross(dp1xdp2, dp1));
-
-    vec3 T = inverseM * vec3(duv1.x, duv2.x, 0.0);
-    vec3 B = inverseM * vec3(duv1.y, duv2.y, 0.0);
-
-    //vec3 T = inverseM * vec2(duv1.x, duv2.x);
-    //vec3 B = inverseM * vec2(duv1.y, duv2.y);
-
-    // construct tangent frame
-    float maxLength = max(length(T), length(B));
-    T = T / maxLength;
-    B = B / maxLength;
-
-    //vec3 tangent = normalize(T);
-    //vec3 binormal = normalize(B);
-
-    return mat3(T, B, N);
-}
-
-float saturate(in float val){
-    return clamp(val,0.0,1.0);
-}
-
-vec3 saturate(in vec3 val){
-    return clamp(val,vec3(0.0),vec3(1.0));
-}
-
-
-vec3 getPosition(in float depth, in vec2 uv){
-    vec4 pos = vec4(uv, depth, 1.0) * 2.0 - 1.0;
-    pos = g_ViewProjectionMatrixInverse * pos;
-    return pos.xyz / pos.w;
-}
-
-// Function calculating fresnel term.
-// - normal - normalized normal vector
-// - eyeVec - normalized eye vector
-float fresnelTerm(in vec3 normal,in vec3 eyeVec){
-    float angle = 1.0 - saturate(dot(normal, eyeVec));
-    float fresnel = angle * angle;
-    fresnel = fresnel * fresnel;
-    fresnel = fresnel * angle;
-    return saturate(fresnel * (1.0 - saturate(m_R0)) + m_R0 - m_RefractionStrength);
-}
-
-vec2 m_FrustumNearFar=vec2(1.0,m_UnderWaterFogDistance);
-const float LOG2 = 1.442695;
-
-vec4 underWater(){
-
-
-    float sceneDepth = texture2D(m_DepthTexture, texCoord).r;
-    vec3 color2 = texture2D(m_Texture, texCoord).rgb;
-    
-    vec3 position = getPosition(sceneDepth, texCoord);
-    float level = m_WaterHeight;
-
-    vec3 eyeVec = position - m_CameraPosition;    
- 
-    // Find intersection with water surface
-    vec3 eyeVecNorm = normalize(eyeVec);
-    float t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-    vec3 surfacePoint = m_CameraPosition + eyeVecNorm * t;
-
-    vec2 texC = vec2(0.0);
-
-    float cameraDepth = length(m_CameraPosition - surfacePoint);  
-    texC = (surfacePoint.xz + eyeVecNorm.xz) * scale + m_Time * 0.03 * m_WindDirection;
-    float bias = texture2D(m_HeightMap, texC).r;
-    level += bias * m_MaxAmplitude;
-    t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-    surfacePoint = m_CameraPosition + eyeVecNorm * t; 
-    eyeVecNorm = normalize(m_CameraPosition - surfacePoint);
-
-    // Find normal of water surface
-    float normal1 = texture2D(m_HeightMap, (texC + vec2(-1.0, 0.0) / 256.0)).r;
-    float normal2 = texture2D(m_HeightMap, (texC + vec2(1.0, 0.0) / 256.0)).r;
-    float normal3 = texture2D(m_HeightMap, (texC + vec2(0.0, -1.0) / 256.0)).r;
-    float normal4 = texture2D(m_HeightMap, (texC + vec2(0.0, 1.0) / 256.0)).r;
-
-    vec3 myNormal = normalize(vec3((normal1 - normal2) * m_MaxAmplitude,m_NormalScale,(normal3 - normal4) * m_MaxAmplitude));
-    vec3 normal = myNormal*-1.0;
-    float fresnel = fresnelTerm(normal, eyeVecNorm); 
-
-    vec3 refraction = color2;
-    #ifdef ENABLE_REFRACTION
-        texC = texCoord.xy *sin (fresnel+1.0);
-        texC = clamp(texC,0.0,1.0);
-        refraction = texture2D(m_Texture, texC).rgb;
-    #endif 
-
-   float waterCol = saturate(length(m_LightColor.rgb) / m_SunScale);
-   refraction = mix(mix(refraction, m_DeepWaterColor.rgb * waterCol, m_WaterTransparency),  m_WaterColor.rgb* waterCol,m_WaterTransparency);
-
-    vec3 foam = vec3(0.0);
-    #ifdef ENABLE_FOAM    
-        texC = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * m_WindDirection + sin(m_Time * 0.001 + position.x) * 0.005;
-        vec2 texCoord2 = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.1 * m_WindDirection + sin(m_Time * 0.001 + position.z) * 0.005;
-
-        if(m_MaxAmplitude - m_FoamExistence.z> 0.0001){
-            foam += ((texture2D(m_FoamMap, texC) + texture2D(m_FoamMap, texCoord2)) * m_FoamIntensity  * m_FoamIntensity * 0.3 *
-               saturate((level - (m_WaterHeight + m_FoamExistence.z)) / (m_MaxAmplitude - m_FoamExistence.z))).rgb;
-        }
-        foam *= m_LightColor.rgb;    
-    #endif
-
-
-
-    vec3 specular = vec3(0.0);   
-    vec3 color ;
-    float fogFactor;
-
-    if(position.y>level){
-        #ifdef ENABLE_SPECULAR
-            if(step(0.9999,sceneDepth)==1.0){
-                vec3 lightDir=normalize(m_LightDir);
-                vec3 mirrorEye = (2.0 * dot(eyeVecNorm, normal) * normal - eyeVecNorm);
-                float dotSpec = saturate(dot(mirrorEye.xyz, -lightDir) * 0.5 + 0.5);
-                specular = vec3((1.0 - fresnel) * saturate(-lightDir.y) * ((pow(dotSpec, 512.0)) * (m_Shininess * 1.8 + 0.2)));
-                specular += specular * 25.0 * saturate(m_Shininess - 0.05);
-                specular=specular * m_LightColor.rgb * 100.0;
-            }
-        #endif
-        float fogIntensity= 8.0 * m_WaterTransparency;
-        fogFactor = exp2( -fogIntensity * fogIntensity * cameraDepth * 0.03 * LOG2 );
-        fogFactor = clamp(fogFactor, 0.0, 1.0);        
-        color =mix(m_DeepWaterColor.rgb,refraction,fogFactor);   
-        specular=specular*fogFactor;    
-        color = saturate(color + max(specular, foam ));
-    }else{
-        vec3 caustics = vec3(0.0);
-        #ifdef ENABLE_CAUSTICS 
-            vec2 windDirection=m_WindDirection;
-            texC = (position.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * windDirection + sin(m_Time  + position.x) * 0.01;
-            vec2 texCoord2 = (position.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * windDirection + sin(m_Time  + position.z) * 0.01;
-            caustics += (texture2D(m_CausticsMap, texC)+ texture2D(m_CausticsMap, texCoord2)).rgb;      
-            caustics=saturate(mix(m_WaterColor.rgb,caustics,m_CausticsIntensity));            
-            color=mix(color2,caustics,m_CausticsIntensity);          
-        #else
-            color=color2;
-        #endif
-                
-        float fogDepth= (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - sceneDepth* (m_FrustumNearFar.y-m_FrustumNearFar.x));
-        float fogIntensity= 18 * m_WaterTransparency;
-        fogFactor = exp2( -fogIntensity * fogIntensity * fogDepth *  fogDepth * LOG2 );
-        fogFactor = clamp(fogFactor, 0.0, 1.0);
-        color =mix(m_DeepWaterColor.rgb,color,fogFactor);
-    }
-
-    return vec4(color, 1.0);   
-}
-
-void main(){
-    float sceneDepth = texture2D(m_DepthTexture, texCoord).r;
-    float isAtFarPlane = step(0.99998, sceneDepth);
-
-    vec3 color2 = texture2D(m_Texture, texCoord).rgb;
-    vec3 color = color2;
-
-    vec3 position = getPosition(sceneDepth,texCoord);
-    float level = m_WaterHeight;
-
-    // If we are underwater let's go to under water function
-    if(level >= m_CameraPosition.y){
-        #ifdef ENABLE_AREA
-            vec3 dist = m_CameraPosition-m_Center;
-            if(isOverExtent(m_CameraPosition, m_Center, m_Radius)){    
-                gl_FragColor = vec4(color2, 1.0);
-                return;
-            }    
-        #endif
-        gl_FragColor = underWater();
-        return ;
-    }
-
-    #ifdef ENABLE_AREA
-        if(isOverExtent(position, m_Center, m_Radius)){    
-            gl_FragColor = vec4(color2, 1.0);
-            return;
-        }  
-    #endif
-
-    //#ifndef ENABLE_RIPPLES
-    // This optimization won't work on NVIDIA cards if ripples are enabled
-    if(position.y > level + m_MaxAmplitude + isAtFarPlane * 100.0){
-        gl_FragColor = vec4(color2, 1.0);
-        return;
-    }
-    //#endif
-
-    vec3 eyeVec = position - m_CameraPosition;
-    float diff = level - position.y;
-    float cameraDepth = m_CameraPosition.y - position.y;
-
-    // Find intersection with water surface
-    vec3 eyeVecNorm = normalize(eyeVec);
-    float t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-    vec3 surfacePoint = m_CameraPosition + eyeVecNorm * t;
-
-    vec2 texC;
-    int samples = 1;
-    #ifdef ENABLE_HQ_SHORELINE
-        samples = 10;
-    #endif
-    float biasFactor = 1.0/samples;
-    for (int i = 0; i < samples; i++){
-        texC = (surfacePoint.xz + eyeVecNorm.xz * biasFactor) * scale + m_Time * 0.03 * m_WindDirection;
-
-        float bias = texture2D(m_HeightMap, texC).r;
-
-        bias *= biasFactor;
-        level += bias * m_MaxAmplitude;
-        t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-        surfacePoint = m_CameraPosition + eyeVecNorm * t;
-    }
-
-    float depth = length(position - surfacePoint);
-    float depth2 = surfacePoint.y - position.y;
-
-    // XXX: HACK ALERT: Increase water depth to infinity if at far plane
-    // Prevents "foam on horizon" issue
-    // For best results, replace the "100.0" below with the
-    // highest value in the m_ColorExtinction vec3
-    depth  += isAtFarPlane * 100.0;
-    depth2 += isAtFarPlane * 100.0;
-
-    eyeVecNorm = normalize(m_CameraPosition - surfacePoint);
-
-    // Find normal of water surface
-    float normal1 = texture2D(m_HeightMap, (texC + vec2(-1.0, 0.0) / 256.0)).r;
-    float normal2 = texture2D(m_HeightMap, (texC + vec2(1.0, 0.0) / 256.0)).r;
-    float normal3 = texture2D(m_HeightMap, (texC + vec2(0.0, -1.0) / 256.0)).r;
-    float normal4 = texture2D(m_HeightMap, (texC + vec2(0.0, 1.0) / 256.0)).r;
-
-    vec3 myNormal = normalize(vec3((normal1 - normal2) * m_MaxAmplitude,m_NormalScale,(normal3 - normal4) * m_MaxAmplitude));
-    vec3 normal = vec3(0.0);
-
-    #ifdef ENABLE_RIPPLES
-        texC = surfacePoint.xz * 0.8 + m_WindDirection * m_Time* 1.6;
-        mat3 tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal0a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
-
-        texC = surfacePoint.xz * 0.4 + m_WindDirection * m_Time* 0.8;
-        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal1a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
-
-        texC = surfacePoint.xz * 0.2 + m_WindDirection * m_Time * 0.4;
-        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal2a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
-
-        texC = surfacePoint.xz * 0.1 + m_WindDirection * m_Time * 0.2;
-        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal3a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
-
-        normal = normalize(normal0a * normalModifier.x + normal1a * normalModifier.y +normal2a * normalModifier.z + normal3a * normalModifier.w);
-        // XXX: Here's another way to fix the terrain edge issue,
-        // But it requires GLSL 1.3 and still looks kinda incorrect
-        // around edges
-        // To make the shader 1.2 compatible we use a trick :
-        // we clamp the x value of the normal and compare it to it's former value instead of using isnan.
-        normal = clamp(normal.x,0.0,1.0)!=normal.x ? myNormal : normal;
-        //if (position.y > level){
-        //    gl_FragColor = vec4(color2 + normal*0.0001, 1.0);
-        //    return;
-        //}
-    #else
-        normal = myNormal;
-    #endif
-    
-    vec3 refraction = color2;
-    #ifdef ENABLE_REFRACTION
-        texC = texCoord.xy;
-        texC += sin(m_Time*1.8  + 3.0 * abs(position.y)) * (refractionScale * min(depth2, 1.0));
-        texC = clamp(texC,0.0,1.0);
-        refraction = texture2D(m_Texture, texC).rgb;
-    #endif
-
-    vec3 waterPosition = surfacePoint.xyz;
-    waterPosition.y -= (level - m_WaterHeight);
-    vec4 texCoordProj = m_TextureProjMatrix * vec4(waterPosition, 1.0);
-
-    texCoordProj.x = texCoordProj.x + m_ReflectionDisplace * normal.x;
-    texCoordProj.z = texCoordProj.z + m_ReflectionDisplace * normal.z;
-    texCoordProj /= texCoordProj.w;
-    texCoordProj.y = 1.0 - texCoordProj.y;
-
-    vec3 reflection = texture2D(m_ReflectionMap, texCoordProj.xy).rgb;
-
-    float fresnel = fresnelTerm(normal, eyeVecNorm);
-
-    float depthN = depth * m_WaterTransparency;
-    float waterCol = saturate(length(m_LightColor.rgb) / m_SunScale);
-    refraction = mix(mix(refraction, m_WaterColor.rgb * waterCol, saturate(depthN / visibility)),
-        m_DeepWaterColor.rgb * waterCol, saturate(depth2 / m_ColorExtinction));
-
-    vec3 foam = vec3(0.0);
-    #ifdef ENABLE_FOAM
-        texC = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * m_WindDirection + sin(m_Time * 0.001 + position.x) * 0.005;
-        vec2 texCoord2 = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.1 * m_WindDirection + sin(m_Time * 0.001 + position.z) * 0.005;
-
-        if(depth2 < m_FoamExistence.x){
-            foam = (texture2D(m_FoamMap, texC).r + texture2D(m_FoamMap, texCoord2)).rgb * m_FoamIntensity;
-        }else if(depth2 < m_FoamExistence.y){
-            foam = mix((texture2D(m_FoamMap, texC) + texture2D(m_FoamMap, texCoord2)) * m_FoamIntensity, vec4(0.0),
-                (depth2 - m_FoamExistence.x) / (m_FoamExistence.y - m_FoamExistence.x)).rgb;
-        }
-
-        if(m_MaxAmplitude - m_FoamExistence.z > 0.0001){
-           foam += ((texture2D(m_FoamMap, texC) + texture2D(m_FoamMap, texCoord2)) * m_FoamIntensity  * m_FoamIntensity * 0.3 *
-               saturate((level - (m_WaterHeight + m_FoamExistence.z)) / (m_MaxAmplitude - m_FoamExistence.z))).rgb;
-        }
-        foam *= m_LightColor.rgb;
-    #endif
-
-    vec3 specular =vec3(0.0);
-    #ifdef ENABLE_SPECULAR
-        vec3 lightDir=normalize(m_LightDir);
-        vec3 mirrorEye = (2.0 * dot(eyeVecNorm, normal) * normal - eyeVecNorm);
-        float dotSpec = saturate(dot(mirrorEye.xyz, -lightDir) * 0.5 + 0.5);
-        specular = vec3((1.0 - fresnel) * saturate(-lightDir.y) * ((pow(dotSpec, 512.0)) * (m_Shininess * 1.8 + 0.2)));
-        specular += specular * 25.0 * saturate(m_Shininess - 0.05);
-        //foam does not shine
-        specular=specular * m_LightColor.rgb - (5.0 * foam);
-    #endif
-
-    color = mix(refraction, reflection, fresnel);
-    color = mix(refraction, color, saturate(depth * m_ShoreHardness));
-    color = saturate(color + max(specular, foam ));
-    color = mix(refraction, color, saturate(depth* m_FoamHardness));
-        
-
-    // XXX: HACK ALERT:
-    // We trick the GeForces to think they have
-    // to calculate the derivatives for all these pixels by using step()!
-    // That way we won't get pixels around the edges of the terrain,
-    // Where the derivatives are undefined
-    gl_FragColor = vec4(mix(color, color2, step(level, position.y)), 1.0);
-    
+#import "Common/ShaderLib/GLSLCompat.glsllib"
+#import "Common/ShaderLib/MultiSample.glsllib"
+#import "Common/ShaderLib/WaterUtil.glsllib"
+
+// Water pixel shader
+// Copyright (C) JMonkeyEngine 3.0
+// by Remy Bouquet (nehon) for JMonkeyEngine 3.0
+// original HLSL version by Wojciech Toman 2009
+
+uniform COLORTEXTURE m_Texture;
+uniform DEPTHTEXTURE m_DepthTexture;
+
+
+uniform sampler2D m_HeightMap;
+uniform sampler2D m_NormalMap;
+uniform sampler2D m_FoamMap;
+uniform sampler2D m_CausticsMap;
+uniform sampler2D m_ReflectionMap;
+
+uniform mat4 g_ViewProjectionMatrixInverse;
+uniform mat4 m_TextureProjMatrix;
+uniform vec3 m_CameraPosition;
+
+uniform float m_WaterHeight;
+uniform float m_Time;
+uniform float m_WaterTransparency;
+uniform float m_NormalScale;
+uniform float m_R0;
+uniform float m_MaxAmplitude;
+uniform vec3 m_LightDir;
+uniform vec4 m_LightColor;
+uniform float m_ShoreHardness;
+uniform float m_FoamHardness;
+uniform float m_RefractionStrength;
+uniform vec3 m_FoamExistence;
+uniform vec3 m_ColorExtinction;
+uniform float m_Shininess;
+uniform vec4 m_WaterColor;
+uniform vec4 m_DeepWaterColor;
+uniform vec2 m_WindDirection;
+uniform float m_SunScale;
+uniform float m_WaveScale;
+uniform float m_UnderWaterFogDistance;
+uniform float m_CausticsIntensity;
+
+#ifdef ENABLE_AREA
+uniform vec3 m_Center;
+uniform float m_Radius;
+#endif
+
+
+vec2 scale = vec2(m_WaveScale, m_WaveScale);
+float refractionScale = m_WaveScale;
+
+// Modifies 4 sampled normals. Increase first values to have more
+// smaller "waves" or last to have more bigger "waves"
+const vec4 normalModifier = vec4(3.0, 2.0, 4.0, 10.0);
+// Strength of displacement along normal.
+uniform float m_ReflectionDisplace;
+// Water transparency along eye vector.
+const float visibility = 3.0;
+// foam intensity
+uniform float m_FoamIntensity ;
+
+varying vec2 texCoord;
+
+mat3 MatrixInverse(in mat3 inMatrix){
+    float det = dot(cross(inMatrix[0], inMatrix[1]), inMatrix[2]);
+    mat3 T = transpose(inMatrix);
+    return mat3(cross(T[1], T[2]),
+        cross(T[2], T[0]),
+        cross(T[0], T[1])) / det;
+}
+
+
+mat3 computeTangentFrame(in vec3 N, in vec3 P, in vec2 UV) {
+    vec3 dp1 = dFdx(P);
+    vec3 dp2 = dFdy(P);
+    vec2 duv1 = dFdx(UV);
+    vec2 duv2 = dFdy(UV);
+
+    // solve the linear system
+    vec3 dp1xdp2 = cross(dp1, dp2);
+    mat2x3 inverseM = mat2x3(cross(dp2, dp1xdp2), cross(dp1xdp2, dp1));
+
+    vec3 T = inverseM * vec2(duv1.x, duv2.x);
+    vec3 B = inverseM * vec2(duv1.y, duv2.y);
+
+    // construct tangent frame
+    float maxLength = max(length(T), length(B));
+    T = T / maxLength;
+    B = B / maxLength;
+
+    return mat3(T, B, N);
+}
+
+float saturate(in float val){
+    return clamp(val,0.0,1.0);
+}
+
+vec3 saturate(in vec3 val){
+    return clamp(val,vec3(0.0),vec3(1.0));
+}
+
+vec3 getPosition(in float depth, in vec2 uv){
+    vec4 pos = vec4(uv, depth, 1.0) * 2.0 - 1.0;
+    pos = g_ViewProjectionMatrixInverse * pos;
+    return pos.xyz / pos.w;
+}
+
+// Function calculating fresnel term.
+// - normal - normalized normal vector
+// - eyeVec - normalized eye vector
+float fresnelTerm(in vec3 normal,in vec3 eyeVec){
+    float angle = 1.0 - max(0.0, dot(normal, eyeVec));
+    float fresnel = angle * angle;
+    fresnel = fresnel * fresnel;
+    fresnel = fresnel * angle;
+    return saturate(fresnel * (1.0 - saturate(m_R0)) + m_R0 - m_RefractionStrength);
+}
+
+vec2 m_FrustumNearFar=vec2(1.0,m_UnderWaterFogDistance);
+const float LOG2 = 1.442695;
+
+vec4 underWater(int sampleNum){
+
+
+    float sceneDepth = fetchTextureSample(m_DepthTexture, texCoord, sampleNum).r;
+    vec3 color2 = fetchTextureSample(m_Texture, texCoord, sampleNum).rgb;
+    
+    vec3 position = getPosition(sceneDepth, texCoord);  
+    float level = m_WaterHeight;
+
+    vec3 eyeVec = position - m_CameraPosition;    
+ 
+    // Find intersection with water surface
+    vec3 eyeVecNorm = normalize(eyeVec);
+    float t = (level - m_CameraPosition.y) / eyeVecNorm.y;
+    vec3 surfacePoint = m_CameraPosition + eyeVecNorm * t;
+
+    vec2 texC = vec2(0.0);
+
+    float cameraDepth = length(m_CameraPosition - surfacePoint);  
+    texC = (surfacePoint.xz + eyeVecNorm.xz) * scale + m_Time * 0.03 * m_WindDirection;
+    float bias = texture2D(m_HeightMap, texC).r;
+    level += bias * m_MaxAmplitude;
+    t = (level - m_CameraPosition.y) / eyeVecNorm.y;
+    surfacePoint = m_CameraPosition + eyeVecNorm * t; 
+    eyeVecNorm = normalize(m_CameraPosition - surfacePoint);
+
+    #if __VERSION__ >= 130
+        // Find normal of water surface
+        float normal1 = textureOffset(m_HeightMap, texC, ivec2(-1.0,  0.0)).r;
+        float normal2 = textureOffset(m_HeightMap, texC, ivec2( 1.0,  0.0)).r;
+        float normal3 = textureOffset(m_HeightMap, texC, ivec2( 0.0, -1.0)).r;
+        float normal4 = textureOffset(m_HeightMap, texC, ivec2( 0.0,  1.0)).r;
+    #else
+        // Find normal of water surface
+        float normal1 = texture2D(m_HeightMap, (texC + vec2(-1.0, 0.0) / 256.0)).r;
+        float normal2 = texture2D(m_HeightMap, (texC + vec2(1.0, 0.0) / 256.0)).r;
+        float normal3 = texture2D(m_HeightMap, (texC + vec2(0.0, -1.0) / 256.0)).r;
+        float normal4 = texture2D(m_HeightMap, (texC + vec2(0.0, 1.0) / 256.0)).r;
+    #endif
+
+    vec3 myNormal = normalize(vec3((normal1 - normal2) * m_MaxAmplitude,m_NormalScale,(normal3 - normal4) * m_MaxAmplitude));
+    vec3 normal = myNormal*-1.0;
+    float fresnel = fresnelTerm(normal, eyeVecNorm); 
+
+    vec3 refraction = color2;
+    #ifdef ENABLE_REFRACTION
+        texC = texCoord.xy *sin (fresnel+1.0);
+        texC = clamp(texC,0.0,1.0);
+        refraction = fetchTextureSample(m_Texture, texC, sampleNum).rgb;
+    #endif
+
+   float waterCol = saturate(length(m_LightColor.rgb) / m_SunScale);
+   refraction = mix(mix(refraction, m_DeepWaterColor.rgb * waterCol, m_WaterTransparency),  m_WaterColor.rgb* waterCol,m_WaterTransparency);
+
+    vec3 foam = vec3(0.0);
+    #ifdef ENABLE_FOAM    
+        texC = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * m_WindDirection + sin(m_Time * 0.001 + position.x) * 0.005;
+        vec2 texCoord2 = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.1 * m_WindDirection + sin(m_Time * 0.001 + position.z) * 0.005;
+
+        if(m_MaxAmplitude - m_FoamExistence.z> 0.0001){
+            foam += ((texture2D(m_FoamMap, texC) + texture2D(m_FoamMap, texCoord2)) * m_FoamIntensity  * m_FoamIntensity * 0.3 *
+               saturate((level - (m_WaterHeight + m_FoamExistence.z)) / (m_MaxAmplitude - m_FoamExistence.z))).rgb;
+        }
+        foam *= m_LightColor.rgb;    
+    #endif
+
+
+
+    vec3 specular = vec3(0.0);   
+    vec3 color ;
+    float fogFactor;
+
+    if(position.y>level){
+        #ifdef ENABLE_SPECULAR
+            if(step(0.9999,sceneDepth)==1.0){
+                vec3 lightDir=normalize(m_LightDir);
+                vec3 mirrorEye = (2.0 * dot(eyeVecNorm, normal) * normal - eyeVecNorm);
+                float dotSpec = saturate(dot(mirrorEye.xyz, -lightDir) * 0.5 + 0.5);
+                specular = vec3((1.0 - fresnel) * saturate(-lightDir.y) * ((pow(dotSpec, 512.0)) * (m_Shininess * 1.8 + 0.2)));
+                specular += specular * 25.0 * saturate(m_Shininess - 0.05);
+                specular=specular * m_LightColor.rgb * 100.0;
+            }
+        #endif
+        float fogIntensity= 8.0 * m_WaterTransparency;
+        fogFactor = exp2( -fogIntensity * fogIntensity * cameraDepth * 0.03 * LOG2 );
+        fogFactor = clamp(fogFactor, 0.0, 1.0);        
+        color =mix(m_DeepWaterColor.rgb,refraction,fogFactor);   
+        specular=specular*fogFactor;    
+        color = saturate(color + max(specular, foam ));
+    }else{
+        vec3 caustics = vec3(0.0);
+        #ifdef ENABLE_CAUSTICS 
+            vec2 windDirection=m_WindDirection;
+            texC = (position.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * windDirection + sin(m_Time  + position.x) * 0.01;
+            vec2 texCoord2 = (position.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * windDirection + sin(m_Time  + position.z) * 0.01;
+            caustics += (texture2D(m_CausticsMap, texC)+ texture2D(m_CausticsMap, texCoord2)).rgb;                  
+            caustics=saturate(mix(m_WaterColor.rgb,caustics,m_CausticsIntensity));            
+            color=mix(color2,caustics,m_CausticsIntensity);
+        #else
+            color=color2;
+        #endif
+                
+        float fogDepth= (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - sceneDepth* (m_FrustumNearFar.y-m_FrustumNearFar.x));
+        float fogIntensity= 18 * m_WaterTransparency;
+        fogFactor = exp2( -fogIntensity * fogIntensity * fogDepth *  fogDepth * LOG2 );
+        fogFactor = clamp(fogFactor, 0.0, 1.0);
+        color =mix(m_DeepWaterColor.rgb,color,fogFactor);
+    }
+
+    return vec4(color, 1.0);   
+}
+
+
+// NOTE: This will be called even for single-sampling
+vec4 main_multiSample(int sampleNum){
+    // If we are underwater let's call the underwater function
+    if(m_WaterHeight >= m_CameraPosition.y){
+        #ifdef ENABLE_AREA      
+            if(isOverExtent(m_CameraPosition, m_Center, m_Radius)){            
+                return fetchTextureSample(m_Texture, texCoord, sampleNum);
+            }   
+        #endif
+        return underWater(sampleNum);
+    }
+
+    float sceneDepth = fetchTextureSample(m_DepthTexture, texCoord, sampleNum).r;
+    vec3 color2 = fetchTextureSample(m_Texture, texCoord, sampleNum).rgb;
+
+    vec3 color = color2;
+    vec3 position = getPosition(sceneDepth, texCoord);
+
+    #ifdef ENABLE_AREA               
+        if(isOverExtent(position, m_Center, m_Radius)){         
+            return vec4(color2, 1.0);
+        }
+    #endif
+
+    float level = m_WaterHeight;
+    
+    float isAtFarPlane = step(0.99998, sceneDepth);
+    //#ifndef ENABLE_RIPPLES
+    // This optimization won't work on NVIDIA cards if ripples are enabled
+    if(position.y > level + m_MaxAmplitude + isAtFarPlane * 100.0){
+
+        return vec4(color2, 1.0);
+    }
+    //#endif
+
+    vec3 eyeVec = position - m_CameraPosition;    
+    float cameraDepth = m_CameraPosition.y - position.y;
+
+    // Find intersection with water surface
+    vec3 eyeVecNorm = normalize(eyeVec);
+    float t = (level - m_CameraPosition.y) / eyeVecNorm.y;
+    vec3 surfacePoint = m_CameraPosition + eyeVecNorm * t;
+
+    vec2 texC = vec2(0.0);
+    int samples = 1;
+    #ifdef ENABLE_HQ_SHORELINE
+        samples = 10;
+    #endif
+
+    float biasFactor = 1.0 / samples;
+    for (int i = 0; i < samples; i++){
+        texC = (surfacePoint.xz + eyeVecNorm.xz * biasFactor) * scale + m_Time * 0.03 * m_WindDirection;
+
+        float bias = texture2D(m_HeightMap, texC).r;
+
+        bias *= biasFactor;
+        level += bias * m_MaxAmplitude;
+        t = (level - m_CameraPosition.y) / eyeVecNorm.y;
+        surfacePoint = m_CameraPosition + eyeVecNorm * t;
+    }
+
+    float depth = length(position - surfacePoint);
+    float depth2 = surfacePoint.y - position.y;
+
+    // XXX: HACK ALERT: Increase water depth to infinity if at far plane
+    // Prevents "foam on horizon" issue
+    // For best results, replace the "100.0" below with the
+    // highest value in the m_ColorExtinction vec3
+    depth  += isAtFarPlane * 100.0;
+    depth2 += isAtFarPlane * 100.0;
+
+    eyeVecNorm = normalize(m_CameraPosition - surfacePoint);
+
+    #if __VERSION__ >= 130
+        // Find normal of water surface
+        float normal1 = textureOffset(m_HeightMap, texC, ivec2(-1.0,  0.0)).r;
+        float normal2 = textureOffset(m_HeightMap, texC, ivec2( 1.0,  0.0)).r;
+        float normal3 = textureOffset(m_HeightMap, texC, ivec2( 0.0, -1.0)).r;
+        float normal4 = textureOffset(m_HeightMap, texC, ivec2( 0.0,  1.0)).r;
+    #else
+        // Find normal of water surface
+        float normal1 = texture2D(m_HeightMap, (texC + vec2(-1.0, 0.0) / 256.0)).r;
+        float normal2 = texture2D(m_HeightMap, (texC + vec2(1.0, 0.0) / 256.0)).r;
+        float normal3 = texture2D(m_HeightMap, (texC + vec2(0.0, -1.0) / 256.0)).r;
+        float normal4 = texture2D(m_HeightMap, (texC + vec2(0.0, 1.0) / 256.0)).r;
+    #endif
+
+    vec3 myNormal = normalize(vec3((normal1 - normal2) * m_MaxAmplitude,m_NormalScale,(normal3 - normal4) * m_MaxAmplitude));
+    vec3 normal = vec3(0.0);
+
+    #ifdef ENABLE_RIPPLES
+        texC = surfacePoint.xz * 0.8 + m_WindDirection * m_Time* 1.6;
+        mat3 tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
+        vec3 normal0a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
+
+        texC = surfacePoint.xz * 0.4 + m_WindDirection * m_Time* 0.8;
+        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
+        vec3 normal1a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
+
+        texC = surfacePoint.xz * 0.2 + m_WindDirection * m_Time * 0.4;
+        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
+        vec3 normal2a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
+
+        texC = surfacePoint.xz * 0.1 + m_WindDirection * m_Time * 0.2;
+        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
+        vec3 normal3a = normalize(tangentFrame*(2.0 * texture2D(m_NormalMap, texC).xyz - 1.0));
+
+        normal = normalize(normal0a * normalModifier.x + normal1a * normalModifier.y +normal2a * normalModifier.z + normal3a * normalModifier.w);
+
+         #if __VERSION__ >= 130
+            // XXX: Here's another way to fix the terrain edge issue,
+            // But it requires GLSL 1.3 and still looks kinda incorrect
+            // around edges
+            normal = isnan(normal.x) ? myNormal : normal;
+        #else
+            // To make the shader 1.2 compatible we use a trick :
+            // we clamp the x value of the normal and compare it to it's former value instead of using isnan.
+            normal = clamp(normal.x,0.0,1.0)!=normal.x ? myNormal : normal;
+        #endif
+
+    #else
+        normal = myNormal;
+    #endif
+
+    vec3 refraction = color2;
+    #ifdef ENABLE_REFRACTION
+       // texC = texCoord.xy+ m_ReflectionDisplace * normal.x;
+        texC = texCoord.xy;
+        texC += sin(m_Time*1.8 + 3.0 * abs(position.y))* (refractionScale * min(depth2, 1.0));
+        texC = clamp(texC,vec2(0.0),vec2(0.999));
+        refraction = fetchTextureSample(m_Texture, texC, sampleNum).rgb;
+    #endif
+    vec3 waterPosition = surfacePoint.xyz;
+    waterPosition.y -= (level - m_WaterHeight);
+    vec4 texCoordProj = m_TextureProjMatrix * vec4(waterPosition, 1.0);
+
+    texCoordProj.x = texCoordProj.x + m_ReflectionDisplace * normal.x;
+    texCoordProj.z = texCoordProj.z + m_ReflectionDisplace * normal.z;
+    texCoordProj /= texCoordProj.w;
+    texCoordProj.y = 1.0 - texCoordProj.y;
+
+    vec3 reflection = texture2D(m_ReflectionMap, texCoordProj.xy).rgb;
+
+    float fresnel = fresnelTerm(normal, eyeVecNorm);
+
+    float depthN = depth * m_WaterTransparency;
+    float waterCol = saturate(length(m_LightColor.rgb) / m_SunScale);
+    refraction = mix(mix(refraction, m_WaterColor.rgb * waterCol, saturate(depthN / visibility)),
+        m_DeepWaterColor.rgb * waterCol, saturate(depth2 / m_ColorExtinction));
+
+
+    vec3 foam = vec3(0.0);
+    #ifdef ENABLE_FOAM
+        texC = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * m_WindDirection + sin(m_Time * 0.001 + position.x) * 0.005;
+        vec2 texCoord2 = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.1 * m_WindDirection + sin(m_Time * 0.001 + position.z) * 0.005;
+
+        vec4 foam1 = texture2D(m_FoamMap, texC);
+        vec4 foam2 = texture2D(m_FoamMap, texCoord2);
+
+        if(depth2 < m_FoamExistence.x){
+            foam = (foam1.r + foam2).rgb * vec3(m_FoamIntensity);
+        }else if(depth2 < m_FoamExistence.y){
+            foam = mix((foam1 + foam2) * m_FoamIntensity , vec4(0.0),
+                (depth2 - m_FoamExistence.x) / (m_FoamExistence.y - m_FoamExistence.x)).rgb;
+        }
+
+        
+        if(m_MaxAmplitude - m_FoamExistence.z> 0.0001){
+            foam += ((foam1 + foam2) * m_FoamIntensity  * m_FoamIntensity * 0.3 *
+               saturate((level - (m_WaterHeight + m_FoamExistence.z)) / (m_MaxAmplitude - m_FoamExistence.z))).rgb;
+        }
+        foam *= m_LightColor.rgb;
+    #endif
+
+    vec3 specular = vec3(0.0);
+    #ifdef ENABLE_SPECULAR
+        vec3 lightDir=normalize(m_LightDir);
+        vec3 mirrorEye = (2.0 * dot(eyeVecNorm, normal) * normal - eyeVecNorm);
+        float dotSpec = saturate(dot(mirrorEye.xyz, -lightDir) * 0.5 + 0.5);
+        specular = vec3((1.0 - fresnel) * saturate(-lightDir.y) * ((pow(dotSpec, 512.0)) * (m_Shininess * 1.8 + 0.2)));
+        specular += specular * 25.0 * saturate(m_Shininess - 0.05);
+        //foam does not shine
+        specular=specular * m_LightColor.rgb - (5.0 * foam);
+    #endif
+
+    color = mix(refraction, reflection, fresnel);
+    color = mix(refraction, color, saturate(depth * m_ShoreHardness));
+    color = saturate(color + max(specular, foam ));
+    color = mix(refraction, color, saturate(depth* m_FoamHardness));
+
+
+    // XXX: HACK ALERT:
+    // We trick the GeForces to think they have
+    // to calculate the derivatives for all these pixels by using step()!
+    // That way we won't get pixels around the edges of the terrain,
+    // Where the derivatives are undefined
+    return vec4(mix(color, color2, step(level, position.y)), 1.0);
+}
+
+void main(){
+    #ifdef RESOLVE_MS
+        vec4 color = vec4(0.0);
+        for (int i = 0; i < m_NumSamples; i++){
+            color += main_multiSample(i);
+        }
+        gl_FragColor = color / m_NumSamples;
+    #else
+        gl_FragColor = main_multiSample(0);
+    #endif
 }

+ 2 - 20
jme3-effects/src/main/resources/Common/MatDefs/Water/Water.j3md

@@ -51,8 +51,8 @@ MaterialDef Advanced Water {
     }
 
     Technique {
-        VertexShader   GLSL150 : Common/MatDefs/Post/Post15.vert
-        FragmentShader GLSL150 : Common/MatDefs/Water/Water15.frag
+        VertexShader   GLSL150 GLSL120 : Common/MatDefs/Post/Post.vert
+        FragmentShader GLSL150 GLSL120: Common/MatDefs/Water/Water.frag
 
         WorldParameters {
             ViewProjectionMatrixInverse
@@ -72,22 +72,4 @@ MaterialDef Advanced Water {
         }
     }
 
-    Technique {
-        VertexShader   GLSL100 : Common/MatDefs/Post/Post.vert
-        FragmentShader GLSL120 : Common/MatDefs/Water/Water.frag
-
-        WorldParameters {
-            ViewProjectionMatrixInverse
-        }
-        Defines {
-            ENABLE_RIPPLES : UseRipples
-            ENABLE_HQ_SHORELINE : UseHQShoreline
-            ENABLE_SPECULAR : UseSpecular
-            ENABLE_FOAM : UseFoam
-            ENABLE_CAUSTICS : UseCaustics
-            ENABLE_REFRACTION : UseRefraction
-            ENABLE_AREA : Center
-            SQUARE_AREA : SquareArea
-        }
-    }
 }

+ 0 - 439
jme3-effects/src/main/resources/Common/MatDefs/Water/Water15.frag

@@ -1,439 +0,0 @@
-#import "Common/ShaderLib/MultiSample.glsllib"
-#import "Common/ShaderLib/WaterUtil.glsllib"
-
-// Water pixel shader
-// Copyright (C) JMonkeyEngine 3.0
-// by Remy Bouquet (nehon) for JMonkeyEngine 3.0
-// original HLSL version by Wojciech Toman 2009
-
-uniform COLORTEXTURE m_Texture;
-uniform DEPTHTEXTURE m_DepthTexture;
-
-
-uniform sampler2D m_HeightMap;
-uniform sampler2D m_NormalMap;
-uniform sampler2D m_FoamMap;
-uniform sampler2D m_CausticsMap;
-uniform sampler2D m_ReflectionMap;
-
-uniform mat4 g_ViewProjectionMatrixInverse;
-uniform mat4 m_TextureProjMatrix;
-uniform vec3 m_CameraPosition;
-
-uniform float m_WaterHeight;
-uniform float m_Time;
-uniform float m_WaterTransparency;
-uniform float m_NormalScale;
-uniform float m_R0;
-uniform float m_MaxAmplitude;
-uniform vec3 m_LightDir;
-uniform vec4 m_LightColor;
-uniform float m_ShoreHardness;
-uniform float m_FoamHardness;
-uniform float m_RefractionStrength;
-uniform vec3 m_FoamExistence;
-uniform vec3 m_ColorExtinction;
-uniform float m_Shininess;
-uniform vec4 m_WaterColor;
-uniform vec4 m_DeepWaterColor;
-uniform vec2 m_WindDirection;
-uniform float m_SunScale;
-uniform float m_WaveScale;
-uniform float m_UnderWaterFogDistance;
-uniform float m_CausticsIntensity;
-
-#ifdef ENABLE_AREA
-uniform vec3 m_Center;
-uniform float m_Radius;
-#endif
-
-
-vec2 scale = vec2(m_WaveScale, m_WaveScale);
-float refractionScale = m_WaveScale;
-
-// Modifies 4 sampled normals. Increase first values to have more
-// smaller "waves" or last to have more bigger "waves"
-const vec4 normalModifier = vec4(3.0, 2.0, 4.0, 10.0);
-// Strength of displacement along normal.
-uniform float m_ReflectionDisplace;
-// Water transparency along eye vector.
-const float visibility = 3.0;
-// foam intensity
-uniform float m_FoamIntensity ;
-
-in vec2 texCoord;
-out vec4 outFragColor;
-
-mat3 MatrixInverse(in mat3 inMatrix){
-    float det = dot(cross(inMatrix[0], inMatrix[1]), inMatrix[2]);
-    mat3 T = transpose(inMatrix);
-    return mat3(cross(T[1], T[2]),
-        cross(T[2], T[0]),
-        cross(T[0], T[1])) / det;
-}
-
-
-mat3 computeTangentFrame(in vec3 N, in vec3 P, in vec2 UV) {
-    vec3 dp1 = dFdx(P);
-    vec3 dp2 = dFdy(P);
-    vec2 duv1 = dFdx(UV);
-    vec2 duv2 = dFdy(UV);
-
-    // solve the linear system
-    vec3 dp1xdp2 = cross(dp1, dp2);
-    mat2x3 inverseM = mat2x3(cross(dp2, dp1xdp2), cross(dp1xdp2, dp1));
-
-    vec3 T = inverseM * vec2(duv1.x, duv2.x);
-    vec3 B = inverseM * vec2(duv1.y, duv2.y);
-
-    // construct tangent frame
-    float maxLength = max(length(T), length(B));
-    T = T / maxLength;
-    B = B / maxLength;
-
-    return mat3(T, B, N);
-}
-
-float saturate(in float val){
-    return clamp(val,0.0,1.0);
-}
-
-vec3 saturate(in vec3 val){
-    return clamp(val,vec3(0.0),vec3(1.0));
-}
-
-vec3 getPosition(in float depth, in vec2 uv){
-    vec4 pos = vec4(uv, depth, 1.0) * 2.0 - 1.0;
-    pos = g_ViewProjectionMatrixInverse * pos;
-    return pos.xyz / pos.w;
-}
-
-// Function calculating fresnel term.
-// - normal - normalized normal vector
-// - eyeVec - normalized eye vector
-float fresnelTerm(in vec3 normal,in vec3 eyeVec){
-    float angle = 1.0 - max(0.0, dot(normal, eyeVec));
-    float fresnel = angle * angle;
-    fresnel = fresnel * fresnel;
-    fresnel = fresnel * angle;
-    return saturate(fresnel * (1.0 - saturate(m_R0)) + m_R0 - m_RefractionStrength);
-}
-
-vec2 m_FrustumNearFar=vec2(1.0,m_UnderWaterFogDistance);
-const float LOG2 = 1.442695;
-
-vec4 underWater(int sampleNum){
-
-
-    float sceneDepth = fetchTextureSample(m_DepthTexture, texCoord, sampleNum).r;
-    vec3 color2 = fetchTextureSample(m_Texture, texCoord, sampleNum).rgb;
-    
-    vec3 position = getPosition(sceneDepth, texCoord);  
-    float level = m_WaterHeight;
-
-    vec3 eyeVec = position - m_CameraPosition;    
- 
-    // Find intersection with water surface
-    vec3 eyeVecNorm = normalize(eyeVec);
-    float t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-    vec3 surfacePoint = m_CameraPosition + eyeVecNorm * t;
-
-    vec2 texC = vec2(0.0);
-
-    float cameraDepth = length(m_CameraPosition - surfacePoint);  
-    texC = (surfacePoint.xz + eyeVecNorm.xz) * scale + m_Time * 0.03 * m_WindDirection;
-    float bias = texture(m_HeightMap, texC).r;
-    level += bias * m_MaxAmplitude;
-    t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-    surfacePoint = m_CameraPosition + eyeVecNorm * t; 
-    eyeVecNorm = normalize(m_CameraPosition - surfacePoint);
-
-    // Find normal of water surface
-    float normal1 = textureOffset(m_HeightMap, texC, ivec2(-1.0,  0.0)).r;
-    float normal2 = textureOffset(m_HeightMap, texC, ivec2( 1.0,  0.0)).r;
-    float normal3 = textureOffset(m_HeightMap, texC, ivec2( 0.0, -1.0)).r;
-    float normal4 = textureOffset(m_HeightMap, texC, ivec2( 0.0,  1.0)).r;
-
-    vec3 myNormal = normalize(vec3((normal1 - normal2) * m_MaxAmplitude,m_NormalScale,(normal3 - normal4) * m_MaxAmplitude));
-    vec3 normal = myNormal*-1.0;
-    float fresnel = fresnelTerm(normal, eyeVecNorm); 
-
-    vec3 refraction = color2;
-    #ifdef ENABLE_REFRACTION
-        texC = texCoord.xy *sin (fresnel+1.0);
-        texC = clamp(texC,0.0,1.0);
-        #ifdef RESOLVE_MS
-            ivec2 iTexC = ivec2(texC * textureSize(m_Texture));
-            refraction = texelFetch(m_Texture, iTexC, sampleNum).rgb;
-        #else
-            ivec2 iTexC = ivec2(texC * textureSize(m_Texture, 0));
-            refraction = texelFetch(m_Texture, iTexC, 0).rgb;
-        #endif
-    #endif 
-
-   float waterCol = saturate(length(m_LightColor.rgb) / m_SunScale);
-   refraction = mix(mix(refraction, m_DeepWaterColor.rgb * waterCol, m_WaterTransparency),  m_WaterColor.rgb* waterCol,m_WaterTransparency);
-
-    vec3 foam = vec3(0.0);
-    #ifdef ENABLE_FOAM    
-        texC = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * m_WindDirection + sin(m_Time * 0.001 + position.x) * 0.005;
-        vec2 texCoord2 = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.1 * m_WindDirection + sin(m_Time * 0.001 + position.z) * 0.005;
-
-        if(m_MaxAmplitude - m_FoamExistence.z> 0.0001){
-            foam += ((texture2D(m_FoamMap, texC) + texture2D(m_FoamMap, texCoord2)) * m_FoamIntensity  * m_FoamIntensity * 0.3 *
-               saturate((level - (m_WaterHeight + m_FoamExistence.z)) / (m_MaxAmplitude - m_FoamExistence.z))).rgb;
-        }
-        foam *= m_LightColor.rgb;    
-    #endif
-
-
-
-    vec3 specular = vec3(0.0);   
-    vec3 color ;
-    float fogFactor;
-
-    if(position.y>level){
-        #ifdef ENABLE_SPECULAR
-            if(step(0.9999,sceneDepth)==1.0){
-                vec3 lightDir=normalize(m_LightDir);
-                vec3 mirrorEye = (2.0 * dot(eyeVecNorm, normal) * normal - eyeVecNorm);
-                float dotSpec = saturate(dot(mirrorEye.xyz, -lightDir) * 0.5 + 0.5);
-                specular = vec3((1.0 - fresnel) * saturate(-lightDir.y) * ((pow(dotSpec, 512.0)) * (m_Shininess * 1.8 + 0.2)));
-                specular += specular * 25.0 * saturate(m_Shininess - 0.05);
-                specular=specular * m_LightColor.rgb * 100.0;
-            }
-        #endif
-        float fogIntensity= 8.0 * m_WaterTransparency;
-        fogFactor = exp2( -fogIntensity * fogIntensity * cameraDepth * 0.03 * LOG2 );
-        fogFactor = clamp(fogFactor, 0.0, 1.0);        
-        color =mix(m_DeepWaterColor.rgb,refraction,fogFactor);   
-        specular=specular*fogFactor;    
-        color = saturate(color + max(specular, foam ));
-    }else{
-        vec3 caustics = vec3(0.0);
-        #ifdef ENABLE_CAUSTICS 
-            vec2 windDirection=m_WindDirection;
-            texC = (position.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * windDirection + sin(m_Time  + position.x) * 0.01;
-            vec2 texCoord2 = (position.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * windDirection + sin(m_Time  + position.z) * 0.01;
-            caustics += (texture2D(m_CausticsMap, texC)+ texture2D(m_CausticsMap, texCoord2)).rgb;                  
-            caustics=saturate(mix(m_WaterColor.rgb,caustics,m_CausticsIntensity));            
-            color=mix(color2,caustics,m_CausticsIntensity);
-        #else
-            color=color2;
-        #endif
-                
-        float fogDepth= (2.0 * m_FrustumNearFar.x) / (m_FrustumNearFar.y + m_FrustumNearFar.x - sceneDepth* (m_FrustumNearFar.y-m_FrustumNearFar.x));
-        float fogIntensity= 18 * m_WaterTransparency;
-        fogFactor = exp2( -fogIntensity * fogIntensity * fogDepth *  fogDepth * LOG2 );
-        fogFactor = clamp(fogFactor, 0.0, 1.0);
-        color =mix(m_DeepWaterColor.rgb,color,fogFactor);
-    }
-
-    return vec4(color, 1.0);   
-}
-
-
-// NOTE: This will be called even for single-sampling
-vec4 main_multiSample(int sampleNum){
-    // If we are underwater let's call the underwater function
-    if(m_WaterHeight >= m_CameraPosition.y){
-        #ifdef ENABLE_AREA      
-            if(isOverExtent(m_CameraPosition, m_Center, m_Radius)){            
-                return fetchTextureSample(m_Texture, texCoord, sampleNum);
-            }   
-        #endif
-        return underWater(sampleNum);
-    }
-
-    float sceneDepth = fetchTextureSample(m_DepthTexture, texCoord, sampleNum).r;
-    vec3 color2 = fetchTextureSample(m_Texture, texCoord, sampleNum).rgb;
-
-    vec3 color = color2;
-    vec3 position = getPosition(sceneDepth, texCoord);
-
-    #ifdef ENABLE_AREA               
-        if(isOverExtent(position, m_Center, m_Radius)){         
-            return vec4(color2, 1.0);
-        }
-    #endif
-
-    float level = m_WaterHeight;
-    
-    float isAtFarPlane = step(0.99998, sceneDepth);
-    //#ifndef ENABLE_RIPPLES
-    // This optimization won't work on NVIDIA cards if ripples are enabled
-    if(position.y > level + m_MaxAmplitude + isAtFarPlane * 100.0){
-
-        return vec4(color2, 1.0);
-    }
-    //#endif
-
-    vec3 eyeVec = position - m_CameraPosition;    
-    float cameraDepth = m_CameraPosition.y - position.y;
-
-    // Find intersection with water surface
-    vec3 eyeVecNorm = normalize(eyeVec);
-    float t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-    vec3 surfacePoint = m_CameraPosition + eyeVecNorm * t;
-
-    vec2 texC = vec2(0.0);
-    int samples = 1;
-    #ifdef ENABLE_HQ_SHORELINE
-        samples = 10;
-    #endif
-
-    float biasFactor = 1.0 / samples;
-    for (int i = 0; i < samples; i++){
-        texC = (surfacePoint.xz + eyeVecNorm.xz * biasFactor) * scale + m_Time * 0.03 * m_WindDirection;
-
-        float bias = texture(m_HeightMap, texC).r;
-
-        bias *= biasFactor;
-        level += bias * m_MaxAmplitude;
-        t = (level - m_CameraPosition.y) / eyeVecNorm.y;
-        surfacePoint = m_CameraPosition + eyeVecNorm * t;
-    }
-
-    float depth = length(position - surfacePoint);
-    float depth2 = surfacePoint.y - position.y;
-
-    // XXX: HACK ALERT: Increase water depth to infinity if at far plane
-    // Prevents "foam on horizon" issue
-    // For best results, replace the "100.0" below with the
-    // highest value in the m_ColorExtinction vec3
-    depth  += isAtFarPlane * 100.0;
-    depth2 += isAtFarPlane * 100.0;
-
-    eyeVecNorm = normalize(m_CameraPosition - surfacePoint);
-
-    // Find normal of water surface
-    float normal1 = textureOffset(m_HeightMap, texC, ivec2(-1.0,  0.0)).r;
-    float normal2 = textureOffset(m_HeightMap, texC, ivec2( 1.0,  0.0)).r;
-    float normal3 = textureOffset(m_HeightMap, texC, ivec2( 0.0, -1.0)).r;
-    float normal4 = textureOffset(m_HeightMap, texC, ivec2( 0.0,  1.0)).r;
-
-    vec3 myNormal = normalize(vec3((normal1 - normal2) * m_MaxAmplitude,m_NormalScale,(normal3 - normal4) * m_MaxAmplitude));
-    vec3 normal = vec3(0.0);
-
-    #ifdef ENABLE_RIPPLES
-        texC = surfacePoint.xz * 0.8 + m_WindDirection * m_Time* 1.6;
-        mat3 tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal0a = normalize(tangentFrame*(2.0 * texture(m_NormalMap, texC).xyz - 1.0));
-
-        texC = surfacePoint.xz * 0.4 + m_WindDirection * m_Time* 0.8;
-        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal1a = normalize(tangentFrame*(2.0 * texture(m_NormalMap, texC).xyz - 1.0));
-
-        texC = surfacePoint.xz * 0.2 + m_WindDirection * m_Time * 0.4;
-        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal2a = normalize(tangentFrame*(2.0 * texture(m_NormalMap, texC).xyz - 1.0));
-
-        texC = surfacePoint.xz * 0.1 + m_WindDirection * m_Time * 0.2;
-        tangentFrame = computeTangentFrame(myNormal, eyeVecNorm, texC);
-        vec3 normal3a = normalize(tangentFrame*(2.0 * texture(m_NormalMap, texC).xyz - 1.0));
-
-        normal = normalize(normal0a * normalModifier.x + normal1a * normalModifier.y +normal2a * normalModifier.z + normal3a * normalModifier.w);
-        // XXX: Here's another way to fix the terrain edge issue,
-        // But it requires GLSL 1.3 and still looks kinda incorrect
-        // around edges
-        normal = isnan(normal.x) ? myNormal : normal;
-        //if (position.y > level){
-        //    gl_FragColor = vec4(color2 + normal*0.0001, 1.0);
-        //    return;
-        //}
-    #else
-        normal = myNormal;
-    #endif
-
-    vec3 refraction = color2;
-    #ifdef ENABLE_REFRACTION
-       // texC = texCoord.xy+ m_ReflectionDisplace * normal.x;
-        texC = texCoord.xy;
-        texC += sin(m_Time*1.8 + 3.0 * abs(position.y))* (refractionScale * min(depth2, 1.0));
-        texC = clamp(texC,vec2(0.0),vec2(0.999));
-        #ifdef RESOLVE_MS
-            ivec2 iTexC = ivec2(texC * textureSize(m_Texture));
-            refraction = texelFetch(m_Texture, iTexC, sampleNum).rgb;
-        #else
-            ivec2 iTexC = ivec2(texC * textureSize(m_Texture, 0));
-            refraction = texelFetch(m_Texture, iTexC, 0).rgb;
-        #endif
-    #endif
-
-    vec3 waterPosition = surfacePoint.xyz;
-    waterPosition.y -= (level - m_WaterHeight);
-    vec4 texCoordProj = m_TextureProjMatrix * vec4(waterPosition, 1.0);
-
-    texCoordProj.x = texCoordProj.x + m_ReflectionDisplace * normal.x;
-    texCoordProj.z = texCoordProj.z + m_ReflectionDisplace * normal.z;
-    texCoordProj /= texCoordProj.w;
-    texCoordProj.y = 1.0 - texCoordProj.y;
-
-    vec3 reflection = texture(m_ReflectionMap, texCoordProj.xy).rgb;
-
-    float fresnel = fresnelTerm(normal, eyeVecNorm);
-
-    float depthN = depth * m_WaterTransparency;
-    float waterCol = saturate(length(m_LightColor.rgb) / m_SunScale);
-    refraction = mix(mix(refraction, m_WaterColor.rgb * waterCol, saturate(depthN / visibility)),
-        m_DeepWaterColor.rgb * waterCol, saturate(depth2 / m_ColorExtinction));
-
-
-
-
-    vec3 foam = vec3(0.0);
-    #ifdef ENABLE_FOAM
-        texC = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.05 * m_WindDirection + sin(m_Time * 0.001 + position.x) * 0.005;
-        vec2 texCoord2 = (surfacePoint.xz + eyeVecNorm.xz * 0.1) * 0.05 + m_Time * 0.1 * m_WindDirection + sin(m_Time * 0.001 + position.z) * 0.005;
-
-        if(depth2 < m_FoamExistence.x){
-            foam = (texture2D(m_FoamMap, texC).r + texture2D(m_FoamMap, texCoord2)).rgb * vec3(m_FoamIntensity);
-        }else if(depth2 < m_FoamExistence.y){
-            foam = mix((texture2D(m_FoamMap, texC) + texture2D(m_FoamMap, texCoord2)) * m_FoamIntensity , vec4(0.0),
-                (depth2 - m_FoamExistence.x) / (m_FoamExistence.y - m_FoamExistence.x)).rgb;
-        }
-
-        
-        if(m_MaxAmplitude - m_FoamExistence.z> 0.0001){
-            foam += ((texture2D(m_FoamMap, texC) + texture2D(m_FoamMap, texCoord2)) * m_FoamIntensity  * m_FoamIntensity * 0.3 *
-               saturate((level - (m_WaterHeight + m_FoamExistence.z)) / (m_MaxAmplitude - m_FoamExistence.z))).rgb;
-        }
-        foam *= m_LightColor.rgb;
-    #endif
-
-    vec3 specular = vec3(0.0);
-    #ifdef ENABLE_SPECULAR
-        vec3 lightDir=normalize(m_LightDir);
-        vec3 mirrorEye = (2.0 * dot(eyeVecNorm, normal) * normal - eyeVecNorm);
-        float dotSpec = saturate(dot(mirrorEye.xyz, -lightDir) * 0.5 + 0.5);
-        specular = vec3((1.0 - fresnel) * saturate(-lightDir.y) * ((pow(dotSpec, 512.0)) * (m_Shininess * 1.8 + 0.2)));
-        specular += specular * 25.0 * saturate(m_Shininess - 0.05);
-        //foam does not shine
-        specular=specular * m_LightColor.rgb - (5.0 * foam);
-    #endif
-
-    color = mix(refraction, reflection, fresnel);
-    color = mix(refraction, color, saturate(depth * m_ShoreHardness));
-    color = saturate(color + max(specular, foam ));
-    color = mix(refraction, color, saturate(depth* m_FoamHardness));
-
-
-    // XXX: HACK ALERT:
-    // We trick the GeForces to think they have
-    // to calculate the derivatives for all these pixels by using step()!
-    // That way we won't get pixels around the edges of the terrain,
-    // Where the derivatives are undefined
-    return vec4(mix(color, color2, step(level, position.y)), 1.0);
-}
-
-void main(){
-    #ifdef RESOLVE_MS
-        vec4 color = vec4(0.0);
-        for (int i = 0; i < m_NumSamples; i++){
-            color += main_multiSample(i);
-        }
-        outFragColor = color / m_NumSamples;
-    #else
-        outFragColor = main_multiSample(0);
-    #endif
-}

+ 9 - 7
jme3-examples/src/main/java/jme3test/post/LightScatteringUI.java

@@ -54,9 +54,7 @@ public class LightScatteringUI {
         System.out.println("-- blur start : press U to increase, J to decrease");
         System.out.println("-- blur width : press I to increase, K to decrease");
         System.out.println("-- Light density : press O to increase, P to decrease");
-//        System.out.println("-- Toggle AO on/off : press space bar");
-//        System.out.println("-- Use only AO : press Num pad 0");
-//        System.out.println("-- Output config declaration : press P");
+        System.out.println("-- Toggle LS on/off : press space bar");
         System.out.println("-------------------------------------------------------");
     
         inputManager.addMapping("sampleUp", new KeyTrigger(KeyInput.KEY_Y));
@@ -68,9 +66,9 @@ public class LightScatteringUI {
         inputManager.addMapping("lightDensityUp", new KeyTrigger(KeyInput.KEY_O));
         inputManager.addMapping("lightDensityDown", new KeyTrigger(KeyInput.KEY_L));
         inputManager.addMapping("outputConfig", new KeyTrigger(KeyInput.KEY_P));
-//        inputManager.addMapping("toggleUseAO", new KeyTrigger(KeyInput.KEY_SPACE));
-//        inputManager.addMapping("toggleUseOnlyAo", new KeyTrigger(KeyInput.KEY_NUMPAD0));
-        
+        inputManager.addMapping("toggle", new KeyTrigger(KeyInput.KEY_SPACE));
+
+
         ActionListener acl = new ActionListener() {
 
             public void onAction(String name, boolean keyPressed, float tpf) {
@@ -83,6 +81,9 @@ public class LightScatteringUI {
                    filter.setNbSamples(filter.getNbSamples()-1);
                    System.out.println("Nb Samples : "+filter.getNbSamples());
                 }
+                if (name.equals("toggle") && keyPressed) {
+                    filter.setEnabled(!filter.isEnabled());
+                }
                 if (name.equals("outputConfig") && keyPressed) {
                    System.out.println("lightScatteringFilter.setNbSamples("+filter.getNbSamples()+");");
                    System.out.println("lightScatteringFilter.setBlurStart("+filter.getBlurStart()+"f);");
@@ -123,9 +124,10 @@ public class LightScatteringUI {
                     System.out.println("light Density : "+filter.getLightDensity());
                 }
 
+
             }
         };
-        inputManager.addListener(acl,"sampleUp","sampleDown","outputConfig");
+        inputManager.addListener(acl, "sampleUp", "sampleDown", "outputConfig", "toggle");
 
         inputManager.addListener(anl, "blurStartUp","blurStartDown","blurWidthUp", "blurWidthDown","lightDensityUp", "lightDensityDown");
      

+ 64 - 22
jme3-examples/src/main/java/jme3test/post/TestFog.java

@@ -40,15 +40,24 @@ import com.jme3.input.controls.ActionListener;
 import com.jme3.input.controls.AnalogListener;
 import com.jme3.input.controls.KeyTrigger;
 import com.jme3.light.DirectionalLight;
+import com.jme3.material.Material;
 import com.jme3.math.ColorRGBA;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector3f;
 import com.jme3.post.FilterPostProcessor;
 import com.jme3.post.filters.FogFilter;
+import com.jme3.renderer.Camera;
+import com.jme3.renderer.queue.RenderQueue;
 import com.jme3.scene.Node;
 import com.jme3.scene.Spatial;
+import com.jme3.terrain.geomipmap.TerrainQuad;
+import com.jme3.terrain.heightmap.AbstractHeightMap;
+import com.jme3.terrain.heightmap.ImageBasedHeightMap;
+import com.jme3.texture.Texture;
 import com.jme3.util.SkyFactory;
 import java.io.File;
+import java.util.ArrayList;
+import java.util.List;
 
 public class TestFog extends SimpleApplication {
 
@@ -56,44 +65,29 @@ public class TestFog extends SimpleApplication {
     private boolean enabled=true;
     private FogFilter fog;
 
-    // set default for applets
-    private static boolean useHttp = true;
-
     public static void main(String[] args) {
-        File file = new File("wildhouse.zip");
-        if (file.exists()) {
-            useHttp = false;
-        }
         TestFog app = new TestFog();
         app.start();
     }
 
     public void simpleInitApp() {
-        this.flyCam.setMoveSpeed(10);
+        this.flyCam.setMoveSpeed(50);
         Node mainScene=new Node();
-        cam.setLocation(new Vector3f(-27.0f, 1.0f, 75.0f));
-        cam.setRotation(new Quaternion(0.03f, 0.9f, 0f, 0.4f));
+        cam.setLocation(new Vector3f(-34.74095f, 95.21318f, -287.4945f));
+        cam.setRotation(new Quaternion(0.023536969f, 0.9361278f, -0.016098259f, -0.35050195f));
 
         // load sky
         mainScene.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", false));
+        createTerrain(mainScene);
+
 
-        // create the geometry and attach it
-        // load the level from zip or http zip
-        if (useHttp) {
-            assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip", HttpZipLocator.class);
-        } else {
-            assetManager.registerLocator("wildhouse.zip", ZipLocator.class);
-        }
-        Spatial scene = assetManager.loadModel("main.scene");
 
         DirectionalLight sun = new DirectionalLight();
         Vector3f lightDir=new Vector3f(-0.37352666f, -0.50444174f, -0.7784704f);
         sun.setDirection(lightDir);
         sun.setColor(ColorRGBA.White.clone().multLocal(2));
-        scene.addLight(sun);
+        mainScene.addLight(sun);
 
-
-        mainScene.attachChild(scene);
         rootNode.attachChild(mainScene);
 
         fpp=new FilterPostProcessor(assetManager);
@@ -105,7 +99,7 @@ public class TestFog extends SimpleApplication {
         fog=new FogFilter();
         fog.setFogColor(new ColorRGBA(0.9f, 0.9f, 0.9f, 1.0f));
         fog.setFogDistance(155);
-        fog.setFogDensity(2.0f);
+        fog.setFogDensity(1.0f);
         fpp.addFilter(fog);
         viewPort.addProcessor(fpp);
         initInputs();
@@ -161,6 +155,54 @@ public class TestFog extends SimpleApplication {
         inputManager.addListener(acl, "toggle");
         inputManager.addListener(anl, "DensityUp","DensityDown","DistanceUp","DistanceDown");
 
+     }
+
+    private void createTerrain(Node rootNode) {
+        Material matRock = new Material(assetManager, "Common/MatDefs/Terrain/TerrainLighting.j3md");
+        matRock.setBoolean("useTriPlanarMapping", false);
+        matRock.setBoolean("WardIso", true);
+        matRock.setTexture("AlphaMap", assetManager.loadTexture("Textures/Terrain/splat/alphamap.png"));
+        Texture heightMapImage = assetManager.loadTexture("Textures/Terrain/splat/mountains512.png");
+        Texture grass = assetManager.loadTexture("Textures/Terrain/splat/grass.jpg");
+        grass.setWrap(Texture.WrapMode.Repeat);
+        matRock.setTexture("DiffuseMap", grass);
+        matRock.setFloat("DiffuseMap_0_scale", 64);
+        Texture dirt = assetManager.loadTexture("Textures/Terrain/splat/dirt.jpg");
+        dirt.setWrap(Texture.WrapMode.Repeat);
+        matRock.setTexture("DiffuseMap_1", dirt);
+        matRock.setFloat("DiffuseMap_1_scale", 16);
+        Texture rock = assetManager.loadTexture("Textures/Terrain/splat/road.jpg");
+        rock.setWrap(Texture.WrapMode.Repeat);
+        matRock.setTexture("DiffuseMap_2", rock);
+        matRock.setFloat("DiffuseMap_2_scale", 128);
+        Texture normalMap0 = assetManager.loadTexture("Textures/Terrain/splat/grass_normal.jpg");
+        normalMap0.setWrap(Texture.WrapMode.Repeat);
+        Texture normalMap1 = assetManager.loadTexture("Textures/Terrain/splat/dirt_normal.png");
+        normalMap1.setWrap(Texture.WrapMode.Repeat);
+        Texture normalMap2 = assetManager.loadTexture("Textures/Terrain/splat/road_normal.png");
+        normalMap2.setWrap(Texture.WrapMode.Repeat);
+        matRock.setTexture("NormalMap", normalMap0);
+        matRock.setTexture("NormalMap_1", normalMap2);
+        matRock.setTexture("NormalMap_2", normalMap2);
+
+        AbstractHeightMap heightmap = null;
+        try {
+            heightmap = new ImageBasedHeightMap(heightMapImage.getImage(), 0.25f);
+            heightmap.load();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        TerrainQuad terrain = new TerrainQuad("terrain", 65, 513, heightmap.getHeightMap());
+        List<Camera> cameras = new ArrayList<Camera>();
+        cameras.add(getCamera());
+        terrain.setMaterial(matRock);
+        terrain.setLocalScale(new Vector3f(5, 5, 5));
+        terrain.setLocalTranslation(new Vector3f(0, -30, 0));
+        terrain.setLocked(false); // unlock it so we can edit the height
+
+        terrain.setShadowMode(RenderQueue.ShadowMode.Receive);
+        rootNode.attachChild(terrain);
+
     }
 }
 

+ 4 - 20
jme3-examples/src/main/java/jme3test/post/TestLightScattering.java

@@ -84,32 +84,16 @@ public class TestLightScattering extends SimpleApplication {
         sun.setColor(ColorRGBA.White.clone().multLocal(2));
         scene.addLight(sun);
 
-        PssmShadowRenderer pssmRenderer = new PssmShadowRenderer(assetManager,1024,4);
-        pssmRenderer.setDirection(lightDir);
-        pssmRenderer.setShadowIntensity(0.55f);
-     //   viewPort.addProcessor(pssmRenderer);
 
         FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
-//        SSAOFilter ssaoFilter= new SSAOFilter(viewPort, new SSAOConfig(0.36f,1.8f,0.84f,0.16f,false,true));
-//        fpp.addFilter(ssaoFilter);
-
-
-//           Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
-//        mat2.setTexture("ColorMap", assetManager.loadTexture("Interface/Logo/Monkey.jpg"));
-//
-//        Sphere lite=new Sphere(8, 8, 10.0f);
-//        Geometry lightSphere=new Geometry("lightsphere", lite);
-//        lightSphere.setMaterial(mat2);
+        int numSamples = getContext().getSettings().getSamples();
+        if (numSamples > 0) {
+            fpp.setNumSamples(numSamples);
+        }
         Vector3f lightPos = lightDir.multLocal(-3000);
-//        lightSphere.setLocalTranslation(lightPos);
-        // rootNode.attachChild(lightSphere);
         LightScatteringFilter filter = new LightScatteringFilter(lightPos);
         LightScatteringUI ui = new LightScatteringUI(inputManager, filter);
         fpp.addFilter(filter);
-//fpp.setNumSamples(4);
-        //fpp.addFilter(new RadialBlurFilter(0.3f,15.0f));
-        //    SSAOUI ui=new SSAOUI(inputManager, ssaoFilter.getConfig());
-
         viewPort.addProcessor(fpp);
     }
 

+ 3 - 3
jme3-examples/src/main/java/jme3test/post/TestPostFilters.java

@@ -60,9 +60,9 @@ public class TestPostFilters extends SimpleApplication implements ActionListener
 
     public static void main(String[] args) {
         TestPostFilters app = new TestPostFilters();
-        AppSettings settings = new AppSettings(true);
-        settings.setRenderer(AppSettings.LWJGL_OPENGL2);
-        app.setSettings(settings);
+//        AppSettings settings = new AppSettings(true);
+//        settings.setRenderer(AppSettings.LWJGL_OPENGL2);
+//        app.setSettings(settings);
         app.start();
     }
 

+ 19 - 14
jme3-examples/src/main/java/jme3test/post/TestPostFiltersCompositing.java

@@ -34,13 +34,16 @@ package jme3test.post;
 import com.jme3.app.SimpleApplication;
 import com.jme3.asset.plugins.HttpZipLocator;
 import com.jme3.light.DirectionalLight;
+import com.jme3.material.Material;
 import com.jme3.math.ColorRGBA;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector3f;
 import com.jme3.post.FilterPostProcessor;
 import com.jme3.post.filters.ColorOverlayFilter;
 import com.jme3.post.filters.ComposeFilter;
+import com.jme3.scene.Geometry;
 import com.jme3.scene.Spatial;
+import com.jme3.scene.shape.Box;
 import com.jme3.system.AppSettings;
 import com.jme3.texture.FrameBuffer;
 import com.jme3.texture.Image;
@@ -55,18 +58,19 @@ import com.jme3.util.SkyFactory;
 public class TestPostFiltersCompositing extends SimpleApplication {
 
     public static void main(String[] args) {
-        TestPostFiltersCompositing app = new TestPostFiltersCompositing();        
-        AppSettings settings = new AppSettings(true);
-        settings.putBoolean("GraphicsDebug", false);
-        app.setSettings(settings);
+        TestPostFiltersCompositing app = new TestPostFiltersCompositing();
+//        AppSettings settings = new AppSettings(true);
+//        settings.putBoolean("GraphicsDebug", false);
+//        app.setSettings(settings);
         app.start();        
         
     }
 
     public void simpleInitApp() {
         this.flyCam.setMoveSpeed(10);
-        cam.setLocation(new Vector3f(6.0344796f, 1.5054002f, 55.572033f));
-        cam.setRotation(new Quaternion(0.0016069f, 0.9810479f, -0.008143323f, 0.19358753f));
+        cam.setLocation(new Vector3f(0.028406568f, 2.015769f, 7.386517f));
+        cam.setRotation(new Quaternion(-1.0729783E-5f, 0.9999721f, -0.0073241726f, -0.0014647911f));
+
 
         makeScene();
 
@@ -80,15 +84,16 @@ public class TestPostFiltersCompositing extends SimpleApplication {
         Texture2D mainVPTexture = new Texture2D(cam.getWidth(), cam.getHeight(), Image.Format.RGBA8);
         mainVPFrameBuffer.addColorTexture(mainVPTexture);
         mainVPFrameBuffer.setDepthBuffer(Image.Format.Depth);
-        viewPort.setOutputFrameBuffer(mainVPFrameBuffer);     
+        viewPort.setOutputFrameBuffer(mainVPFrameBuffer);
 
         //creating the post processor for the gui viewport
-        final FilterPostProcessor guifpp = new FilterPostProcessor(assetManager);           
-        guifpp.addFilter(new ColorOverlayFilter(ColorRGBA.Red));       
+        final FilterPostProcessor guifpp = new FilterPostProcessor(assetManager);
+        guifpp.setFrameBufferFormat(Image.Format.RGBA8);
+        guifpp.addFilter(new ColorOverlayFilter(ColorRGBA.Red));
         //this will compose the main viewport texture with the guiviewport back buffer.
-        //Note that you can swich the order of the filters so that guiviewport filters are applied or not to the main viewport texture    
+        //Note that you can switch the order of the filters so that guiviewport filters are applied or not to the main viewport texture
         guifpp.addFilter(new ComposeFilter(mainVPTexture));
-        
+
         guiViewPort.addProcessor(guifpp);
         
         //compositing is done by mixing texture depending on the alpha channel, 
@@ -102,12 +107,12 @@ public class TestPostFiltersCompositing extends SimpleApplication {
     private void makeScene() {
         // load sky
         rootNode.attachChild(SkyFactory.createSky(assetManager, "Textures/Sky/Bright/BrightSky.dds", SkyFactory.EnvMapType.CubeMap));
-        assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip", HttpZipLocator.class);        
-        Spatial scene = assetManager.loadModel("main.scene");
+        //assetManager.registerLocator("http://jmonkeyengine.googlecode.com/files/wildhouse.zip", HttpZipLocator.class);
+        Spatial scene = assetManager.loadModel("Models/Test/CornellBox.j3o");
         DirectionalLight sun = new DirectionalLight();
         sun.setDirection(new Vector3f(-0.4790551f, -0.39247334f, -0.7851566f));
-        sun.setColor(ColorRGBA.White.clone().multLocal(2));
         scene.addLight(sun);
         rootNode.attachChild(scene);
+
     }
 }

+ 11 - 28
jme3-examples/src/main/java/jme3test/post/TestPosterization.java

@@ -42,6 +42,7 @@ import com.jme3.math.ColorRGBA;
 import com.jme3.math.Quaternion;
 import com.jme3.math.Vector3f;
 import com.jme3.post.FilterPostProcessor;
+import com.jme3.post.filters.ColorOverlayFilter;
 import com.jme3.post.filters.PosterizationFilter;
 import com.jme3.renderer.queue.RenderQueue.ShadowMode;
 import com.jme3.scene.Geometry;
@@ -52,13 +53,8 @@ import com.jme3.util.SkyFactory;
 
 public class TestPosterization extends SimpleApplication {
 
-    float angle;
-    Spatial lightMdl;
     Spatial teapot;
-    Geometry frustumMdl;
-    WireFrustum frustum;
-    boolean active=true;
-    FilterPostProcessor fpp;
+    PosterizationFilter pf;
     
     public static void main(String[] args){
         TestPosterization app = new TestPosterization();
@@ -70,8 +66,6 @@ public class TestPosterization extends SimpleApplication {
         // put the camera in a bad position
         cam.setLocation(new Vector3f(-2.336393f, 11.91392f, -7.139601f));
         cam.setRotation(new Quaternion(0.23602544f, 0.11321983f, -0.027698677f, 0.96473104f));
-        //cam.setFrustumFar(1000);
-
 
         Material mat = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
         mat.setFloat("Shininess", 15f);
@@ -80,17 +74,12 @@ public class TestPosterization extends SimpleApplication {
         mat.setColor("Diffuse", ColorRGBA.Yellow.mult(0.2f));
         mat.setColor("Specular", ColorRGBA.Yellow.mult(0.8f));
 
-    
-
-
         Material matSoil = new Material(assetManager,"Common/MatDefs/Light/Lighting.j3md");
         matSoil.setFloat("Shininess", 15f);
         matSoil.setBoolean("UseMaterialColors", true);
         matSoil.setColor("Ambient", ColorRGBA.Gray);
         matSoil.setColor("Diffuse", ColorRGBA.Black);
         matSoil.setColor("Specular", ColorRGBA.Gray);
-       
-
 
         teapot = assetManager.loadModel("Models/Teapot/Teapot.obj");
         teapot.setLocalTranslation(0,0,10);
@@ -100,8 +89,6 @@ public class TestPosterization extends SimpleApplication {
         teapot.setLocalScale(10.0f);
         rootNode.attachChild(teapot);
 
-  
-
         Geometry soil = new Geometry("soil", new Box(800, 10, 700));
         soil.setLocalTranslation(0, -13, 550);
         soil.setMaterial(matSoil);
@@ -114,17 +101,19 @@ public class TestPosterization extends SimpleApplication {
         rootNode.addLight(light);
 
         // load sky
-        Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Bright/FullskiesBlueClear03.dds", false);
+        Spatial sky = SkyFactory.createSky(assetManager, "Textures/Sky/Bright/FullskiesBlueClear03.dds", SkyFactory.EnvMapType.CubeMap);
         sky.setCullHint(Spatial.CullHint.Never);
         rootNode.attachChild(sky);
 
-        fpp=new FilterPostProcessor(assetManager);
-        PosterizationFilter pf=new PosterizationFilter();
-        
-   
+        FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
+        int numSamples = getContext().getSettings().getSamples();
+        if (numSamples > 0) {
+            fpp.setNumSamples(numSamples);
+        }
+        pf = new PosterizationFilter();
+        fpp.addFilter(pf);
 
         viewPort.addProcessor(fpp);
-        fpp.addFilter(pf);
         initInputs();
 
     }
@@ -136,13 +125,7 @@ public class TestPosterization extends SimpleApplication {
 
             public void onAction(String name, boolean keyPressed, float tpf) {
                 if (name.equals("toggle") && keyPressed) {
-                    if(active){
-                        active=false;
-                        viewPort.removeProcessor(fpp);
-                    }else{
-                        active=true;
-                        viewPort.addProcessor(fpp);
-                    }
+                    pf.setEnabled(!pf.isEnabled());
                 }
             }
         };

+ 1 - 0
jme3-examples/src/main/java/jme3test/post/TestSSAO2.java

@@ -96,6 +96,7 @@ public class TestSSAO2 extends SimpleApplication {
 
         FilterPostProcessor fpp = new FilterPostProcessor(assetManager);
         SSAOFilter ssaoFilter = new SSAOFilter(2.9299974f,25f,5.8100376f,0.091000035f);
+        ssaoFilter.setApproximateNormals(true);
         fpp.addFilter(ssaoFilter);
         SSAOUI ui = new SSAOUI(inputManager, ssaoFilter);
 

+ 5 - 2
jme3-examples/src/main/java/jme3test/water/TestPostWater.java

@@ -140,9 +140,12 @@ public class TestPostWater extends SimpleApplication {
         fpp.addFilter(lsf);
         fpp.addFilter(new FXAAFilter());
         
-//      fpp.addFilter(new GammaCorrectionFilter());
 //      fpp.addFilter(new TranslucentBucketFilter());
-//      fpp.setNumSamples(4);    
+        int numSamples = getContext().getSettings().getSamples();
+        if (numSamples > 0) {
+            fpp.setNumSamples(numSamples);
+        }
+
         
         uw = cam.getLocation().y < waterHeight;