PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 247 lines | 187 code | 60 blank | 0 comment | 0 complexity | cc6c4bd2d96375e91173b3298f1f506b 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
 26#ifdef DEFINE_GL_FRAGCOLOR
 27out vec4 gl_FragColor;
 28#endif
 29
 30//class 1 -- no shadows
 31
 32#extension GL_ARB_texture_rectangle : enable
 33
 34uniform sampler2DRect diffuseRect;
 35uniform sampler2DRect specularRect;
 36uniform sampler2DRect depthMap;
 37uniform sampler2DRect normalMap;
 38uniform samplerCube environmentMap;
 39uniform sampler2D noiseMap;
 40uniform sampler2D projectionMap;
 41
 42uniform mat4 proj_mat; //screen space to light space
 43uniform float proj_near; //near clip for projection
 44uniform vec3 proj_p; //plane projection is emitting from (in screen space)
 45uniform vec3 proj_n;
 46uniform float proj_focus; //distance from plane to begin blurring
 47uniform float proj_lod;  //(number of mips in proj map)
 48uniform float proj_range; //range between near clip and far clip plane of projection
 49uniform float proj_ambient_lod;
 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
 59uniform vec3 center;
 60uniform vec3 color;
 61uniform float falloff;
 62uniform float size;
 63
 64VARYING vec4 vary_fragcoord;
 65uniform vec2 screen_res;
 66
 67uniform mat4 inv_proj;
 68
 69vec4 texture2DLodSpecular(sampler2D projectionMap, vec2 tc, float lod)
 70{
 71	vec4 ret = texture2DLod(projectionMap, tc, lod);
 72	
 73	vec2 dist = tc-vec2(0.5);
 74	
 75	float det = max(1.0-lod/(proj_lod*0.5), 0.0);
 76	
 77	float d = dot(dist,dist);
 78		
 79	ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0)+det, 1.0);
 80	
 81	return ret;
 82}
 83
 84vec4 texture2DLodDiffuse(sampler2D projectionMap, vec2 tc, float lod)
 85{
 86	vec4 ret = texture2DLod(projectionMap, tc, lod);
 87	
 88	vec2 dist = vec2(0.5) - abs(tc-vec2(0.5));
 89	
 90	float det = min(lod/(proj_lod*0.5), 1.0);
 91	
 92	float d = min(dist.x, dist.y);
 93	
 94	float edge = 0.25*det;
 95		
 96	ret *= clamp(d/edge, 0.0, 1.0);
 97	
 98	return ret;
 99}
100
101vec4 texture2DLodAmbient(sampler2D projectionMap, vec2 tc, float lod)
102{
103	vec4 ret = texture2DLod(projectionMap, tc, lod);
104	
105	vec2 dist = tc-vec2(0.5);
106	
107	float d = dot(dist,dist);
108		
109	ret *= min(clamp((0.25-d)/0.25, 0.0, 1.0), 1.0);
110	
111	return ret;
112}
113
114
115vec4 getPosition(vec2 pos_screen)
116{
117	float depth = texture2DRect(depthMap, pos_screen.xy).r;
118	vec2 sc = pos_screen.xy*2.0;
119	sc /= screen_res;
120	sc -= vec2(1.0,1.0);
121	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
122	vec4 pos = inv_proj * ndc;
123	pos /= pos.w;
124	pos.w = 1.0;
125	return pos;
126}
127
128void main() 
129{
130	vec4 frag = vary_fragcoord;
131	frag.xyz /= frag.w;
132	frag.xyz = frag.xyz*0.5+0.5;
133	frag.xy *= screen_res;
134	
135	vec3 pos = getPosition(frag.xy).xyz;
136	vec3 lv = center.xyz-pos.xyz;
137	float dist2 = dot(lv,lv);
138	dist2 /= size;
139	if (dist2 > 1.0)
140	{
141		discard;
142	}
143		
144	vec3 norm = texture2DRect(normalMap, frag.xy).xyz*2.0-1.0;
145	
146	norm = normalize(norm);
147	float l_dist = -dot(lv, proj_n);
148	
149	vec4 proj_tc = (proj_mat * vec4(pos.xyz, 1.0));
150	if (proj_tc.z < 0.0)
151	{
152		discard;
153	}
154	
155	proj_tc.xyz /= proj_tc.w;
156	
157	float fa = falloff+1.0;
158	float dist_atten = min(1.0-(dist2-1.0*(1.0-fa))/fa, 1.0);
159	if (dist_atten <= 0.0)
160	{
161		discard;
162	}
163	
164	lv = proj_origin-pos.xyz;
165	lv = normalize(lv);
166	float da = dot(norm, lv);
167		
168	vec3 col = vec3(0,0,0);
169		
170	vec3 diff_tex = texture2DRect(diffuseRect, frag.xy).rgb;
171		
172	float noise = texture2D(noiseMap, frag.xy/128.0).b;
173	if (proj_tc.z > 0.0 &&
174		proj_tc.x < 1.0 &&
175		proj_tc.y < 1.0 &&
176		proj_tc.x > 0.0 &&
177		proj_tc.y > 0.0)
178	{
179		float lit = 0.0;
180		float amb_da = proj_ambiance;
181		
182		if (da > 0.0)
183		{
184			float diff = clamp((l_dist-proj_focus)/proj_range, 0.0, 1.0);
185			float lod = diff * proj_lod;
186			
187			vec4 plcol = texture2DLodDiffuse(projectionMap, proj_tc.xy, lod);
188		
189			vec3 lcol = color.rgb * plcol.rgb * plcol.a;
190			
191			lit = da * dist_atten * noise;
192			
193			col = lcol*lit*diff_tex;
194			amb_da += (da*0.5)*proj_ambiance;
195		}
196		
197		//float diff = clamp((proj_range-proj_focus)/proj_range, 0.0, 1.0);
198		vec4 amb_plcol = texture2DLodAmbient(projectionMap, proj_tc.xy, proj_lod);
199							
200		amb_da += (da*da*0.5+0.5)*proj_ambiance;
201				
202		amb_da *= dist_atten * noise;
203			
204		amb_da = min(amb_da, 1.0-lit);
205			
206		col += amb_da*color.rgb*diff_tex.rgb*amb_plcol.rgb*amb_plcol.a;
207	}
208	
209	
210	vec4 spec = texture2DRect(specularRect, frag.xy);
211	if (spec.a > 0.0)
212	{
213		vec3 ref = reflect(normalize(pos), norm);
214		
215		//project from point pos in direction ref to plane proj_p, proj_n
216		vec3 pdelta = proj_p-pos;
217		float ds = dot(ref, proj_n);
218		
219		if (ds < 0.0)
220		{
221			vec3 pfinal = pos + ref * dot(pdelta, proj_n)/ds;
222			
223			vec4 stc = (proj_mat * vec4(pfinal.xyz, 1.0));
224
225			if (stc.z > 0.0)
226			{
227				stc.xy /= stc.w;
228
229				float fatten = clamp(spec.a*spec.a+spec.a*0.5, 0.25, 1.0);
230				
231				stc.xy = (stc.xy - vec2(0.5)) * fatten + vec2(0.5);
232								
233				if (stc.x < 1.0 &&
234					stc.y < 1.0 &&
235					stc.x > 0.0 &&
236					stc.y > 0.0)
237				{
238					vec4 scol = texture2DLodSpecular(projectionMap, stc.xy, proj_lod-spec.a*proj_lod);
239					col += dist_atten*scol.rgb*color.rgb*scol.a*spec.rgb;
240				}
241			}
242		}
243	}
244	
245	gl_FragColor.rgb = col;	
246	gl_FragColor.a = 0.0;
247}