2
0

webgl_mirror.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. } );
  65. groundMirror.position.y = 0.5;
  66. groundMirror.rotateX( - Math.PI / 2 );
  67. scene.add( groundMirror );
  68. var geometry = new THREE.PlaneBufferGeometry( 100, 100 );
  69. var verticalMirror = new Reflector( geometry, {
  70. clipBias: 0.003,
  71. textureWidth: WIDTH * window.devicePixelRatio,
  72. textureHeight: HEIGHT * window.devicePixelRatio,
  73. color: 0x889999
  74. } );
  75. verticalMirror.position.y = 50;
  76. verticalMirror.position.z = - 50;
  77. scene.add( verticalMirror );
  78. sphereGroup = new THREE.Object3D();
  79. scene.add( sphereGroup );
  80. var geometry = new THREE.CylinderBufferGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  81. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  82. var sphereCap = new THREE.Mesh( geometry, material );
  83. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  84. sphereCap.rotateX( - Math.PI );
  85. var geometry = new THREE.SphereBufferGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  86. var halfSphere = new THREE.Mesh( geometry, material );
  87. halfSphere.add( sphereCap );
  88. halfSphere.rotateX( - Math.PI / 180 * 135 );
  89. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  90. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  91. sphereGroup.add( halfSphere );
  92. var geometry = new THREE.IcosahedronBufferGeometry( 5, 0 );
  93. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x333333, flatShading: true } );
  94. smallSphere = new THREE.Mesh( geometry, material );
  95. scene.add( smallSphere );
  96. // walls
  97. var planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  98. planeTop.position.y = 100;
  99. planeTop.rotateX( Math.PI / 2 );
  100. scene.add( planeTop );
  101. var planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  102. planeBottom.rotateX( - Math.PI / 2 );
  103. scene.add( planeBottom );
  104. var planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  105. planeFront.position.z = 50;
  106. planeFront.position.y = 50;
  107. planeFront.rotateY( Math.PI );
  108. scene.add( planeFront );
  109. var planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  110. planeRight.position.x = 50;
  111. planeRight.position.y = 50;
  112. planeRight.rotateY( - Math.PI / 2 );
  113. scene.add( planeRight );
  114. var planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  115. planeLeft.position.x = - 50;
  116. planeLeft.position.y = 50;
  117. planeLeft.rotateY( Math.PI / 2 );
  118. scene.add( planeLeft );
  119. // lights
  120. var mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  121. mainLight.position.y = 60;
  122. scene.add( mainLight );
  123. var greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  124. greenLight.position.set( 550, 50, 0 );
  125. scene.add( greenLight );
  126. var redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  127. redLight.position.set( - 550, 50, 0 );
  128. scene.add( redLight );
  129. var blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  130. blueLight.position.set( 0, 50, 550 );
  131. scene.add( blueLight );
  132. }
  133. function animate() {
  134. requestAnimationFrame( animate );
  135. var timer = Date.now() * 0.01;
  136. sphereGroup.rotation.y -= 0.002;
  137. smallSphere.position.set(
  138. Math.cos( timer * 0.1 ) * 30,
  139. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  140. Math.sin( timer * 0.1 ) * 30
  141. );
  142. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  143. smallSphere.rotation.z = timer * 0.8;
  144. renderer.render( scene, camera );
  145. }
  146. </script>
  147. </body>
  148. </html>