2
0

WaterRefractionShader.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. console.warn( "THREE.WaterRefractionShader: As part of the transition to ES6 Modules, the files in 'examples/js' were deprecated in May 2020 (r117) and will be deleted in December 2020 (r124). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author Mugen87 / https://github.com/Mugen87
  4. *
  5. */
  6. THREE.WaterRefractionShader = {
  7. uniforms: {
  8. "color": {
  9. value: null
  10. },
  11. "time": {
  12. value: 0
  13. },
  14. "tDiffuse": {
  15. value: null
  16. },
  17. "tDudv": {
  18. value: null
  19. },
  20. "textureMatrix": {
  21. value: null
  22. }
  23. },
  24. vertexShader: [
  25. "uniform mat4 textureMatrix;",
  26. "varying vec2 vUv;",
  27. "varying vec4 vUvRefraction;",
  28. "void main() {",
  29. " vUv = uv;",
  30. " vUvRefraction = textureMatrix * vec4( position, 1.0 );",
  31. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  32. "}"
  33. ].join( "\n" ),
  34. fragmentShader: [
  35. "uniform vec3 color;",
  36. "uniform float time;",
  37. "uniform sampler2D tDiffuse;",
  38. "uniform sampler2D tDudv;",
  39. "varying vec2 vUv;",
  40. "varying vec4 vUvRefraction;",
  41. "float blendOverlay( float base, float blend ) {",
  42. " return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );",
  43. "}",
  44. "vec3 blendOverlay( vec3 base, vec3 blend ) {",
  45. " return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ),blendOverlay( base.b, blend.b ) );",
  46. "}",
  47. "void main() {",
  48. " float waveStrength = 0.1;",
  49. " float waveSpeed = 0.03;",
  50. // simple distortion (ripple) via dudv map (see https://www.youtube.com/watch?v=6B7IF6GOu7s)
  51. " vec2 distortedUv = texture2D( tDudv, vec2( vUv.x + time * waveSpeed, vUv.y ) ).rg * waveStrength;",
  52. " distortedUv = vUv.xy + vec2( distortedUv.x, distortedUv.y + time * waveSpeed );",
  53. " vec2 distortion = ( texture2D( tDudv, distortedUv ).rg * 2.0 - 1.0 ) * waveStrength;",
  54. // new uv coords
  55. " vec4 uv = vec4( vUvRefraction );",
  56. " uv.xy += distortion;",
  57. " vec4 base = texture2DProj( tDiffuse, uv );",
  58. " gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );",
  59. "}"
  60. ].join( "\n" )
  61. };