KBLLR commited on
Commit
e97156e
·
verified ·
1 Parent(s): 9ff3e07

threejs is failling to load

Browse files
Files changed (1) hide show
  1. index.html +26 -29
index.html CHANGED
@@ -5,9 +5,9 @@
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>3D Character World</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
- <script src="https://cdn.jsdelivr.net/npm/three@0.167.0/build/three.min.js"></script>
9
- <script src="https://cdn.jsdelivr.net/npm/three@0.167.0/examples/jsm/controls/OrbitControls.js"></script>
10
- <script src="https://cdn.jsdelivr.net/npm/three@0.167.0/examples/jsm/loaders/GLTFLoader.js"></script>
11
  <style>
12
  body {
13
  margin: 0;
@@ -369,15 +369,13 @@
369
 
370
  // Initialize audio context on user interaction
371
  document.addEventListener('click', initializeAudio, { once: true });
372
-
373
- // Check if Three.js modules are available
374
- if (typeof THREE === 'undefined') {
375
- console.error('Three.js not loaded');
376
- // Try to load modules dynamically if needed
377
- loadThreeJSModules();
378
- }
379
- });
380
-
381
  // Function to dynamically load Three.js modules if needed
382
  function loadThreeJSModules() {
383
  // Check if OrbitControls is available
@@ -582,18 +580,21 @@ card.addEventListener('click', () => {
582
  function initThreeJS() {
583
  if (typeof THREE === 'undefined') {
584
  console.error('Three.js is not loaded properly');
585
- // Try to reload or show error
586
- setTimeout(() => {
587
- if (typeof THREE === 'undefined') {
588
- alert('Three.js failed to load. Please refresh the page.');
589
- } else {
590
- initThreeJS(); // Retry initialization
591
- }
592
- }, 100);
 
 
 
 
593
  return;
594
  }
595
-
596
- try {
597
  // Set up Three.js scene
598
  scene = new THREE.Scene();
599
  scene.background = new THREE.Color(0x87CEEB); // Sky blue
@@ -615,7 +616,6 @@ card.addEventListener('click', () => {
615
 
616
  // Clock for animations
617
  clock = new THREE.Clock();
618
-
619
  // Loader - check if GLTFLoader is available
620
  if (typeof THREE.GLTFLoader !== 'undefined') {
621
  loader = new THREE.GLTFLoader();
@@ -623,8 +623,7 @@ card.addEventListener('click', () => {
623
  console.warn('GLTFLoader not available, using fallback geometry');
624
  loader = null;
625
  }
626
-
627
- // Add environment map with error handling
628
  const envMapLoader = new THREE.CubeTextureLoader();
629
  envMapLoader.load([
630
  'https://threejs.org/examples/textures/cube/SwedishRoyalCastle/px.jpg',
@@ -676,7 +675,6 @@ card.addEventListener('click', () => {
676
 
677
  // Add some environment objects
678
  addEnvironmentObjects();
679
-
680
  // Initialize OrbitControls with fallback
681
  if (typeof THREE.OrbitControls !== 'undefined') {
682
  controls = new THREE.OrbitControls(camera, renderer.domElement);
@@ -689,10 +687,9 @@ card.addEventListener('click', () => {
689
  enabled: false,
690
  update: () => {},
691
  target: new THREE.Vector3()
692
- };
693
  }
694
-
695
- // Load player character
696
  loadPlayerCharacter();
697
 
698
  // Load NPC characters
 
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
  <title>3D Character World</title>
7
  <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r164/three.min.js"></script>
9
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r164/examples/jsm/controls/OrbitControls.js"></script>
10
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r164/examples/jsm/loaders/GLTFLoader.js"></script>
11
  <style>
12
  body {
13
  margin: 0;
 
369
 
370
  // Initialize audio context on user interaction
371
  document.addEventListener('click', initializeAudio, { once: true });
372
+ // Check if Three.js modules are available
373
+ if (typeof THREE === 'undefined') {
374
+ console.error('Three.js not loaded');
375
+ // Try to load modules dynamically if needed
376
+ loadThreeJSModules();
377
+ }
378
+ });
 
 
379
  // Function to dynamically load Three.js modules if needed
380
  function loadThreeJSModules() {
381
  // Check if OrbitControls is available
 
580
  function initThreeJS() {
581
  if (typeof THREE === 'undefined') {
582
  console.error('Three.js is not loaded properly');
583
+ // Load Three.js from a reliable CDN
584
+ const script = document.createElement('script');
585
+ script.src = 'https://cdnjs.cloudflare.com/ajax/libs/three.js/r164/three.min.js"></script>
586
+ script.onload = () => {
587
+ console.log('Three.js loaded successfully');
588
+ initThreeJS(); // Retry initialization
589
+ };
590
+ script.onerror = () => {
591
+ console.error('Failed to load Three.js');
592
+ alert('Three.js failed to load. Please check your internet connection and refresh the page.');
593
+ };
594
+ document.head.appendChild(script);
595
  return;
596
  }
597
+ try {
 
598
  // Set up Three.js scene
599
  scene = new THREE.Scene();
600
  scene.background = new THREE.Color(0x87CEEB); // Sky blue
 
616
 
617
  // Clock for animations
618
  clock = new THREE.Clock();
 
619
  // Loader - check if GLTFLoader is available
620
  if (typeof THREE.GLTFLoader !== 'undefined') {
621
  loader = new THREE.GLTFLoader();
 
623
  console.warn('GLTFLoader not available, using fallback geometry');
624
  loader = null;
625
  }
626
+ // Add environment map with error handling
 
627
  const envMapLoader = new THREE.CubeTextureLoader();
628
  envMapLoader.load([
629
  'https://threejs.org/examples/textures/cube/SwedishRoyalCastle/px.jpg',
 
675
 
676
  // Add some environment objects
677
  addEnvironmentObjects();
 
678
  // Initialize OrbitControls with fallback
679
  if (typeof THREE.OrbitControls !== 'undefined') {
680
  controls = new THREE.OrbitControls(camera, renderer.domElement);
 
687
  enabled: false,
688
  update: () => {},
689
  target: new THREE.Vector3()
690
+ };
691
  }
692
+ // Load player character
 
693
  loadPlayerCharacter();
694
 
695
  // Load NPC characters