2
0

webgl_refraction.html 5.1 KB

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