1
0

webgl_mirror.html 6.2 KB

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