PageRenderTime 58ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 206 lines | 166 code | 40 blank | 0 comment | 0 complexity | e7c7702d9a961c933e600b71de6400a3 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file spotLightF.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. VARYING vec4 vertex_color;
  30. uniform sampler2DRect diffuseRect;
  31. uniform sampler2DRect specularRect;
  32. uniform sampler2DRect depthMap;
  33. uniform sampler2DRect normalMap;
  34. uniform samplerCube environmentMap;
  35. uniform sampler2DRect lightMap;
  36. uniform sampler2D noiseMap;
  37. uniform sampler2D projectionMap;
  38. uniform mat4 proj_mat; //screen space to light space
  39. uniform float proj_near; //near clip for projection
  40. uniform vec3 proj_p; //plane projection is emitting from (in screen space)
  41. uniform vec3 proj_n;
  42. uniform float proj_focus; //distance from plane to begin blurring
  43. uniform float proj_lod; //(number of mips in proj map)
  44. uniform float proj_range; //range between near clip and far clip plane of projection
  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. VARYING vec4 vary_light;
  53. VARYING vec4 vary_fragcoord;
  54. uniform vec2 screen_res;
  55. uniform mat4 inv_proj;
  56. vec4 getPosition(vec2 pos_screen)
  57. {
  58. float depth = texture2DRect(depthMap, pos_screen.xy).a;
  59. vec2 sc = pos_screen.xy*2.0;
  60. sc /= screen_res;
  61. sc -= vec2(1.0,1.0);
  62. vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
  63. vec4 pos = inv_proj * ndc;
  64. pos /= pos.w;
  65. pos.w = 1.0;
  66. return pos;
  67. }
  68. void main()
  69. {
  70. vec4 frag = vary_fragcoord;
  71. frag.xyz /= frag.w;
  72. frag.xyz = frag.xyz*0.5+0.5;
  73. frag.xy *= screen_res;
  74. float shadow = 1.0;
  75. if (proj_shadow_idx >= 0)
  76. {
  77. vec4 shd = texture2DRect(lightMap, frag.xy);
  78. float sh[2];
  79. sh[0] = shd.b;
  80. sh[1] = shd.a;
  81. shadow = min(sh[proj_shadow_idx]+shadow_fade, 1.0);
  82. }
  83. vec3 pos = getPosition(frag.xy).xyz;
  84. vec3 lv = vary_light.xyz-pos.xyz;
  85. float dist2 = dot(lv,lv);
  86. dist2 /= vary_light.w;
  87. if (dist2 > 1.0)
  88. {
  89. discard;
  90. }
  91. vec3 norm = texture2DRect(normalMap, frag.xy).xyz;
  92. norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
  93. norm = normalize(norm);
  94. float l_dist = -dot(lv, proj_n);
  95. vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
  96. if (proj_tc.z < 0.0)
  97. {
  98. discard;
  99. }
  100. proj_tc.xyz /= proj_tc.w;
  101. float fa = vertex_color.a+1.0;
  102. float dist_atten = clamp(1.0-(dist2-1.0*(1.0-fa))/fa, 0.0, 1.0);
  103. lv = proj_origin-pos.xyz;
  104. lv = normalize(lv);
  105. float da = dot(norm, lv);
  106. vec3 col = vec3(0,0,0);
  107. vec3 diff_tex = texture2DRect(diffuseRect, frag.xy).rgb;
  108. float noise = texture2D(noiseMap, frag.xy/128.0).b;
  109. if (proj_tc.z > 0.0 &&
  110. proj_tc.x < 1.0 &&
  111. proj_tc.y < 1.0 &&
  112. proj_tc.x > 0.0 &&
  113. proj_tc.y > 0.0)
  114. {
  115. float lit = 0.0;
  116. if (da > 0.0)
  117. {
  118. float diff = clamp((l_dist-proj_focus)/proj_range, 0.0, 1.0);
  119. float lod = diff * proj_lod;
  120. vec4 plcol = texture2DLod(projectionMap, proj_tc.xy, lod);
  121. vec3 lcol = vertex_color.rgb * plcol.rgb * plcol.a;
  122. lit = da * dist_atten * noise;
  123. col = lcol*lit*diff_tex*shadow;
  124. }
  125. float diff = clamp((proj_range-proj_focus)/proj_range, 0.0, 1.0);
  126. float lod = diff * proj_lod;
  127. vec4 amb_plcol = texture2DLod(projectionMap, proj_tc.xy, lod);
  128. //float amb_da = mix(proj_ambiance, proj_ambiance*max(-da, 0.0), max(da, 0.0));
  129. float amb_da = proj_ambiance;
  130. if (da > 0.0)
  131. {
  132. amb_da += (da*0.5)*(1.0-shadow)*proj_ambiance;
  133. }
  134. amb_da += (da*da*0.5+0.5)*proj_ambiance;
  135. amb_da *= dist_atten * noise;
  136. amb_da = min(amb_da, 1.0-lit);
  137. col += amb_da*vertex_color.rgb*diff_tex.rgb*amb_plcol.rgb*amb_plcol.a;
  138. }
  139. vec4 spec = texture2DRect(specularRect, frag.xy);
  140. if (spec.a > 0.0)
  141. {
  142. vec3 ref = reflect(normalize(pos), norm);
  143. //project from point pos in direction ref to plane proj_p, proj_n
  144. vec3 pdelta = proj_p-pos;
  145. float ds = dot(ref, proj_n);
  146. if (ds < 0.0)
  147. {
  148. vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds;
  149. vec3 stc = (proj_mat * vec4(pfinal.xyz, 1.0)).xyz;
  150. if (stc.z > 0.0)
  151. {
  152. stc.xy /= stc.z+proj_near;
  153. if (stc.x < 1.0 &&
  154. stc.y < 1.0 &&
  155. stc.x > 0.0 &&
  156. stc.y > 0.0)
  157. {
  158. vec4 scol = texture2DLod(projectionMap, stc.xy, proj_lod-spec.a*proj_lod);
  159. col += dist_atten*scol.rgb*vertex_color.rgb*scol.a*spec.rgb*shadow;
  160. }
  161. }
  162. }
  163. }
  164. gl_FragColor.rgb = col;
  165. gl_FragColor.a = 0.0;
  166. }