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

https://bitbucket.org/lindenlab/viewer-beta/ · GLSL · 335 lines · 217 code · 59 blank · 59 comment · 2 complexity · 8b0c5883496402cd615c9d9cd346baea 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 normalMap;
  32. uniform sampler2DRect lightMap;
  33. uniform sampler2DRect depthMap;
  34. uniform samplerCube environmentMap;
  35. uniform sampler2D lightFunc;
  36. uniform vec3 gi_quad;
  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 vec4 shadow_clip;
  59. uniform mat3 ssao_effect_mat;
  60. uniform mat4 inv_proj;
  61. uniform vec2 screen_res;
  62. uniform vec3 sun_dir;
  63. VARYING vec2 vary_fragcoord;
  64. vec3 vary_PositionEye;
  65. vec3 vary_SunlitColor;
  66. vec3 vary_AmblitColor;
  67. vec3 vary_AdditiveColor;
  68. vec3 vary_AtmosAttenuation;
  69. vec4 getPosition_d(vec2 pos_screen, float depth)
  70. {
  71. vec2 sc = pos_screen.xy*2.0;
  72. sc /= screen_res;
  73. sc -= vec2(1.0,1.0);
  74. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  75. vec4 pos = inv_proj * ndc;
  76. pos /= pos.w;
  77. pos.w = 1.0;
  78. return pos;
  79. }
  80. vec4 getPosition(vec2 pos_screen)
  81. { //get position in screen space (world units) given window coordinate and depth map
  82. float depth = texture2DRect(depthMap, pos_screen.xy).r;
  83. return getPosition_d(pos_screen, depth);
  84. }
  85. vec3 getPositionEye()
  86. {
  87. return vary_PositionEye;
  88. }
  89. vec3 getSunlitColor()
  90. {
  91. return vary_SunlitColor;
  92. }
  93. vec3 getAmblitColor()
  94. {
  95. return vary_AmblitColor;
  96. }
  97. vec3 getAdditiveColor()
  98. {
  99. return vary_AdditiveColor;
  100. }
  101. vec3 getAtmosAttenuation()
  102. {
  103. return vary_AtmosAttenuation;
  104. }
  105. void setPositionEye(vec3 v)
  106. {
  107. vary_PositionEye = v;
  108. }
  109. void setSunlitColor(vec3 v)
  110. {
  111. vary_SunlitColor = v;
  112. }
  113. void setAmblitColor(vec3 v)
  114. {
  115. vary_AmblitColor = v;
  116. }
  117. void setAdditiveColor(vec3 v)
  118. {
  119. vary_AdditiveColor = v;
  120. }
  121. void setAtmosAttenuation(vec3 v)
  122. {
  123. vary_AtmosAttenuation = v;
  124. }
  125. void calcAtmospherics(vec3 inPositionEye, float ambFactor) {
  126. vec3 P = inPositionEye;
  127. setPositionEye(P);
  128. vec3 tmpLightnorm = lightnorm.xyz;
  129. vec3 Pn = normalize(P);
  130. float Plen = length(P);
  131. vec4 temp1 = vec4(0);
  132. vec3 temp2 = vec3(0);
  133. vec4 blue_weight;
  134. vec4 haze_weight;
  135. vec4 sunlight = sunlight_color;
  136. vec4 light_atten;
  137. //sunlight attenuation effect (hue and brightness) due to atmosphere
  138. //this is used later for sunlight modulation at various altitudes
  139. light_atten = (blue_density * 1.0 + vec4(haze_density.r) * 0.25) * (density_multiplier.x * max_y.x);
  140. //I had thought blue_density and haze_density should have equal weighting,
  141. //but attenuation due to haze_density tends to seem too strong
  142. temp1 = blue_density + vec4(haze_density.r);
  143. blue_weight = blue_density / temp1;
  144. haze_weight = vec4(haze_density.r) / temp1;
  145. //(TERRAIN) compute sunlight from lightnorm only (for short rays like terrain)
  146. temp2.y = max(0.0, tmpLightnorm.y);
  147. temp2.y = 1. / temp2.y;
  148. sunlight *= exp( - light_atten * temp2.y);
  149. // main atmospheric scattering line integral
  150. temp2.z = Plen * density_multiplier.x;
  151. // Transparency (-> temp1)
  152. // ATI Bugfix -- can't store temp1*temp2.z*distance_multiplier.x in a variable because the ati
  153. // compiler gets confused.
  154. temp1 = exp(-temp1 * temp2.z * distance_multiplier.x);
  155. //final atmosphere attenuation factor
  156. setAtmosAttenuation(temp1.rgb);
  157. //compute haze glow
  158. //(can use temp2.x as temp because we haven't used it yet)
  159. temp2.x = dot(Pn, tmpLightnorm.xyz);
  160. temp2.x = 1. - temp2.x;
  161. //temp2.x is 0 at the sun and increases away from sun
  162. temp2.x = max(temp2.x, .03); //was glow.y
  163. //set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
  164. temp2.x *= glow.x;
  165. //higher glow.x gives dimmer glow (because next step is 1 / "angle")
  166. temp2.x = pow(temp2.x, glow.z);
  167. //glow.z should be negative, so we're doing a sort of (1 / "angle") function
  168. //add "minimum anti-solar illumination"
  169. temp2.x += .25;
  170. //increase ambient when there are more clouds
  171. vec4 tmpAmbient = ambient + (vec4(1.) - ambient) * cloud_shadow.x * 0.5;
  172. /* decrease value and saturation (that in HSV, not HSL) for occluded areas
  173. * // for HSV color/geometry used here, see http://gimp-savvy.com/BOOK/index.html?node52.html
  174. * // The following line of code performs the equivalent of:
  175. * float ambAlpha = tmpAmbient.a;
  176. * float ambValue = dot(vec3(tmpAmbient), vec3(0.577)); // projection onto <1/rt(3), 1/rt(3), 1/rt(3)>, the neutral white-black axis
  177. * vec3 ambHueSat = vec3(tmpAmbient) - vec3(ambValue);
  178. * tmpAmbient = vec4(RenderSSAOEffect.valueFactor * vec3(ambValue) + RenderSSAOEffect.saturationFactor *(1.0 - ambFactor) * ambHueSat, ambAlpha);
  179. */
  180. tmpAmbient = vec4(mix(ssao_effect_mat * tmpAmbient.rgb, tmpAmbient.rgb, ambFactor), tmpAmbient.a);
  181. //haze color
  182. setAdditiveColor(
  183. vec3(blue_horizon * blue_weight * (sunlight*(1.-cloud_shadow.x) + tmpAmbient)
  184. + (haze_horizon.r * haze_weight) * (sunlight*(1.-cloud_shadow.x) * temp2.x
  185. + tmpAmbient)));
  186. //brightness of surface both sunlight and ambient
  187. setSunlitColor(vec3(sunlight * .5));
  188. setAmblitColor(vec3(tmpAmbient * .25));
  189. setAdditiveColor(getAdditiveColor() * vec3(1.0 - temp1));
  190. }
  191. vec3 atmosLighting(vec3 light)
  192. {
  193. light *= getAtmosAttenuation().r;
  194. light += getAdditiveColor();
  195. return (2.0 * light);
  196. }
  197. vec3 atmosTransport(vec3 light) {
  198. light *= getAtmosAttenuation().r;
  199. light += getAdditiveColor() * 2.0;
  200. return light;
  201. }
  202. vec3 atmosGetDiffuseSunlightColor()
  203. {
  204. return getSunlitColor();
  205. }
  206. vec3 scaleDownLight(vec3 light)
  207. {
  208. return (light / scene_light_strength );
  209. }
  210. vec3 scaleUpLight(vec3 light)
  211. {
  212. return (light * scene_light_strength);
  213. }
  214. vec3 atmosAmbient(vec3 light)
  215. {
  216. return getAmblitColor() + light / 2.0;
  217. }
  218. vec3 atmosAffectDirectionalLight(float lightIntensity)
  219. {
  220. return getSunlitColor() * lightIntensity;
  221. }
  222. vec3 scaleSoftClip(vec3 light)
  223. {
  224. //soft clip effect:
  225. light = 1. - clamp(light, vec3(0.), vec3(1.));
  226. light = 1. - pow(light, gamma.xxx);
  227. return light;
  228. }
  229. void main()
  230. {
  231. vec2 tc = vary_fragcoord.xy;
  232. float depth = texture2DRect(depthMap, tc.xy).r;
  233. vec3 pos = getPosition_d(tc, depth).xyz;
  234. vec3 norm = texture2DRect(normalMap, tc).xyz;
  235. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  236. float da = max(dot(norm.xyz, sun_dir.xyz), 0.0);
  237. vec4 diffuse = texture2DRect(diffuseRect, tc);
  238. vec3 col;
  239. float bloom = 0.0;
  240. if (diffuse.a < 0.9)
  241. {
  242. vec4 spec = texture2DRect(specularRect, vary_fragcoord.xy);
  243. vec2 scol_ambocc = texture2DRect(lightMap, vary_fragcoord.xy).rg;
  244. float scol = max(scol_ambocc.r, diffuse.a);
  245. float ambocc = scol_ambocc.g;
  246. calcAtmospherics(pos.xyz, ambocc);
  247. col = atmosAmbient(vec3(0));
  248. col += atmosAffectDirectionalLight(max(min(da, scol), diffuse.a));
  249. col *= diffuse.rgb;
  250. if (spec.a > 0.0) // specular reflection
  251. {
  252. // the old infinite-sky shiny reflection
  253. //
  254. vec3 refnormpersp = normalize(reflect(pos.xyz, norm.xyz));
  255. float sa = dot(refnormpersp, sun_dir.xyz);
  256. vec3 dumbshiny = vary_SunlitColor*scol_ambocc.r*texture2D(lightFunc, vec2(sa, spec.a)).r;
  257. // add the two types of shiny together
  258. vec3 spec_contrib = dumbshiny * spec.rgb;
  259. bloom = dot(spec_contrib, spec_contrib);
  260. col += spec_contrib;
  261. //add environmentmap
  262. vec3 env_vec = env_mat * refnormpersp;
  263. col = mix(col.rgb, textureCube(environmentMap, env_vec).rgb,
  264. max(spec.a-diffuse.a*2.0, 0.0));
  265. }
  266. col = atmosLighting(col);
  267. col = scaleSoftClip(col);
  268. col = mix(col, diffuse.rgb, diffuse.a);
  269. }
  270. else
  271. {
  272. col = diffuse.rgb;
  273. }
  274. gl_FragColor.rgb = col;
  275. gl_FragColor.a = bloom;
  276. }