PageRenderTime 417ms CodeModel.GetById 191ms app.highlight 4ms RepoModel.GetById 219ms app.codeStats 0ms

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

https://bitbucket.org/lindenlab/viewer-beta/
text | 337 lines | 276 code | 61 blank | 0 comment | 0 complexity | f3492b377ff40daa2c992e859d227ee5 MD5 | raw file
  1/** 
  2 * @file softenLightMSF.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#extension GL_ARB_texture_rectangle : enable
 29#extension GL_ARB_texture_multisample : enable
 30
 31uniform sampler2DMS diffuseRect;
 32uniform sampler2DMS specularRect;
 33uniform sampler2DMS normalMap;
 34uniform sampler2DRect lightMap;
 35uniform sampler2DMS depthMap;
 36uniform sampler2D	  noiseMap;
 37uniform samplerCube environmentMap;
 38uniform sampler2D	  lightFunc;
 39uniform vec3 gi_quad;
 40
 41uniform float blur_size;
 42uniform float blur_fidelity;
 43
 44// Inputs
 45uniform vec4 morphFactor;
 46uniform vec3 camPosLocal;
 47//uniform vec4 camPosWorld;
 48uniform vec4 gamma;
 49uniform vec4 lightnorm;
 50uniform vec4 sunlight_color;
 51uniform vec4 ambient;
 52uniform vec4 blue_horizon;
 53uniform vec4 blue_density;
 54uniform vec4 haze_horizon;
 55uniform vec4 haze_density;
 56uniform vec4 cloud_shadow;
 57uniform vec4 density_multiplier;
 58uniform vec4 distance_multiplier;
 59uniform vec4 max_y;
 60uniform vec4 glow;
 61uniform float scene_light_strength;
 62uniform vec3 env_mat[3];
 63uniform vec4 shadow_clip;
 64uniform mat3 ssao_effect_mat;
 65
 66uniform mat4 inv_proj;
 67uniform vec2 screen_res;
 68
 69varying vec4 vary_light;
 70varying vec2 vary_fragcoord;
 71
 72vec3 vary_PositionEye;
 73
 74vec3 vary_SunlitColor;
 75vec3 vary_AmblitColor;
 76vec3 vary_AdditiveColor;
 77vec3 vary_AtmosAttenuation;
 78
 79vec4 getPosition_d(vec2 pos_screen, float depth)
 80{
 81	vec2 sc = pos_screen.xy*2.0;
 82	sc /= screen_res;
 83	sc -= vec2(1.0,1.0);
 84	vec4 ndc = vec4(sc.x, sc.y, 2.0*depth-1.0, 1.0);
 85	vec4 pos = inv_proj * ndc;
 86	pos /= pos.w;
 87	pos.w = 1.0;
 88	return pos;
 89}
 90
 91vec3 getPositionEye()
 92{
 93	return vary_PositionEye;
 94}
 95vec3 getSunlitColor()
 96{
 97	return vary_SunlitColor;
 98}
 99vec3 getAmblitColor()
100{
101	return vary_AmblitColor;
102}
103vec3 getAdditiveColor()
104{
105	return vary_AdditiveColor;
106}
107vec3 getAtmosAttenuation()
108{
109	return vary_AtmosAttenuation;
110}
111
112
113void setPositionEye(vec3 v)
114{
115	vary_PositionEye = v;
116}
117
118void setSunlitColor(vec3 v)
119{
120	vary_SunlitColor = v;
121}
122
123void setAmblitColor(vec3 v)
124{
125	vary_AmblitColor = v;
126}
127
128void setAdditiveColor(vec3 v)
129{
130	vary_AdditiveColor = v;
131}
132
133void setAtmosAttenuation(vec3 v)
134{
135	vary_AtmosAttenuation = v;
136}
137
138void calcAtmospherics(vec3 inPositionEye, float ambFactor) {
139
140	vec3 P = inPositionEye;
141	setPositionEye(P);
142	
143	//(TERRAIN) limit altitude
144	if (P.y > max_y.x) P *= (max_y.x / P.y);
145	if (P.y < -max_y.x) P *= (-max_y.x / P.y);
146
147	vec3 tmpLightnorm = lightnorm.xyz;
148
149	vec3 Pn = normalize(P);
150	float Plen = length(P);
151
152	vec4 temp1 = vec4(0);
153	vec3 temp2 = vec3(0);
154	vec4 blue_weight;
155	vec4 haze_weight;
156	vec4 sunlight = sunlight_color;
157	vec4 light_atten;
158
159	//sunlight attenuation effect (hue and brightness) due to atmosphere
160	//this is used later for sunlight modulation at various altitudes
161	light_atten = (blue_density * 1.0 + vec4(haze_density.r) * 0.25) * (density_multiplier.x * max_y.x);
162		//I had thought blue_density and haze_density should have equal weighting,
163		//but attenuation due to haze_density tends to seem too strong
164
165	temp1 = blue_density + vec4(haze_density.r);
166	blue_weight = blue_density / temp1;
167	haze_weight = vec4(haze_density.r) / temp1;
168
169	//(TERRAIN) compute sunlight from lightnorm only (for short rays like terrain)
170	temp2.y = max(0.0, tmpLightnorm.y);
171	temp2.y = 1. / temp2.y;
172	sunlight *= exp( - light_atten * temp2.y);
173
174	// main atmospheric scattering line integral
175	temp2.z = Plen * density_multiplier.x;
176
177	// Transparency (-> temp1)
178	// ATI Bugfix -- can't store temp1*temp2.z*distance_multiplier.x in a variable because the ati
179	// compiler gets confused.
180	temp1 = exp(-temp1 * temp2.z * distance_multiplier.x);
181
182	//final atmosphere attenuation factor
183	setAtmosAttenuation(temp1.rgb);
184	
185	//compute haze glow
186	//(can use temp2.x as temp because we haven't used it yet)
187	temp2.x = dot(Pn, tmpLightnorm.xyz);
188	temp2.x = 1. - temp2.x;
189		//temp2.x is 0 at the sun and increases away from sun
190	temp2.x = max(temp2.x, .03);	//was glow.y
191		//set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
192	temp2.x *= glow.x;
193		//higher glow.x gives dimmer glow (because next step is 1 / "angle")
194	temp2.x = pow(temp2.x, glow.z);
195		//glow.z should be negative, so we're doing a sort of (1 / "angle") function
196
197	//add "minimum anti-solar illumination"
198	temp2.x += .25;
199	
200	//increase ambient when there are more clouds
201	vec4 tmpAmbient = ambient + (vec4(1.) - ambient) * cloud_shadow.x * 0.5;
202	
203	/*  decrease value and saturation (that in HSV, not HSL) for occluded areas
204	 * // for HSV color/geometry used here, see http://gimp-savvy.com/BOOK/index.html?node52.html
205	 * // The following line of code performs the equivalent of:
206	 * float ambAlpha = tmpAmbient.a;
207	 * float ambValue = dot(vec3(tmpAmbient), vec3(0.577)); // projection onto <1/rt(3), 1/rt(3), 1/rt(3)>, the neutral white-black axis
208	 * vec3 ambHueSat = vec3(tmpAmbient) - vec3(ambValue);
209	 * tmpAmbient = vec4(RenderSSAOEffect.valueFactor * vec3(ambValue) + RenderSSAOEffect.saturationFactor *(1.0 - ambFactor) * ambHueSat, ambAlpha);
210	 */
211	tmpAmbient = vec4(mix(ssao_effect_mat * tmpAmbient.rgb, tmpAmbient.rgb, ambFactor), tmpAmbient.a);
212
213	//haze color
214	setAdditiveColor(
215		vec3(blue_horizon * blue_weight * (sunlight*(1.-cloud_shadow.x) + tmpAmbient)
216	  + (haze_horizon.r * haze_weight) * (sunlight*(1.-cloud_shadow.x) * temp2.x
217		  + tmpAmbient)));
218
219	//brightness of surface both sunlight and ambient
220	setSunlitColor(vec3(sunlight * .5));
221	setAmblitColor(vec3(tmpAmbient * .25));
222	setAdditiveColor(getAdditiveColor() * vec3(1.0 - temp1));
223}
224
225vec3 atmosLighting(vec3 light)
226{
227	light *= getAtmosAttenuation().r;
228	light += getAdditiveColor();
229	return (2.0 * light);
230}
231
232vec3 atmosTransport(vec3 light) {
233	light *= getAtmosAttenuation().r;
234	light += getAdditiveColor() * 2.0;
235	return light;
236}
237vec3 atmosGetDiffuseSunlightColor()
238{
239	return getSunlitColor();
240}
241
242vec3 scaleDownLight(vec3 light)
243{
244	return (light / scene_light_strength );
245}
246
247vec3 scaleUpLight(vec3 light)
248{
249	return (light * scene_light_strength);
250}
251
252vec3 atmosAmbient(vec3 light)
253{
254	return getAmblitColor() + light / 2.0;
255}
256
257vec3 atmosAffectDirectionalLight(float lightIntensity)
258{
259	return getSunlitColor() * lightIntensity;
260}
261
262vec3 scaleSoftClip(vec3 light)
263{
264	//soft clip effect:
265	light = 1. - clamp(light, vec3(0.), vec3(1.));
266	light = 1. - pow(light, gamma.xxx);
267
268	return light;
269}
270
271void main() 
272{
273	vec2 tc = vary_fragcoord.xy;
274	ivec2 itc = ivec2(tc);
275
276	vec4 fcol = vec4(0,0,0,0);
277
278	vec2 scol_ambocc = texture2DRect(lightMap, tc).rg;
279	float ambocc = scol_ambocc.g;
280
281	for (int i = 0; i < samples; ++i)
282	{
283		float depth = texelFetch(depthMap, itc.xy, i).r;
284		vec3 pos = getPosition_d(tc, depth).xyz;
285		vec3 norm = texelFetch(normalMap, itc, i).xyz;
286		norm = vec3((norm.xy-0.5)*2.0,norm.z); // unpack norm
287			
288		float da = max(dot(norm.xyz, vary_light.xyz), 0.0);
289	
290		vec4 diffuse = texelFetch(diffuseRect, itc, i);
291		vec3 col;
292		float bloom = 0.0;
293		if (diffuse.a < 0.9)
294		{
295			vec4 spec = texelFetch(specularRect, itc, i);
296	
297			float amb = 0;
298
299			float scol = max(scol_ambocc.r, diffuse.a); 
300			amb += ambocc;
301
302			calcAtmospherics(pos.xyz, ambocc);
303	
304			col = atmosAmbient(vec3(0));
305			col += atmosAffectDirectionalLight(max(min(da, scol), diffuse.a));
306	
307			col *= diffuse.rgb;
308	
309			if (spec.a > 0.0) // specular reflection
310			{
311				// the old infinite-sky shiny reflection
312				//
313				vec3 refnormpersp = normalize(reflect(pos.xyz, norm.xyz));
314				float sa = dot(refnormpersp, vary_light.xyz);
315				vec3 dumbshiny = vary_SunlitColor*scol_ambocc.r*texture2D(lightFunc, vec2(sa, spec.a)).a;
316
317				// add the two types of shiny together
318				vec3 spec_contrib = dumbshiny * spec.rgb;
319				bloom = dot(spec_contrib, spec_contrib);
320				col += spec_contrib;
321			}
322	
323			col = atmosLighting(col);
324			col = scaleSoftClip(col);
325
326			col = mix(col, diffuse.rgb, diffuse.a);
327		}
328		else
329		{
330			col = diffuse.rgb;
331		}
332
333		fcol += vec4(col, bloom);
334	}
335		
336	gl_FragColor = fcol/samples; 
337}