|
| 1 | +import * as THREE from 'three' |
| 2 | +import { Setup } from '../Setup' |
| 3 | +import { Meta } from '@storybook/html' |
| 4 | +import { EXRLoader } from 'three/examples/jsm/loaders/EXRLoader.js' |
| 5 | +import { GroundedSkybox } from 'three/examples/jsm/objects/GroundedSkybox.js' |
| 6 | +import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' |
| 7 | +import { GUI } from 'lil-gui' |
| 8 | +import { Fisheye } from '../../src/core/Fisheye.ts' |
| 9 | + |
| 10 | +export default { |
| 11 | + title: 'Portals/Fisheye', |
| 12 | +} as Meta // TODO: this should be `satisfies Meta` but commit hooks lag behind TS |
| 13 | +let gui: GUI |
| 14 | + |
| 15 | +let cylinderMeshes: THREE.Mesh[] = [] |
| 16 | +let fisheye: Fisheye |
| 17 | + |
| 18 | +const params = { |
| 19 | + fisheyeEnabled: true, |
| 20 | + zoom: 0, |
| 21 | + resolution: 896, |
| 22 | +} |
| 23 | + |
| 24 | +export const FisheyeStory = async () => { |
| 25 | + gui = new GUI({ title: 'Fisheye Story', closeFolders: true }) |
| 26 | + const { renderer, scene, camera } = Setup() |
| 27 | + renderer.shadowMap.enabled = true |
| 28 | + camera.position.set(0, 1, 2) |
| 29 | + const controls = new OrbitControls(camera, renderer.domElement) |
| 30 | + controls.target.set(0, 1, 0) |
| 31 | + controls.update() |
| 32 | + |
| 33 | + const floor = new THREE.Mesh( |
| 34 | + new THREE.PlaneGeometry(60, 60).rotateX(-Math.PI / 2), |
| 35 | + new THREE.ShadowMaterial({ opacity: 0.3 }) |
| 36 | + ) |
| 37 | + floor.receiveShadow = true |
| 38 | + scene.add(floor) |
| 39 | + |
| 40 | + const dirLight = new THREE.DirectionalLight(0xabcdef, 5) |
| 41 | + dirLight.position.set(15, 15, 15) |
| 42 | + dirLight.castShadow = true |
| 43 | + dirLight.shadow.mapSize.width = 1024 |
| 44 | + dirLight.shadow.mapSize.height = 1024 |
| 45 | + dirLight.shadow.camera.top = 15 |
| 46 | + dirLight.shadow.camera.bottom = -15 |
| 47 | + dirLight.shadow.camera.left = -15 |
| 48 | + dirLight.shadow.camera.right = 15 |
| 49 | + scene.add(dirLight) |
| 50 | + |
| 51 | + fisheye = new Fisheye({ |
| 52 | + resolution: params.resolution, |
| 53 | + zoom: params.zoom, |
| 54 | + }) |
| 55 | + |
| 56 | + addFisheyeGUI(fisheye) |
| 57 | + |
| 58 | + setupEnvironment(scene) |
| 59 | + addMeshes(scene) |
| 60 | + setupRaycaster(renderer, camera, cylinderMeshes) |
| 61 | + |
| 62 | + const onWindowResize = () => { |
| 63 | + // renderer and camera handled by Setup.ts |
| 64 | + fisheye.onResize(window.innerWidth, window.innerHeight) |
| 65 | + } |
| 66 | + window.addEventListener('resize', onWindowResize) |
| 67 | + onWindowResize() |
| 68 | + |
| 69 | + renderer.setAnimationLoop(() => { |
| 70 | + controls.update() |
| 71 | + if (params.fisheyeEnabled) { |
| 72 | + fisheye.render(renderer, scene, camera) |
| 73 | + } else { |
| 74 | + renderer.render(scene, camera) |
| 75 | + } |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +const addMeshes = (scene: THREE.Scene) => { |
| 80 | + const geo = new THREE.CylinderGeometry(0.25, 0.25, 1, 32).translate(0, 0.5, 0) |
| 81 | + |
| 82 | + const count = 20 |
| 83 | + for (let i = 0; i < count; i++) { |
| 84 | + const mesh = new THREE.Mesh( |
| 85 | + geo, |
| 86 | + new THREE.MeshStandardMaterial({ color: new THREE.Color().setHSL(i / count, 1, 0.5), roughness: 0.5 }) |
| 87 | + ) |
| 88 | + mesh.scale.y = THREE.MathUtils.randFloat(1, 5) |
| 89 | + mesh.position.set(THREE.MathUtils.randFloatSpread(15), 0, THREE.MathUtils.randFloatSpread(15)) |
| 90 | + mesh.castShadow = true |
| 91 | + mesh.receiveShadow = true |
| 92 | + scene.add(mesh) |
| 93 | + cylinderMeshes.push(mesh) // for raycasting |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Add scene.environment and groundProjected skybox |
| 99 | + */ |
| 100 | +const setupEnvironment = (scene: THREE.Scene) => { |
| 101 | + const exrLoader = new EXRLoader() |
| 102 | + |
| 103 | + // exr from polyhaven.com |
| 104 | + exrLoader.load('round_platform_1k.exr', (exrTex) => { |
| 105 | + exrTex.mapping = THREE.EquirectangularReflectionMapping |
| 106 | + scene.environment = exrTex |
| 107 | + scene.background = exrTex |
| 108 | + |
| 109 | + const groundProjection = new GroundedSkybox(exrTex, 5, 50) |
| 110 | + groundProjection.position.set(0, 5, 0) |
| 111 | + scene.add(groundProjection) |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +const addFisheyeGUI = (fisheye: Fisheye) => { |
| 116 | + const folder = gui.addFolder('Fisheye Settings') |
| 117 | + folder.add(params, 'fisheyeEnabled').name('enabled') |
| 118 | + folder |
| 119 | + .add(params, 'zoom', 0, 1, 0.01) |
| 120 | + .onChange((value: number) => { |
| 121 | + fisheye.zoom = value |
| 122 | + fisheye.onResize(window.innerWidth, window.innerHeight) |
| 123 | + }) |
| 124 | + .name('zoom') |
| 125 | + folder |
| 126 | + .add(params, 'resolution', 256, 1024, 128) |
| 127 | + .onChange((value: number) => { |
| 128 | + fisheye.resolution = value |
| 129 | + fisheye.updateResolution(value) |
| 130 | + }) |
| 131 | + .name('resolution') |
| 132 | +} |
| 133 | + |
| 134 | +const setupRaycaster = (renderer: THREE.WebGLRenderer, camera: THREE.Camera, cylinderMeshes: THREE.Mesh[]) => { |
| 135 | + const raycaster = new THREE.Raycaster() |
| 136 | + const pointer = new THREE.Vector2() |
| 137 | + let pointerDownTime = 0 |
| 138 | + |
| 139 | + renderer.domElement.addEventListener('pointerdown', () => { |
| 140 | + pointerDownTime = performance.now() |
| 141 | + }) |
| 142 | + |
| 143 | + renderer.domElement.addEventListener('pointerup', (event) => { |
| 144 | + const pointerUpTime = performance.now() |
| 145 | + const timeDiff = pointerUpTime - pointerDownTime |
| 146 | + |
| 147 | + // Only treat as click if time between down/up is short |
| 148 | + if (timeDiff < 200) { |
| 149 | + // Calculate pointer position in normalized device coordinates (-1 to +1) |
| 150 | + pointer.x = (event.offsetX / renderer.domElement.clientWidth) * 2 - 1 |
| 151 | + pointer.y = -(event.offsetY / renderer.domElement.clientHeight) * 2 + 1 |
| 152 | + |
| 153 | + if (params.fisheyeEnabled) { |
| 154 | + fisheye.computeRaycastRayDirection(raycaster, pointer) |
| 155 | + } else { |
| 156 | + raycaster.setFromCamera(pointer, camera) |
| 157 | + } |
| 158 | + |
| 159 | + const intersects = raycaster.intersectObjects(cylinderMeshes, true) |
| 160 | + if (intersects.length > 0) { |
| 161 | + // change color of first hit object |
| 162 | + const hit = intersects[0].object as THREE.Mesh |
| 163 | + hit.scale.y = THREE.MathUtils.randFloat(1, 5) |
| 164 | + const mat = hit.material as THREE.MeshStandardMaterial |
| 165 | + mat.color.setHSL(Math.random(), 1, 0.5) |
| 166 | + } |
| 167 | + } |
| 168 | + }) |
| 169 | +} |
| 170 | + |
| 171 | +FisheyeStory.storyName = 'Default' |
0 commit comments