diff --git a/engine/src/core-data/Common/MatDefs/Water/Water.frag b/engine/src/core-data/Common/MatDefs/Water/Water.frag index cbb173901..2966c0140 100644 --- a/engine/src/core-data/Common/MatDefs/Water/Water.frag +++ b/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 diff --git a/engine/src/core-data/Common/MatDefs/Water/Water.j3md b/engine/src/core-data/Common/MatDefs/Water/Water.j3md index 4d8a55a68..160cadb98 100644 --- a/engine/src/core-data/Common/MatDefs/Water/Water.j3md +++ b/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 diff --git a/engine/src/core-data/Common/MatDefs/Water/Water15.frag b/engine/src/core-data/Common/MatDefs/Water/Water15.frag index 8a9d7a736..52f902ce8 100644 --- a/engine/src/core-data/Common/MatDefs/Water/Water15.frag +++ b/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 diff --git a/engine/src/desktop-fx/com/jme3/water/WaterFilter.java b/engine/src/desktop-fx/com/jme3/water/WaterFilter.java index 67e2cdda8..092221577 100644 --- a/engine/src/desktop-fx/com/jme3/water/WaterFilter.java +++ b/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); + } + } }