PageRenderTime 264ms CodeModel.GetById 121ms app.highlight 2ms RepoModel.GetById 139ms app.codeStats 0ms

/indra/newview/app_settings/shaders/class1/environment/waterF.glsl

https://bitbucket.org/lindenlab/viewer-beta/
Unknown | 139 lines | 109 code | 30 blank | 0 comment | 0 complexity | 68358269a616544d1d790bf9dcb64d34 MD5 | raw file
  1/** 
  2 * @file waterF.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#ifdef DEFINE_GL_FRAGCOLOR
 27out vec4 gl_FragColor;
 28#endif
 29
 30vec3 scaleSoftClip(vec3 inColor);
 31vec3 atmosTransport(vec3 inColor);
 32
 33uniform sampler2D bumpMap;   
 34uniform sampler2D screenTex;
 35uniform sampler2D refTex;
 36
 37uniform float sunAngle;
 38uniform float sunAngle2;
 39uniform vec3 lightDir;
 40uniform vec3 specular;
 41uniform float lightExp;
 42uniform float refScale;
 43uniform float kd;
 44uniform vec2 screenRes;
 45uniform vec3 normScale;
 46uniform float fresnelScale;
 47uniform float fresnelOffset;
 48uniform float blurMultiplier;
 49
 50
 51//bigWave is (refCoord.w, view.w);
 52VARYING vec4 refCoord;
 53VARYING vec4 littleWave;
 54VARYING vec4 view;
 55
 56void main() 
 57{
 58	vec4 color;
 59	
 60	float dist = length(view.xy);
 61	
 62	//normalize view vector
 63	vec3 viewVec = normalize(view.xyz);
 64	
 65	//get wave normals
 66	vec3 wave1 = texture2D(bumpMap, vec2(refCoord.w, view.w)).xyz*2.0-1.0;
 67	vec3 wave2 = texture2D(bumpMap, littleWave.xy).xyz*2.0-1.0;
 68	vec3 wave3 = texture2D(bumpMap, littleWave.zw).xyz*2.0-1.0;
 69	//get base fresnel components	
 70	
 71	vec3 df = vec3(
 72					dot(viewVec, wave1),
 73					dot(viewVec, (wave2 + wave3) * 0.5),
 74					dot(viewVec, wave3)
 75				 ) * fresnelScale + fresnelOffset;
 76	df *= df;
 77		    
 78	vec2 distort = (refCoord.xy/refCoord.z) * 0.5 + 0.5;
 79	
 80	float dist2 = dist;
 81	dist = max(dist, 5.0);
 82	
 83	float dmod = sqrt(dist);
 84	
 85	vec2 dmod_scale = vec2(dmod*dmod, dmod);
 86	
 87	//get reflected color
 88	vec2 refdistort1 = wave1.xy*normScale.x;
 89	vec2 refvec1 = distort+refdistort1/dmod_scale;
 90	vec4 refcol1 = texture2D(refTex, refvec1);
 91	
 92	vec2 refdistort2 = wave2.xy*normScale.y;
 93	vec2 refvec2 = distort+refdistort2/dmod_scale;
 94	vec4 refcol2 = texture2D(refTex, refvec2);
 95	
 96	vec2 refdistort3 = wave3.xy*normScale.z;
 97	vec2 refvec3 = distort+refdistort3/dmod_scale;
 98	vec4 refcol3 = texture2D(refTex, refvec3);
 99
100	vec4 refcol = refcol1 + refcol2 + refcol3;
101	float df1 = df.x + df.y + df.z;
102	refcol *= df1 * 0.333;
103	
104	vec3 wavef = (wave1 + wave2 * 0.4 + wave3 * 0.6) * 0.5;
105	
106	wavef.z *= max(-viewVec.z, 0.1);
107	wavef = normalize(wavef);
108	
109	float df2 = dot(viewVec, wavef) * fresnelScale+fresnelOffset;
110	
111	vec2 refdistort4 = wavef.xy*0.125;
112	refdistort4.y -= abs(refdistort4.y);
113	vec2 refvec4 = distort+refdistort4/dmod;
114	float dweight = min(dist2*blurMultiplier, 1.0);
115	vec4 baseCol = texture2D(refTex, refvec4);
116	refcol = mix(baseCol*df2, refcol, dweight);
117
118	//get specular component
119	float spec = clamp(dot(lightDir, (reflect(viewVec,wavef))),0.0,1.0);
120		
121	//harden specular
122	spec = pow(spec, 128.0);
123
124	//figure out distortion vector (ripply)   
125	vec2 distort2 = distort+wavef.xy*refScale/max(dmod*df1, 1.0);
126		
127	vec4 fb = texture2D(screenTex, distort2);
128	
129	//mix with reflection
130	// Note we actually want to use just df1, but multiplying by 0.999999 gets around and nvidia compiler bug
131	color.rgb = mix(fb.rgb, refcol.rgb, df1 * 0.99999);
132	color.rgb += spec * specular;
133	
134	color.rgb = atmosTransport(color.rgb);
135	color.rgb = scaleSoftClip(color.rgb);
136	color.a = spec * sunAngle2;
137
138	gl_FragColor = color;
139}