webgl_portal.html 8.7 KB

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