webgl_portal.html 8.7 KB

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