webgl_mirror.html 5.7 KB

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