webgl_mirror.html 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. <meta property="og:title" content="three.js webgl - mirror">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_mirror.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_mirror.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. body {
  14. color: #444;
  15. }
  16. a {
  17. color: #08f;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div id="container"></div>
  23. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - mirror
  24. </div>
  25. <script type="importmap">
  26. {
  27. "imports": {
  28. "three": "../build/three.module.js",
  29. "three/addons/": "./jsm/"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { Reflector } from 'three/addons/objects/Reflector.js';
  38. let camera, scene, renderer;
  39. let cameraControls;
  40. let sphereGroup, smallSphere;
  41. let groundMirror, verticalMirror;
  42. let resolutionScale = 1; // render target scale factor in [ 0, 1 ]
  43. const size = new THREE.Vector2();
  44. init();
  45. function init() {
  46. const container = document.getElementById( 'container' );
  47. // renderer
  48. renderer = new THREE.WebGLRenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. renderer.setAnimationLoop( animate );
  52. container.appendChild( renderer.domElement );
  53. // scene
  54. scene = new THREE.Scene();
  55. // camera
  56. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  57. camera.position.set( 0, 75, 160 );
  58. cameraControls = new OrbitControls( camera, renderer.domElement );
  59. cameraControls.target.set( 0, 40, 0 );
  60. cameraControls.maxDistance = 400;
  61. cameraControls.minDistance = 10;
  62. cameraControls.update();
  63. //
  64. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  65. // reflectors/mirrors
  66. let geometry, material;
  67. renderer.getDrawingBufferSize( size );
  68. size.multiplyScalar( resolutionScale ).round();
  69. geometry = new THREE.CircleGeometry( 40, 64 );
  70. groundMirror = new Reflector( geometry, {
  71. clipBias: 0.003,
  72. textureWidth: size.width,
  73. textureHeight: size.height,
  74. color: 0xb5b5b5
  75. } );
  76. groundMirror.position.y = 0.5;
  77. groundMirror.rotateX( - Math.PI / 2 );
  78. scene.add( groundMirror );
  79. geometry = new THREE.PlaneGeometry( 100, 100 );
  80. verticalMirror = new Reflector( geometry, {
  81. clipBias: 0.003,
  82. textureWidth: size.width,
  83. textureHeight: size.height,
  84. color: 0xc1cbcb
  85. } );
  86. verticalMirror.position.y = 50;
  87. verticalMirror.position.z = - 50;
  88. scene.add( verticalMirror );
  89. sphereGroup = new THREE.Object3D();
  90. scene.add( sphereGroup );
  91. geometry = new THREE.CylinderGeometry( 0.1, 15 * Math.cos( Math.PI / 180 * 30 ), 0.1, 24, 1 );
  92. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x8d8d8d } );
  93. const sphereCap = new THREE.Mesh( geometry, material );
  94. sphereCap.position.y = - 15 * Math.sin( Math.PI / 180 * 30 ) - 0.05;
  95. sphereCap.rotateX( - Math.PI );
  96. geometry = new THREE.SphereGeometry( 15, 24, 24, Math.PI / 2, Math.PI * 2, 0, Math.PI / 180 * 120 );
  97. const halfSphere = new THREE.Mesh( geometry, material );
  98. halfSphere.add( sphereCap );
  99. halfSphere.rotateX( - Math.PI / 180 * 135 );
  100. halfSphere.rotateZ( - Math.PI / 180 * 20 );
  101. halfSphere.position.y = 7.5 + 15 * Math.sin( Math.PI / 180 * 30 );
  102. sphereGroup.add( halfSphere );
  103. geometry = new THREE.IcosahedronGeometry( 5, 0 );
  104. material = new THREE.MeshPhongMaterial( { color: 0xffffff, emissive: 0x7b7b7b, flatShading: true } );
  105. smallSphere = new THREE.Mesh( geometry, material );
  106. scene.add( smallSphere );
  107. // walls
  108. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  109. planeTop.position.y = 100;
  110. planeTop.rotateX( Math.PI / 2 );
  111. scene.add( planeTop );
  112. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  113. planeBottom.rotateX( - Math.PI / 2 );
  114. scene.add( planeBottom );
  115. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  116. planeFront.position.z = 50;
  117. planeFront.position.y = 50;
  118. planeFront.rotateY( Math.PI );
  119. scene.add( planeFront );
  120. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  121. planeRight.position.x = 50;
  122. planeRight.position.y = 50;
  123. planeRight.rotateY( - Math.PI / 2 );
  124. scene.add( planeRight );
  125. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  126. planeLeft.position.x = - 50;
  127. planeLeft.position.y = 50;
  128. planeLeft.rotateY( Math.PI / 2 );
  129. scene.add( planeLeft );
  130. // lights
  131. const mainLight = new THREE.PointLight( 0xe7e7e7, 2.5, 250, 0 );
  132. mainLight.position.y = 60;
  133. scene.add( mainLight );
  134. const greenLight = new THREE.PointLight( 0x00ff00, 0.5, 1000, 0 );
  135. greenLight.position.set( 550, 50, 0 );
  136. scene.add( greenLight );
  137. const redLight = new THREE.PointLight( 0xff0000, 0.5, 1000, 0 );
  138. redLight.position.set( - 550, 50, 0 );
  139. scene.add( redLight );
  140. const blueLight = new THREE.PointLight( 0xbbbbfe, 0.5, 1000, 0 );
  141. blueLight.position.set( 0, 50, 550 );
  142. scene.add( blueLight );
  143. // GUI
  144. const params = {
  145. resolution: resolutionScale,
  146. };
  147. const gui = new GUI();
  148. const folder = gui.addFolder( 'Mirrors' );
  149. folder.add( params, 'resolution', 0.2, 1, 0.1 )
  150. .onChange( function ( val ) {
  151. resolutionScale = val;
  152. onWindowResize();
  153. } );
  154. folder.open();
  155. window.addEventListener( 'resize', onWindowResize );
  156. }
  157. function onWindowResize() {
  158. camera.aspect = window.innerWidth / window.innerHeight;
  159. camera.updateProjectionMatrix();
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. renderer.getDrawingBufferSize( size );
  162. size.multiplyScalar( resolutionScale ).round();
  163. groundMirror.getRenderTarget().setSize( size.width, size.height );
  164. verticalMirror.getRenderTarget().setSize( size.width, size.height );
  165. }
  166. function animate() {
  167. const timer = Date.now() * 0.01;
  168. sphereGroup.rotation.y -= 0.002;
  169. smallSphere.position.set(
  170. Math.cos( timer * 0.1 ) * 30,
  171. Math.abs( Math.cos( timer * 0.2 ) ) * 20 + 5,
  172. Math.sin( timer * 0.1 ) * 30
  173. );
  174. smallSphere.rotation.y = ( Math.PI / 2 ) - timer * 0.1;
  175. smallSphere.rotation.z = timer * 0.8;
  176. renderer.render( scene, camera );
  177. }
  178. </script>
  179. </body>
  180. </html>
粤ICP备19079148号