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. var WIDTH = window.innerWidth;
  27. var HEIGHT = window.innerHeight;
  28. // camera
  29. var VIEW_ANGLE = 45;
  30. var ASPECT = WIDTH / HEIGHT;
  31. var NEAR = 1;
  32. var FAR = 500;
  33. var camera, scene, renderer;
  34. var cameraControls;
  35. var sphereGroup, smallSphere;
  36. init();
  37. animate();
  38. function init() {
  39. var 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. var planeGeo = new THREE.PlaneBufferGeometry( 100.1, 100.1 );
  57. // reflectors/mirrors
  58. var geometry = new THREE.CircleBufferGeometry( 40, 64 );
  59. var groundMirror = new Reflector( geometry, {
  60. clipBias: 0.003,
  61. textureWidth: WIDTH * window.devicePixelRatio,
  62. textureHeight: HEIGHT * window.devicePixelRatio,
  63. color: 0x777777,
  64. recursion: 1
  65. } );
  66. groundMirror.position.y = 0.5;
  67. groundMirror.rotateX( - Math.PI / 2 );
  68. scene.add( groundMirror );
  69. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  70. var verticalMirror = new Reflector( geometry, {
  71. clipBias: 0.003,
  72. textureWidth: WIDTH * window.devicePixelRatio,
  73. textureHeight: HEIGHT * window.devicePixelRatio,
  74. color: 0x889999,
  75. recursion: 1
  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. var geometry = new THREE.CylinderBufferGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  83. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  84. var 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. var geometry = new THREE.SphereBufferGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  88. var 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. var geometry = new THREE.IcosahedronBufferGeometry( 5, 0 );
  95. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  96. smallSphere = new THREE.Mesh( geometry, material );
  97. scene.add( smallSphere );
  98. // walls
  99. var 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. var planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  104. planeBottom.rotateX( - Math.PI / 2 );
  105. scene.add( planeBottom );
  106. var 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. var 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. var 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. var mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  123. mainLight.position.y = 60;
  124. scene.add( mainLight );
  125. var greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  126. greenLight.position.set( 550, 50, 0 );
  127. scene.add( greenLight );
  128. var redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  129. redLight.position.set( - 550, 50, 0 );
  130. scene.add( redLight );
  131. var blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  132. blueLight.position.set( 0, 50, 550 );
  133. scene.add( blueLight );
  134. }
  135. function animate() {
  136. requestAnimationFrame( animate );
  137. var timer = Date.now() * 0.01;
  138. sphereGroup.rotation.y -= 0.002;
  139. smallSphere.position.set(
  140. Math.cos( timer * 0.1 ) * 30,
  141. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  142. Math.sin( timer * 0.1 ) * 30
  143. );
  144. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  145. smallSphere.rotation.z = timer * 0.8;
  146. renderer.render( scene, camera );
  147. }
  148. </script>
  149. </body>
  150. </html>