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