webgl_mirror.html 5.9 KB

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