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