-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
69 lines (57 loc) · 2.36 KB
/
script.js
File metadata and controls
69 lines (57 loc) · 2.36 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
let scene, camera, renderer, mixer;
function initThreeJS(containerId) {
// Create a new Three.js scene for each container
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, 1, 0.1, 1000); // Aspect ratio set to 1 (square containers)
// Create WebGL renderer
renderer = new THREE.WebGLRenderer();
renderer.setSize(300, 300); // Adjust size for each container
document.getElementById(containerId).appendChild(renderer.domElement);
// Add lights to the scene
const light = new THREE.AmbientLight(0x404040); // Ambient light
scene.add(light);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1); // Directional light
directionalLight.position.set(5, 5, 5);
scene.add(directionalLight);
// Position the camera
camera.position.z = 5;
// Animation loop
function animate() {
requestAnimationFrame(animate);
if (mixer) mixer.update(0.01); // Update animation mixer if animations exist
renderer.render(scene, camera);
}
animate();
}
// Function to load a yoga pose 3D model
function loadYogaPose(poseId) {
const containerId = `pose-${poseId}`;
// Prevent loading the model if it's already loaded in the container
if (!document.getElementById(containerId).childElementCount) {
initThreeJS(containerId); // Initialize Three.js for this container
// Load the 3D model for the selected yoga pose
const loader = new THREE.GLTFLoader();
const modelPath = `path/to/yoga-pose-${poseId}.glb`; // Update with actual model paths
loader.load(modelPath, function (gltf) {
const model = gltf.scene;
scene.add(model); // Add model to the scene
// If the model contains animations, set up the animation mixer
if (gltf.animations && gltf.animations.length) {
mixer = new THREE.AnimationMixer(model);
gltf.animations.forEach((clip) => {
mixer.clipAction(clip).play();
});
}
}, undefined, function (error) {
console.error('Error loading the model:', error);
});
}
}
// Optional: Resize the renderer if the window size changes
window.addEventListener('resize', () => {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});