-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
93 lines (81 loc) · 3.02 KB
/
Copy pathscript.js
File metadata and controls
93 lines (81 loc) · 3.02 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
import * as THREE from "https://cdn.skypack.dev/three@0.129.0/build/three.module.js";
import { GLTFLoader } from "https://cdn.skypack.dev/three@0.129.0/examples/jsm/loaders/GLTFLoader.js";
import { gsap } from "https://cdn.skypack.dev/gsap";
const camera = new THREE.PerspectiveCamera(10, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 13;
const scene = new THREE.Scene();
let bee;
let mixer;
const loader = new GLTFLoader();
loader.load(
"https://raw.githubusercontent.com/DennysDionigi/bee-glb/94253437c023643dd868592e11a0fd2c228cfe07/demon_bee_full_texture.glb",
function (gltf) {
bee = gltf.scene;
scene.add(bee);
mixer = new THREE.AnimationMixer(bee);
mixer.clipAction(gltf.animations[0]).play();
modelMove();
},
function (xhr) {
console.log((xhr.loaded / xhr.total) * 100 + "% loaded");
},
function (error) {
console.error("Error loading model:", error);
}
);
const renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setSize(window.innerWidth, window.innerHeight);
document.getElementById("container3D").appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 1.3);
scene.add(ambientLight);
const topLight = new THREE.DirectionalLight(0xffffff, 1);
topLight.position.set(500, 500, 500);
scene.add(topLight);
const reRender3D = () => {
requestAnimationFrame(reRender3D);
renderer.render(scene, camera);
if (mixer) mixer.update(0.02);
};
reRender3D();
let arrPositionModel = [
{ id: "banner", position: { x: 0, y: -1, z: 0 }, rotation: { x: 0, y: 1.5, z: 0 } },
{ id: "intro", position: { x: 1, y: -1, z: -5 }, rotation: { x: 0.5, y: -0.5, z: 0.5 } },
{ id: "description", position: { x: -1, y: -1, z: -5 }, rotation: { x: 0, y: 0.5, z: 0.2 } },
{ id: "contact", position: { x: 0.45, y: -2, z: -10 }, rotation: { x: 0.2, y: -0.5, z: -0.2 } }
];
const modelMove = () => {
const sections = document.querySelectorAll(".section");
let currentSection;
sections.forEach((section) => {
const rect = section.getBoundingClientRect();
if (rect.top <= window.innerHeight / 3) {
currentSection = section.id;
}
});
let position_active = arrPositionModel.findIndex(val => val.id == currentSection);
if (position_active >= 0) {
let new_coordinates = arrPositionModel[position_active];
gsap.to(bee.position, {
x: new_coordinates.position.x,
y: new_coordinates.position.y,
z: new_coordinates.position.z,
duration: 3,
ease: "power1.out"
});
gsap.to(bee.rotation, {
x: new_coordinates.rotation.x,
y: new_coordinates.rotation.y,
z: new_coordinates.rotation.z,
duration: 3,
ease: "power1.out"
});
}
};
window.addEventListener("scroll", () => {
if (bee) modelMove();
});
window.addEventListener("resize", () => {
renderer.setSize(window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
});