Reaperxxxx commited on
Commit
bee0526
·
verified ·
1 Parent(s): cdd5a7e

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +28 -20
index.html CHANGED
@@ -15,51 +15,60 @@
15
  import { GLTFLoader } from 'https://unpkg.com/three@0.165.0/examples/jsm/loaders/GLTFLoader.js';
16
  import { OrbitControls } from 'https://unpkg.com/three@0.165.0/examples/jsm/controls/OrbitControls.js';
17
 
18
- // Scene setup
19
  const scene = new THREE.Scene();
20
- scene.background = new THREE.Color(0x222222);
21
 
22
  const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
23
- camera.position.set(0, 2, 5);
24
-
25
  const renderer = new THREE.WebGLRenderer({ antialias: true });
26
  renderer.setSize(window.innerWidth, window.innerHeight);
27
  document.body.appendChild(renderer.domElement);
28
 
29
- // Lights
30
- const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 1);
31
- hemiLight.position.set(0, 20, 0);
32
- scene.add(hemiLight);
33
 
34
  const dirLight = new THREE.DirectionalLight(0xffffff, 1);
35
- dirLight.position.set(5, 10, 7.5);
36
  scene.add(dirLight);
37
 
38
- // Controls
39
  const controls = new OrbitControls(camera, renderer.domElement);
40
  controls.enableDamping = true;
41
- controls.dampingFactor = 0.05;
42
- controls.target.set(0, 1, 0);
43
 
44
- // Load GLB map
45
  const loader = new GLTFLoader();
46
  loader.load(
47
- './map.glb', // make sure your map.glb is in the same folder
48
  (gltf) => {
49
  const map = gltf.scene;
50
- map.position.set(0, 0, 0);
51
  scene.add(map);
52
- console.log('Map loaded ✅');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  },
54
  (xhr) => {
55
- console.log(`Loading map: ${(xhr.loaded / xhr.total * 100).toFixed(2)}%`);
56
  },
57
  (error) => {
58
- console.error('Error loading GLB:', error);
59
  }
60
  );
61
 
62
- // Animation loop
63
  function animate() {
64
  requestAnimationFrame(animate);
65
  controls.update();
@@ -67,7 +76,6 @@
67
  }
68
  animate();
69
 
70
- // Responsive resize
71
  window.addEventListener('resize', () => {
72
  camera.aspect = window.innerWidth / window.innerHeight;
73
  camera.updateProjectionMatrix();
 
15
  import { GLTFLoader } from 'https://unpkg.com/three@0.165.0/examples/jsm/loaders/GLTFLoader.js';
16
  import { OrbitControls } from 'https://unpkg.com/three@0.165.0/examples/jsm/controls/OrbitControls.js';
17
 
 
18
  const scene = new THREE.Scene();
19
+ scene.background = new THREE.Color(0x111111);
20
 
21
  const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
 
 
22
  const renderer = new THREE.WebGLRenderer({ antialias: true });
23
  renderer.setSize(window.innerWidth, window.innerHeight);
24
  document.body.appendChild(renderer.domElement);
25
 
26
+ // Lighting setup
27
+ scene.add(new THREE.AmbientLight(0xffffff, 1.2));
 
 
28
 
29
  const dirLight = new THREE.DirectionalLight(0xffffff, 1);
30
+ dirLight.position.set(10, 10, 10);
31
  scene.add(dirLight);
32
 
 
33
  const controls = new OrbitControls(camera, renderer.domElement);
34
  controls.enableDamping = true;
 
 
35
 
 
36
  const loader = new GLTFLoader();
37
  loader.load(
38
+ './map.glb',
39
  (gltf) => {
40
  const map = gltf.scene;
 
41
  scene.add(map);
42
+
43
+ // Auto-center and scale fix
44
+ const box = new THREE.Box3().setFromObject(map);
45
+ const center = box.getCenter(new THREE.Vector3());
46
+ const size = box.getSize(new THREE.Vector3());
47
+
48
+ map.position.x += (map.position.x - center.x);
49
+ map.position.y += (map.position.y - center.y);
50
+ map.position.z += (map.position.z - center.z);
51
+
52
+ const maxDim = Math.max(size.x, size.y, size.z);
53
+ const fov = camera.fov * (Math.PI / 180);
54
+ let cameraZ = Math.abs(maxDim / Math.sin(fov / 2));
55
+
56
+ camera.position.set(center.x, center.y + maxDim / 4, cameraZ);
57
+ camera.lookAt(center);
58
+
59
+ controls.target.copy(center);
60
+ controls.update();
61
+
62
+ console.log('✅ Map loaded and centered.');
63
  },
64
  (xhr) => {
65
+ console.log(`Loading: ${(xhr.loaded / xhr.total * 100).toFixed(2)}%`);
66
  },
67
  (error) => {
68
+ console.error('❌ Error loading map:', error);
69
  }
70
  );
71
 
 
72
  function animate() {
73
  requestAnimationFrame(animate);
74
  controls.update();
 
76
  }
77
  animate();
78
 
 
79
  window.addEventListener('resize', () => {
80
  camera.aspect = window.innerWidth / window.innerHeight;
81
  camera.updateProjectionMatrix();