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

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

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