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

https://bitbucket.org/lindenlab/viewer-beta/ · GLSL · 163 lines · 85 code · 33 blank · 45 comment · 0 complexity · a3c2e543f075a0b5dc447656151c446b 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. #extension GL_ARB_texture_rectangle : enable
  26. #ifdef DEFINE_GL_FRAGCOLOR
  27. out vec4 gl_FragData[3];
  28. #endif
  29. vec3 scaleSoftClip(vec3 inColor);
  30. vec3 atmosTransport(vec3 inColor);
  31. uniform sampler2D bumpMap;
  32. uniform sampler2D screenTex;
  33. uniform sampler2D refTex;
  34. uniform sampler2DRectShadow shadowMap0;
  35. uniform sampler2DRectShadow shadowMap1;
  36. uniform sampler2DRectShadow shadowMap2;
  37. uniform sampler2DRectShadow shadowMap3;
  38. uniform sampler2D noiseMap;
  39. uniform mat4 shadow_matrix[6];
  40. uniform vec4 shadow_clip;
  41. uniform float sunAngle;
  42. uniform float sunAngle2;
  43. uniform vec3 lightDir;
  44. uniform vec3 specular;
  45. uniform float lightExp;
  46. uniform float refScale;
  47. uniform float kd;
  48. uniform vec2 screenRes;
  49. uniform vec3 normScale;
  50. uniform float fresnelScale;
  51. uniform float fresnelOffset;
  52. uniform float blurMultiplier;
  53. uniform vec2 screen_res;
  54. uniform mat4 norm_mat; //region space to screen space
  55. //bigWave is (refCoord.w, view.w);
  56. VARYING vec4 refCoord;
  57. VARYING vec4 littleWave;
  58. VARYING vec4 view;
  59. VARYING vec4 vary_position;
  60. void main()
  61. {
  62. vec4 color;
  63. float dist = length(view.xy);
  64. //normalize view vector
  65. vec3 viewVec = normalize(view.xyz);
  66. //get wave normals
  67. vec3 wave1 = texture2D(bumpMap, vec2(refCoord.w, view.w)).xyz*2.0-1.0;
  68. vec3 wave2 = texture2D(bumpMap, littleWave.xy).xyz*2.0-1.0;
  69. vec3 wave3 = texture2D(bumpMap, littleWave.zw).xyz*2.0-1.0;
  70. //get base fresnel components
  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. vec2 distort = (refCoord.xy/refCoord.z) * 0.5 + 0.5;
  78. float dist2 = dist;
  79. dist = max(dist, 5.0);
  80. float dmod = sqrt(dist);
  81. vec2 dmod_scale = vec2(dmod*dmod, dmod);
  82. //get reflected color
  83. vec2 refdistort1 = wave1.xy*normScale.x;
  84. vec2 refvec1 = distort+refdistort1/dmod_scale;
  85. vec4 refcol1 = texture2D(refTex, refvec1);
  86. vec2 refdistort2 = wave2.xy*normScale.y;
  87. vec2 refvec2 = distort+refdistort2/dmod_scale;
  88. vec4 refcol2 = texture2D(refTex, refvec2);
  89. vec2 refdistort3 = wave3.xy*normScale.z;
  90. vec2 refvec3 = distort+refdistort3/dmod_scale;
  91. vec4 refcol3 = texture2D(refTex, refvec3);
  92. vec4 refcol = refcol1 + refcol2 + refcol3;
  93. float df1 = df.x + df.y + df.z;
  94. refcol *= df1 * 0.333;
  95. vec3 wavef = (wave1 + wave2 * 0.4 + wave3 * 0.6) * 0.5;
  96. //wavef.z *= max(-viewVec.z, 0.1);
  97. wavef = normalize(wavef);
  98. float df2 = dot(viewVec, wavef) * fresnelScale+fresnelOffset;
  99. vec2 refdistort4 = wavef.xy*0.125;
  100. refdistort4.y -= abs(refdistort4.y);
  101. vec2 refvec4 = distort+refdistort4/dmod;
  102. float dweight = min(dist2*blurMultiplier, 1.0);
  103. vec4 baseCol = texture2D(refTex, refvec4);
  104. refcol = mix(baseCol*df2, refcol, dweight);
  105. //get specular component
  106. //float spec = clamp(dot(lightDir, (reflect(viewVec,wavef))),0.0,1.0);
  107. //harden specular
  108. //spec = pow(spec, 128.0);
  109. //figure out distortion vector (ripply)
  110. vec2 distort2 = distort+wavef.xy*refScale/max(dmod*df1, 1.0);
  111. vec4 fb = texture2D(screenTex, distort2);
  112. //mix with reflection
  113. // Note we actually want to use just df1, but multiplying by 0.999999 gets around an nvidia compiler bug
  114. color.rgb = mix(fb.rgb, refcol.rgb, df1 * 0.99999);
  115. float shadow = 1.0;
  116. vec4 pos = vary_position;
  117. //vec3 nz = texture2D(noiseMap, gl_FragCoord.xy/128.0).xyz;
  118. vec4 spos = pos;
  119. //spec *= shadow;
  120. //color.rgb += spec * specular;
  121. //color.rgb = atmosTransport(color.rgb);
  122. //color.rgb = scaleSoftClip(color.rgb);
  123. //color.a = spec * sunAngle2;
  124. //wavef.z *= 0.1f;
  125. //wavef = normalize(wavef);
  126. vec3 screenspacewavef = (norm_mat*vec4(wavef, 1.0)).xyz;
  127. gl_FragData[0] = vec4(color.rgb, 0.5); // diffuse
  128. gl_FragData[1] = vec4(0.5,0.5,0.5, 0.95); // speccolor*spec, spec
  129. gl_FragData[2] = vec4(screenspacewavef.xy*0.5+0.5, screenspacewavef.z, screenspacewavef.z*0.5); // normalxyz, displace
  130. }