PageRenderTime 18ms CodeModel.GetById 15ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/app_settings/shaders/class2/windlight/atmosphericsV.glsl

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 157 lines | 127 code | 30 blank | 0 comment | 0 complexity | 3f9bc5f2b1dd76fae798aa51db2cfec7 MD5 | raw file
  1/**
  2 * @file atmosphericsV.glsl
  3 *
  4 * $LicenseInfo:firstyear=2005&license=viewerlgpl$
  5 * Second Life Viewer Source Code
  6 * Copyright (C) 2005, 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// VARYING param funcs
 29void setSunlitColor(vec3 v);
 30void setAmblitColor(vec3 v);
 31void setAdditiveColor(vec3 v);
 32void setAtmosAttenuation(vec3 v);
 33void setPositionEye(vec3 v);
 34
 35vec3 getAdditiveColor();
 36
 37//VARYING vec4 vary_CloudUVs;
 38//VARYING float vary_CloudDensity;
 39
 40// Inputs
 41uniform vec4 morphFactor;
 42uniform vec3 camPosLocal;
 43//uniform vec4 camPosWorld;
 44
 45uniform vec4 lightnorm;
 46uniform vec4 sunlight_color;
 47uniform vec4 ambient;
 48uniform vec4 blue_horizon;
 49uniform vec4 blue_density;
 50uniform vec4 haze_horizon;
 51uniform vec4 haze_density;
 52uniform vec4 cloud_shadow;
 53uniform vec4 density_multiplier;
 54uniform vec4 distance_multiplier;
 55uniform vec4 max_y;
 56uniform vec4 glow;
 57
 58void calcAtmospherics(vec3 inPositionEye) {
 59
 60	vec3 P = inPositionEye;
 61	setPositionEye(P);
 62	
 63	//(TERRAIN) limit altitude
 64	if (P.y > max_y.x) P *= (max_y.x / P.y);
 65	if (P.y < -max_y.x) P *= (-max_y.x / P.y);
 66
 67	vec3 tmpLightnorm = lightnorm.xyz;
 68
 69	vec3 Pn = normalize(P);
 70	float  Plen = length(P);
 71
 72	vec4 temp1 = vec4(0);
 73	vec3 temp2 = vec3(0);
 74	vec4 blue_weight;
 75	vec4 haze_weight;
 76	vec4 sunlight = sunlight_color;
 77	vec4 light_atten;
 78
 79	//sunlight attenuation effect (hue and brightness) due to atmosphere
 80	//this is used later for sunlight modulation at various altitudes
 81	light_atten = (blue_density * 1.0 + vec4(haze_density.r) * 0.25) * (density_multiplier.x * max_y.x);
 82		//I had thought blue_density and haze_density should have equal weighting,
 83		//but attenuation due to haze_density tends to seem too strong
 84
 85	temp1 = blue_density + vec4(haze_density.r);
 86	blue_weight = blue_density / temp1;
 87	haze_weight = vec4(haze_density.r) / temp1;
 88
 89	//(TERRAIN) compute sunlight from lightnorm only (for short rays like terrain)
 90	temp2.y = max(0.0, tmpLightnorm.y);
 91	temp2.y = 1. / temp2.y;
 92	sunlight *= exp( - light_atten * temp2.y);
 93
 94	// main atmospheric scattering line integral
 95	temp2.z = Plen * density_multiplier.x;
 96
 97	// Transparency (-> temp1)
 98	// ATI Bugfix -- can't store temp1*temp2.z*distance_multiplier.x in a variable because the ati
 99	// compiler gets confused.
100	temp1 = exp(-temp1 * temp2.z * distance_multiplier.x);
101
102	//final atmosphere attenuation factor
103	setAtmosAttenuation(temp1.rgb);
104	//vary_AtmosAttenuation = distance_multiplier / 10000.;
105	//vary_AtmosAttenuation = density_multiplier * 100.;
106	//vary_AtmosAttenuation = vec4(Plen / 100000., 0., 0., 1.);
107
108	//compute haze glow
109	//(can use temp2.x as temp because we haven't used it yet)
110	temp2.x = dot(Pn, tmpLightnorm.xyz);
111	temp2.x = 1. - temp2.x;
112		//temp2.x is 0 at the sun and increases away from sun
113	temp2.x = max(temp2.x, .03);	//was glow.y
114		//set a minimum "angle" (smaller glow.y allows tighter, brighter hotspot)
115	temp2.x *= glow.x;
116		//higher glow.x gives dimmer glow (because next step is 1 / "angle")
117	temp2.x = pow(temp2.x, glow.z);
118		//glow.z should be negative, so we're doing a sort of (1 / "angle") function
119
120	//add "minimum anti-solar illumination"
121	temp2.x += .25;
122
123
124	//increase ambient when there are more clouds
125	vec4 tmpAmbient = ambient + (vec4(1.) - ambient) * cloud_shadow.x * 0.5;
126
127	//haze color
128	setAdditiveColor(
129		vec3(blue_horizon * blue_weight * (sunlight*(1.-cloud_shadow.x) + tmpAmbient)
130	  + (haze_horizon.r * haze_weight) * (sunlight*(1.-cloud_shadow.x) * temp2.x
131		  + tmpAmbient)));
132
133	//brightness of surface both sunlight and ambient
134	setSunlitColor(vec3(sunlight * .5));
135	setAmblitColor(vec3(tmpAmbient * .25));
136	setAdditiveColor(getAdditiveColor() * vec3(1.0 - temp1));
137
138	// vary_SunlitColor = vec3(0);
139	// vary_AmblitColor = vec3(0);
140	// vary_AdditiveColor = vec4(Pn, 1.0);
141
142	/*
143	const float cloudShadowScale = 100.;
144	// Get cloud uvs for shadowing
145	vec3 cloudPos = inPositionEye + camPosWorld - cloudShadowScale / 2.;
146	vary_CloudUVs.xy = cloudPos.xz / cloudShadowScale;
147
148	// We can take uv1 and multiply it by (TerrainSpan / CloudSpan)
149//	cloudUVs *= (((worldMaxZ - worldMinZ) * 20) /40000.);
150	vary_CloudUVs *= (10000./40000.);
151
152	// Offset by sun vector * (CloudAltitude / CloudSpan)
153	vary_CloudUVs.x += tmpLightnorm.x / tmpLightnorm.y * (3000./40000.);
154	vary_CloudUVs.y += tmpLightnorm.z / tmpLightnorm.y * (3000./40000.);
155	*/
156}
157