-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstageloader.html
211 lines (180 loc) · 10.3 KB
/
stageloader.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Coordinates Editor</title>
</head>
<body>
<div id="readystate">...If vmd will cause keychar err, use https://www.mediafire.com/file/9olqch9pazq3fzd/AnimationSmoother.rar...</div>
<input id="coordinates" value="x y z">
<div class="keyboard">
<button id="." onclick="togglePhysicsAndReload()" style="background-color: transparent; border: none; color: transparent;">Move L3.__</button>
<button id="moveUpButton">Move Up</button><br>
<button id="moveLeftButton">Move Left</button>
<button id="moveDownButton">Move Down</button>
<button id="moveRightButton">Move Right</button>
<button id="pullUpButton">PullUp</button><br>
<button id="rotateleftButton">RotateLeft</button>
<button id="rotaterightButton">RotateRight</button>
<a target="_blank" href="https://www.youtube.com/playlist?list=PLBva3abEZvyT-_ajETBGeOCGBA_AFBT5Z#https://www.reddit.com/r/mikumikudance" style="color:blue">r/MMD</a>
<a target="_blank" href="https://codepen.io/ryedai1/pens/tags/?selected_tag=mikumikudance" style="color:gray">(tools)</a>
(<a target="_blank" href="https://ry3yr.github.io/SampleWebMMD-master.zip" style="color:blue">src-DLD</a>)
</div>
<div id="controls">
<button id="birdseye-view">Birdseye View</button>
<button id="standard-view">Standard View</button>
</div>
<script src="./libs/three.min.js"></script>
<script src="./libs/three.js"></script>
<script src="./libs/MMDLoader.js"></script>
<script src="./libs/mmdparser.min.js"></script>
<script>
let scene, renderer, camera, mixer, clock, raycaster, mouse;
const MotionObjects = [
{ id: "001", pose: "001", VmdClip: null, AudioClip: false },
{ id: "002", pose: "002", VmdClip: null, AudioClip: false },
];
let clickedCoordinates = { x: 0, y: 0, z: 0 };
function getQueryStringValue(key) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(key);
}
const stage = getQueryStringValue('stage') || "/sorclair/sorclair.pmx";
let StagePath = stage ? `./stages/${stage.trim()}` : './stages/sorclair/sorclair.pmx';
console.log('StagePath:', StagePath);
function handleClick(x, y, z) {
clickedCoordinates = { x, y, z };
// Update coordinate input field
document.getElementById('coordinates').value = `X: ${x.toFixed(2)}, Y: ${y.toFixed(2)}, Z: ${z.toFixed(2)}`;
// Update local storage
localStorage.setItem('xyz', `${x.toFixed(2)}, ${y.toFixed(2)}, ${z.toFixed(2)}`);
}
function updateCoordinate(axis, delta) {
const inputId = `${axis}-coordinate`;
const inputElement = document.getElementById(inputId);
let currentValue = parseFloat(inputElement.value);
if (isNaN(currentValue)) {
currentValue = 0;
}
currentValue += delta;
inputElement.value = currentValue.toFixed(2);
// Update coordinates display
const x = parseFloat(document.getElementById('x-coordinate').value);
const y = parseFloat(document.getElementById('y-coordinate').value);
const z = parseFloat(document.getElementById('z-coordinate').value);
document.getElementById('coordinates').value = `X: ${x.toFixed(2)}, Y: ${y.toFixed(2)}, Z: ${z.toFixed(2)}`;
// Update local storage
localStorage.setItem('xyz', `${x.toFixed(2)}, ${y.toFixed(2)}, ${z.toFixed(2)}`);
}
function init() {
scene = new THREE.Scene();
renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
camera = new THREE.PerspectiveCamera(100, window.innerWidth / window.innerHeight, 1, 1000);
camera.position.set(0, 19, 20);
clock = new THREE.Clock();
raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2();
const loader = new THREE.MMDLoader();
const lastIndex = StagePath.lastIndexOf("/");
const basePath = StagePath.substring(0, lastIndex);
const vmd1Path = `${basePath}/001.vmd`;
const vmd2Path = `${basePath}/002.vmd`;
loader.load(StagePath, (stageObject) => {
var ambientLight = new THREE.AmbientLight(0xffffff, 1.0); // hardcoded
scene.add(ambientLight);
scene.add(stageObject);
mixer = new THREE.AnimationMixer(stageObject);
loader.loadAnimation(vmd1Path, stageObject, (vmd1Clip) => {
vmd1Clip.name = "001";
console.log(`Loaded VMD: ${vmd1Path}`);
const motionObject1 = MotionObjects.find(obj => obj.id === "001");
if (motionObject1) {
motionObject1.VmdClip = vmd1Clip;
const action1 = mixer.clipAction(vmd1Clip);
action1.play();
} else {
console.warn(`Motion object with id "001" not found.`);
}
}, onProgress, onError);
loader.loadAnimation(vmd2Path, stageObject, (vmd2Clip) => {
vmd2Clip.name = "002";
console.log(`Loaded VMD: ${vmd2Path}`);
const motionObject2 = MotionObjects.find(obj => obj.id === "002");
if (motionObject2) {
motionObject2.VmdClip = vmd2Clip;
const action2 = mixer.clipAction(vmd2Clip);
action2.play();
} else {
console.warn(`Motion object with id "002" not found.`);
}
}, onProgress, onError);
const animate = () => {
requestAnimationFrame(animate);
const delta = clock.getDelta();
mixer.update(delta);
renderer.render(scene, camera);
};
animate();
// Add click event listener to the renderer's DOM element
renderer.domElement.addEventListener('click', (event) => {
mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
raycaster.setFromCamera(mouse, camera);
const intersects = raycaster.intersectObjects(scene.children, true);
if (intersects.length > 0) {
const intersection = intersects[0];
const { x, y, z } = intersection.point;
handleClick(x, y, z);
}
});
}, onProgress, onError);
}
function onProgress(xhr) {
if (xhr.lengthComputable) {
const percentComplete = xhr.loaded / xhr.total * 100;
console.log(Math.round(percentComplete) + '% downloaded');
document.getElementById('readystate').innerHTML = Math.round(percentComplete) + '% downloaded (if stuck, click <a href="audio" target="_blank">here</a>) ' + 'Camera: ready - ' + localStorage.getItem('camid');
}
}
function onError(xhr) {
console.error("Error loading resource:", xhr);
document.getElementById('readystate').textContent = "Error loading resource: " + xhr.statusText;
}
document.getElementById("moveLeftButton").addEventListener("click", () => { camera.position.x -= 1; });
document.getElementById("moveRightButton").addEventListener("click", () => { camera.position.x += 1; });
document.getElementById("moveUpButton").addEventListener("click", () => { camera.position.z -= 5; });
document.getElementById("moveDownButton").addEventListener("click", () => { camera.position.z += 5; });
document.getElementById("pullUpButton").addEventListener("click", () => { camera.position.y += 5; });
document.getElementById('birdseye-view').addEventListener('click', () => {
camera.position.set(0, 100, 0);
camera.lookAt(0, 0, 0);
});
document.getElementById('standard-view').addEventListener('click', () => {
camera.position.set(0, 19, 20);
camera.lookAt(0, 19, 20);
});
window.onload = () => {
init();
};
document.getElementById('coordinates').addEventListener('change', (event) => {
const coordinates = event.target.value.replace('X: ', '').replace('Y: ', '').replace('Z: ', '').split(',').map(val => parseFloat(val.trim()));
const [x, y, z] = coordinates;
if (!isNaN(x) && !isNaN(y) && !isNaN(z)) {
clickedCoordinates = { x, y, z };
localStorage.setItem('xyz', `${x.toFixed(2)}, ${y.toFixed(2)}, ${z.toFixed(2)}`);
}
});
document.getElementById('moveLeftButton').addEventListener('click', () => updateCoordinate('x', -0.1));
document.getElementById('moveRightButton').addEventListener('click', () => updateCoordinate('x', 0.1));
document.getElementById('moveUpButton').addEventListener('click', () => updateCoordinate('y', 0.1));
document.getElementById('moveDownButton').addEventListener('click', () => updateCoordinate('y', -0.1));
document.getElementById('pullUpButton').addEventListener('click', () => updateCoordinate('z', 0.1));
document.getElementById('rotateleftButton').addEventListener('click', () => updateCoordinate('z', -0.1));
document.getElementById('rotaterightButton').addEventListener('click', () => updateCoordinate('z', 0.1));
</script>
</body>
</html>