/indra/newview/app_settings/shaders/class1/deferred/softenLightF.glsl

https://bitbucket.org/lindenlab/viewer-beta/ · GLSL · 328 lines · 213 code · 56 blank · 59 comment · 2 complexity · 844bf1541d19d4f2f0891890a6800574 MD5 · raw file

  1. /**
  2. * @file softenLightF.glsl
  3. *
  4. * $LicenseInfo:firstyear=2007&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2007, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #extension GL_ARB_texture_rectangle : enable
  26. #ifdef DEFINE_GL_FRAGCOLOR
  27. out vec4 gl_FragColor;
  28. #endif
  29. uniform sampler2DRect diffuseRect;
  30. uniform sampler2DRect specularRect;
  31. uniform sampler2DRect positionMap;
  32. uniform sampler2DRect normalMap;
  33. uniform sampler2DRect lightMap;
  34. uniform sampler2DRect depthMap;
  35. uniform samplerCube environmentMap;
  36. uniform sampler2D lightFunc;
  37. uniform float blur_size;
  38. uniform float blur_fidelity;
  39. // Inputs
  40. uniform vec4 morphFactor;
  41. uniform vec3 camPosLocal;
  42. //uniform vec4 camPosWorld;
  43. uniform vec4 gamma;
  44. uniform vec4 lightnorm;
  45. uniform vec4 sunlight_color;
  46. uniform vec4 ambient;
  47. uniform vec4 blue_horizon;
  48. uniform vec4 blue_density;
  49. uniform vec4 haze_horizon;
  50. uniform vec4 haze_density;
  51. uniform vec4 cloud_shadow;
  52. uniform vec4 density_multiplier;
  53. uniform vec4 distance_multiplier;
  54. uniform vec4 max_y;
  55. uniform vec4 glow;
  56. uniform float scene_light_strength;
  57. uniform mat3 env_mat;
  58. uniform mat3 ssao_effect_mat;
  59. uniform vec3 sun_dir;
  60. VARYING vec2 vary_fragcoord;
  61. vec3 vary_PositionEye;
  62. vec3 vary_SunlitColor;
  63. vec3 vary_AmblitColor;
  64. vec3 vary_AdditiveColor;
  65. vec3 vary_AtmosAttenuation;
  66. uniform mat4 inv_proj;
  67. uniform vec2 screen_res;
  68. vec4 getPosition_d(vec2 pos_screen, float depth)
  69. {
  70. vec2 sc = pos_screen.xy*2.0;
  71. sc /= screen_res;
  72. sc -= vec2(1.0,1.0);
  73. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  74. vec4 pos = inv_proj * ndc;
  75. pos /= pos.w;
  76. pos.w = 1.0;
  77. return pos;
  78. }
  79. vec4 getPosition(vec2 pos_screen)
  80. { //get position in screen space (world units) given window coordinate and depth map
  81. float depth = texture2DRect(depthMap, pos_screen.xy).a;
  82. return getPosition_d(pos_screen, depth);
  83. }
  84. vec3 getPositionEye()
  85. {
  86. return vary_PositionEye;
  87. }
  88. vec3 getSunlitColor()
  89. {
  90. return vary_SunlitColor;
  91. }
  92. vec3 getAmblitColor()
  93. {
  94. return vary_AmblitColor;
  95. }
  96. vec3 getAdditiveColor()
  97. {
  98. return vary_AdditiveColor;
  99. }
  100. vec3 getAtmosAttenuation()
  101. {
  102. return vary_AtmosAttenuation;
  103. }
  104. void setPositionEye(vec3 v)
  105. {
  106. vary_PositionEye = v;
  107. }
  108. void setSunlitColor(vec3 v)
  109. {
  110. vary_SunlitColor = v;
  111. }
  112. void setAmblitColor(vec3 v)
  113. {
  114. vary_AmblitColor = v;
  115. }
  116. void setAdditiveColor(vec3 v)
  117. {
  118. vary_AdditiveColor = v;
  119. }
  120. void setAtmosAttenuation(vec3 v)
  121. {
  122. vary_AtmosAttenuation = v;
  123. }
  124. void calcAtmospherics(vec3 inPositionEye, float ambFactor) {
  125. vec3 P = inPositionEye;
  126. setPositionEye(P);
  127. vec3 tmpLightnorm = lightnorm.xyz;
  128. vec3 Pn = normalize(P);
  129. float Plen = length(P);
  130. vec4 temp1 = vec4(0);
  131. vec3 temp2 = vec3(0);
  132. vec4 blue_weight;
  133. vec4 haze_weight;
  134. vec4 sunlight = sunlight_color;
  135. vec4 light_atten;
  136. //sunlight attenuation effect (hue and brightness) due to atmosphere
  137. //this is used later for sunlight modulation at various altitudes
  138. light_atten = (blue_density * 1.0 + vec4(haze_density.r) * 0.25) * (density_multiplier.x * max_y.x);
  139. //I had thought blue_density and haze_density should have equal weighting,
  140. //but attenuation due to haze_density tends to seem too strong
  141. temp1 = blue_density + vec4(haze_density.r);
  142. blue_weight = blue_density / temp1;
  143. haze_weight = vec4(haze_density.r) / temp1;
  144. //(TERRAIN) compute sunlight from lightnorm only (for short rays like terrain)
  145. temp2.y = max(0.0, tmpLightnorm.y);
  146. temp2.y = 1. / temp2.y;
  147. sunlight *= exp( - light_atten * temp2.y);
  148. // main atmospheric scattering line integral
  149. temp2.z = Plen * density_multiplier.x;
  150. // Transparency (-> temp1)
  151. // ATI Bugfix -- can't store temp1*temp2.z*distance_multiplier.x in a variable because the ati
  152. // compiler gets confused.
  153. temp1 = exp(-temp1 * temp2.z * distance_multiplier.x);
  154. //final atmosphere attenuation factor
  155. setAtmosAttenuation(temp1.rgb);
  156. //compute haze glow
  157. //(can use temp2.x as temp because we haven't used it yet)
  158. temp2.x = dot(Pn, tmpLightnorm.xyz);
  159. temp2.x = 1. - temp2.x;
  160. //temp2.x is 0 at the sun and increases away from sun
  161. temp2.x = max(temp2.x, .03); //was glow.y
  162. //set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
  163. temp2.x *= glow.x;
  164. //higher glow.x gives dimmer glow (because next step is 1 / "angle")
  165. temp2.x = pow(temp2.x, glow.z);
  166. //glow.z should be negative, so we're doing a sort of (1 / "angle") function
  167. //add "minimum anti-solar illumination"
  168. temp2.x += .25;
  169. //increase ambient when there are more clouds
  170. vec4 tmpAmbient = ambient + (vec4(1.) - ambient) * cloud_shadow.x * 0.5;
  171. /* decrease value and saturation (that in HSV, not HSL) for occluded areas
  172. * // for HSV color/geometry used here, see http://gimp-savvy.com/BOOK/index.html?node52.html
  173. * // The following line of code performs the equivalent of:
  174. * float ambAlpha = tmpAmbient.a;
  175. * float ambValue = dot(vec3(tmpAmbient), vec3(0.577)); // projection onto <1/rt(3), 1/rt(3), 1/rt(3)>, the neutral white-black axis
  176. * vec3 ambHueSat = vec3(tmpAmbient) - vec3(ambValue);
  177. * tmpAmbient = vec4(RenderSSAOEffect.valueFactor * vec3(ambValue) + RenderSSAOEffect.saturationFactor *(1.0 - ambFactor) * ambHueSat, ambAlpha);
  178. */
  179. tmpAmbient = vec4(mix(ssao_effect_mat * tmpAmbient.rgb, tmpAmbient.rgb, ambFactor), tmpAmbient.a);
  180. //haze color
  181. setAdditiveColor(
  182. vec3(blue_horizon * blue_weight * (sunlight*(1.-cloud_shadow.x) + tmpAmbient)
  183. + (haze_horizon.r * haze_weight) * (sunlight*(1.-cloud_shadow.x) * temp2.x
  184. + tmpAmbient)));
  185. //brightness of surface both sunlight and ambient
  186. setSunlitColor(vec3(sunlight * .5));
  187. setAmblitColor(vec3(tmpAmbient * .25));
  188. setAdditiveColor(getAdditiveColor() * vec3(1.0 - temp1));
  189. }
  190. vec3 atmosLighting(vec3 light)
  191. {
  192. light *= getAtmosAttenuation().r;
  193. light += getAdditiveColor();
  194. return (2.0 * light);
  195. }
  196. vec3 atmosTransport(vec3 light) {
  197. light *= getAtmosAttenuation().r;
  198. light += getAdditiveColor() * 2.0;
  199. return light;
  200. }
  201. vec3 atmosGetDiffuseSunlightColor()
  202. {
  203. return getSunlitColor();
  204. }
  205. vec3 scaleDownLight(vec3 light)
  206. {
  207. return (light / scene_light_strength );
  208. }
  209. vec3 scaleUpLight(vec3 light)
  210. {
  211. return (light * scene_light_strength);
  212. }
  213. vec3 atmosAmbient(vec3 light)
  214. {
  215. return getAmblitColor() + light / 2.0;
  216. }
  217. vec3 atmosAffectDirectionalLight(float lightIntensity)
  218. {
  219. return getSunlitColor() * lightIntensity;
  220. }
  221. vec3 scaleSoftClip(vec3 light)
  222. {
  223. //soft clip effect:
  224. light = 1. - clamp(light, vec3(0.), vec3(1.));
  225. light = 1. - pow(light, gamma.xxx);
  226. return light;
  227. }
  228. void main()
  229. {
  230. vec2 tc = vary_fragcoord.xy;
  231. float depth = texture2DRect(depthMap, tc.xy).r;
  232. vec3 pos = getPosition_d(tc, depth).xyz;
  233. vec3 norm = texture2DRect(normalMap, tc).xyz;
  234. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  235. float da = max(dot(norm.xyz, sun_dir.xyz), 0.0);
  236. vec4 diffuse = texture2DRect(diffuseRect, tc);
  237. vec4 spec = texture2DRect(specularRect, vary_fragcoord.xy);
  238. vec3 col;
  239. float bloom = 0.0;
  240. if (diffuse.a < 0.9)
  241. {
  242. calcAtmospherics(pos.xyz, 1.0);
  243. col = atmosAmbient(vec3(0));
  244. col += atmosAffectDirectionalLight(max(min(da, 1.0), diffuse.a));
  245. col *= diffuse.rgb;
  246. if (spec.a > 0.0) // specular reflection
  247. {
  248. // the old infinite-sky shiny reflection
  249. //
  250. vec3 refnormpersp = normalize(reflect(pos.xyz, norm.xyz));
  251. float sa = dot(refnormpersp, sun_dir.xyz);
  252. vec3 dumbshiny = vary_SunlitColor*texture2D(lightFunc, vec2(sa, spec.a)).r;
  253. // add the two types of shiny together
  254. vec3 spec_contrib = dumbshiny * spec.rgb;
  255. bloom = dot(spec_contrib, spec_contrib);
  256. col += spec_contrib;
  257. //add environmentmap
  258. vec3 env_vec = env_mat * refnormpersp;
  259. col = mix(col.rgb, textureCube(environmentMap, env_vec).rgb,
  260. max(spec.a-diffuse.a*2.0, 0.0));
  261. }
  262. col = atmosLighting(col);
  263. col = scaleSoftClip(col);
  264. col = mix(col.rgb, diffuse.rgb, diffuse.a);
  265. }
  266. else
  267. {
  268. col = diffuse.rgb;
  269. }
  270. gl_FragColor.rgb = col;
  271. gl_FragColor.a = bloom;
  272. }