|
| 1 | +import { |
| 2 | + LoadingManager, |
| 3 | + Mesh, |
| 4 | + MeshBasicMaterial, |
| 5 | + PerspectiveCamera, |
| 6 | + PlaneGeometry, |
| 7 | + SRGBColorSpace, |
| 8 | + Scene, |
| 9 | + Texture, |
| 10 | + TextureLoader, |
| 11 | + WebGLRenderer |
| 12 | +} from "three"; |
| 13 | + |
| 14 | +import { |
| 15 | + ClearPass, |
| 16 | + EffectPass, |
| 17 | + GeometryPass, |
| 18 | + MixBlendFunction, |
| 19 | + NoiseEffect, |
| 20 | + RenderPipeline |
| 21 | +} from "postprocessing"; |
| 22 | + |
| 23 | +import { Pane } from "tweakpane"; |
| 24 | +import { ControlMode, SpatialControls } from "spatial-controls"; |
| 25 | +import * as Utils from "../utils/index.js"; |
| 26 | + |
| 27 | +function load(): Promise<Map<string, Texture>> { |
| 28 | + |
| 29 | + const assets = new Map<string, Texture>(); |
| 30 | + const loadingManager = new LoadingManager(); |
| 31 | + const textureLoader = new TextureLoader(loadingManager); |
| 32 | + |
| 33 | + return new Promise<Map<string, Texture>>((resolve, reject) => { |
| 34 | + |
| 35 | + loadingManager.onLoad = () => resolve(assets); |
| 36 | + loadingManager.onError = (url) => reject(new Error(`Failed to load ${url}`)); |
| 37 | + |
| 38 | + textureLoader.load(`${document.baseURI}img/textures/photos/GEDC0053.jpg`, (t) => { |
| 39 | + |
| 40 | + t.colorSpace = SRGBColorSpace; |
| 41 | + assets.set("photo", t); |
| 42 | + |
| 43 | + }); |
| 44 | + |
| 45 | + }); |
| 46 | + |
| 47 | +} |
| 48 | + |
| 49 | +window.addEventListener("load", () => void load().then((assets) => { |
| 50 | + |
| 51 | + // Renderer |
| 52 | + |
| 53 | + const renderer = new WebGLRenderer({ |
| 54 | + powerPreference: "high-performance", |
| 55 | + antialias: false, |
| 56 | + stencil: false, |
| 57 | + depth: false |
| 58 | + }); |
| 59 | + |
| 60 | + renderer.setPixelRatio(window.devicePixelRatio); |
| 61 | + renderer.debug.checkShaderErrors = Utils.isLocalhost; |
| 62 | + |
| 63 | + // Camera & Controls |
| 64 | + |
| 65 | + const camera = new PerspectiveCamera(); |
| 66 | + const controls = new SpatialControls(camera.position, camera.quaternion, renderer.domElement); |
| 67 | + const settings = controls.settings; |
| 68 | + settings.general.mode = ControlMode.THIRD_PERSON; |
| 69 | + settings.zoom.sensitivity = 0.05; |
| 70 | + settings.zoom.damping = 0.1; |
| 71 | + settings.rotation.sensitivity = 0; |
| 72 | + settings.translation.enabled = false; |
| 73 | + controls.position.set(0, 0, 1.4); |
| 74 | + |
| 75 | + // Scene & Objects |
| 76 | + |
| 77 | + const scene = new Scene(); |
| 78 | + const mesh = new Mesh( |
| 79 | + new PlaneGeometry(), |
| 80 | + new MeshBasicMaterial({ |
| 81 | + map: assets.get("photo")! |
| 82 | + }) |
| 83 | + ); |
| 84 | + |
| 85 | + mesh.scale.x = 2; |
| 86 | + scene.add(mesh); |
| 87 | + |
| 88 | + // Post Processing |
| 89 | + |
| 90 | + const effect = new NoiseEffect(); |
| 91 | + //effect.blendMode.blendFunction = new MixBlendFunction(); |
| 92 | + |
| 93 | + const pipeline = new RenderPipeline(renderer); |
| 94 | + pipeline.add( |
| 95 | + new ClearPass(), |
| 96 | + new GeometryPass(scene, camera, { samples: 4 }), |
| 97 | + new EffectPass(effect) |
| 98 | + ); |
| 99 | + |
| 100 | + // Settings |
| 101 | + |
| 102 | + const container = document.getElementById("viewport")!; |
| 103 | + const pane = new Pane({ container: container.querySelector<HTMLElement>(".tp")! }); |
| 104 | + const fpsGraph = Utils.createFPSGraph(pane); |
| 105 | + const folder = pane.addFolder({ title: "Settings" }); |
| 106 | + folder.addBinding(effect, "rgb"); |
| 107 | + folder.addBinding(effect, "premultiply"); |
| 108 | + folder.addBinding(effect, "fps", { min: 0, max: 240, step: 1 }); |
| 109 | + Utils.addBlendModeBindings(folder, effect.blendMode); |
| 110 | + |
| 111 | + // Resize Handler |
| 112 | + |
| 113 | + function onResize(): void { |
| 114 | + |
| 115 | + const width = container.clientWidth; |
| 116 | + const height = container.clientHeight; |
| 117 | + camera.aspect = width / height; |
| 118 | + camera.fov = Utils.calculateVerticalFoV(90, Math.max(camera.aspect, 16 / 9)); |
| 119 | + camera.updateProjectionMatrix(); |
| 120 | + pipeline.setSize(width, height); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + window.addEventListener("resize", onResize); |
| 125 | + onResize(); |
| 126 | + |
| 127 | + // Render Loop |
| 128 | + |
| 129 | + pipeline.compile().then(() => { |
| 130 | + |
| 131 | + container.prepend(renderer.domElement); |
| 132 | + |
| 133 | + renderer.setAnimationLoop((timestamp) => { |
| 134 | + |
| 135 | + fpsGraph.begin(); |
| 136 | + controls.update(timestamp); |
| 137 | + pipeline.render(timestamp); |
| 138 | + fpsGraph.end(); |
| 139 | + |
| 140 | + }); |
| 141 | + |
| 142 | + }).catch((e) => console.error(e)); |
| 143 | + |
| 144 | +})); |
0 commit comments