PageRenderTime 29ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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