|
| 1 | +import { |
| 2 | + CubeTextureLoader, |
| 3 | + FogExp2, |
| 4 | + LoadingManager, |
| 5 | + PerspectiveCamera, |
| 6 | + Scene, |
| 7 | + SRGBColorSpace, |
| 8 | + WebGLRenderer |
| 9 | +} from "three"; |
| 10 | + |
| 11 | +import { |
| 12 | + ASCIIEffect, |
| 13 | + ASCIITexture, |
| 14 | + BlendFunction, |
| 15 | + EffectComposer, |
| 16 | + EffectPass, |
| 17 | + RenderPass |
| 18 | +} from "postprocessing"; |
| 19 | + |
| 20 | +import { Pane } from "tweakpane"; |
| 21 | +import { SpatialControls } from "spatial-controls"; |
| 22 | +import { calculateVerticalFoV, FPSMeter } from "../utils"; |
| 23 | +import * as Domain from "../objects/Domain"; |
| 24 | + |
| 25 | +function load() { |
| 26 | + |
| 27 | + const assets = new Map(); |
| 28 | + const loadingManager = new LoadingManager(); |
| 29 | + const cubeTextureLoader = new CubeTextureLoader(loadingManager); |
| 30 | + |
| 31 | + const path = document.baseURI + "img/textures/skies/sunset/"; |
| 32 | + const format = ".png"; |
| 33 | + const urls = [ |
| 34 | + path + "px" + format, path + "nx" + format, |
| 35 | + path + "py" + format, path + "ny" + format, |
| 36 | + path + "pz" + format, path + "nz" + format |
| 37 | + ]; |
| 38 | + |
| 39 | + return new Promise((resolve, reject) => { |
| 40 | + |
| 41 | + loadingManager.onLoad = () => resolve(assets); |
| 42 | + loadingManager.onError = (url) => reject(new Error(`Failed to load ${url}`)); |
| 43 | + |
| 44 | + cubeTextureLoader.load(urls, (t) => { |
| 45 | + |
| 46 | + t.colorSpace = SRGBColorSpace; |
| 47 | + assets.set("sky", t); |
| 48 | + |
| 49 | + }); |
| 50 | + |
| 51 | + }); |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +window.addEventListener("load", () => load().then((assets) => { |
| 56 | + |
| 57 | + // Renderer |
| 58 | + |
| 59 | + const renderer = new WebGLRenderer({ |
| 60 | + powerPreference: "high-performance", |
| 61 | + antialias: false, |
| 62 | + stencil: false, |
| 63 | + depth: false |
| 64 | + }); |
| 65 | + |
| 66 | + renderer.debug.checkShaderErrors = (window.location.hostname === "localhost"); |
| 67 | + const container = document.querySelector(".viewport"); |
| 68 | + container.prepend(renderer.domElement); |
| 69 | + |
| 70 | + // Camera & Controls |
| 71 | + |
| 72 | + const camera = new PerspectiveCamera(); |
| 73 | + const controls = new SpatialControls(camera.position, camera.quaternion, renderer.domElement); |
| 74 | + const settings = controls.settings; |
| 75 | + settings.rotation.sensitivity = 2.2; |
| 76 | + settings.rotation.damping = 0.05; |
| 77 | + settings.translation.damping = 0.1; |
| 78 | + controls.position.set(0, 10, 1); |
| 79 | + controls.lookAt(0, 10, -1); |
| 80 | + |
| 81 | + // Scene, Lights, Objects |
| 82 | + |
| 83 | + const scene = new Scene(); |
| 84 | + scene.fog = new FogExp2(0x373134, 0.06); |
| 85 | + scene.background = assets.get("sky"); |
| 86 | + scene.add(Domain.createLights()); |
| 87 | + scene.add(Domain.createEnvironment(scene.background)); |
| 88 | + scene.add(Domain.createActors(scene.background)); |
| 89 | + |
| 90 | + // Post Processing |
| 91 | + |
| 92 | + const composer = new EffectComposer(renderer, { |
| 93 | + multisampling: Math.min(4, renderer.capabilities.maxSamples) |
| 94 | + }); |
| 95 | + |
| 96 | + const effect = new ASCIIEffect({ |
| 97 | + asciiTexture: new ASCIITexture({ |
| 98 | + characters: " .:,'-^=*+?!|0#X%WM@", |
| 99 | + font: "Arial", |
| 100 | + fontSize: 54, |
| 101 | + size: 1024, |
| 102 | + maxCharsPerRow: 16 |
| 103 | + }), |
| 104 | + cellSize: 12, |
| 105 | + inverted: false |
| 106 | + }); |
| 107 | + |
| 108 | + composer.addPass(new RenderPass(scene, camera)); |
| 109 | + composer.addPass(new EffectPass(camera, effect)); |
| 110 | + |
| 111 | + // Settings |
| 112 | + |
| 113 | + const params = { useSceneColor: true }; |
| 114 | + |
| 115 | + const fpsMeter = new FPSMeter(); |
| 116 | + const pane = new Pane({ container: container.querySelector(".tp") }); |
| 117 | + pane.addBinding(fpsMeter, "fps", { readonly: true, label: "FPS" }); |
| 118 | + |
| 119 | + const folder = pane.addFolder({ title: "Settings" }); |
| 120 | + folder.addBinding(effect, "inverted"); |
| 121 | + folder.addBinding(effect, "cellSize", { min: 2, max: 24, step: 2 }); |
| 122 | + folder.addBinding(effect, "color", { color: { type: "float" } }); |
| 123 | + folder.addBinding(params, "useSceneColor").on("change", |
| 124 | + (e) => void (effect.color = e.value ? null : effect.color.getHex())); |
| 125 | + |
| 126 | + folder.addBinding(effect.blendMode.opacity, "value", { label: "opacity", min: 0, max: 1, step: 0.01 }); |
| 127 | + folder.addBinding(effect.blendMode, "blendFunction", { options: BlendFunction }); |
| 128 | + |
| 129 | + // Resize Handler |
| 130 | + |
| 131 | + function onResize() { |
| 132 | + |
| 133 | + const width = container.clientWidth, height = container.clientHeight; |
| 134 | + camera.aspect = width / height; |
| 135 | + camera.fov = calculateVerticalFoV(90, Math.max(camera.aspect, 16 / 9)); |
| 136 | + camera.updateProjectionMatrix(); |
| 137 | + composer.setSize(width, height); |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + window.addEventListener("resize", onResize); |
| 142 | + onResize(); |
| 143 | + |
| 144 | + // Render Loop |
| 145 | + |
| 146 | + requestAnimationFrame(function render(timestamp) { |
| 147 | + |
| 148 | + fpsMeter.update(timestamp); |
| 149 | + controls.update(timestamp); |
| 150 | + composer.render(); |
| 151 | + requestAnimationFrame(render); |
| 152 | + |
| 153 | + }); |
| 154 | + |
| 155 | +})); |
0 commit comments