-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
86 lines (76 loc) · 3.15 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Interactive 3D Model</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three/examples/js/controls/OrbitControls.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three/examples/js/loaders/GLTFLoader.js"></script>
<script>
// 1. Set up the scene, camera, and renderer
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 2. Add lighting to the scene
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(5, 5, 5);
scene.add(directionalLight);
// 3. OrbitControls for mouse interaction
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Adds smoothness to the interaction
controls.dampingFactor = 0.25; // Adjust this for smoother/slower rotation
controls.enableZoom = true; // Enable zooming
// 4. Load the GLB model
const loader = new THREE.GLTFLoader();
loader.load(
'https://loco-coder.github.io/Website/House_001_GLB.glb', // Correct URL for GLB model
function(gltf) {
const model = gltf.scene;
scene.add(model);
// Optional: Adjust the model's position and scale
model.position.set(0, -1, 0); // Move it down if necessary
model.scale.set(0.5, 0.5, 0.5); // Adjust scale as needed
},
undefined,
function(error) {
console.error('An error occurred while loading the model:', error);
}
);
// 5. Set the camera position further back to better view the model
camera.position.set(0, 2, 10); // Move the camera farther away from the model
// 6. Animate the scene and add rotation to the model
function animate() {
requestAnimationFrame(animate);
// Enable rotation on the model if you want continuous spinning
if (scene.children[2]) { // Assuming the model is the 3rd child in the scene
scene.children[2].rotation.y += 0.005; // Rotate the model
}
controls.update(); // Update the OrbitControls
renderer.render(scene, camera);
}
animate();
// Handle window resizing
window.addEventListener('resize', function() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
</script>
</body>
</html>