-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathfxaa.js
142 lines (105 loc) · 3.81 KB
/
fxaa.js
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
import {
CubeTextureLoader,
LoadingManager,
PerspectiveCamera,
Scene,
SRGBColorSpace,
VSMShadowMap,
WebGLRenderer
} from "three";
import {
BlendFunction,
EffectComposer,
EffectPass,
FXAAEffect,
RenderPass
} from "postprocessing";
import { Pane } from "tweakpane";
import { ControlMode, SpatialControls } from "spatial-controls";
import { calculateVerticalFoV, FPSMeter } from "../utils";
import * as CornellBox from "../objects/CornellBox";
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");
renderer.shadowMap.type = VSMShadowMap;
renderer.shadowMap.autoUpdate = false;
renderer.shadowMap.needsUpdate = true;
renderer.shadowMap.enabled = true;
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.general.mode = ControlMode.THIRD_PERSON;
settings.rotation.sensitivity = 2.2;
settings.rotation.damping = 0.05;
settings.zoom.damping = 0.1;
settings.translation.enabled = false;
controls.position.set(0, 0, 5);
// Scene, Lights, Objects
const scene = new Scene();
scene.background = assets.get("sky");
scene.add(CornellBox.createLights());
scene.add(CornellBox.createEnvironment());
scene.add(CornellBox.createActors());
// Post Processing
const effect = new FXAAEffect({ blendFunction: BlendFunction.NORMAL });
const composer = new EffectComposer(renderer);
composer.addPass(new RenderPass(scene, camera));
composer.addPass(new EffectPass(camera, effect));
// Settings
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, "samples", { min: 0, max: 24, step: 1 });
folder.addBinding(effect, "minEdgeThreshold", { min: 0.01, max: 0.3, step: 1e-4 });
folder.addBinding(effect, "maxEdgeThreshold", { min: 0.01, max: 0.3, step: 1e-4 });
folder.addBinding(effect, "subpixelQuality", { min: 0, max: 1.0, step: 1e-4 });
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);
});
}));