PageRenderTime 40ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

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

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