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