-
-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathblur.js
More file actions
178 lines (133 loc) · 4.68 KB
/
Copy pathblur.js
File metadata and controls
178 lines (133 loc) · 4.68 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import {
CubeTextureLoader,
LoadingManager,
PerspectiveCamera,
Scene,
SRGBColorSpace,
WebGLRenderer
} from "three";
import {
EffectComposer,
GaussianBlurPass,
KawaseBlurPass,
KernelSize,
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.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.environment = assets.get("sky");
scene.environmentIntensity = 0.33;
scene.add(CornellBox.createLights());
scene.add(CornellBox.createEnvironment());
scene.add(CornellBox.createActors());
// Post Processing
const composer = new EffectComposer(renderer, {
multisampling: Math.min(4, renderer.capabilities.maxSamples)
});
const gaussianBlurPass = new GaussianBlurPass({ resolutionScale: 0.5, kernelSize: 35 });
const kawaseBlurPass = new KawaseBlurPass({ resolutionScale: 0.5, kernelSize: KernelSize.MEDIUM });
gaussianBlurPass.renderToScreen = true;
kawaseBlurPass.renderToScreen = true;
kawaseBlurPass.enabled = false;
composer.addPass(new RenderPass(scene, camera));
composer.addPass(gaussianBlurPass);
composer.addPass(kawaseBlurPass);
// 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" });
const tab = folder.addTab({
pages: [
{ title: "Gaussian" },
{ title: "Kawase" }
]
});
tab.on("select", (event) => {
gaussianBlurPass.enabled = (event.index === 0);
kawaseBlurPass.enabled = (event.index === 1);
});
tab.pages[0].addBinding(gaussianBlurPass.blurMaterial, "kernelSize", {
options: {
"7x7": 7,
"15x15": 15,
"25x25": 25,
"35x35": 35,
"63x63": 63,
"127x127": 127,
"255x255": 255
}
});
tab.pages[0].addBinding(gaussianBlurPass.blurMaterial, "scale", { min: 0, max: 2, step: 0.01 });
tab.pages[0].addBinding(gaussianBlurPass.resolution, "scale", { label: "resolution", min: 0.5, max: 1, step: 0.05 });
tab.pages[0].addBinding(gaussianBlurPass, "iterations", { min: 1, max: 8, step: 1 });
tab.pages[1].addBinding(kawaseBlurPass.blurMaterial, "kernelSize", { options: KernelSize });
tab.pages[1].addBinding(kawaseBlurPass.blurMaterial, "scale", { min: 0, max: 2, step: 0.01 });
tab.pages[1].addBinding(kawaseBlurPass.resolution, "scale", { label: "resolution", min: 0.5, max: 1, step: 0.05 });
// 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);
});
}));