WaterRefractionShader.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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/#manual/en/introduction/Installation." );
  2. THREE.WaterRefractionShader = {
  3. uniforms: {
  4. "color": {
  5. value: null
  6. },
  7. "time": {
  8. value: 0
  9. },
  10. "tDiffuse": {
  11. value: null
  12. },
  13. "tDudv": {
  14. value: null
  15. },
  16. "textureMatrix": {
  17. value: null
  18. }
  19. },
  20. vertexShader: [
  21. "uniform mat4 textureMatrix;",
  22. "varying vec2 vUv;",
  23. "varying vec4 vUvRefraction;",
  24. "void main() {",
  25. " vUv = uv;",
  26. " vUvRefraction = textureMatrix * vec4( position, 1.0 );",
  27. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  28. "}"
  29. ].join( "\n" ),
  30. fragmentShader: [
  31. "uniform vec3 color;",
  32. "uniform float time;",
  33. "uniform sampler2D tDiffuse;",
  34. "uniform sampler2D tDudv;",
  35. "varying vec2 vUv;",
  36. "varying vec4 vUvRefraction;",
  37. "float blendOverlay( float base, float blend ) {",
  38. " return( base < 0.5 ? ( 2.0 * base * blend ) : ( 1.0 - 2.0 * ( 1.0 - base ) * ( 1.0 - blend ) ) );",
  39. "}",
  40. "vec3 blendOverlay( vec3 base, vec3 blend ) {",
  41. " return vec3( blendOverlay( base.r, blend.r ), blendOverlay( base.g, blend.g ),blendOverlay( base.b, blend.b ) );",
  42. "}",
  43. "void main() {",
  44. " float waveStrength = 0.1;",
  45. " float waveSpeed = 0.03;",
  46. // simple distortion (ripple) via dudv map (see https://www.youtube.com/watch?v=6B7IF6GOu7s)
  47. " vec2 distortedUv = texture2D( tDudv, vec2( vUv.x + time * waveSpeed, vUv.y ) ).rg * waveStrength;",
  48. " distortedUv = vUv.xy + vec2( distortedUv.x, distortedUv.y + time * waveSpeed );",
  49. " vec2 distortion = ( texture2D( tDudv, distortedUv ).rg * 2.0 - 1.0 ) * waveStrength;",
  50. // new uv coords
  51. " vec4 uv = vec4( vUvRefraction );",
  52. " uv.xy += distortion;",
  53. " vec4 base = texture2DProj( tDiffuse, uv );",
  54. " gl_FragColor = vec4( blendOverlay( base.rgb, color ), 1.0 );",
  55. "}"
  56. ].join( "\n" )
  57. };