Jump to content

Test: Difference between revisions

No edit summary
No edit summary
Line 41: Line 41:
modelViewer.addEventListener('load', async () => {
modelViewer.addEventListener('load', async () => {
   try {
   try {
     // Get the scene and materials
    console.log('Model loaded, attempting to change texture...');
   
     // Method 1: Try to access Three.js scene directly
     const scene = modelViewer.model.scene || modelViewer.model;
     const scene = modelViewer.model.scene || modelViewer.model;
      
      
     // Function to traverse and update materials
     if (scene) {
    function updateMaterials(object) {
      scene.traverse((child) => {
      if (object.material) {
        if (child.isMesh && child.material) {
        // Load the new texture
          console.log('Found mesh with material:', child.name);
        const textureLoader = new THREE.TextureLoader();
         
        textureLoader.load(
          // Create a new texture loader
           'https://survivalcraft.wiki/content/Textures/Creatures/Bear_Brown.png',
          const loader = new Image();
          (texture) => {
          loader.crossOrigin = 'anonymous';
             // Apply the texture to the material
           loader.onload = () => {
             if (Array.isArray(object.material)) {
            // Create canvas to convert image to texture
               object.material.forEach(mat => {
            const canvas = document.createElement('canvas');
                 mat.map = texture;
            const ctx = canvas.getContext('2d');
            canvas.width = loader.width;
            canvas.height = loader.height;
            ctx.drawImage(loader, 0, 0);
           
             // Apply to material
             if (Array.isArray(child.material)) {
               child.material.forEach(mat => {
                 if (mat.map) {
                  mat.map.image = canvas;
                  mat.map.needsUpdate = true;
                }
                 mat.needsUpdate = true;
                 mat.needsUpdate = true;
               });
               });
             } else {
             } else {
               object.material.map = texture;
               if (child.material.map) {
               object.material.needsUpdate = true;
                child.material.map.image = canvas;
                child.material.map.needsUpdate = true;
               }
              child.material.needsUpdate = true;
             }
             }
              
              
            // Force a re-render
             modelViewer.requestUpdate();
             modelViewer.requestUpdate();
          },
             console.log('Texture updated successfully');
          undefined,
           };
          (error) => {
          loader.src = 'https://survivalcraft.wiki/content/Textures/Creatures/Bear_Brown.png';
             console.error('Error loading texture:', error);
         }
           }
       });
        );
      }
     
      // Recursively check children
      if (object.children) {
         object.children.forEach(child => updateMaterials(child));
       }
     }
     }
      
      
     // Start the material update process
     // Method 2: Try model-viewer's material system
    updateMaterials(scene);
   
  } catch (error) {
    console.error('Error updating materials:', error);
  }
});
 
// Alternative approach using model-viewer's createTexture method (if available)
modelViewer.addEventListener('load', async () => {
  try {
    // This is an alternative approach that might work with newer versions
     const materials = modelViewer.model.materials;
     const materials = modelViewer.model.materials;
     console.log('Found materials:', materials.map(m => m.name));
     console.log('Found materials:', materials ? materials.map(m => m.name) : 'none');
      
      
    if (materials.length > 0) {
      const material = materials[0];
     
      // Try to use the Three.js material directly
      const threeMaterial = material.pbrMetallicRoughness || material;
     
      if (threeMaterial && typeof threeMaterial.setValues === 'function') {
        // Load texture using Three.js loader
        const loader = new THREE.TextureLoader();
        loader.load('https://survivalcraft.wiki/content/Textures/Creatures/Bear_Brown.png', (texture) => {
          threeMaterial.setValues({
            baseColorTexture: texture
          });
          modelViewer.requestUpdate();
        });
      }
    }
   } catch (error) {
   } catch (error) {
     console.log('Alternative approach failed:', error);
     console.error('Error updating texture:', error);
   }
   }
});
});