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