webgl_mirror.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. THREE.ColorManagement.enabled = true;
  41. init();
  42. animate();
  43. function init() {
  44. const container = document.getElementById( 'container' );
  45. // renderer
  46. renderer = new THREE.WebGLRenderer( { antialias: true } );
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. container.appendChild( renderer.domElement );
  50. // scene
  51. scene = new THREE.Scene();
  52. // camera
  53. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  54. camera.position.set( 0, 75, 160 );
  55. cameraControls = new OrbitControls( camera, renderer.domElement );
  56. cameraControls.target.set( 0, 40, 0 );
  57. cameraControls.maxDistance = 400;
  58. cameraControls.minDistance = 10;
  59. cameraControls.update();
  60. //
  61. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  62. // reflectors/mirrors
  63. let geometry, material;
  64. geometry = new THREE.CircleGeometry( 40, 64 );
  65. groundMirror = new Reflector( geometry, {
  66. clipBias: 0.003,
  67. textureWidth: window.innerWidth * window.devicePixelRatio,
  68. textureHeight: window.innerHeight * window.devicePixelRatio,
  69. color: 0xb5b5b5
  70. } );
  71. groundMirror.position.y = 0.5;
  72. groundMirror.rotateX( - Math.PI / 2 );
  73. scene.add( groundMirror );
  74. geometry = new THREE.PlaneGeometry( 100, 100 );
  75. verticalMirror = new Reflector( geometry, {
  76. clipBias: 0.003,
  77. textureWidth: window.innerWidth * window.devicePixelRatio,
  78. textureHeight: window.innerHeight * window.devicePixelRatio,
  79. color: 0xc1cbcb
  80. } );
  81. verticalMirror.position.y = 50;
  82. verticalMirror.position.z = - 50;
  83. scene.add( verticalMirror );
  84. sphereGroup = new THREE.Object3D();
  85. scene.add( sphereGroup );
  86. geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  87. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x8d8d8d } );
  88. const sphereCap = new THREE.Mesh( geometry, material );
  89. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  90. sphereCap.rotateX( - Math.PI );
  91. geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  92. const halfSphere = new THREE.Mesh( geometry, material );
  93. halfSphere.add( sphereCap );
  94. halfSphere.rotateX( - Math.PI / 180 * 135 );
  95. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  96. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  97. sphereGroup.add( halfSphere );
  98. geometry = new THREE.IcosahedronGeometry( 5, 0 );
  99. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x7b7b7b, flatShading: true } );
  100. smallSphere = new THREE.Mesh( geometry, material );
  101. scene.add( smallSphere );
  102. // walls
  103. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  104. planeTop.position.y = 100;
  105. planeTop.rotateX( Math.PI / 2 );
  106. scene.add( planeTop );
  107. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  108. planeBottom.rotateX( - Math.PI / 2 );
  109. scene.add( planeBottom );
  110. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xbbbbfe } ) );
  111. planeFront.position.z = 50;
  112. planeFront.position.y = 50;
  113. planeFront.rotateY( Math.PI );
  114. scene.add( planeFront );
  115. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  116. planeRight.position.x = 50;
  117. planeRight.position.y = 50;
  118. planeRight.rotateY( - Math.PI / 2 );
  119. scene.add( planeRight );
  120. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  121. planeLeft.position.x = - 50;
  122. planeLeft.position.y = 50;
  123. planeLeft.rotateY( Math.PI / 2 );
  124. scene.add( planeLeft );
  125. // lights
  126. const mainLight = new THREE.PointLight( 0xe7e7e7, 1.5, 250 );
  127. mainLight.position.y = 60;
  128. scene.add( mainLight );
  129. const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  130. greenLight.position.set( 550, 50, 0 );
  131. scene.add( greenLight );
  132. const redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  133. redLight.position.set( - 550, 50, 0 );
  134. scene.add( redLight );
  135. const blueLight = new THREE.PointLight( 0xbbbbfe, 0.25, 1000 );
  136. blueLight.position.set( 0, 50, 550 );
  137. scene.add( blueLight );
  138. window.addEventListener( 'resize', onWindowResize );
  139. }
  140. function onWindowResize() {
  141. camera.aspect = window.innerWidth / window.innerHeight;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. groundMirror.getRenderTarget().setSize(
  145. window.innerWidth * window.devicePixelRatio,
  146. window.innerHeight * window.devicePixelRatio
  147. );
  148. verticalMirror.getRenderTarget().setSize(
  149. window.innerWidth * window.devicePixelRatio,
  150. window.innerHeight * window.devicePixelRatio
  151. );
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. const timer = Date.now() * 0.01;
  156. sphereGroup.rotation.y -= 0.002;
  157. smallSphere.position.set(
  158. Math.cos( timer * 0.1 ) * 30,
  159. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  160. Math.sin( timer * 0.1 ) * 30
  161. );
  162. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  163. smallSphere.rotation.z = timer * 0.8;
  164. renderer.render( scene, camera );
  165. }
  166. </script>
  167. </body>
  168. </html>