-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodelloader.html
More file actions
434 lines (388 loc) · 13.4 KB
/
Copy pathmodelloader.html
File metadata and controls
434 lines (388 loc) · 13.4 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
<a href="javascript:location.href=location.href+(location.href.includes('?')?'&':'?')+'redraw=yes'">Redraw_Model_Tex_Periodically</a>
<br>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js MMD Model Viewer</title>
<style>
body {
margin: 0;
overflow: hidden;
}
#material-list, #morph-list {
position: fixed;
top: 10%;
width: 200px;
height: 80%;
background: rgba(0, 0, 0, 0.5);
color: white;
padding: 10px;
overflow-y: auto;
border-radius: 8px;
font-family: Arial, sans-serif;
}
#material-list { right: 10px; }
#morph-list { left: 10px; }
#material-list ul, #morph-list ul {
list-style: none;
padding: 0;
margin: 0;
}
#material-list li, #morph-list li {
cursor: pointer;
margin: 5px 0;
display: flex;
align-items: center;
}
#material-list li:hover, #morph-list li:hover {
background-color: rgba(255, 255, 255, 0.1);
}
.active { color: green; }
.inactive { color: red; }
#controls {
position: fixed;
bottom: 20px;
left: 50%;
transform: translateX(-50%);
display: flex;
gap: 10px;
}
#controls button {
padding: 10px 15px;
background-color: #333;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#controls button:hover { background-color: #555; }
#zoom-control {
position: fixed;
top: 20px;
left: 50%;
transform: translateX(-50%);
width: 300px;
color: white;
}
#zoom-control input { width: 100%; }
#pan-control {
position: fixed;
top: 20px;
left: 225px;
height: 80%;
background-color: rgba(0, 0, 0, 0.5);
color: white;
display: flex;
flex-direction: column;
justify-content: space-between;
padding: 10px;
}
#pan-control input {
transform: rotate(90deg);
width: 100%;
height: 20px;
}
.material-checkbox {
margin-right: 8px;
}
#check-all-btn {
margin-top: 10px;
padding: 5px 10px;
background-color: #444;
color: white;
border: none;
border-radius: 3px;
cursor: pointer;
}
#check-all-btn:hover {
background-color: #666;
}
.hidden {
display: none !important;
}
</style>
</head>
<body>
<div id="material-list">
<h3>Material List</h3>
<button id="check-all-btn" class="hidden">Check All</button>
<ul id="materials"></ul>
</div>
<div id="morph-list">
<h3>Morph List</h3>
<ul id="morphs"></ul>
</div>
<div id="controls">
<button onclick="rotateModelLeft()">Rotate Left</button>
<button onclick="rotateModelRight()">Rotate Right</button>
<button onclick="slowRotateLeft()">Slow Rotate Left</button>
<button onclick="slowRotateRight()">Slow Rotate Right</button>
</div>
<div id="zoom-control">
<label for="zoom-slider">Zoom:</label>
<input type="range" id="zoom-slider" min="10" max="200" value="50" step="1">
</div>
<div id="pan-control">
<label for="pan-slider">Pan:</label>
<input type="range" id="pan-slider" min="10" max="20" value="50" step="0.5">
</div>
<script src="./libs/three.js"></script>
<script src="./libs/mmdparser.min.js"></script>
<script src="./libs/ammo.min.js"></script>
<script src="./libs/TGALoader.js"></script>
<script src="./libs/MMDLoader.js"></script>
<script src="./libs/MMDAnimationHelper.js"></script>
<script src="./libs/mmd-loader.min.js"></script>
<script src="./libs/CCDIKSolver.js"></script>
<script src="./libs/MMDPhysics.js"></script>
<script>
let scene, renderer, camera, mesh, helper;
let ready = false;
const clock = new THREE.Clock();
const MotionObjects = [{ id: "001", VmdClip: null, AudioClip: false }];
let rotateSpeed = 0.01;
let isDragging = false;
let previousMousePosition = { x: 0, y: 0 };
// New variables for texture redraw
let redrawEnabled = false;
let redrawInterval = null;
let materialCheckboxes = new Map(); // Map to track checkbox states
function getQueryStringParam(param) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(param);
}
let Pmx = getQueryStringParam('pmx') || "AoiZaizen/AoiZaizen.pmx";
Pmx = "pmx/pronama/" + Pmx;
window.onload = () => {
Init();
LoadModeler();
Render();
setupZoomControl();
setupPanControl();
setupModelDrag();
// Check if redraw is enabled via query string
if (getQueryStringParam('redraw') === 'yes') {
redrawEnabled = true;
console.log("Texture redraw enabled");
}
}
Init = () => {
scene = new THREE.Scene();
const ambient = new THREE.AmbientLight(0xeeeeee);
scene.add(ambient);
renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setClearColor(0xcccccc, 0);
document.body.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(40, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 10, 50);
camera.lookAt(0, 10, 0);
}
LoadModeler = async () => {
const loader = new THREE.MMDLoader();
loader.load(Pmx, (object) => {
mesh = object;
scene.add(mesh);
updateMaterialList();
updateMorphList();
// Start redraw interval if enabled
if (redrawEnabled) {
startTextureRedraw();
}
});
}
updateMaterialList = () => {
const materialListContainer = document.getElementById("materials");
materialListContainer.innerHTML = '';
materialCheckboxes.clear();
mesh.material.forEach((material, index) => {
const listItem = document.createElement("li");
// Only create checkbox if redraw is enabled
if (redrawEnabled) {
// Create checkbox
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.className = "material-checkbox";
checkbox.checked = index === 0; // Only first material checked by default
// Store checkbox state
materialCheckboxes.set(material, checkbox.checked);
// Add change event listener
checkbox.addEventListener("change", () => {
materialCheckboxes.set(material, checkbox.checked);
});
listItem.appendChild(checkbox);
}
// Material name
const nameSpan = document.createElement("span");
nameSpan.textContent = material.name || `Material ${index + 1}`;
// Add click event for visibility toggle
nameSpan.onclick = () => handleMaterialClick(material, listItem);
// Assemble list item
listItem.appendChild(nameSpan);
// Set initial visibility state
listItem.classList.add(material.visible ? "active" : "inactive");
materialListContainer.appendChild(listItem);
});
// Show/hide check all button based on redraw status
const checkAllBtn = document.getElementById("check-all-btn");
if (redrawEnabled) {
checkAllBtn.classList.remove("hidden");
checkAllBtn.addEventListener("click", () => {
const checkboxes = document.querySelectorAll(".material-checkbox");
checkboxes.forEach(checkbox => {
checkbox.checked = true;
// Update our checkbox state map
const materialIndex = Array.from(checkboxes).indexOf(checkbox);
if (materialIndex !== -1) {
materialCheckboxes.set(mesh.material[materialIndex], true);
}
});
});
} else {
checkAllBtn.classList.add("hidden");
}
}
updateMorphList = () => {
const morphListContainer = document.getElementById("morphs");
morphListContainer.innerHTML = '';
mesh.morphTargetInfluences.forEach((_, index) => {
const listItem = document.createElement("li");
listItem.textContent = mesh.morphTargetDictionary
? Object.keys(mesh.morphTargetDictionary)[index]
: `Morph ${index + 1}`;
listItem.onclick = () => handleMorphClick(index, listItem);
listItem.classList.add(mesh.morphTargetInfluences[index] > 0 ? "active" : "inactive");
morphListContainer.appendChild(listItem);
});
}
handleMaterialClick = (material, listItem) => {
material.visible = !material.visible;
if (material.visible) {
listItem.classList.remove("inactive");
listItem.classList.add("active");
} else {
listItem.classList.remove("active");
listItem.classList.add("inactive");
}
}
handleMorphClick = (index, listItem) => {
mesh.morphTargetInfluences[index] = mesh.morphTargetInfluences[index] === 0 ? 1 : 0;
if (mesh.morphTargetInfluences[index] > 0) {
listItem.classList.remove("inactive");
listItem.classList.add("active");
} else {
listItem.classList.remove("active");
listItem.classList.add("inactive");
}
}
startTextureRedraw = () => {
// Clear any existing interval
if (redrawInterval) {
clearInterval(redrawInterval);
}
// Set up new interval for texture redraw
redrawInterval = setInterval(() => {
redrawTextures();
}, 7000); // 7 seconds
}
redrawTextures = () => {
if (!mesh || !mesh.material) return;
console.log("Redrawing textures...");
mesh.material.forEach((material, index) => {
// Only redraw if the material is checked
if (materialCheckboxes.get(material)) {
// Force texture reload by modifying the texture URL
// This is a common technique to bypass browser caching
if (material.map) {
material.map.needsUpdate = true;
// Add a timestamp to force reload
const originalSrc = material.map.image?.currentSrc || material.map.image?.src;
if (originalSrc) {
material.map.image.src = originalSrc + '?t=' + Date.now();
}
}
// Also update other texture maps if they exist
const textureTypes = ['normalMap', 'specularMap', 'alphaMap', 'emissiveMap', 'bumpMap'];
textureTypes.forEach(type => {
if (material[type]) {
material[type].needsUpdate = true;
}
});
// Mark material for update
material.needsUpdate = true;
}
});
}
onProgress = (xhr) => {
if (xhr.lengthComputable) {
const percentComplete = xhr.loaded / xhr.total * 100;
console.log(Math.round(percentComplete, 2) + '% downloaded');
}
}
onError = (xhr) => { console.log("ERROR"); }
Render = () => {
requestAnimationFrame(Render);
renderer.clear();
renderer.render(scene, camera);
if (ready) {
helper.update(clock.getDelta());
}
}
rotateModelLeft = () => { mesh.rotation.y -= 0.1; }
rotateModelRight = () => { mesh.rotation.y += 0.1; }
slowRotateLeft = () => {
let angle = 0;
const rotateLeft = () => {
if (angle < Math.PI * 2) {
mesh.rotation.y -= rotateSpeed;
angle += rotateSpeed;
requestAnimationFrame(rotateLeft);
}
};
rotateLeft();
}
slowRotateRight = () => {
let angle = 0;
const rotateRight = () => {
if (angle < Math.PI * 2) {
mesh.rotation.y += rotateSpeed;
angle += rotateSpeed;
requestAnimationFrame(rotateRight);
}
};
rotateRight();
}
setupZoomControl = () => {
const zoomSlider = document.getElementById("zoom-slider");
zoomSlider.addEventListener("input", (event) => {
const zoomValue = event.target.value;
camera.position.z = Math.max(10, Math.min(200, zoomValue));
});
}
setupPanControl = () => {
const panSlider = document.getElementById("pan-slider");
panSlider.addEventListener("input", (event) => {
const panValue = event.target.value;
camera.position.y = panValue;
});
}
setupModelDrag = () => {
document.addEventListener("mousedown", (event) => { isDragging = true; });
document.addEventListener("mousemove", (event) => {
if (isDragging) {
const deltaMove = {
x: event.clientX - previousMousePosition.x,
y: event.clientY - previousMousePosition.y,
};
mesh.rotation.y += deltaMove.x * 0.0005;
}
previousMousePosition = { x: event.clientX, y: event.clientY };
});
document.addEventListener("mouseup", () => { isDragging = false; });
}
</script>
</body>
</html>