MMDExporter.js 4.0 KB

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