Skip to content

Commit 24f807b

Browse files
fix character loading
1 parent b1e8b85 commit 24f807b

File tree

2 files changed

+71
-59
lines changed

2 files changed

+71
-59
lines changed

characterLoader.js

+1-50
Original file line numberDiff line numberDiff line change
@@ -15,56 +15,7 @@ window.addEventListener('sceneReady', () => {
1515
const blobUrl = URL.createObjectURL(blob);
1616

1717
BABYLON.SceneLoader.LoadAssetContainer("", blobUrl, scene, function (container) {
18-
// Clear previous character completely
19-
if (window.currentCharacter) {
20-
// Stop and dispose all animations
21-
scene.animationGroups.slice().forEach(group => {
22-
group.stop();
23-
group.dispose();
24-
});
25-
26-
// Remove all meshes related to current character
27-
scene.meshes.slice().forEach(mesh => {
28-
if (mesh !== scene.ground) { // Keep the ground
29-
mesh.dispose();
30-
}
31-
});
32-
33-
// Clear any skeletons
34-
scene.skeletons.slice().forEach(skeleton => {
35-
skeleton.dispose();
36-
});
37-
38-
window.currentCharacter = null;
39-
window.currentAnimationGroup = null;
40-
}
41-
42-
// Add new meshes and animations
43-
container.addAllToScene();
44-
window.currentCharacter = container.meshes[0];
45-
window.animationGroups = container.animationGroups;
46-
47-
// Position the new character
48-
window.currentCharacter.position = new BABYLON.Vector3(0, 0, 0);
49-
window.currentCharacter.rotation = new BABYLON.Vector3(0, Math.PI/2, 0);
50-
window.currentCharacter.scaling = new BABYLON.Vector3(1, 1, 1);
51-
52-
// Update animation list
53-
const animationList = document.getElementById('animationList');
54-
animationList.innerHTML = '';
55-
container.animationGroups.forEach((group, index) => {
56-
const option = document.createElement('option');
57-
option.value = index;
58-
option.text = group.name;
59-
animationList.appendChild(option);
60-
});
61-
62-
// Play first animation if available
63-
if (container.animationGroups.length > 0) {
64-
window.currentAnimationGroup = container.animationGroups[0];
65-
window.currentAnimationGroup.start(true);
66-
}
67-
18+
window.replaceCharacter(container);
6819
statusMessage.textContent = 'Character loaded successfully!';
6920
URL.revokeObjectURL(blobUrl);
7021
}, null, function (error) {

js/fbxViewer.js

+70-9
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ document.addEventListener('DOMContentLoaded', async function() {
140140
}
141141

142142
// Clamp zoom limits
143-
zoomFactor = Math.max(1, Math.min(20, zoomFactor));
143+
zoomFactor = Math.max(0.001, Math.min(50, zoomFactor));
144144

145145
// Apply the new zoom level
146146
updateOrthoCamera();
@@ -166,16 +166,16 @@ document.addEventListener('DOMContentLoaded', async function() {
166166
camera.panningAxis = new BABYLON.Vector3(1, 1, 0); // Only allow panning in X and Y
167167

168168
// Create a simple ground
169-
const ground = BABYLON.MeshBuilder.CreateGround('ground', {width: 50, height: 50}, scene);
170-
const groundMaterial = new BABYLON.StandardMaterial('groundMat', scene);
171-
groundMaterial.diffuseColor = new BABYLON.Color3(0.2, 0.2, 0.2);
172-
groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
173-
groundMaterial.alpha = 0; // Make the ground transparent
174-
ground.material = groundMaterial;
175-
ground.position.y = 0;
169+
// const ground = BABYLON.MeshBuilder.CreateGround('ground', {width: 50, height: 50}, scene);
170+
// const groundMaterial = new BABYLON.StandardMaterial('groundMat', scene);
171+
// groundMaterial.diffuseColor = new BABYLON.Color3(0.2, 0.2, 0.2);
172+
// groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
173+
// groundMaterial.alpha = 0; // Make the ground transparent
174+
// ground.material = groundMaterial;
175+
// ground.position.y = 0;
176176

177177
// Try to load the character model
178-
await loadCharacterModel();
178+
// await loadCharacterModel();
179179

180180
// Create camera adjustment UI
181181
createCameraControls();
@@ -186,6 +186,67 @@ document.addEventListener('DOMContentLoaded', async function() {
186186
return scene;
187187
};
188188

189+
// Add this after the createScene function but before the event listeners
190+
191+
function replaceCharacter(container) {
192+
if (!scene) return;
193+
194+
// Clear previous character completely
195+
if (window.currentCharacter) {
196+
// Stop and dispose all animations
197+
scene.animationGroups.slice().forEach(group => {
198+
group.stop();
199+
group.dispose();
200+
});
201+
202+
// Remove all meshes related to current character
203+
scene.meshes.slice().forEach(mesh => {
204+
if (mesh !== scene.ground) { // Keep the ground
205+
mesh.dispose();
206+
}
207+
});
208+
209+
// Clear any skeletons
210+
scene.skeletons.slice().forEach(skeleton => {
211+
skeleton.dispose();
212+
});
213+
214+
window.currentCharacter = null;
215+
window.currentAnimationGroup = null;
216+
}
217+
218+
// Add new meshes and animations
219+
container.addAllToScene();
220+
window.currentCharacter = container.meshes[0];
221+
window.animationGroups = container.animationGroups;
222+
223+
// Position the new character
224+
window.currentCharacter.position = new BABYLON.Vector3(0, 0, 0);
225+
window.currentCharacter.rotation = new BABYLON.Vector3(0, Math.PI/2, 0);
226+
window.currentCharacter.scaling = new BABYLON.Vector3(1, 1, 1);
227+
228+
// Update animation list
229+
const animationList = document.getElementById('animationList');
230+
animationList.innerHTML = '';
231+
container.animationGroups.forEach((group, index) => {
232+
const option = document.createElement('option');
233+
option.value = index;
234+
option.text = group.name;
235+
animationList.appendChild(option);
236+
});
237+
238+
// Play first animation if available
239+
if (container.animationGroups.length > 0) {
240+
window.currentAnimationGroup = container.animationGroups[0];
241+
window.currentAnimationGroup.start(true);
242+
}
243+
244+
return window.currentCharacter;
245+
}
246+
247+
// Make the function globally accessible
248+
window.replaceCharacter = replaceCharacter;
249+
189250
// Function to try loading the character model with fallbacks
190251
async function loadCharacterModel() {
191252
showStatus("Loading character model...");

0 commit comments

Comments
 (0)