|
| 1 | +import * as THREE from 'three' |
| 2 | +import GUI from 'lil-gui' |
| 3 | +import { Meta } from '@storybook/html' |
| 4 | +import { Setup } from '../Setup' |
| 5 | +import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' |
| 6 | +import { MeshPortalMaterial, meshPortalMaterialApplySDF } from '../../src/core/MeshPortalMaterial' |
| 7 | +import { EXRLoader } from 'three/examples/jsm/Addons.js' |
| 8 | + |
| 9 | +export default { |
| 10 | + title: 'Shaders/MeshPortalMaterial/sdf', |
| 11 | +} as Meta // TODO: this should be `satisfies Meta` but commit hooks lag behind TS |
| 12 | + |
| 13 | +let gui: GUI |
| 14 | +let scene: THREE.Scene, |
| 15 | + portalScene: THREE.Scene, |
| 16 | + camera: THREE.PerspectiveCamera, |
| 17 | + renderer: THREE.WebGLRenderer, |
| 18 | + portalMesh: THREE.Mesh |
| 19 | + |
| 20 | +const rendererSize = new THREE.Vector2() |
| 21 | + |
| 22 | +const portalParams = { |
| 23 | + resolution: 1024, |
| 24 | + renderTarget: new THREE.WebGLRenderTarget(), |
| 25 | +} |
| 26 | + |
| 27 | +export const MPMStory = async () => { |
| 28 | + gui = new GUI({ title: MPMStory.storyName }) |
| 29 | + |
| 30 | + renderer = new THREE.WebGLRenderer({ alpha: true, canvas, context }) |
| 31 | + renderer.toneMapping = THREE.ACESFilmicToneMapping |
| 32 | + |
| 33 | + scene = new THREE.Scene() |
| 34 | + portalScene = new THREE.Scene() |
| 35 | + |
| 36 | + camera = new THREE.PerspectiveCamera(45, 1, 1, 1000) |
| 37 | + camera.position.set(2.5, 0, 2.5) |
| 38 | + |
| 39 | + const controls = new OrbitControls(camera, renderer.domElement) |
| 40 | + controls.update() |
| 41 | + |
| 42 | + setupMainScene() |
| 43 | + setupPortalScene() |
| 44 | + |
| 45 | + const onResize = () => { |
| 46 | + // resize canvas |
| 47 | + renderer.setPixelRatio(Math.min(2, Math.max(1, window.devicePixelRatio))) |
| 48 | + renderer.setSize(root.clientWidth, root.clientHeight) |
| 49 | + camera.aspect = root.clientWidth / root.clientHeight |
| 50 | + camera.updateProjectionMatrix() |
| 51 | + |
| 52 | + // update 'rendererSize' vector |
| 53 | + renderer.getSize(rendererSize) |
| 54 | + rendererSize.multiplyScalar(renderer.getPixelRatio()) |
| 55 | + } |
| 56 | + |
| 57 | + onResize() |
| 58 | + window.addEventListener('resize', onResize) |
| 59 | + |
| 60 | + renderer.setAnimationLoop(() => { |
| 61 | + // render portal scene |
| 62 | + renderer.setRenderTarget(portalParams.renderTarget) |
| 63 | + renderer.render(portalScene, camera) |
| 64 | + renderer.setRenderTarget(null) |
| 65 | + |
| 66 | + // render main scene |
| 67 | + renderer.render(scene, camera) |
| 68 | + }) |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Setup the main/root scene |
| 73 | + */ |
| 74 | +function setupMainScene() { |
| 75 | + // in the main scene use basic lights |
| 76 | + const ambientLight = new THREE.AmbientLight() |
| 77 | + scene.add(ambientLight) |
| 78 | + |
| 79 | + const dirLight = new THREE.DirectionalLight(0xabcdef, 10) |
| 80 | + dirLight.position.set(1, 20, 1) |
| 81 | + scene.add(dirLight) |
| 82 | + |
| 83 | + scene.background = new THREE.Color().set(0xffffff * Math.random()) |
| 84 | + const geometry = new THREE.TorusKnotGeometry(0.5, 0.25, 150, 20) |
| 85 | + const material = new THREE.MeshStandardMaterial({ |
| 86 | + metalness: 0, |
| 87 | + roughness: 0.2, |
| 88 | + color: 0xffffff * Math.random(), |
| 89 | + }) |
| 90 | + const torusMesh = new THREE.Mesh(geometry, material) |
| 91 | + portalScene.add(torusMesh) |
| 92 | + torusMesh.position.z = -1 |
| 93 | + scene.add(torusMesh) |
| 94 | + |
| 95 | + // add GUI |
| 96 | + const fol = gui.addFolder('Main scene') |
| 97 | + fol.open() |
| 98 | + fol.addColor(scene, 'background') |
| 99 | + fol.addColor(material, 'color').name('torus color') |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Setup the portal and the contents in the portalScene |
| 104 | + */ |
| 105 | +function setupPortalScene() { |
| 106 | + // in the portal scene just use and hdri for lighting |
| 107 | + const exrLoader = new EXRLoader() |
| 108 | + exrLoader.load('round_platform_1k.exr', (exrTex) => { |
| 109 | + // exr from polyhaven.com |
| 110 | + exrTex.mapping = THREE.EquirectangularReflectionMapping |
| 111 | + portalScene.environment = exrTex |
| 112 | + portalScene.background = exrTex |
| 113 | + }) |
| 114 | + |
| 115 | + // setup the portal |
| 116 | + portalParams.renderTarget.setSize(portalParams.resolution, portalParams.resolution) |
| 117 | + |
| 118 | + renderer.getSize(rendererSize) |
| 119 | + rendererSize.multiplyScalar(renderer.getPixelRatio()) |
| 120 | + |
| 121 | + const portalGeometry = new THREE.CircleGeometry(1.5, 64) |
| 122 | + const portalMaterial = new MeshPortalMaterial({ |
| 123 | + map: portalParams.renderTarget.texture, |
| 124 | + resolution: rendererSize, |
| 125 | + transparent: true, |
| 126 | + blur: 0.5, |
| 127 | + }) |
| 128 | + |
| 129 | + portalMesh = new THREE.Mesh(portalGeometry, portalMaterial) |
| 130 | + meshPortalMaterialApplySDF(portalMesh, 512, renderer) |
| 131 | + scene.add(portalMesh) |
| 132 | + |
| 133 | + // add another torusKnot in the same spot as the main scene |
| 134 | + const geometry = new THREE.TorusKnotGeometry(0.5, 0.25, 150, 20) |
| 135 | + const material = new THREE.MeshStandardMaterial({ |
| 136 | + metalness: 1, |
| 137 | + roughness: 0.2, |
| 138 | + color: 0xffffff * Math.random(), |
| 139 | + }) |
| 140 | + const torusMesh = new THREE.Mesh(geometry, material) |
| 141 | + torusMesh.position.z = -1 |
| 142 | + portalScene.add(torusMesh) |
| 143 | + |
| 144 | + // add gui |
| 145 | + const fol = gui.addFolder('Portal Scene') |
| 146 | + fol.open() |
| 147 | + fol.add(portalScene, 'backgroundBlurriness', 0, 1) |
| 148 | + fol.addColor(material, 'color').name('torus color') |
| 149 | + |
| 150 | + const pFol = fol.addFolder('Portal settings') |
| 151 | + pFol.add(portalMesh.material, 'blur', 0, 2) |
| 152 | + pFol.add(portalParams, 'resolution', 128, 2048, 256).onChange(() => { |
| 153 | + portalParams.renderTarget.setSize(portalParams.resolution, portalParams.resolution) |
| 154 | + }) |
| 155 | + pFol.add(portalMesh.material, 'toneMapped') |
| 156 | + |
| 157 | + pFol.add(portalMesh.scale, 'x', 0.1, 2).name('scale X') |
| 158 | + pFol.add(portalMesh.scale, 'y', 0.1, 2).name('scale Y') |
| 159 | +} |
| 160 | + |
| 161 | +MPMStory.storyName = 'circle' |
0 commit comments