File size: 8,813 Bytes
8c0ca14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!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>