webgl_refraction.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js refraction</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - refraction
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  17. import { Refractor } from './jsm/objects/Refractor.js';
  18. import { WaterRefractionShader } from './jsm/shaders/WaterRefractionShader.js';
  19. var scene, camera, clock, renderer, refractor;
  20. init();
  21. function init() {
  22. // scene
  23. scene = new THREE.Scene();
  24. // camera
  25. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  26. camera.position.set( - 10, 0, 15 );
  27. camera.lookAt( scene.position );
  28. // clock
  29. clock = new THREE.Clock();
  30. // mesh
  31. var geometry = new THREE.TorusKnotBufferGeometry( 3, 1, 256, 32 );
  32. var material = new THREE.MeshStandardMaterial( { color: 0x6083c2 } );
  33. var mesh = new THREE.Mesh( geometry, material );
  34. scene.add( mesh );
  35. // refractor
  36. var refractorGeometry = new THREE.PlaneBufferGeometry( 10, 10 );
  37. refractor = new Refractor( refractorGeometry, {
  38. color: 0x999999,
  39. textureWidth: 1024,
  40. textureHeight: 1024,
  41. shader: WaterRefractionShader
  42. } );
  43. refractor.position.set( 0, 0, 5 );
  44. scene.add( refractor );
  45. // load dudv map for distortion effect
  46. var dudvMap = new THREE.TextureLoader().load( 'textures/waterdudv.jpg', function () {
  47. animate();
  48. } );
  49. dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping;
  50. refractor.material.uniforms[ "tDudv" ].value = dudvMap;
  51. // light
  52. var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.4 );
  53. scene.add( ambientLight );
  54. var pointLight = new THREE.PointLight( 0xffffff, 0.8 );
  55. camera.add( pointLight );
  56. scene.add( camera );
  57. // renderer
  58. renderer = new THREE.WebGLRenderer( { antialias: true } );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.setClearColor( 0x20252f );
  61. renderer.setPixelRatio( window.devicePixelRatio );
  62. document.body.appendChild( renderer.domElement );
  63. //
  64. var controls = new OrbitControls( camera, renderer.domElement );
  65. //
  66. window.addEventListener( 'resize', onResize, false );
  67. }
  68. function onResize() {
  69. camera.aspect = window.innerWidth / window.innerHeight;
  70. camera.updateProjectionMatrix();
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. }
  73. function animate() {
  74. requestAnimationFrame( animate );
  75. render();
  76. }
  77. function render() {
  78. refractor.material.uniforms[ "time" ].value += clock.getDelta();
  79. renderer.render( scene, camera );
  80. }
  81. </script>
  82. </body>
  83. </html>