webgl_mirror.html 6.7 KB

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