PageRenderTime 17ms CodeModel.GetById 9ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

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

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