Spaces:
Running
on
Zero
Running
on
Zero
File size: 11,870 Bytes
600759a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
<!DOCTYPE html>
<html>
<head>
<!-- Import the component -->
<script src="https://cdn.jsdelivr.net/npm/@google/[email protected]/dist/model-viewer.min.js"
type="module"></script>
<style>
body {
margin: 0;
font-family: Arial, sans-serif;
}
.centered-container {
display: flex;
justify-content: center;
align-items: center;
}
.modelviewer-panel-button {
height: 30px;
margin: 4px 4px;
padding: 0px 14px;
background: white;
border-radius: 10px;
box-shadow: 0px 0px 4px rgba(0, 0, 0, 0.25);
font-size: 14px;
font-weight: 600;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
transition: all 0.2s ease;
}
.modelviewer-panel-button.checked {
background: #6567C9;
color: white;
}
.modelviewer-panel-button:hover {
background-color: #e2e6ea;
}
.modelviewer-panel-button-container {
display: flex;
justify-content: space-around;
}
.centered-container {
display: flex;
flex-direction: column;
align-items: center;
}
</style>
</head>
<body>
<div class="centered-container">
<div class="centered-container">
<div class="column is-mobile is-centered">
<model-viewer id="modelviewer" style="height: #height#px; width: #width#px;"
rotation-per-second="10deg"
src="#src#" disable-tap
environment-image="neutral"
camera-target="0m 0m 0m"
camera-orbit="0deg 90deg 12m"
orientation="0deg 0deg 0deg"
shadow-intensity=".9"
ar auto-rotate
camera-controls>
</model-viewer>
</div>
<div class="modelviewer-panel-button-container">
<div id="appearance-button" class="modelviewer-panel-button small checked" onclick="showTexture()">
Appearance
</div>
<div id="geometry-button" class="modelviewer-panel-button small" onclick="hideTexture()">Geometry</div>
</div>
</div>
</div>
<script>
let modelViewer;
let window_state = {
isModelLoaded: false,
isHidden: false,
savedMaterials: null,
savedExposure: null
};
document.addEventListener('DOMContentLoaded', () => {
modelViewer = document.getElementById('modelviewer');
// 等待模型加载完成
modelViewer.addEventListener('load', () => {
console.log('Model loaded, materials available:', modelViewer.model.materials.length);
window_state.isModelLoaded = true;
// 调试:打印材质信息
modelViewer.model.materials.forEach((mat, idx) => {
console.log(`Material ${idx}:`, {
name: mat.name,
hasBaseColor: !!mat.pbrMetallicRoughness.baseColorTexture,
hasMetallicRoughness: !!mat.pbrMetallicRoughness.metallicRoughnessTexture,
hasNormal: !!mat.normalTexture
});
});
});
modelViewer.addEventListener('error', (event) => {
console.error('Model loading error:', event);
});
});
function ensureModelLoaded() {
if (!window_state.isModelLoaded || !modelViewer.model || !modelViewer.model.materials) {
console.warn('Model not loaded yet');
return false;
}
return true;
}
function saveMaterialsState() {
if (!ensureModelLoaded()) return false;
console.log('Saving materials state...');
window_state.savedMaterials = [];
window_state.savedExposure = modelViewer.exposure;
for (let i = 0; i < modelViewer.model.materials.length; i++) {
const material = modelViewer.model.materials[i];
const pbr = material.pbrMetallicRoughness;
const materialState = {
baseColorTexture: null,
metallicRoughnessTexture: null,
normalTexture: null,
baseColorFactor: null,
metallicFactor: null,
roughnessFactor: null
};
// 保存纹理引用
try {
if (pbr.baseColorTexture && pbr.baseColorTexture.texture) {
materialState.baseColorTexture = pbr.baseColorTexture.texture;
console.log(`Saved baseColorTexture for material ${i}`);
}
if (pbr.metallicRoughnessTexture && pbr.metallicRoughnessTexture.texture) {
materialState.metallicRoughnessTexture = pbr.metallicRoughnessTexture.texture;
console.log(`Saved metallicRoughnessTexture for material ${i}`);
}
if (material.normalTexture && material.normalTexture.texture) {
materialState.normalTexture = material.normalTexture.texture;
console.log(`Saved normalTexture for material ${i}`);
}
// 保存材质参数
materialState.baseColorFactor = [...pbr.baseColorFactor];
materialState.metallicFactor = pbr.metallicFactor;
materialState.roughnessFactor = pbr.roughnessFactor;
} catch (error) {
console.error(`Error saving material ${i}:`, error);
}
window_state.savedMaterials.push(materialState);
}
console.log('Materials state saved:', window_state.savedMaterials);
return true;
}
function hideTexture() {
console.log('hideTexture called');
if (!ensureModelLoaded()) {
console.error('Cannot hide texture: model not loaded');
return;
}
let appearanceButton = document.getElementById('appearance-button');
let geometryButton = document.getElementById('geometry-button');
appearanceButton.classList.remove('checked');
geometryButton.classList.add('checked');
// 如果已经隐藏,直接返回
if (window_state.isHidden) return;
// 第一次隐藏时保存状态
if (!window_state.savedMaterials) {
if (!saveMaterialsState()) return;
}
// 隐藏所有纹理
try {
for (let i = 0; i < modelViewer.model.materials.length; i++) {
const material = modelViewer.model.materials[i];
const pbr = material.pbrMetallicRoughness;
if (pbr.baseColorTexture) {
pbr.baseColorTexture.setTexture(null);
}
if (pbr.metallicRoughnessTexture) {
pbr.metallicRoughnessTexture.setTexture(null);
}
if (material.normalTexture) {
material.normalTexture.setTexture(null);
}
}
window_state.isHidden = true;
modelViewer.environmentImage = '/static/env_maps/gradient.jpg';
modelViewer.exposure = 4;
console.log('Textures hidden successfully');
} catch (error) {
console.error('Error hiding textures:', error);
}
}
function showTexture() {
console.log('showTexture called');
if (!ensureModelLoaded()) {
console.error('Cannot show texture: model not loaded');
return;
}
let appearanceButton = document.getElementById('appearance-button');
let geometryButton = document.getElementById('geometry-button');
appearanceButton.classList.add('checked');
geometryButton.classList.remove('checked');
// 如果不在隐藏状态,直接返回
if (!window_state.isHidden) return;
// 如果没有保存的材质状态,无法恢复
if (!window_state.savedMaterials) {
console.warn('No saved materials to restore');
return;
}
// 恢复纹理
try {
for (let i = 0; i < modelViewer.model.materials.length && i < window_state.savedMaterials.length; i++) {
const material = modelViewer.model.materials[i];
const pbr = material.pbrMetallicRoughness;
const savedMaterial = window_state.savedMaterials[i];
// 恢复纹理
if (savedMaterial.baseColorTexture && pbr.baseColorTexture) {
pbr.baseColorTexture.setTexture(savedMaterial.baseColorTexture);
console.log(`Restored baseColorTexture for material ${i}`);
}
if (savedMaterial.metallicRoughnessTexture && pbr.metallicRoughnessTexture) {
pbr.metallicRoughnessTexture.setTexture(savedMaterial.metallicRoughnessTexture);
console.log(`Restored metallicRoughnessTexture for material ${i}`);
}
if (savedMaterial.normalTexture && material.normalTexture) {
material.normalTexture.setTexture(savedMaterial.normalTexture);
console.log(`Restored normalTexture for material ${i}`);
}
// 恢复材质参数
if (savedMaterial.baseColorFactor) {
pbr.setBaseColorFactor(savedMaterial.baseColorFactor);
}
if (typeof savedMaterial.metallicFactor === 'number') {
pbr.setMetallicFactor(savedMaterial.metallicFactor);
}
if (typeof savedMaterial.roughnessFactor === 'number') {
pbr.setRoughnessFactor(savedMaterial.roughnessFactor);
}
}
// 恢复环境设置
modelViewer.environmentImage = '/static/env_maps/white.jpg';
if (window_state.savedExposure !== undefined) {
modelViewer.exposure = window_state.savedExposure;
}
window_state.isHidden = false;
console.log('Textures restored successfully');
} catch (error) {
console.error('Error restoring textures:', error);
}
}
// 添加调试函数
function debugMaterials() {
if (!ensureModelLoaded()) return;
console.log('=== Current Materials Debug ===');
modelViewer.model.materials.forEach((mat, idx) => {
const pbr = mat.pbrMetallicRoughness;
console.log(`Material ${idx}:`, {
name: mat.name,
baseColorTexture: pbr.baseColorTexture?.texture || null,
metallicRoughnessTexture: pbr.metallicRoughnessTexture?.texture || null,
normalTexture: mat.normalTexture?.texture || null,
baseColorFactor: pbr.baseColorFactor,
metallicFactor: pbr.metallicFactor,
roughnessFactor: pbr.roughnessFactor
});
});
console.log('Window state:', window_state);
console.log('===============================');
}
// 暴露调试函数到全局
window.debugMaterials = debugMaterials;
</script>
</body>
</html> |