webgl_mirror.html 6.3 KB

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