PageRenderTime 218ms CodeModel.GetById 100ms app.highlight 4ms RepoModel.GetById 112ms app.codeStats 0ms

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

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