webgl_mirror.html 6.4 KB

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