webgl_mirror.html 6.6 KB

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