webgl_portal.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - portal</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> - portal
  20. </div>
  21. <script type="module">
  22. import * as THREE from '../build/three.module.js';
  23. import * as CameraUtils from './jsm/utils/CameraUtils.js';
  24. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  25. let camera, scene, renderer;
  26. let cameraControls;
  27. let smallSphereOne, smallSphereTwo;
  28. let portalCamera, leftPortal, rightPortal, leftPortalTexture, reflectedPosition,
  29. rightPortalTexture, bottomLeftCorner, bottomRightCorner, topLeftCorner;
  30. init();
  31. animate();
  32. function init() {
  33. const container = document.getElementById( 'container' );
  34. // renderer
  35. renderer = new THREE.WebGLRenderer( { antialias: true } );
  36. renderer.setPixelRatio( window.devicePixelRatio );
  37. renderer.setSize( window.innerWidth, window.innerHeight );
  38. container.appendChild( renderer.domElement );
  39. renderer.localClippingEnabled = true;
  40. // scene
  41. scene = new THREE.Scene();
  42. // camera
  43. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 5000 );
  44. camera.position.set( 0, 75, 160 );
  45. cameraControls = new OrbitControls( camera, renderer.domElement );
  46. cameraControls.target.set( 0, 40, 0 );
  47. cameraControls.maxDistance = 400;
  48. cameraControls.minDistance = 10;
  49. cameraControls.update();
  50. //
  51. const planeGeo = new THREE.PlaneGeometry( 100.1, 100.1 );
  52. // bouncing icosphere
  53. const portalPlane = new THREE.Plane( new THREE.Vector3( 0, 0, 1 ), 0.0 );
  54. const geometry = new THREE.IcosahedronGeometry( 5, 0 );
  55. const material = new THREE.MeshPhongMaterial( {
  56. color: 0xffffff, emissive: 0x333333, flatShading: true,
  57. clippingPlanes: [ portalPlane ], clipShadows: true } );
  58. smallSphereOne = new THREE.Mesh( geometry, material );
  59. scene.add( smallSphereOne );
  60. smallSphereTwo = new THREE.Mesh( geometry, material );
  61. scene.add( smallSphereTwo );
  62. // portals
  63. portalCamera = new THREE.PerspectiveCamera( 45, 1.0, 0.1, 500.0 );
  64. scene.add( portalCamera );
  65. //frustumHelper = new THREE.CameraHelper( portalCamera );
  66. //scene.add( frustumHelper );
  67. bottomLeftCorner = new THREE.Vector3();
  68. bottomRightCorner = new THREE.Vector3();
  69. topLeftCorner = new THREE.Vector3();
  70. reflectedPosition = new THREE.Vector3();
  71. leftPortalTexture = new THREE.WebGLRenderTarget( 256, 256 );
  72. leftPortal = new THREE.Mesh( planeGeo, new THREE.MeshBasicMaterial( { map: leftPortalTexture.texture } ) );
  73. leftPortal.position.x = - 30;
  74. leftPortal.position.y = 20;
  75. leftPortal.scale.set( 0.35, 0.35, 0.35 );
  76. scene.add( leftPortal );
  77. rightPortalTexture = new THREE.WebGLRenderTarget( 256, 256 );
  78. rightPortal = new THREE.Mesh( planeGeo, new THREE.MeshBasicMaterial( { map: rightPortalTexture.texture } ) );
  79. rightPortal.position.x = 30;
  80. rightPortal.position.y = 20;
  81. rightPortal.scale.set( 0.35, 0.35, 0.35 );
  82. scene.add( rightPortal );
  83. // walls
  84. const planeTop = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  85. planeTop.position.y = 100;
  86. planeTop.rotateX( Math.PI / 2 );
  87. scene.add( planeTop );
  88. const planeBottom = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xffffff } ) );
  89. planeBottom.rotateX( - Math.PI / 2 );
  90. scene.add( planeBottom );
  91. const planeFront = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x7f7fff } ) );
  92. planeFront.position.z = 50;
  93. planeFront.position.y = 50;
  94. planeFront.rotateY( Math.PI );
  95. scene.add( planeFront );
  96. const planeBack = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff7fff } ) );
  97. planeBack.position.z = - 50;
  98. planeBack.position.y = 50;
  99. //planeBack.rotateY( Math.PI );
  100. scene.add( planeBack );
  101. const planeRight = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0x00ff00 } ) );
  102. planeRight.position.x = 50;
  103. planeRight.position.y = 50;
  104. planeRight.rotateY( - Math.PI / 2 );
  105. scene.add( planeRight );
  106. const planeLeft = new THREE.Mesh( planeGeo, new THREE.MeshPhongMaterial( { color: 0xff0000 } ) );
  107. planeLeft.position.x = - 50;
  108. planeLeft.position.y = 50;
  109. planeLeft.rotateY( Math.PI / 2 );
  110. scene.add( planeLeft );
  111. // lights
  112. const mainLight = new THREE.PointLight( 0xcccccc, 1.5, 250 );
  113. mainLight.position.y = 60;
  114. scene.add( mainLight );
  115. const greenLight = new THREE.PointLight( 0x00ff00, 0.25, 1000 );
  116. greenLight.position.set( 550, 50, 0 );
  117. scene.add( greenLight );
  118. const redLight = new THREE.PointLight( 0xff0000, 0.25, 1000 );
  119. redLight.position.set( - 550, 50, 0 );
  120. scene.add( redLight );
  121. const blueLight = new THREE.PointLight( 0x7f7fff, 0.25, 1000 );
  122. blueLight.position.set( 0, 50, 550 );
  123. scene.add( blueLight );
  124. window.addEventListener( 'resize', onWindowResize );
  125. }
  126. function onWindowResize() {
  127. camera.aspect = window.innerWidth / window.innerHeight;
  128. camera.updateProjectionMatrix();
  129. renderer.setSize( window.innerWidth, window.innerHeight );
  130. }
  131. function renderPortal( thisPortalMesh, otherPortalMesh, thisPortalTexture ) {
  132. // set the portal camera position to be reflected about the portal plane
  133. thisPortalMesh.worldToLocal( reflectedPosition.copy( camera.position ) );
  134. reflectedPosition.x *= - 1.0; reflectedPosition.z *= - 1.0;
  135. otherPortalMesh.localToWorld( reflectedPosition );
  136. portalCamera.position.copy( reflectedPosition );
  137. // grab the corners of the other portal
  138. // - note: the portal is viewed backwards; flip the left/right coordinates
  139. otherPortalMesh.localToWorld( bottomLeftCorner.set( 50.05, - 50.05, 0.0 ) );
  140. otherPortalMesh.localToWorld( bottomRightCorner.set( - 50.05, - 50.05, 0.0 ) );
  141. otherPortalMesh.localToWorld( topLeftCorner.set( 50.05, 50.05, 0.0 ) );
  142. // set the projection matrix to encompass the portal's frame
  143. CameraUtils.frameCorners( portalCamera, bottomLeftCorner, bottomRightCorner, topLeftCorner, false );
  144. // render the portal
  145. thisPortalTexture.texture.encoding = renderer.outputEncoding;
  146. renderer.setRenderTarget( thisPortalTexture );
  147. renderer.state.buffers.depth.setMask( true ); // make sure the depth buffer is writable so it can be properly cleared, see #18897
  148. if ( renderer.autoClear === false ) renderer.clear();
  149. thisPortalMesh.visible = false; // hide this portal from its own rendering
  150. renderer.render( scene, portalCamera );
  151. thisPortalMesh.visible = true; // re-enable this portal's visibility for general rendering
  152. }
  153. function animate() {
  154. requestAnimationFrame( animate );
  155. // move the bouncing sphere(s)
  156. const timerOne = Date.now() * 0.01;
  157. const timerTwo = timerOne + Math.PI * 10.0;
  158. smallSphereOne.position.set(
  159. Math.cos( timerOne * 0.1 ) * 30,
  160. Math.abs( Math.cos( timerOne * 0.2 ) ) * 20 + 5,
  161. Math.sin( timerOne * 0.1 ) * 30
  162. );
  163. smallSphereOne.rotation.y = ( Math.PI / 2 ) - timerOne * 0.1;
  164. smallSphereOne.rotation.z = timerOne * 0.8;
  165. smallSphereTwo.position.set(
  166. Math.cos( timerTwo * 0.1 ) * 30,
  167. Math.abs( Math.cos( timerTwo * 0.2 ) ) * 20 + 5,
  168. Math.sin( timerTwo * 0.1 ) * 30
  169. );
  170. smallSphereTwo.rotation.y = ( Math.PI / 2 ) - timerTwo * 0.1;
  171. smallSphereTwo.rotation.z = timerTwo * 0.8;
  172. // save the original camera properties
  173. const currentRenderTarget = renderer.getRenderTarget();
  174. const currentXrEnabled = renderer.xr.enabled;
  175. const currentShadowAutoUpdate = renderer.shadowMap.autoUpdate;
  176. renderer.xr.enabled = false; // Avoid camera modification
  177. renderer.shadowMap.autoUpdate = false; // Avoid re-computing shadows
  178. // render the portal effect
  179. renderPortal( leftPortal, rightPortal, leftPortalTexture );
  180. renderPortal( rightPortal, leftPortal, rightPortalTexture );
  181. // restore the original rendering properties
  182. renderer.xr.enabled = currentXrEnabled;
  183. renderer.shadowMap.autoUpdate = currentShadowAutoUpdate;
  184. renderer.setRenderTarget( currentRenderTarget );
  185. // render the main scene
  186. renderer.render( scene, camera );
  187. }
  188. </script>
  189. </body>
  190. </html>