Test: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 41: | Line 41: | ||
modelViewer.addEventListener('load', async () => { | modelViewer.addEventListener('load', async () => { | ||
try { | try { | ||
// | 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; | ||
if (scene) { | |||
scene.traverse((child) => { | |||
if (child.isMesh && child.material) { | |||
console.log('Found mesh with material:', child.name); | |||
// Create a new texture loader | |||
const loader = new Image(); | |||
loader.crossOrigin = 'anonymous'; | |||
// Apply | loader.onload = () => { | ||
if (Array.isArray( | // Create canvas to convert image to texture | ||
const canvas = document.createElement('canvas'); | |||
mat.map = | 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 { | ||
if (child.material.map) { | |||
child.material.map.image = canvas; | |||
child.material.map.needsUpdate = true; | |||
} | |||
child.material.needsUpdate = true; | |||
} | } | ||
modelViewer.requestUpdate(); | modelViewer.requestUpdate(); | ||
console.log('Texture updated successfully'); | |||
}; | |||
loader.src = 'https://survivalcraft.wiki/content/Textures/Creatures/Bear_Brown.png'; | |||
console. | } | ||
} | }); | ||
} | |||
} | } | ||
// | // Method 2: Try model-viewer's material system | ||
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'); | ||
} catch (error) { | } catch (error) { | ||
console. | console.error('Error updating texture:', error); | ||
} | } | ||
}); | }); |