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

https://bitbucket.org/lindenlab/viewer-beta/ · GLSL · 263 lines · 175 code · 62 blank · 26 comment · 12 complexity · 80c871a697abe2eb3a81e6469056921b MD5 · raw file

  1. /**
  2. * @file multiSpotLightF.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 depthMap;
  30. uniform sampler2DMS normalMap;
  31. uniform sampler2DRect lightMap;
  32. uniform sampler2D noiseMap;
  33. uniform sampler2D lightFunc;
  34. uniform sampler2D projectionMap;
  35. uniform mat4 proj_mat; //screen space to light space
  36. uniform float proj_near; //near clip for projection
  37. uniform vec3 proj_p; //plane projection is emitting from (in screen space)
  38. uniform vec3 proj_n;
  39. uniform float proj_focus; //distance from plane to begin blurring
  40. uniform float proj_lod; //(number of mips in proj map)
  41. uniform float proj_range; //range between near clip and far clip plane of projection
  42. uniform float proj_ambient_lod;
  43. uniform float proj_ambiance;
  44. uniform float near_clip;
  45. uniform float far_clip;
  46. uniform vec3 proj_origin; //origin of projection to be used for angular attenuation
  47. uniform float sun_wash;
  48. uniform int proj_shadow_idx;
  49. uniform float shadow_fade;
  50. varying vec4 vary_light;
  51. varying vec4 vary_fragcoord;
  52. uniform vec2 screen_res;
  53. uniform mat4 inv_proj;
  54. vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
  55. {
  56. vec4 ret = texture2DLod(projectionMap, tc, lod);
  57. vec2 dist = tc-vec2(0.5);
  58. float det = max(1.0-lod/(proj_lod*0.5), 0.0);
  59. float d = dot(dist,dist);
  60. ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0)+det, 1.0);
  61. return ret;
  62. }
  63. vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
  64. {
  65. vec4 ret = texture2DLod(projectionMap, tc, lod);
  66. vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
  67. float det = min(lod/(proj_lod*0.5), 1.0);
  68. float d = min(dist.x, dist.y);
  69. float edge = 0.25*det;
  70. ret *= clamp(d/edge, 0.0, 1.0);
  71. return ret;
  72. }
  73. vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod)
  74. {
  75. vec4 ret = texture2DLod(projectionMap, tc, lod);
  76. vec2 dist = tc-vec2(0.5);
  77. float d = dot(dist,dist);
  78. ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0), 1.0);
  79. return ret;
  80. }
  81. vec4 getPosition(ivec2 pos_screen, int sample)
  82. {
  83. float depth = texelFetch(depthMap, pos_screen, sample).r;
  84. vec2 sc = vec2(pos_screen.xy)*2.0;
  85. sc /= screen_res;
  86. sc -= vec2(1.0,1.0);
  87. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  88. vec4 pos = inv_proj * ndc;
  89. pos /= pos.w;
  90. pos.w = 1.0;
  91. return pos;
  92. }
  93. void main()
  94. {
  95. vec4 frag = vary_fragcoord;
  96. frag.xyz /= frag.w;
  97. frag.xyz = frag.xyz*0.5+0.5;
  98. frag.xy *= screen_res;
  99. ivec2 itc = ivec2(frag.xy);
  100. vec3 fcol = vec3(0,0,0);
  101. int wght = 0;
  102. float shadow = 1.0;
  103. if (proj_shadow_idx >= 0)
  104. {
  105. vec4 shd = texture2DRect(lightMap, frag.xy);
  106. float sh[2];
  107. sh[0] = shd.b;
  108. sh[1] = shd.a;
  109. shadow = min(sh[proj_shadow_idx]+shadow_fade, 1.0);
  110. }
  111. for (int i = 0; i < samples; i++)
  112. {
  113. vec3 pos = getPosition(itc, i).xyz;
  114. vec3 lv = vary_light.xyz-pos.xyz;
  115. float dist2 = dot(lv,lv);
  116. dist2 /= vary_light.w;
  117. if (dist2 <= 1.0)
  118. {
  119. vec3 norm = texelFetch(normalMap, itc, i).xyz;
  120. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  121. norm = normalize(norm);
  122. float l_dist = -dot(lv, proj_n);
  123. vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
  124. if (proj_tc.z >= 0.0)
  125. {
  126. proj_tc.xyz /= proj_tc.w;
  127. float fa = gl_Color.a+1.0;
  128. float dist_atten = min(1.0-(dist2-1.0*(1.0-fa))/fa, 1.0);
  129. if (dist_atten > 0.0)
  130. {
  131. lv = proj_origin-pos.xyz;
  132. lv = normalize(lv);
  133. float da = dot(norm, lv);
  134. vec3 col = vec3(0,0,0);
  135. vec3 diff_tex = texelFetch(diffuseRect, itc, i).rgb;
  136. float noise = texture2D(noiseMap, frag.xy/128.0).b;
  137. if (proj_tc.z > 0.0 &&
  138. proj_tc.x < 1.0 &&
  139. proj_tc.y < 1.0 &&
  140. proj_tc.x > 0.0 &&
  141. proj_tc.y > 0.0)
  142. {
  143. float lit = 0.0;
  144. float amb_da = proj_ambiance;
  145. if (da > 0.0)
  146. {
  147. float diff = clamp((l_dist-proj_focus)/proj_range, 0.0, 1.0);
  148. float lod = diff * proj_lod;
  149. vec4 plcol = texture2DLodDiffuse(projectionMap, proj_tc.xy, lod);
  150. vec3 lcol = gl_Color.rgb * plcol.rgb * plcol.a;
  151. lit = da * dist_atten * noise;
  152. col = lcol*lit*diff_tex*shadow;
  153. amb_da += (da*0.5)*(1.0-shadow)*proj_ambiance;
  154. }
  155. //float diff = clamp((proj_range-proj_focus)/proj_range, 0.0, 1.0);
  156. vec4 amb_plcol = texture2DLodAmbient(projectionMap, proj_tc.xy, proj_lod);
  157. amb_da += (da*da*0.5+0.5)*proj_ambiance;
  158. amb_da *= dist_atten * noise;
  159. amb_da = min(amb_da, 1.0-lit);
  160. col += amb_da*gl_Color.rgb*diff_tex.rgb*amb_plcol.rgb*amb_plcol.a;
  161. }
  162. vec4 spec = texelFetch(specularRect, itc, i);
  163. if (spec.a > 0.0)
  164. {
  165. vec3 ref = reflect(normalize(pos), norm);
  166. //project from point pos in direction ref to plane proj_p, proj_n
  167. vec3 pdelta = proj_p-pos;
  168. float ds = dot(ref, proj_n);
  169. if (ds < 0.0)
  170. {
  171. vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds;
  172. vec4 stc = (proj_mat * vec4(pfinal.xyz, 1.0));
  173. if (stc.z > 0.0)
  174. {
  175. stc.xy /= stc.w;
  176. float fatten = clamp(spec.a*spec.a+spec.a*0.5, 0.25, 1.0);
  177. stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
  178. if (stc.x < 1.0 &&
  179. stc.y < 1.0 &&
  180. stc.x > 0.0 &&
  181. stc.y > 0.0)
  182. {
  183. vec4 scol = texture2DLodSpecular(projectionMap, stc.xy, proj_lod-spec.a*proj_lod);
  184. col += dist_atten*scol.rgb*gl_Color.rgb*scol.a*spec.rgb*shadow;
  185. }
  186. }
  187. }
  188. }
  189. fcol += col;
  190. wght++;
  191. }
  192. }
  193. }
  194. }
  195. if (wght <= 0)
  196. {
  197. discard;
  198. }
  199. gl_FragColor.rgb = fcol/wght;
  200. gl_FragColor.a = 0.0;
  201. }