webgl_mirror.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. import Stats from "./jsm/libs/stats.module.js";
  26. // camera
  27. const VIEW_ANGLE = 45;
  28. let aspect = window.innerWidth / window.innerHeight;
  29. const NEAR = 1;
  30. const FAR = 500;
  31. let camera, scene, renderer;
  32. let cameraControls;
  33. let sphereGroup, smallSphere;
  34. let groundMirrorRenderTarget, verticalMirrorRenderTarget;
  35. let stats;
  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( window.innerWidth, window.innerHeight );
  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: window.innerWidth * window.devicePixelRatio,
  63. textureHeight: window.innerHeight * window.devicePixelRatio,
  64. color: 0x777777
  65. } );
  66. groundMirrorRenderTarget = groundMirror.getRenderTarget()
  67. groundMirror.position.y = 0.5;
  68. groundMirror.rotateX( - Math.PI / 2 );
  69. scene.add( groundMirror );
  70. geometry = new THREE.PlaneGeometry( 100, 100 );
  71. const verticalMirror = new Reflector( geometry, {
  72. clipBias: 0.003,
  73. textureWidth: window.innerWidth * window.devicePixelRatio,
  74. textureHeight: window.innerHeight * window.devicePixelRatio,
  75. color: 0x889999
  76. } );
  77. verticalMirrorRenderTarget = verticalMirror.getRenderTarget()
  78. verticalMirror.position.y = 50;
  79. verticalMirror.position.z = - 50;
  80. scene.add( verticalMirror );
  81. sphereGroup = new THREE.Object3D();
  82. scene.add( sphereGroup );
  83. geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  84. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  85. const sphereCap = new THREE.Mesh( geometry, material );
  86. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  87. sphereCap.rotateX( - Math.PI );
  88. geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  89. const halfSphere = new THREE.Mesh( geometry, material );
  90. halfSphere.add( sphereCap );
  91. halfSphere.rotateX( - Math.PI / 180 * 135 );
  92. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  93. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  94. sphereGroup.add( halfSphere );
  95. geometry = new THREE.IcosahedronGeometry( 5, 0 );
  96. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  97. smallSphere = new THREE.Mesh( geometry, material );
  98. scene.add( smallSphere );
  99. // walls
  100. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  101. planeTop.position.y = 100;
  102. planeTop.rotateX( Math.PI / 2 );
  103. scene.add( planeTop );
  104. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  105. planeBottom.rotateX( - Math.PI / 2 );
  106. scene.add( planeBottom );
  107. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  108. planeFront.position.z = 50;
  109. planeFront.position.y = 50;
  110. planeFront.rotateY( Math.PI );
  111. scene.add( planeFront );
  112. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  113. planeRight.position.x = 50;
  114. planeRight.position.y = 50;
  115. planeRight.rotateY( - Math.PI / 2 );
  116. scene.add( planeRight );
  117. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  118. planeLeft.position.x = - 50;
  119. planeLeft.position.y = 50;
  120. planeLeft.rotateY( Math.PI / 2 );
  121. scene.add( planeLeft );
  122. // lights
  123. const mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  124. mainLight.position.y = 60;
  125. scene.add( mainLight );
  126. const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  127. greenLight.position.set( 550, 50, 0 );
  128. scene.add( greenLight );
  129. const redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  130. redLight.position.set( - 550, 50, 0 );
  131. scene.add( redLight );
  132. const blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  133. blueLight.position.set( 0, 50, 550 );
  134. scene.add( blueLight );
  135. stats = new Stats();
  136. document.body.appendChild( stats.dom );
  137. window.addEventListener( "resize", onWindowResize, false );
  138. }
  139. function onWindowResize() {
  140. aspect = window.innerWidth / window.innerHeight;
  141. camera.aspect = aspect;
  142. camera.updateProjectionMatrix();
  143. renderer.setSize( window.innerWidth, window.innerHeight );
  144. groundMirrorRenderTarget.setSize(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio);
  145. verticalMirrorRenderTarget.setSize(window.innerWidth * window.devicePixelRatio, window.innerHeight * window.devicePixelRatio);
  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. stats.update();
  160. }
  161. </script>
  162. </body>
  163. </html>