@@ -355,6 +355,9 @@ THREE.MMDLoader = ( function () {
355355
356356 var mesh = new THREE . SkinnedMesh ( geometry , material ) ;
357357
358+ var skeleton = new THREE . Skeleton ( initBones ( mesh ) ) ;
359+ mesh . bind ( skeleton ) ;
360+
358361 // console.log( mesh ); // for console debug
359362
360363 return mesh ;
@@ -363,6 +366,70 @@ THREE.MMDLoader = ( function () {
363366
364367 } ;
365368
369+ // TODO: Try to remove this function
370+
371+ function initBones ( mesh ) {
372+
373+ var geometry = mesh . geometry ;
374+
375+ var bones = [ ] , bone , gbone ;
376+ var i , il ;
377+
378+ if ( geometry && geometry . bones !== undefined ) {
379+
380+ // first, create array of 'Bone' objects from geometry data
381+
382+ for ( i = 0 , il = geometry . bones . length ; i < il ; i ++ ) {
383+
384+ gbone = geometry . bones [ i ] ;
385+
386+ // create new 'Bone' object
387+
388+ bone = new THREE . Bone ( ) ;
389+ bones . push ( bone ) ;
390+
391+ // apply values
392+
393+ bone . name = gbone . name ;
394+ bone . position . fromArray ( gbone . pos ) ;
395+ bone . quaternion . fromArray ( gbone . rotq ) ;
396+ if ( gbone . scl !== undefined ) bone . scale . fromArray ( gbone . scl ) ;
397+
398+ }
399+
400+ // second, create bone hierarchy
401+
402+ for ( i = 0 , il = geometry . bones . length ; i < il ; i ++ ) {
403+
404+ gbone = geometry . bones [ i ] ;
405+
406+ if ( ( gbone . parent !== - 1 ) && ( gbone . parent !== null ) && ( bones [ gbone . parent ] !== undefined ) ) {
407+
408+ // subsequent bones in the hierarchy
409+
410+ bones [ gbone . parent ] . add ( bones [ i ] ) ;
411+
412+ } else {
413+
414+ // topmost bone, immediate child of the skinned mesh
415+
416+ mesh . add ( bones [ i ] ) ;
417+
418+ }
419+
420+ }
421+
422+ }
423+
424+ // now the bones are part of the scene graph and children of the skinned mesh.
425+ // let's update the corresponding matrices
426+
427+ mesh . updateMatrixWorld ( true ) ;
428+
429+ return bones ;
430+
431+ }
432+
366433 //
367434
368435 function GeometryBuilder ( ) {
0 commit comments