/* -------------------------------------------------------------------------------- This source file is part of SkyX. Visit http://www.paradise-studios.net/products/skyx/ Copyright (C) 2009-2012 Xavier Verguín González This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt. -------------------------------------------------------------------------------- */ #ifdef GL_ES #extension GL_OES_texture_3D : enable precision highp int; precision highp float; precision mediump sampler3D; #endif // ------------------- SkyX volumetric clouds + lightning ----------------------- // ----------------------------- GLSL Fragment ---------------------------------- #define USE_OGRE_FROM_FUTURE #include SAMPLER3D(uDensity0, 0); SAMPLER3D(uDensity1, 1); SAMPLER2D(uNoise, 2); OGRE_UNIFORMS( uniform float uInterpolation; uniform vec3 uSunDirection; uniform vec3 uAmbientColor; uniform vec3 uSunColor; uniform vec4 uLightResponse; uniform vec4 uAmbientFactors; uniform vec4 uLightning0; uniform vec4 uLightning1; uniform vec4 uLightning2; uniform vec3 uLightningColor; ) MAIN_PARAMETERS IN(vec3 v3DCoord, TEXCOORD0) IN(vec2 vNoiseUV, TEXCOORD1) IN(float vOpacity, TEXCOORD2) IN(vec3 vEyePixel, TEXCOORD3) IN(float vDistance, TEXCOORD4) IN(vec4 vPositionAtt, TEXCOORD5) MAIN_DECLARATION { // x - Sun light power // y - Sun beta multiplier // z - Ambient color multiplier // w - Distance attenuation // uLightResponse = vec4(0.25, 0.2, 1.0, 0.1); // Ambient light factors // x - constant, y - linear, z - cuadratic, w - cubic // vec4 uAmbientFactors = vec4(0.4, 1.0, 1.0, 1.0); vec3 Noise = texture2D(uNoise, vNoiseUV * vec2_splat(5.0)).xyz; vec3 Final3DCoord = v3DCoord + vec3_splat(0.002575) * (Noise - vec3_splat(0.5)) * vec3_splat(2.0); Final3DCoord.z = saturate(Final3DCoord.z); vec3 Density0 = texture3D(uDensity0, Final3DCoord).xyz; vec3 Density1 = texture3D(uDensity1, Final3DCoord).xyz; vec3 Density = Density0 * vec3_splat(1.0 - uInterpolation) + Density1 * vec3_splat(uInterpolation); vec3 finalcolor = vec3(0.0, 0.0 ,0.0); float Opacity = 0.0; if (Density.x > 0.0) { float cos0 = saturate(dot(uSunDirection, vEyePixel)); float c2 = cos0 * cos0; float Beta = c2 * uLightResponse.y * (0.5 + 2.5 * saturate(1.0 - 2.0 * uSunDirection.y) * Density.y); float sunaccumulation = max(0.2, saturate(Beta + Density.y * uLightResponse.x + pow(vDistance, 1.5) * uLightResponse.w)); float ambientaccumulation = saturate(uAmbientFactors.x + uAmbientFactors.y * v3DCoord.z + uAmbientFactors.z * pow(v3DCoord.z, 2.0) + uAmbientFactors.w * pow(v3DCoord.z, 3.0)) * uLightResponse.z; finalcolor = uAmbientColor * vec3_splat(ambientaccumulation) + uSunColor * vec3_splat(sunaccumulation); Opacity = (1.0 - exp(-Density.x * (7.5 - 6.5 * v3DCoord.z))) * vOpacity; vec3 lightningColor = uLightningColor * vec3_splat(2.0) * (vec3_splat(1.0) - saturate(finalcolor)); // Lightning 0 finalcolor += lightningColor * vec3_splat(uLightning0.w) / vec3_splat(0.85 + vPositionAtt.w * length(uLightning0.xyz - vPositionAtt.xyz)); // Lightning 1 finalcolor += lightningColor * vec3_splat(uLightning1.w) / vec3_splat(0.85 + vPositionAtt.w * length(uLightning1.xyz - vPositionAtt.xyz)); // Lightning 2 finalcolor += lightningColor * vec3_splat(uLightning2.w) / vec3_splat(0.85 + vPositionAtt.w * length(uLightning2.xyz - vPositionAtt.xyz)); } gl_FragColor = vec4(finalcolor, Opacity); }