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

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