webgl_mirror.html 6.1 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. <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">three.js</a> - mirror
  31. </div>
  32. <script src="../build/three.min.js"></script>
  33. <script src="js/Mirror.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, scane, renderer;
  45. var cameraControls;
  46. var verticalMirror, groundMirror;
  47. var sphereGroup, smallSphere;
  48. function init() {
  49. // renderer
  50. renderer = new THREE.WebGLRenderer();
  51. renderer.setSize( WIDTH, HEIGHT );
  52. renderer.autoClear = true;
  53. renderer.setClearColor( 0x000000, 1 );
  54. // scene
  55. scene = new THREE.Scene();
  56. // camera
  57. camera = new THREE.PerspectiveCamera(VIEW_ANGLE, ASPECT, NEAR, FAR);
  58. camera.position.set( 0, 75, 160 );
  59. cameraControls = new THREE.OrbitControls(camera, renderer.domElement);
  60. cameraControls.target.set( 0, 40, 0);
  61. cameraControls.maxDistance = 400;
  62. cameraControls.minDistance = 10;
  63. cameraControls.update();
  64. var container = document.getElementById( 'container' );
  65. container.appendChild( renderer.domElement );
  66. }
  67. function fillScene() {
  68. var planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  69. //MIRORR planes
  70. groundMirror = new THREE.Mirror( renderer, camera, { clipBias: 0.003, textureWidth: WIDTH, textureHeight: HEIGHT, color: 0x777777 } );
  71. var mirrorMesh = new THREE.Mesh( planeGeo, groundMirror.material );
  72. mirrorMesh.add( groundMirror );
  73. mirrorMesh.rotateX( - Math.PI / 2 );
  74. scene.add( mirrorMesh );
  75. verticalMirror = new THREE.Mirror( renderer, camera, { clipBias: 0.003, textureWidth: WIDTH, textureHeight: HEIGHT, color:0x889999 } );
  76. var verticalMirrorMesh = new THREE.Mesh( new THREE.PlaneGeometry( 60, 60 ), verticalMirror.material );
  77. verticalMirrorMesh.add( verticalMirror );
  78. verticalMirrorMesh.position.y = 35;
  79. verticalMirrorMesh.position.z = -45;
  80. scene.add( verticalMirrorMesh );
  81. sphereGroup = new THREE.Object3D();
  82. scene.add( sphereGroup );
  83. var geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  84. var material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x444444 } );
  85. var 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. var geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  89. var 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. var geometry = new THREE.IcosahedronGeometry( 5, 0 );
  96. var material = new THREE.MeshLambertMaterial( { color: 0xffffff, emissive: 0x333333, shading: THREE.FlatShading } );
  97. smallSphere = new THREE.Mesh( geometry, material );
  98. scene.add(smallSphere);
  99. // walls
  100. var 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. var planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  105. planeBack.position.z = -50;
  106. planeBack.position.y = 50;
  107. scene.add( planeBack );
  108. var planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  109. planeFront.position.z = 50;
  110. planeFront.position.y = 50;
  111. planeFront.rotateY( Math.PI );
  112. scene.add( planeFront );
  113. var planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  114. planeRight.position.x = 50;
  115. planeRight.position.y = 50;
  116. planeRight.rotateY( - Math.PI / 2 );
  117. scene.add( planeRight );
  118. var planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  119. planeLeft.position.x = -50;
  120. planeLeft.position.y = 50;
  121. planeLeft.rotateY( Math.PI / 2 );
  122. scene.add( planeLeft );
  123. // lights
  124. var mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  125. mainLight.position.y = 60;
  126. scene.add( mainLight );
  127. var greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  128. greenLight.position.set( 550, 50, 0 );
  129. scene.add( greenLight );
  130. var redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  131. redLight.position.set( - 550, 50, 0 );
  132. scene.add( redLight );
  133. var blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  134. blueLight.position.set( 0, 50, 550 );
  135. scene.add( blueLight );
  136. }
  137. function render() {
  138. // render (update) the mirrors
  139. groundMirror.renderWithMirror( verticalMirror );
  140. verticalMirror.renderWithMirror( groundMirror );
  141. renderer.render(scene, camera);
  142. }
  143. function update() {
  144. requestAnimationFrame( update );
  145. var timer = Date.now() * 0.01;
  146. sphereGroup.rotation.y -= 0.002;
  147. smallSphere.position.set(
  148. Math.cos( timer * 0.1 ) * 30,
  149. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  150. Math.sin( timer * 0.1 ) * 30
  151. );
  152. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  153. smallSphere.rotation.z = timer * 0.8;
  154. cameraControls.update();
  155. render();
  156. }
  157. init();
  158. fillScene();
  159. update();
  160. </script>
  161. </body>
  162. </html>