webgl_refraction.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.module.js",
  26. "three/addons/": "./jsm/"
  27. }
  28. }
  29. </script>
  30. <script type="module">
  31. import * as THREE from 'three';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { Refractor } from 'three/addons/objects/Refractor.js';
  34. import { WaterRefractionShader } from 'three/addons/shaders/WaterRefractionShader.js';
  35. let camera, scene, renderer, clock;
  36. let refractor, smallSphere;
  37. init();
  38. async function init() {
  39. const container = document.getElementById( 'container' );
  40. clock = new THREE.Clock();
  41. // scene
  42. scene = new THREE.Scene();
  43. // camera
  44. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  45. camera.position.set( 0, 75, 160 );
  46. // refractor
  47. const refractorGeometry = new THREE.PlaneGeometry( 90, 90 );
  48. refractor = new Refractor( refractorGeometry, {
  49. color: 0xcbcbcb,
  50. textureWidth: 1024,
  51. textureHeight: 1024,
  52. shader: WaterRefractionShader
  53. } );
  54. refractor.position.set( 0, 50, 0 );
  55. scene.add( refractor );
  56. // load dudv map for distortion effect
  57. const loader = new THREE.TextureLoader();
  58. const dudvMap = await loader.loadAsync( 'textures/waterdudv.jpg' );
  59. dudvMap.wrapS = dudvMap.wrapT = THREE.RepeatWrapping;
  60. refractor.material.uniforms.tDudv.value = dudvMap;
  61. //
  62. const geometry = new THREE.IcosahedronGeometry( 5, 0 );
  63. const material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  64. smallSphere = new THREE.Mesh( geometry, material );
  65. scene.add( smallSphere );
  66. // walls
  67. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  68. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  69. planeTop.position.y = 100;
  70. planeTop.rotateX( Math.PI / 2 );
  71. scene.add( planeTop );
  72. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  73. planeBottom.rotateX( - Math.PI / 2 );
  74. scene.add( planeBottom );
  75. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  76. planeBack.position.z = - 50;
  77. planeBack.position.y = 50;
  78. scene.add( planeBack );
  79. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  80. planeRight.position.x = 50;
  81. planeRight.position.y = 50;
  82. planeRight.rotateY( - Math.PI / 2 );
  83. scene.add( planeRight );
  84. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  85. planeLeft.position.x = - 50;
  86. planeLeft.position.y = 50;
  87. planeLeft.rotateY( Math.PI / 2 );
  88. scene.add( planeLeft );
  89. // lights
  90. const mainLight = new THREE.PointLight( 0xe7e7e7, 2.5, 250, 0 );
  91. mainLight.position.y = 60;
  92. scene.add( mainLight );
  93. const greenLight = new THREE.PointLight( 0x00ff00, 0.5, 1000, 0 );
  94. greenLight.position.set( 550, 50, 0 );
  95. scene.add( greenLight );
  96. const redLight = new THREE.PointLight( 0xff0000, 0.5, 1000, 0 );
  97. redLight.position.set( - 550, 50, 0 );
  98. scene.add( redLight );
  99. const blueLight = new THREE.PointLight( 0xbbbbfe, 0.5, 1000, 0 );
  100. blueLight.position.set( 0, 50, 550 );
  101. scene.add( blueLight );
  102. // renderer
  103. renderer = new THREE.WebGLRenderer( { antialias: true } );
  104. renderer.setPixelRatio( window.devicePixelRatio );
  105. renderer.setSize( window.innerWidth, window.innerHeight );
  106. renderer.setAnimationLoop( animate );
  107. container.appendChild( renderer.domElement );
  108. // controls
  109. const controls = new OrbitControls( camera, renderer.domElement );
  110. controls.target.set( 0, 40, 0 );
  111. controls.maxDistance = 400;
  112. controls.minDistance = 10;
  113. controls.update();
  114. window.addEventListener( 'resize', onWindowResize );
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function animate() {
  122. const time = clock.getElapsedTime();
  123. refractor.material.uniforms.time.value = time;
  124. smallSphere.position.set(
  125. Math.cos( time ) * 30,
  126. Math.abs( Math.cos( time * 2 ) ) * 20 + 5,
  127. Math.sin( time ) * 30
  128. );
  129. smallSphere.rotation.y = ( Math.PI / 2 ) - time;
  130. smallSphere.rotation.z = time * 8;
  131. renderer.render( scene, camera );
  132. }
  133. </script>
  134. </body>
  135. </html>