MMDExporter.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. ( function () {
  2. /**
  3. * Dependencies
  4. * - mmd-parser https://github.com/takahirox/mmd-parser
  5. */
  6. class MMDExporter {
  7. /* TODO: implement
  8. // mesh -> pmd
  9. this.parsePmd = function ( object ) {
  10. };
  11. */
  12. /* TODO: implement
  13. // mesh -> pmx
  14. this.parsePmx = function ( object ) {
  15. };
  16. */
  17. /* TODO: implement
  18. // animation + skeleton -> vmd
  19. this.parseVmd = function ( object ) {
  20. };
  21. */
  22. /*
  23. * skeleton -> vpd
  24. * Returns Shift_JIS encoded Uint8Array. Otherwise return strings.
  25. */
  26. parseVpd( skin, outputShiftJis, useOriginalBones ) {
  27. if ( skin.isSkinnedMesh !== true ) {
  28. console.warn( 'THREE.MMDExporter: parseVpd() requires SkinnedMesh instance.' );
  29. return null;
  30. }
  31. function toStringsFromNumber( num ) {
  32. if ( Math.abs( num ) < 1e-6 ) num = 0;
  33. let a = num.toString();
  34. if ( a.indexOf( '.' ) === - 1 ) {
  35. a += '.';
  36. }
  37. a += '000000';
  38. const index = a.indexOf( '.' );
  39. const d = a.slice( 0, index );
  40. const p = a.slice( index + 1, index + 7 );
  41. return d + '.' + p;
  42. }
  43. function toStringsFromArray( array ) {
  44. const a = [];
  45. for ( let i = 0, il = array.length; i < il; i ++ ) {
  46. a.push( toStringsFromNumber( array[ i ] ) );
  47. }
  48. return a.join( ',' );
  49. }
  50. skin.updateMatrixWorld( true );
  51. const bones = skin.skeleton.bones;
  52. const bones2 = getBindBones( skin );
  53. const position = new THREE.Vector3();
  54. const quaternion = new THREE.Quaternion();
  55. const quaternion2 = new THREE.Quaternion();
  56. const matrix = new THREE.Matrix4();
  57. const array = [];
  58. array.push( 'Vocaloid Pose Data file' );
  59. array.push( '' );
  60. array.push( ( skin.name !== '' ? skin.name.replace( /\s/g, '_' ) : 'skin' ) + '.osm;' );
  61. array.push( bones.length + ';' );
  62. array.push( '' );
  63. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  64. const bone = bones[ i ];
  65. const bone2 = bones2[ i ];
  66. /*
  67. * use the bone matrix saved before solving IK.
  68. * see CCDIKSolver for the detail.
  69. */
  70. if ( useOriginalBones === true && bone.userData.ik !== undefined && bone.userData.ik.originalMatrix !== undefined ) {
  71. matrix.fromArray( bone.userData.ik.originalMatrix );
  72. } else {
  73. matrix.copy( bone.matrix );
  74. }
  75. position.setFromMatrixPosition( matrix );
  76. quaternion.setFromRotationMatrix( matrix );
  77. const pArray = position.sub( bone2.position ).toArray();
  78. const qArray = quaternion2.copy( bone2.quaternion ).conjugate().multiply( quaternion ).toArray(); // right to left
  79. pArray[ 2 ] = - pArray[ 2 ];
  80. qArray[ 0 ] = - qArray[ 0 ];
  81. qArray[ 1 ] = - qArray[ 1 ];
  82. array.push( 'Bone' + i + '{' + bone.name );
  83. array.push( ' ' + toStringsFromArray( pArray ) + ';' );
  84. array.push( ' ' + toStringsFromArray( qArray ) + ';' );
  85. array.push( '}' );
  86. array.push( '' );
  87. }
  88. array.push( '' );
  89. const lines = array.join( '\n' );
  90. return outputShiftJis === true ? unicodeToShiftjis( lines ) : lines;
  91. }
  92. } // Unicode to Shift_JIS table
  93. let u2sTable;
  94. function unicodeToShiftjis( str ) {
  95. if ( u2sTable === undefined ) {
  96. const encoder = new MMDParser.CharsetEncoder(); // eslint-disable-line no-undef
  97. const table = encoder.s2uTable;
  98. u2sTable = {};
  99. const keys = Object.keys( table );
  100. for ( let i = 0, il = keys.length; i < il; i ++ ) {
  101. let key = keys[ i ];
  102. const value = table[ key ];
  103. key = parseInt( key );
  104. u2sTable[ value ] = key;
  105. }
  106. }
  107. const array = [];
  108. for ( let i = 0, il = str.length; i < il; i ++ ) {
  109. const code = str.charCodeAt( i );
  110. const value = u2sTable[ code ];
  111. if ( value === undefined ) {
  112. throw 'cannot convert charcode 0x' + code.toString( 16 );
  113. } else if ( value > 0xff ) {
  114. array.push( value >> 8 & 0xff );
  115. array.push( value & 0xff );
  116. } else {
  117. array.push( value & 0xff );
  118. }
  119. }
  120. return new Uint8Array( array );
  121. }
  122. function getBindBones( skin ) {
  123. // any more efficient ways?
  124. const poseSkin = skin.clone();
  125. poseSkin.pose();
  126. return poseSkin.skeleton.bones;
  127. }
  128. THREE.MMDExporter = MMDExporter;
  129. } )();