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