webgl_mirror.html 6.7 KB

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