webgl_refraction.html 5.3 KB

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