PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

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

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