-
-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathoutline.js
More file actions
257 lines (190 loc) · 7.04 KB
/
Copy pathoutline.js
File metadata and controls
257 lines (190 loc) · 7.04 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import {
AnimationMixer,
Color,
CubeTextureLoader,
GLTFLoader,
LoadingManager,
PerspectiveCamera,
Raycaster,
Scene,
SRGBColorSpace,
TextureLoader,
Vector2,
WebGLRenderer
} from "three";
import {
BlendFunction,
OutlineEffect,
OverrideMaterialManager,
EffectComposer,
EffectPass,
KernelSize,
RenderPass
} from "postprocessing";
import { Pane } from "tweakpane";
import { ControlMode, SpatialControls } from "spatial-controls";
import { calculateVerticalFoV, FPSMeter } from "../utils";
import * as Shapes from "../objects/Shapes";
function load() {
const assets = new Map();
const loadingManager = new LoadingManager();
const gltfLoader = new GLTFLoader(loadingManager);
const textureLoader = new TextureLoader(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}`));
gltfLoader.load(`${document.baseURI}models/rigged-simple/RiggedSimple.gltf`, (gltf) => {
gltf.scene.traverse((object) => {
if(object.isMesh) {
object.castShadow = object.receiveShadow = true;
}
});
assets.set("rigged-simple", gltf);
});
textureLoader.load(`${document.baseURI}img/textures/pattern.png`, (t) => {
t.colorSpace = SRGBColorSpace;
assets.set("pattern", t);
});
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(2, 2, 10);
// Scene, Lights, Objects
const scene = new Scene();
scene.background = assets.get("sky");
scene.environment = assets.get("sky");
scene.environmentIntensity = 0.33;
scene.add(Shapes.createLights());
const actors = Shapes.createActors();
scene.add(actors);
const riggedSimple = assets.get("rigged-simple");
riggedSimple.scene.scale.multiplyScalar(0.2);
actors.add(riggedSimple.scene);
const animationMixer = new AnimationMixer(riggedSimple.scene);
const action = animationMixer.clipAction(riggedSimple.animations[0]);
action.play();
const step = 2.0 * Math.PI / actors.children.length;
const radius = 3.0;
let angle = 3.5;
for(const mesh of actors.children) {
// Arrange the objects in a circle.
mesh.position.set(radius * Math.cos(angle), 0, radius * Math.sin(angle));
angle += step;
}
// Post Processing
OverrideMaterialManager.workaroundEnabled = true;
const multisampling = Math.min(4, renderer.capabilities.maxSamples);
const composer = new EffectComposer(renderer, { multisampling });
const effect = new OutlineEffect(scene, camera, {
blendFunction: BlendFunction.SCREEN,
patternScale: 40,
visibleEdgeColor: 0xffffff,
hiddenEdgeColor: 0x22090a,
resolutionScale: 0.75,
blur: false,
xRay: true,
multisampling
});
effect.selection.add(actors.children[0]);
composer.addPass(new RenderPass(scene, camera));
composer.addPass(new EffectPass(camera, effect));
// Object Picking
const ndc = new Vector2();
const raycaster = new Raycaster();
renderer.domElement.addEventListener("pointerdown", (event) => {
const clientRect = container.getBoundingClientRect();
const clientX = event.clientX - clientRect.left;
const clientY = event.clientY - clientRect.top;
ndc.x = (clientX / container.clientWidth) * 2.0 - 1.0;
ndc.y = -(clientY / container.clientHeight) * 2.0 + 1.0;
raycaster.setFromCamera(ndc, camera);
const intersects = raycaster.intersectObjects(scene.children, true);
if(intersects.length > 0) {
effect.selection.toggle(intersects[0].object);
}
});
// Settings
const fpsMeter = new FPSMeter();
const color = new Color();
const pane = new Pane({ container: container.querySelector(".tp") });
pane.addBinding(fpsMeter, "fps", { readonly: true, label: "FPS" });
const params = {
"patternTexture": false,
"multisampling": true,
"visibleEdgeColor": color.copy(effect.visibleEdgeColor).convertLinearToSRGB().getHex(),
"hiddenEdgeColor": color.copy(effect.hiddenEdgeColor).convertLinearToSRGB().getHex()
};
const folder = pane.addFolder({ title: "Settings" });
folder.addBinding(effect.resolution, "scale", { label: "resolution", min: 0.5, max: 1, step: 0.05 });
folder.addBinding(params, "multisampling")
.on("change", (e) => effect.multisampling = e.value ? multisampling : 0);
folder.addBinding(effect.blurPass, "kernelSize", { options: KernelSize });
folder.addBinding(effect.blurPass, "enabled", { label: "blur" });
folder.addBinding(params, "patternTexture")
.on("change", (e) => effect.patternTexture = (e.value ? assets.get("pattern") : null));
folder.addBinding(effect, "patternScale", { min: 20, max: 100, step: 0.1 });
folder.addBinding(effect, "edgeStrength", { min: 0, max: 10, step: 0.01 });
folder.addBinding(effect, "pulseSpeed", { min: 0, max: 2, step: 0.01 });
folder.addBinding(params, "visibleEdgeColor", { view: "color" })
.on("change", (e) => effect.visibleEdgeColor.setHex(e.value).convertSRGBToLinear());
folder.addBinding(params, "hiddenEdgeColor", { view: "color" })
.on("change", (e) => effect.hiddenEdgeColor.setHex(e.value).convertSRGBToLinear());
folder.addBinding(effect, "xRay");
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
let t0 = 0;
requestAnimationFrame(function render(timestamp) {
const deltaTime = timestamp - t0;
t0 = timestamp;
fpsMeter.update(timestamp);
controls.update(timestamp);
animationMixer.update(deltaTime * 1e-3);
composer.render();
requestAnimationFrame(render);
});
}));