webgl_mirror.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - mirror</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"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - mirror
  20. </div>
  21. <!-- Import maps polyfill -->
  22. <!-- Remove this when import maps will be widely supported -->
  23. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. import { Reflector } from 'three/addons/objects/Reflector.js';
  36. let camera, scene, renderer;
  37. let cameraControls;
  38. let sphereGroup, smallSphere;
  39. let groundMirror, verticalMirror;
  40. init();
  41. animate();
  42. function init() {
  43. const container = document.getElementById( 'container' );
  44. // renderer
  45. renderer = new THREE.WebGLRenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setSize( window.innerWidth, window.innerHeight );
  48. container.appendChild( renderer.domElement );
  49. // scene
  50. scene = new THREE.Scene();
  51. // camera
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  53. camera.position.set( 0, 75, 160 );
  54. cameraControls = new OrbitControls( camera, renderer.domElement );
  55. cameraControls.target.set( 0, 40, 0 );
  56. cameraControls.maxDistance = 400;
  57. cameraControls.minDistance = 10;
  58. cameraControls.update();
  59. //
  60. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  61. // reflectors/mirrors
  62. let geometry, material;
  63. geometry = new THREE.CircleGeometry( 40, 64 );
  64. groundMirror = new Reflector( geometry, {
  65. clipBias: 0.003,
  66. textureWidth: window.innerWidth * window.devicePixelRatio,
  67. textureHeight: window.innerHeight * window.devicePixelRatio,
  68. color: 0x777777
  69. } );
  70. groundMirror.position.y = 0.5;
  71. groundMirror.rotateX( - Math.PI / 2 );
  72. scene.add( groundMirror );
  73. geometry = new THREE.PlaneGeometry( 100, 100 );
  74. verticalMirror = new Reflector( geometry, {
  75. clipBias: 0.003,
  76. textureWidth: window.innerWidth * window.devicePixelRatio,
  77. textureHeight: window.innerHeight * window.devicePixelRatio,
  78. color: 0x889999
  79. } );
  80. verticalMirror.position.y = 50;
  81. verticalMirror.position.z = - 50;
  82. scene.add( verticalMirror );
  83. sphereGroup = new THREE.Object3D();
  84. scene.add( sphereGroup );
  85. geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  86. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  87. const sphereCap = new THREE.Mesh( geometry, material );
  88. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  89. sphereCap.rotateX( - Math.PI );
  90. geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  91. const halfSphere = new THREE.Mesh( geometry, material );
  92. halfSphere.add( sphereCap );
  93. halfSphere.rotateX( - Math.PI / 180 * 135 );
  94. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  95. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  96. sphereGroup.add( halfSphere );
  97. geometry = new THREE.IcosahedronGeometry( 5, 0 );
  98. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  99. smallSphere = new THREE.Mesh( geometry, material );
  100. scene.add( smallSphere );
  101. // walls
  102. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  103. planeTop.position.y = 100;
  104. planeTop.rotateX( Math.PI / 2 );
  105. scene.add( planeTop );
  106. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  107. planeBottom.rotateX( - Math.PI / 2 );
  108. scene.add( planeBottom );
  109. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  110. planeFront.position.z = 50;
  111. planeFront.position.y = 50;
  112. planeFront.rotateY( Math.PI );
  113. scene.add( planeFront );
  114. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  115. planeRight.position.x = 50;
  116. planeRight.position.y = 50;
  117. planeRight.rotateY( - Math.PI / 2 );
  118. scene.add( planeRight );
  119. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  120. planeLeft.position.x = - 50;
  121. planeLeft.position.y = 50;
  122. planeLeft.rotateY( Math.PI / 2 );
  123. scene.add( planeLeft );
  124. // lights
  125. const mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  126. mainLight.position.y = 60;
  127. scene.add( mainLight );
  128. const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  129. greenLight.position.set( 550, 50, 0 );
  130. scene.add( greenLight );
  131. const redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  132. redLight.position.set( - 550, 50, 0 );
  133. scene.add( redLight );
  134. const blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  135. blueLight.position.set( 0, 50, 550 );
  136. scene.add( blueLight );
  137. window.addEventListener( 'resize', onWindowResize );
  138. }
  139. function onWindowResize() {
  140. camera.aspect = window.innerWidth / window.innerHeight;
  141. camera.updateProjectionMatrix();
  142. renderer.setSize( window.innerWidth, window.innerHeight );
  143. groundMirror.getRenderTarget().setSize(
  144. window.innerWidth * window.devicePixelRatio,
  145. window.innerHeight * window.devicePixelRatio
  146. );
  147. verticalMirror.getRenderTarget().setSize(
  148. window.innerWidth * window.devicePixelRatio,
  149. window.innerHeight * window.devicePixelRatio
  150. );
  151. }
  152. function animate() {
  153. requestAnimationFrame( animate );
  154. const timer = Date.now() * 0.01;
  155. sphereGroup.rotation.y -= 0.002;
  156. smallSphere.position.set(
  157. Math.cos( timer * 0.1 ) * 30,
  158. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  159. Math.sin( timer * 0.1 ) * 30
  160. );
  161. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  162. smallSphere.rotation.z = timer * 0.8;
  163. renderer.render( scene, camera );
  164. }
  165. </script>
  166. </body>
  167. </html>