PageRenderTime 25ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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

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