-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathangles.js
435 lines (355 loc) · 12 KB
/
angles.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
import * as THREE from "three";
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
import { RGBELoader } from "three/addons/loaders/RGBELoader.js";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";
import { DRACOLoader } from "three/addons/loaders/DRACOLoader.js"; // for compressed version
import { RenderPass } from "three/examples/jsm/postprocessing/RenderPass.js";
import { EffectComposer } from "three/examples/jsm/postprocessing/EffectComposer.js";
import { UnrealBloomPass } from "three/examples/jsm/postprocessing/UnrealBloomPass.js";
import { OutputPass } from "three/examples/jsm/postprocessing/OutputPass.js";
import { ShaderPass } from "three/examples/jsm/postprocessing/ShaderPass.js";
import { SMAAPass } from "three/examples/jsm/Addons.js";
import gsap from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import { load } from "./loading";
import { cursorAnim } from "./interactions.js";
// if your element is not appearing more than once then pass it in rectangular brackets as array
cursorAnim(document.querySelector(".cursor"), [
document.querySelectorAll("a"),
[document.querySelector(".hamburger")],
[document.querySelector(".close")],
document.querySelectorAll(".aa"),
document.querySelectorAll(".cards"),
]);
// making variable to store model
let Model;
// creating scene
const scene = new THREE.Scene();
let debugObject = {};
// creating debug UI
// creating lights
const ambientLight = new THREE.AmbientLight(0xffffff, 0);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.32);
directionalLight.position.set(0, 2.64, 0);
// adding intensity B
directionalLight.intensity = 0.82;
directionalLight.castShadow = true;
// removing shadow acne
directionalLight.shadow.normalBias = 0.05;
// adjusting shadow map size
directionalLight.shadow.mapSize.width = 1024;
directionalLight.shadow.mapSize.height = 1024;
const directionalLight2 = new THREE.DirectionalLight(0xffffff);
directionalLight2.position.set(5, 5, 1.38);
// adding intensity B
directionalLight2.intensity = 3;
directionalLight2.castShadow = true;
directionalLight2.shadow.normalBias = 0.05;
// adjusting shadow map size
directionalLight2.shadow.mapSize.width = 1024;
directionalLight2.shadow.mapSize.height = 1024;
// all helpers are updated in animate loop functions
scene.add(ambientLight, directionalLight, directionalLight2);
// adding loading manager
const loadingManager = new THREE.LoadingManager();
loadingManager.onProgress = (url, objectLoaded, totalObject) => {
load(Math.floor((objectLoaded / totalObject) * 100));
};
// adding environment map
// using RGBELoader
const rgbeLoader = new RGBELoader(loadingManager);
rgbeLoader.load("./assets/enviroment/darkhdri.hdr", (environmentMap) => {
const updateAllMaterials = () => {
scene.traverse((child) => {
if (
child instanceof THREE.Mesh &&
(child.material instanceof THREE.MeshStandardMaterial ||
child.material instanceof THREE.MeshPhysicalMaterial)
) {
child.material.envMap = environmentMap;
child.material.envMapIntensity = debugObject.envMapIntensity;
// enabling shadow of object
child.castShadow = true;
child.receiveShadow = true;
}
});
};
environmentMap.mapping = THREE.EquirectangularReflectionMapping;
scene.environment = environmentMap;
// scene.background = environmentMap;
debugObject.envMapIntensity = 1;
debugObject.metalness = 0;
debugObject.roughness = 0;
debugObject.reflectivity = 0;
environmentMap.encoding = THREE.sRGBEncoding;
// adding mesh
const dracoLoader = new DRACOLoader(loadingManager);
dracoLoader.setDecoderPath("./assets/libraries/draco/");
const gltfLoader = new GLTFLoader(loadingManager);
gltfLoader.setDRACOLoader(dracoLoader);
gltfLoader.load("./assets/models/draco/gt3r/gt3r.gltf", (gltf) => {
Model = gltf.scene;
scene.add(Model);
updateAllMaterials();
const applyBloomToPart = (partName) => {
const part = Model.getObjectByName(partName);
if (part) {
part.layers.enable(BLOOM_SCENE);
}
};
applyBloomToPart("Tail_light");
applyBloomToPart("tail_glass_mirror");
applyBloomToPart("tail_light_side_light");
applyBloomToPart("ring");
applyBloomToPart("head_light");
applyBloomToPart("head_light_right");
applyBloomToPart("head_light_left");
applyBloomToPart("Headlight_Mirror");
applyBloomToPart("Headlight_Mirror_right");
applyBloomToPart("Headlight_Mirror_left");
});
// setting up sizes for rendering
const sizes = {
width: window.innerWidth,
height: window.innerHeight,
};
// adding camera
const camera = new THREE.PerspectiveCamera(45, sizes.width / sizes.height);
// adding position B
camera.position.set(
-0.014562269457166203,
1.3209874023720694,
-6.004923732399126
);
if (sizes.width > 850) {
camera.zoom = 2;
} else {
camera.zoom = 1;
}
scene.add(camera);
gsap.registerPlugin(ScrollTrigger);
// Define camera positions
const cameraPositions = [
// giving positions of camera as object
{ x: -0.014, y: 1.32, z: -6.0 },
{ x: 2.7023127197580825, y: 1.7049162056063678, z: 5.02001983140576 },
{ x: 0.0001710000006823394, y: 12.326639, z: 0.0003240000012928536 },
{ x: 2.1209853924759434, y: 0.9604913289220418, z: 4.310617568395401 },
{ x: -5.550212999743161, y: 1.4869090011130943, z: -2.740565000144729 },
];
const controlsTarget = [
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(0, 1, 0),
new THREE.Vector3(
-0.4175308165148711,
0.957633291500015,
0.7474954119541828
),
];
// for model rotation
const modelRotations = [
{ x: 0, y: 0, z: 0 },
{ x: 0, y: 0, z: 0 },
{ x: 0, y: Math.PI + 0.47, z: 0 },
{ x: 0, y: Math.PI * 2 + 0.46, z: 0 },
{ x: 0, y: Math.PI * 2 + 0.46, z: 0 },
];
// Function to update the camera position
const updateCamera = (index) => {
const position = cameraPositions[index];
const rotation = modelRotations[index];
const controlTarget = controlsTarget[index];
if (position && rotation) {
const timeline = gsap.timeline({
onUpdate: () => {
camera.updateProjectionMatrix();
controls.update();
},
});
timeline.to(
camera.position,
{
x: position.x,
y: position.y,
z: position.z,
duration: 2,
ease: "power2.inOut",
immediateRender: true, // Ensure immediate rendering
},
0
);
if (Model) {
timeline.to(
Model.rotation,
{
x: rotation.x,
y: rotation.y,
z: rotation.z,
duration: 2,
ease: "power2.inOut",
immediateRender: true, // Ensure immediate rendering
},
0
);
}
timeline.to(
controls.target,
{
x: controlTarget.x,
y: controlTarget.y,
z: controlTarget.z,
duration: 2,
ease: "power2.inOut",
immediateRender: true, // Ensure immediate rendering
},
0
);
}
};
const context = gsap.context(()=>{
cameraPositions.forEach((obj, index) => {
ScrollTrigger.create({
trigger: `.page-${index + 1}`,
start: "top center",
end: "bottom center",
scrub: true,
onEnter: () => updateCamera(index),
onEnterBack: () => updateCamera(index),
refreshPriority: index, // Ensure the correct order of ScrollTrigger instances
invalidateOnRefresh: true, // Recalculate positions when resizing
});
});
})
// Initialize ScrollTrigger
// defining canvas
let canvas = document.querySelector(".webgl");
// adding orbit controls
const controls = new OrbitControls(camera, canvas);
controls.enableDamping = true;
// controls.enabled = false;
controls.target.y = scene.position.y + 1;
// adding renderer
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
// multi sampling for avoiding stair-like effect when zoom
// you can do antialias only here no other option
antialias: true,
alpha: true,
});
// setting renderer size
renderer.setSize(sizes.width, sizes.height);
// setting pixel ratio for better quality
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
// enabling shadows
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// enabling physically correct lights rather than arbitrary lights
renderer.physicallyCorrectLights = true;
// changing output encoding
renderer.outputEncoding = THREE.sRGBEncoding;
// changing renderer tone mapping
renderer.toneMapping = THREE.ACESFilmicToneMapping;
// creating bloom
const renderScene = new RenderPass(scene, camera);
const composer = new EffectComposer(renderer);
// adding renderpass to composer
composer.addPass(renderScene);
// adding Bloom to composer
const bloomPass = new UnrealBloomPass(
new THREE.Vector2(sizes.width, sizes.height),
0.21,
0.9,
0
);
composer.addPass(bloomPass);
composer.renderToScreen = false;
// adding shader pass
const mixPass = new ShaderPass(
new THREE.ShaderMaterial({
uniforms: {
baseTexture: { value: null },
bloomTexture: { value: composer.renderTarget2.texture },
},
vertexShader: document.getElementById("vertexshader").textContent,
fragmentShader: document.getElementById("fragmentshader").textContent,
}),
"baseTexture"
);
// final composer
const finalComposer = new EffectComposer(renderer);
finalComposer.addPass(renderScene);
finalComposer.addPass(mixPass);
// adding outputpass
const outputPass = new OutputPass();
finalComposer.addPass(outputPass);
// adding antializing
const smaaPass = new SMAAPass();
// composer.addPass(smaaPass);
finalComposer.addPass(smaaPass);
// adding bloom to selected part
const BLOOM_SCENE = 1;
const bloomLayer = new THREE.Layers();
bloomLayer.set(BLOOM_SCENE);
const darkMaterial = new THREE.MeshBasicMaterial({ color: 0x000000 });
const materials = {};
function nonBloomed(obj) {
if (obj.isMesh) {
if (obj.layers === undefined) {
console.warn("Object without layers:", obj);
obj.layers = new THREE.Layers();
}
if (bloomLayer.test(obj.layers) === false) {
materials[obj.uuid] = obj.material;
obj.material = darkMaterial;
}
}
}
function restoreMaterial(obj) {
if (materials[obj.uuid]) {
obj.material = materials[obj.uuid];
delete materials[obj.uuid];
}
}
// Traverse scene and assign default layers
scene.traverse((obj) => {
if (obj.layers === undefined) {
obj.layers = new THREE.Layers();
}
});
window.addEventListener("resize", () => {
sizes.width = window.innerWidth;
sizes.height = window.innerHeight;
renderer.setSize(sizes.width, sizes.height);
camera.aspect = sizes.width / sizes.height;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
composer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
finalComposer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
composer.setSize(sizes.width, sizes.height);
finalComposer.setSize(sizes.width, sizes.height);
if (sizes.width > 850) {
camera.zoom = 2;
} else {
camera.zoom = 1.1;
}
// Refresh ScrollTrigger instances on resize
ScrollTrigger.refresh();
});
let animate = () => {
controls.update();
// update camera
camera.updateProjectionMatrix();
// traversing through scene
scene.traverse(nonBloomed);
// calling effect composer to render the effect
composer.render();
scene.traverse(restoreMaterial);
finalComposer.render();
// play animation on next frame
window.requestAnimationFrame(animate);
};
animate();
});