Created
April 22, 2019 11:33
-
-
Save Ali-RS/06a6ea5e542d318714083c103982cac9 to your computer and use it in GitHub Desktop.
GLSL Fog library for jmonkeyengine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Redistribution and use in source and binary forms, with or without | |
| * modification, are permitted provided that the following conditions are | |
| * met: | |
| * | |
| * * Redistributions of source code must retain the above copyright | |
| * notice, this list of conditions and the following disclaimer. | |
| * | |
| * * Redistributions in binary form must reproduce the above copyright | |
| * notice, this list of conditions and the following disclaimer in the | |
| * documentation and/or other materials provided with the distribution. | |
| * | |
| * * Neither the name of 'jMonkeyEngine' nor the names of its contributors | |
| * may be used to endorse or promote products derived from this software | |
| * without specific prior written permission. | |
| * | |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED | |
| * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
| * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| */ | |
| // @see: http://in2gpu.com/2014/07/22/create-fog-shader/ | |
| // @see: https://hub.jmonkeyengine.org/t/fog-glsl-library-for-materials/40943 | |
| // Float FogDensity: 0.05 | |
| // Float FogDensity: 0.003 | |
| // Float FogStart = 20.0 | |
| // Float FogEnd = 80.0 | |
| // .vert: varying float dist; | |
| // .vert: dist = distance(g_CameraPosition, (g_WorldViewMatrix * modelSpacePos).xyz); | |
| // pick one... | |
| // .frag: vec4 fogResult = getFogLinear(gl_FragColor, fogColor, start, end, dist); | |
| // .frag: vec4 fogResult = getFogExp(gl_FragColor, fogColor, m_FogDensity, dist); | |
| // .frag: vec4 fogResult = getFogExpSquare(gl_FragColor, fogColor, m_FogDensity, dist); | |
| vec4 getFogLinear(in vec4 diffuseColor, in vec4 fogColor, in float start, in float end, in float distance) { | |
| float fogFactor = (end - distance) / (end - start); | |
| fogFactor = clamp(fogFactor, 0.0, 1.0); | |
| return mix(fogColor, diffuseColor, fogFactor); | |
| } | |
| vec4 getFogExp(in vec4 diffuseColor, in vec4 fogColor, in float fogDensity, in float distance) { | |
| float fogFactor = 1.0 / exp(distance * fogDensity); | |
| fogFactor = clamp( fogFactor, 0.0, 1.0 ); | |
| return mix(fogColor, diffuseColor, fogFactor); | |
| } | |
| vec4 getFogExpSquare(in vec4 diffuseColor, in vec4 fogColor, in float fogDensity, in float distance) { | |
| float fogFactor = 1.0 / exp( (distance * fogDensity) * (distance * fogDensity)); | |
| fogFactor = clamp( fogFactor, 0.0, 1.0 ); | |
| vec4 finalColor = mix(fogColor, diffuseColor, fogFactor); | |
| return finalColor; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment