Test: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
Line 18: | Line 18: | ||
<html> | <html> | ||
<script type="module" src="https://cdn.jsdelivr.net/npm/@google/model-viewer@3.4.0/dist/model-viewer.min.js"></script> | <script type="module" src="https://cdn.jsdelivr.net/npm/@google/model-viewer@3.4.0/dist/model-viewer.min.js"></script> | ||
<div style="margin-bottom: 10px;"> | <div style="margin-bottom: 10px;"> | ||
<label for="bearTexture" style="font-weight: bold; margin-right: 10px;">Bear Type:</label> | <label for="bearTexture" style="font-weight: bold; margin-right: 10px;">Bear Type:</label> | ||
Line 43: | Line 41: | ||
loading="eager"> | loading="eager"> | ||
<div slot="error" style="display: flex; align-items: center; justify-content: center; height: 100%; background: #ffe6e6; color: #d63031;"> | <div slot="error" style="display: flex; align-items: center; justify-content: center; height: 100%; background: #ffe6e6; color: #d63031;"> | ||
<p>⚠️ Unable to load 3D model<br><small>Check console for details</small></p> | <p>⚠️ Unable to load 3D model<br><small>Check console for details</small></p> | ||
Line 51: | Line 46: | ||
</model-viewer> | </model-viewer> | ||
<div id="loadingStatus" style="margin-top: 10px; font-size: 12px; color: #666;"> | <div id="loadingStatus" style="margin-top: 10px; font-size: 12px; color: #666;"> | ||
Loading model... | Loading model... | ||
</div> | </div> | ||
<div id="controlsInfo" style="margin-top: 10px; font-size: 12px; color: #666; display: none;"> | <div id="controlsInfo" style="margin-top: 10px; font-size: 12px; color: #666; display: none;"> | ||
<strong>Controls:</strong> Click and drag to rotate • Scroll to zoom • Right-click and drag to pan | <strong>Controls:</strong> Click and drag to rotate • Scroll to zoom • Right-click and drag to pan | ||
</div> | </div> | ||
<script> | <script type="module"> // Added type="module" here as well | ||
const modelViewer = document.getElementById('bearModel'); | const modelViewer = document.getElementById('bearModel'); | ||
const loadingStatus = document.getElementById('loadingStatus'); | const loadingStatus = document.getElementById('loadingStatus'); | ||
const controlsInfo = document.getElementById('controlsInfo'); | const controlsInfo = document.getElementById('controlsInfo'); | ||
const bearTextureSelector = document.getElementById('bearTexture'); | |||
// Define texture URLs | // Define texture URLs | ||
Line 71: | Line 65: | ||
black: 'https://survivalcraft.wiki/content/Textures/Creatures/Bear_Black.png' | black: 'https://survivalcraft.wiki/content/Textures/Creatures/Bear_Black.png' | ||
}; | }; | ||
let currentMaterial = null; // To store a reference to the material we want to modify | |||
// Track loading state | // Track loading state | ||
modelViewer.addEventListener('load', () => { | modelViewer.addEventListener('load', async () => { | ||
console.log('Model loaded successfully'); | console.log('Model loaded successfully'); | ||
loadingStatus.textContent = 'Model loaded successfully!'; | loadingStatus.textContent = 'Model loaded successfully!'; | ||
controlsInfo.style.display = 'block'; | controlsInfo.style.display = 'block'; | ||
// | // Get the first material from the model | ||
// Assuming your bear model has one main material that controls its color/texture | |||
changeBearTexture(); | // You might need to inspect your .glb file to find the correct material index or name | ||
} | if (modelViewer.model && modelViewer.model.materials.length > 0) { | ||
currentMaterial = modelViewer.model.materials[0]; // Get the first material | |||
console.log('Found model material:', currentMaterial); | |||
// Apply the initial texture | |||
changeBearTexture(); | |||
} else { | |||
console.warn('No materials found in the model.'); | |||
} | |||
}); | }); | ||
Line 95: | Line 98: | ||
}); | }); | ||
function changeBearTexture() { | // Event listener for texture selection change | ||
bearTextureSelector.addEventListener('change', changeBearTexture); | |||
const selectedTexture = | |||
async function changeBearTexture() { | |||
console.log('Attempting to change texture to:', selectedTexture); | if (!currentMaterial) { | ||
console.warn('Model material not yet available for texture change.'); | |||
return; | |||
if ( | } | ||
const selectedTexture = bearTextureSelector.value; | |||
const textureUrl = textures[selectedTexture]; | |||
console.log('Attempting to change texture to:', selectedTexture, 'at URL:', textureUrl); | |||
if (textureUrl) { | |||
try { | try { | ||
// | // Create a new texture using model-viewer's createTexture method | ||
const | const texture = await modelViewer.createTexture(textureUrl); | ||
// Apply the new texture to the baseColorTexture of the material | |||
// This is the standard way to change the main color texture in glTF PBR materials | |||
currentMaterial.pbrMetallicRoughness.baseColorTexture.setTexture(texture); | |||
console.log('Texture applied successfully:', selectedTexture); | |||
} catch (error) { | } catch (error) { | ||
console.error('Error applying texture:', error); | console.error('Error creating or applying texture:', error); | ||
// Optionally, revert to a default or show an error state | |||
} | } | ||
} else { | } else { | ||
console. | console.warn('No texture URL found for:', selectedTexture); | ||
} | } | ||
} | } |