Browse Source

Water Filter :
- fix for glsl 1.0 shader
- better caustics color management
- added a couple of paramters to tweak, underWaterFogDistance, causticsIntensity

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

rem..om 14 years ago
parent
commit
602a5099f3

+ 6 - 4
engine/src/core-data/Common/MatDefs/Water/Water.frag

@@ -34,6 +34,8 @@ uniform vec4 m_DeepWaterColor;
 uniform vec2 m_WindDirection;
 uniform float m_SunScale;
 uniform float m_WaveScale;
+uniform float m_UnderWaterFogDistance;
+uniform float m_CausticsIntensity;
 
 vec2 scale = vec2(m_WaveScale, m_WaveScale);
 float refractionScale = m_WaveScale;
@@ -115,7 +117,7 @@ float fresnelTerm(in vec3 normal,in vec3 eyeVec){
     return saturate(fresnel * (1.0 - saturate(m_R0)) + m_R0 - m_RefractionStrength);
 }
 
-vec2 m_FrustumNearFar=vec2(1.0,50);
+vec2 m_FrustumNearFar=vec2(1.0,m_UnderWaterFogDistance);
 const float LOG2 = 1.442695;
 
 vec4 underWater(){
@@ -138,7 +140,7 @@ vec4 underWater(){
 
     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;
+    float bias = texture2D(m_HeightMap, texC).r;
     level += bias * m_MaxAmplitude;
     t = (level - m_CameraPosition.y) / eyeVecNorm.y;
     surfacePoint = m_CameraPosition + eyeVecNorm * t; 
@@ -205,8 +207,8 @@ vec4 underWater(){
             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 *= m_WaterColor.rgb;
-            color=mix(color2, caustics,0.6);
+            caustics=saturate(mix(m_WaterColor.rgb,caustics,m_CausticsIntensity));            
+            color=mix(color2,caustics,m_CausticsIntensity);          
         #else
             color=color2;
         #endif

+ 2 - 0
engine/src/core-data/Common/MatDefs/Water/Water.j3md

@@ -35,6 +35,8 @@ MaterialDef Advanced Water {
         Vector2 WindDirection
         Float ReflectionDisplace
         Float FoamIntensity
+        Float CausticsIntensity
+        Float UnderWaterFogDistance
 
         Boolean UseRipples
         Boolean UseHQShoreline

+ 6 - 4
engine/src/core-data/Common/MatDefs/Water/Water15.frag

@@ -38,6 +38,8 @@ uniform vec4 m_DeepWaterColor;
 uniform vec2 m_WindDirection;
 uniform float m_SunScale;
 uniform float m_WaveScale;
+uniform float m_UnderWaterFogDistance;
+uniform float m_CausticsIntensity;
 
 
 vec2 scale = vec2(m_WaveScale, m_WaveScale);
@@ -111,7 +113,7 @@ float fresnelTerm(in vec3 normal,in vec3 eyeVec){
     return saturate(fresnel * (1.0 - saturate(m_R0)) + m_R0 - m_RefractionStrength);
 }
 
-vec2 m_FrustumNearFar=vec2(1.0,50);
+vec2 m_FrustumNearFar=vec2(1.0,m_UnderWaterFogDistance);
 const float LOG2 = 1.442695;
 
 vec4 underWater(int sampleNum){
@@ -206,9 +208,9 @@ vec4 underWater(int sampleNum){
             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 *= m_WaterColor.rgb;
-            color=mix(color2, caustics,0.6);
+            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

+ 48 - 6
engine/src/desktop-fx/com/jme3/water/WaterFilter.java

@@ -95,7 +95,7 @@ public class WaterFilter extends Filter {
     private Vector3f colorExtinction = new Vector3f(5.0f, 20.0f, 30.0f);
     private float sunScale = 3.0f;
     private float shininess = 0.7f;
-    private ColorRGBA waterColor = new ColorRGBA(0.0078f, 0.5176f, 0.5f, 1.0f);
+    private ColorRGBA waterColor = new ColorRGBA(0.0078f, 0.3176f, 0.5f, 1.0f);
     private ColorRGBA deepWaterColor = new ColorRGBA(0.0039f, 0.00196f, 0.145f, 1.0f);
     private Vector2f windDirection = new Vector2f(0.0f, -1.0f);
     private int reflectionMapSize = 512;
@@ -110,6 +110,8 @@ public class WaterFilter extends Filter {
     private float reflectionDisplace = 30;
     private float foamIntensity = 0.5f;
     private boolean underWater;
+    private float underWaterFogDistance = 120;
+    private float causticsIntensity = 0.5f;
 
     /**
      * Create a Water Filter
@@ -192,9 +194,9 @@ public class WaterFilter extends Filter {
             }
             renderManager.getRenderer().setFrameBuffer(viewPort.getOutputFrameBuffer());
             renderManager.setCamera(sceneCam, false);
-            underWater=false;
-        }else{
-            underWater=true;
+            underWater = false;
+        } else {
+            underWater = true;
         }
     }
 
@@ -263,6 +265,9 @@ public class WaterFilter extends Filter {
         material.setBoolean("UseRefraction", useRefraction);
         material.setFloat("ReflectionDisplace", reflectionDisplace);
         material.setFloat("FoamIntensity", foamIntensity);
+        material.setFloat("UnderWaterFogDistance", underWaterFogDistance);
+        material.setFloat("m_CausticsIntensity", causticsIntensity);
+
 
     }
 
@@ -871,6 +876,43 @@ public class WaterFilter extends Filter {
     public boolean isUnderWater() {
         return underWater;
     }
-    
-    
+
+    /**
+     * returns the distance of the fog when under water
+     * @return 
+     */
+    public float getUnderWaterFogDistance() {
+        return underWaterFogDistance;
+    }
+
+    /**
+     * sets the distance of the fog when under water.
+     * default is 120 (120 world units) use a high value to raise the view range under water
+     * @param underWaterFogDistance 
+     */
+    public void setUnderWaterFogDistance(float underWaterFogDistance) {
+        this.underWaterFogDistance = underWaterFogDistance;
+        if (material != null) {
+            material.setFloat("m_UnderWaterFogDistance", underWaterFogDistance);
+        }
+    }
+
+    /**
+     * get the intensity of caustics under water
+     * @return 
+     */
+    public float getCausticsIntensity() {
+        return causticsIntensity;
+    }
+
+    /**
+     * sets the intensity of caustics under water. goes from 0 to 1, default is 0.5f
+     * @param causticsIntensity 
+     */
+    public void setCausticsIntensity(float causticsIntensity) {
+        this.causticsIntensity = causticsIntensity;
+        if (material != null) {
+            material.setFloat("m_CausticsIntensity", causticsIntensity);
+        }
+    }
 }