Test: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 20: | Line 20: | ||
<head> | <head> | ||
<script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script> | <script type="module" src="https://unpkg.com/@google/model-viewer/dist/model-viewer.min.js"></script> | ||
<style> | |||
model-viewer { | |||
width: 100%; | |||
height: 500px; | |||
background-color: #eee; | |||
} | |||
</style> | |||
</head> | </head> | ||
<body> | <body> | ||
<model-viewer id="bearModel" | <model-viewer id="bearModel" | ||
src="https://survivalcraft.wiki/content/Models/Bear.glb" | src="https://survivalcraft.wiki/content/Models/Bear.glb" | ||
ar shadow-intensity="1" camera-controls touch-action="pan-y"> | ar shadow-intensity="1" camera-controls touch-action="pan-y" | ||
environment-image="neutral" | |||
auto-rotate> | |||
</model-viewer> | </model-viewer> | ||
Line 31: | Line 40: | ||
modelViewer.addEventListener('load', async () => { | modelViewer.addEventListener('load', async () => { | ||
const materials = modelViewer. | try { | ||
// Get the scene and materials | |||
const scene = modelViewer.model.scene || modelViewer.model; | |||
// Function to traverse and update materials | |||
function updateMaterials(object) { | |||
if (object.material) { | |||
// Load the new texture | |||
const textureLoader = new THREE.TextureLoader(); | |||
textureLoader.load( | |||
'https://survivalcraft.wiki/content/Textures/Creatures/Bear_Brown.png', | |||
(texture) => { | |||
// Apply the texture to the material | |||
if (Array.isArray(object.material)) { | |||
object.material.forEach(mat => { | |||
mat.map = texture; | |||
mat.needsUpdate = true; | |||
}); | |||
} else { | |||
object.material.map = texture; | |||
object.material.needsUpdate = true; | |||
} | |||
// Force a re-render | |||
modelViewer.requestUpdate(); | |||
}, | |||
undefined, | |||
(error) => { | |||
console.error('Error loading texture:', error); | |||
} | |||
); | |||
} | |||
// Recursively check children | |||
if (object.children) { | |||
object.children.forEach(child => updateMaterials(child)); | |||
} | |||
} | |||
// Start the material update process | |||
updateMaterials(scene); | |||
} catch (error) { | |||
console.error('Error updating materials:', error); | |||
} | |||
}); | |||
console.log('Found materials:', materials.map(m => m.name)); | // 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; | |||
console.log('Found materials:', materials.map(m => m.name)); | |||
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) { | |||
console.log('Alternative approach failed:', error); | |||
} | |||
}); | }); | ||