webgl_refraction.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. <style>
  9. body {
  10. color: #444;
  11. }
  12. a {
  13. color: #08f;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="container"></div>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener noreferrer">three.js</a> - refraction
  21. </div>
  22. <!-- Import maps polyfill -->
  23. <!-- Remove this when import maps will be widely supported -->
  24. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import { Refractor } from 'three/addons/objects/Refractor.js';
  37. import { WaterRefractionShader } from 'three/addons/shaders/WaterRefractionShader.js';
  38. THREE.ColorManagement.enabled = false; // TODO: Confirm correct color management.
  39. let camera, scene, renderer, clock;
  40. let refractor, smallSphere;
  41. init();
  42. function init() {
  43. const container = document.getElementById( 'container' );
  44. clock = new THREE.Clock();
  45. // renderer
  46. renderer = new THREE.WebGLRenderer( { antialias: true } );
  47. renderer.outputColorSpace = THREE.LinearSRGBColorSpace;
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( window.innerWidth, window.innerHeight );
  50. container.appendChild( renderer.domElement );
  51. // scene
  52. scene = new THREE.Scene();
  53. // camera
  54. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  55. camera.position.set( 0, 75, 160 );
  56. const controls = new OrbitControls( camera, renderer.domElement );
  57. controls.target.set( 0, 40, 0 );
  58. controls.maxDistance = 400;
  59. controls.minDistance = 10;
  60. controls.update();
  61. // refractor
  62. const refractorGeometry = new THREE.PlaneGeometry( 90, 90 );
  63. refractor = new Refractor( refractorGeometry, {
  64. color: 0x999999,
  65. textureWidth: 1024,
  66. textureHeight: 1024,
  67. shader: WaterRefractionShader
  68. } );
  69. refractor.position.set( 0, 50, 0 );
  70. scene.add( refractor );
  71. // load dudv map for distortion effect
  72. const dudvMap = new THREE.TextureLoader().load( 'textures/waterdudv.jpg', function () {
  73. animate();
  74. } );
  75. dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping;
  76. refractor.material.uniforms.tDudv.value = dudvMap;
  77. //
  78. const geometry = new THREE.IcosahedronGeometry( 5, 0 );
  79. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  80. smallSphere = new THREE.Mesh( geometry, material );
  81. scene.add( smallSphere );
  82. // walls
  83. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  84. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  85. planeTop.position.y = 100;
  86. planeTop.rotateX( Math.PI / 2 );
  87. scene.add( planeTop );
  88. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  89. planeBottom.rotateX( - Math.PI / 2 );
  90. scene.add( planeBottom );
  91. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  92. planeBack.position.z = - 50;
  93. planeBack.position.y = 50;
  94. scene.add( planeBack );
  95. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  96. planeRight.position.x = 50;
  97. planeRight.position.y = 50;
  98. planeRight.rotateY( - Math.PI / 2 );
  99. scene.add( planeRight );
  100. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  101. planeLeft.position.x = - 50;
  102. planeLeft.position.y = 50;
  103. planeLeft.rotateY( Math.PI / 2 );
  104. scene.add( planeLeft );
  105. // lights
  106. const mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  107. mainLight.position.y = 60;
  108. scene.add( mainLight );
  109. const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  110. greenLight.position.set( 550, 50, 0 );
  111. scene.add( greenLight );
  112. const redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  113. redLight.position.set( - 550, 50, 0 );
  114. scene.add( redLight );
  115. const blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  116. blueLight.position.set( 0, 50, 550 );
  117. scene.add( blueLight );
  118. window.addEventListener( 'resize', onWindowResize );
  119. }
  120. function onWindowResize() {
  121. camera.aspect = window.innerWidth / window.innerHeight;
  122. camera.updateProjectionMatrix();
  123. renderer.setSize( window.innerWidth, window.innerHeight );
  124. }
  125. function animate() {
  126. requestAnimationFrame( animate );
  127. const time = clock.getElapsedTime();
  128. refractor.material.uniforms.time.value = time;
  129. smallSphere.position.set(
  130. Math.cos( time ) * 30,
  131. Math.abs( Math.cos( time * 2 ) ) * 20 + 5,
  132. Math.sin( time ) * 30
  133. );
  134. smallSphere.rotation.y = ( Math.PI / 2 ) - time;
  135. smallSphere.rotation.z = time * 8;
  136. renderer.render( scene, camera );
  137. }
  138. </script>
  139. </body>
  140. </html>