npy / stpete-viewer.html
stpete2's picture
Upload stpete-viewer.html
8c0ca14 verified
raw
history blame
8.81 kB
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>GVRM Viewer</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
font-family: Arial, sans-serif;
background-color: black;
color: white;
}
#threejs-container {
position: absolute;
width: 100%;
height: 100%;
}
#controls {
position: absolute;
top: 20px;
left: 20px;
background: rgba(0, 0, 0, 0.8);
padding: 15px;
border-radius: 8px;
color: white;
min-width: 250px;
}
#controls label {
display: block;
margin-bottom: 8px;
font-weight: bold;
font-size: 14px;
}
#controls input[type="file"] {
width: 100%;
padding: 8px;
margin-bottom: 15px;
border-radius: 4px;
border: none;
font-size: 13px;
background: white;
color: black;
}
#info {
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid rgba(255, 255, 255, 0.3);
font-size: 12px;
line-height: 1.6;
color: #aaa;
}
#info .loaded {
color: #4CAF50;
font-weight: bold;
}
#info .warning {
color: #ff9800;
font-size: 11px;
margin-top: 5px;
}
</style>
</head>
<body>
<div id="threejs-container"></div>
<div id="controls">
<label for="gvrmFile">GVRM File:</label>
<input type="file" id="gvrmFile" accept=".gvrm">
<label for="fbxFile">FBX Motion:</label>
<input type="file" id="fbxFile" accept=".fbx">
<div id="info">
Load a GVRM file, then optionally load an FBX motion file.
</div>
</div>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.min.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/",
"@pixiv/three-vrm": "https://cdn.jsdelivr.net/npm/@pixiv/three-vrm@2.1.0/lib/three-vrm.module.js",
"gaussian-splats-3d": "https://naruya.github.io/gs-edit/lib/gaussian-splats-3d.module.js",
"jszip": "https://cdn.jsdelivr.net/npm/jszip@3.10.1/+esm"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
// πŸ”§ FIXED: Use local gvrm.js instead of external minified version
import { GVRM } from '../gvrm-format/gvrm.js';
// Setup
const container = document.getElementById('threejs-container');
let width = window.innerWidth;
let height = window.innerHeight;
// Renderer (same as main.js)
const renderer = new THREE.WebGLRenderer({ antialias: true });
container.appendChild(renderer.domElement);
renderer.setSize(width, height);
// Camera (same as main.js)
const camera = new THREE.PerspectiveCamera(65.0, width / height, 0.01, 2000.0);
camera.position.set(0.0, 0.8, 2.4);
camera.updateProjectionMatrix();
// OrbitControls (same as main.js)
const controls = new OrbitControls(camera, renderer.domElement);
controls.screenSpacePanning = true;
controls.target.set(0.0, 0.0, 0.0);
controls.minDistance = 0.1;
controls.maxDistance = 1000;
controls.enableDamping = true;
controls.enableZoom = false;
controls.enablePan = false;
controls.update();
// TrackballControls (same as main.js)
const controls2 = new TrackballControls(camera, renderer.domElement);
controls2.noRotate = true;
controls2.target.set(0.0, 0.0, 0.0);
controls2.noPan = false;
controls2.noZoom = false;
controls2.zoomSpeed = 0.25;
controls2.useDummyMouseWheel = true;
controls2.update();
// Scene (same as main.js)
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x000000);
const light = new THREE.DirectionalLight(0xffffff, Math.PI);
light.position.set(10.0, 10.0, 10.0);
scene.add(light);
let gvrm = null;
let hasUpdateError = false;
const infoDiv = document.getElementById('info');
// GVRM file loader
document.getElementById('gvrmFile').addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
if (!file.name.endsWith('.gvrm')) {
console.warn('Please select a .gvrm file');
infoDiv.innerHTML = 'Please select a .gvrm file';
return;
}
try {
infoDiv.innerHTML = 'Loading GVRM...';
const url = URL.createObjectURL(file);
// Remove old GVRM (same as main.js)
if (gvrm) {
if (gvrm.remove) {
await gvrm.remove(scene);
}
gvrm = null;
}
// Load GVRM (same call signature as main.js)
gvrm = await GVRM.load(url, scene, camera, renderer, file.name);
window.gvrm = gvrm;
hasUpdateError = false;
console.log('βœ“ GVRM loaded:', file.name);
console.log('GVRM object:', gvrm);
// Check for bone structure
if (gvrm.character && gvrm.character.currentVrm) {
console.log('βœ“ VRM character found');
console.log('Humanoid bones:', gvrm.character.currentVrm.humanoid.humanBones);
} else {
console.warn('⚠ No VRM character found in GVRM');
}
infoDiv.innerHTML = `<span class="loaded">βœ“ GVRM loaded:</span> ${file.name}<br>Now load an FBX file for motion`;
URL.revokeObjectURL(url);
} catch (err) {
console.error('Failed to load GVRM:', err);
infoDiv.innerHTML = `Error: ${err.message}`;
}
});
// FBX file loader
document.getElementById('fbxFile').addEventListener('change', async (e) => {
const file = e.target.files[0];
if (!file) return;
if (!gvrm) {
console.warn('Load GVRM first');
infoDiv.innerHTML = 'Please load a GVRM file first';
return;
}
if (!file.name.endsWith('.fbx')) {
console.warn('Please select a .fbx file');
infoDiv.innerHTML = 'Please select a .fbx file';
return;
}
try {
infoDiv.innerHTML = 'Loading FBX motion...';
const url = URL.createObjectURL(file);
// Change FBX (same as main.js)
if (gvrm.changeFBX) {
await gvrm.changeFBX(url);
}
console.log('βœ“ FBX motion loaded:', file.name);
// Check if update will work
if (hasUpdateError) {
infoDiv.innerHTML = `<span class="loaded">βœ“ Motion loaded:</span> ${file.name}<br><span class="warning">⚠ Static display mode (animation data incomplete)</span>`;
} else {
infoDiv.innerHTML = `<span class="loaded">βœ“ Motion loaded:</span> ${file.name}`;
}
URL.revokeObjectURL(url);
} catch (err) {
console.error('Failed to load FBX:', err);
infoDiv.innerHTML = `FBX Error: ${err.message}`;
}
});
// Window resize
window.addEventListener('resize', () => {
width = window.innerWidth;
height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.render(scene, camera);
});
// ========================================
// πŸ”§ FIXED: Animation loop like main.js
// No error handling - just continue rendering
// ========================================
function animate() {
// Always continue animation loop, even if gvrm is null
if (!gvrm) {
controls.update();
controls2.update();
renderer.render(scene, camera);
return;
}
// Try to update GVRM animation
// If it fails, just log once and continue rendering
if (gvrm.update) {
try {
gvrm.update();
// Reset error flag if update succeeds
if (hasUpdateError) {
hasUpdateError = false;
console.log('βœ“ GVRM update working again');
}
} catch (err) {
// Only log error once
if (!hasUpdateError) {
hasUpdateError = true;
console.warn('⚠ GVRM update error (will continue rendering):', err.message);
console.log('This is normal for some manually-created GVRM files');
console.log('The model will display correctly but may not animate');
}
// Continue rendering even with error
}
}
controls.update();
controls2.update();
renderer.render(scene, camera);
}
renderer.setAnimationLoop(animate);
</script>
</body>
</html>