-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
104 lines (84 loc) · 2.98 KB
/
Copy pathmain.js
File metadata and controls
104 lines (84 loc) · 2.98 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
// Import required modules from Three.js
import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
// Initialize the WebGL renderer
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.outputColorSpace = THREE.SRGBColorSpace;
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0x000000, 0);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
// Append renderer's DOM element to the document body
document.body.appendChild(renderer.domElement);
// Create a Three.js scene
const scene = new THREE.Scene();
// Create a perspective camera
const camera = new THREE.PerspectiveCamera(5, window.innerWidth / window.innerHeight, 1, 1000);
// const tabWidths = window.innerWidth;
// if (tabWidths > 800){
// camera.position.set(0, 7.5, 0);
// } else {
// camera.position.set(0, 12.5, 0);
// }
// Function to calculate and set camera distance based on tab width
function updateCameraDistance() {
const tabWidth = window.innerWidth;
if (tabWidth > 800) {
const scaleFactor = -1 * (1 - (tabWidth - 600) / 400);
Math.abs(scaleFactor);
const newDistance = 90 / scaleFactor;
if(tabWidth <= 1200){
camera.position.set(5, 170, 0);
}else{
camera.position.set(0, newDistance, 0);
}
} else if (tabWidth <= 800) {
camera.position.set(0, 170, 0);
}
}
// Call the function when the tab is initially loaded
updateCameraDistance();
// Update camera aspect ratio and renderer size on window resize
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
// Call the function to update camera distance
updateCameraDistance();
});
// Initialize OrbitControls for camera movement
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.enablePan = true;
controls.minDistance = 20;
controls.maxDistance = 170;
controls.minPolarAngle = 0;
controls.maxPolarAngle = 5;
controls.autoRotate = false;
controls.target = new THREE.Vector3(0, 0, 0);
controls.update();
// Load the 3D model using GLTFLoader
const loader = new GLTFLoader().setPath('public/3dModel/');
loader.load('scene.gltf', (gltf) => {
console.log('loading model');
const mesh = gltf.scene;
// Set position and rotation of the loaded model
mesh.position.set(0, 0, 0);
camera.lookAt(0, 0, 0);
scene.add(mesh);
// Hide the loading progress container
document.getElementById('progress-container').style.display = 'none';
}, (xhr) => {
console.log(`loading ${xhr.loaded / xhr.total * 100}%`);
}, (error) => {
console.error(error);
});
// Function to animate the scene
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();