MMDLoader.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  1. ( function () {
  2. /**
  3. * Dependencies
  4. * - mmd-parser https://github.com/takahirox/mmd-parser
  5. * - THREE.TGALoader
  6. * - OutlineEffect
  7. *
  8. * MMDLoader creates Three.js Objects from MMD resources as
  9. * PMD, PMX, VMD, and VPD files.
  10. *
  11. * PMD/PMX is a model data format, VMD is a motion data format
  12. * VPD is a posing data format used in MMD(Miku Miku Dance).
  13. *
  14. * MMD official site
  15. * - https://sites.google.com/view/evpvp/
  16. *
  17. * PMD, VMD format (in Japanese)
  18. * - http://blog.goo.ne.jp/torisu_tetosuki/e/209ad341d3ece2b1b4df24abf619d6e4
  19. *
  20. * PMX format
  21. * - https://gist.github.com/felixjones/f8a06bd48f9da9a4539f
  22. *
  23. * TODO
  24. * - light motion in vmd support.
  25. * - SDEF support.
  26. * - uv/material/bone morphing support.
  27. * - more precise grant skinning support.
  28. * - shadow support.
  29. */
  30. /**
  31. * @param {THREE.LoadingManager} manager
  32. */
  33. class MMDLoader extends THREE.Loader {
  34. constructor( manager ) {
  35. super( manager );
  36. this.loader = new THREE.FileLoader( this.manager );
  37. this.parser = null; // lazy generation
  38. this.meshBuilder = new MeshBuilder( this.manager );
  39. this.animationBuilder = new AnimationBuilder();
  40. }
  41. /**
  42. * @param {string} animationPath
  43. * @return {MMDLoader}
  44. */
  45. setAnimationPath( animationPath ) {
  46. this.animationPath = animationPath;
  47. return this;
  48. } // Load MMD assets as Three.js Object
  49. /**
  50. * Loads Model file (.pmd or .pmx) as a THREE.SkinnedMesh.
  51. *
  52. * @param {string} url - url to Model(.pmd or .pmx) file
  53. * @param {function} onLoad
  54. * @param {function} onProgress
  55. * @param {function} onError
  56. */
  57. load( url, onLoad, onProgress, onError ) {
  58. const builder = this.meshBuilder.setCrossOrigin( this.crossOrigin ); // resource path
  59. let resourcePath;
  60. if ( this.resourcePath !== '' ) {
  61. resourcePath = this.resourcePath;
  62. } else if ( this.path !== '' ) {
  63. resourcePath = this.path;
  64. } else {
  65. resourcePath = THREE.LoaderUtils.extractUrlBase( url );
  66. }
  67. const modelExtension = this._extractExtension( url ).toLowerCase(); // Should I detect by seeing header?
  68. if ( modelExtension !== 'pmd' && modelExtension !== 'pmx' ) {
  69. if ( onError ) onError( new Error( 'THREE.MMDLoader: Unknown model file extension .' + modelExtension + '.' ) );
  70. return;
  71. }
  72. this[ modelExtension === 'pmd' ? 'loadPMD' : 'loadPMX' ]( url, function ( data ) {
  73. onLoad( builder.build( data, resourcePath, onProgress, onError ) );
  74. }, onProgress, onError );
  75. }
  76. /**
  77. * Loads Motion file(s) (.vmd) as a THREE.AnimationClip.
  78. * If two or more files are specified, they'll be merged.
  79. *
  80. * @param {string|Array<string>} url - url(s) to animation(.vmd) file(s)
  81. * @param {SkinnedMesh|THREE.Camera} object - tracks will be fitting to this object
  82. * @param {function} onLoad
  83. * @param {function} onProgress
  84. * @param {function} onError
  85. */
  86. loadAnimation( url, object, onLoad, onProgress, onError ) {
  87. const builder = this.animationBuilder;
  88. this.loadVMD( url, function ( vmd ) {
  89. onLoad( object.isCamera ? builder.buildCameraAnimation( vmd ) : builder.build( vmd, object ) );
  90. }, onProgress, onError );
  91. }
  92. /**
  93. * Loads mode file and motion file(s) as an object containing
  94. * a THREE.SkinnedMesh and a THREE.AnimationClip.
  95. * Tracks of THREE.AnimationClip are fitting to the model.
  96. *
  97. * @param {string} modelUrl - url to Model(.pmd or .pmx) file
  98. * @param {string|Array{string}} vmdUrl - url(s) to animation(.vmd) file
  99. * @param {function} onLoad
  100. * @param {function} onProgress
  101. * @param {function} onError
  102. */
  103. loadWithAnimation( modelUrl, vmdUrl, onLoad, onProgress, onError ) {
  104. const scope = this;
  105. this.load( modelUrl, function ( mesh ) {
  106. scope.loadAnimation( vmdUrl, mesh, function ( animation ) {
  107. onLoad( {
  108. mesh: mesh,
  109. animation: animation
  110. } );
  111. }, onProgress, onError );
  112. }, onProgress, onError );
  113. } // Load MMD assets as Object data parsed by MMDParser
  114. /**
  115. * Loads .pmd file as an Object.
  116. *
  117. * @param {string} url - url to .pmd file
  118. * @param {function} onLoad
  119. * @param {function} onProgress
  120. * @param {function} onError
  121. */
  122. loadPMD( url, onLoad, onProgress, onError ) {
  123. const parser = this._getParser();
  124. this.loader.setMimeType( undefined ).setPath( this.path ).setResponseType( 'arraybuffer' ).setRequestHeader( this.requestHeader ).setWithCredentials( this.withCredentials ).load( url, function ( buffer ) {
  125. onLoad( parser.parsePmd( buffer, true ) );
  126. }, onProgress, onError );
  127. }
  128. /**
  129. * Loads .pmx file as an Object.
  130. *
  131. * @param {string} url - url to .pmx file
  132. * @param {function} onLoad
  133. * @param {function} onProgress
  134. * @param {function} onError
  135. */
  136. loadPMX( url, onLoad, onProgress, onError ) {
  137. const parser = this._getParser();
  138. this.loader.setMimeType( undefined ).setPath( this.path ).setResponseType( 'arraybuffer' ).setRequestHeader( this.requestHeader ).setWithCredentials( this.withCredentials ).load( url, function ( buffer ) {
  139. onLoad( parser.parsePmx( buffer, true ) );
  140. }, onProgress, onError );
  141. }
  142. /**
  143. * Loads .vmd file as an Object. If two or more files are specified
  144. * they'll be merged.
  145. *
  146. * @param {string|Array<string>} url - url(s) to .vmd file(s)
  147. * @param {function} onLoad
  148. * @param {function} onProgress
  149. * @param {function} onError
  150. */
  151. loadVMD( url, onLoad, onProgress, onError ) {
  152. const urls = Array.isArray( url ) ? url : [ url ];
  153. const vmds = [];
  154. const vmdNum = urls.length;
  155. const parser = this._getParser();
  156. this.loader.setMimeType( undefined ).setPath( this.animationPath ).setResponseType( 'arraybuffer' ).setRequestHeader( this.requestHeader ).setWithCredentials( this.withCredentials );
  157. for ( let i = 0, il = urls.length; i < il; i ++ ) {
  158. this.loader.load( urls[ i ], function ( buffer ) {
  159. vmds.push( parser.parseVmd( buffer, true ) );
  160. if ( vmds.length === vmdNum ) onLoad( parser.mergeVmds( vmds ) );
  161. }, onProgress, onError );
  162. }
  163. }
  164. /**
  165. * Loads .vpd file as an Object.
  166. *
  167. * @param {string} url - url to .vpd file
  168. * @param {boolean} isUnicode
  169. * @param {function} onLoad
  170. * @param {function} onProgress
  171. * @param {function} onError
  172. */
  173. loadVPD( url, isUnicode, onLoad, onProgress, onError ) {
  174. const parser = this._getParser();
  175. this.loader.setMimeType( isUnicode ? undefined : 'text/plain; charset=shift_jis' ).setPath( this.animationPath ).setResponseType( 'text' ).setRequestHeader( this.requestHeader ).setWithCredentials( this.withCredentials ).load( url, function ( text ) {
  176. onLoad( parser.parseVpd( text, true ) );
  177. }, onProgress, onError );
  178. } // private methods
  179. _extractExtension( url ) {
  180. const index = url.lastIndexOf( '.' );
  181. return index < 0 ? '' : url.slice( index + 1 );
  182. }
  183. _getParser() {
  184. if ( this.parser === null ) {
  185. if ( typeof MMDParser === 'undefined' ) {
  186. throw new Error( 'THREE.MMDLoader: Import MMDParser https://github.com/takahirox/mmd-parser' );
  187. }
  188. this.parser = new MMDParser.Parser(); // eslint-disable-line no-undef
  189. }
  190. return this.parser;
  191. }
  192. } // Utilities
  193. /*
  194. * base64 encoded defalut toon textures toon00.bmp - toon10.bmp.
  195. * We don't need to request external toon image files.
  196. * This idea is from http://www20.atpages.jp/katwat/three.js_r58/examples/mytest37/mmd.three.js
  197. */
  198. const DEFAULT_TOON_TEXTURES = [ 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAN0lEQVRYR+3WQREAMBACsZ5/bWiiMvgEBTt5cW37hjsBBAgQIECAwFwgyfYPCCBAgAABAgTWAh8aBHZBl14e8wAAAABJRU5ErkJggg==', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAOUlEQVRYR+3WMREAMAwDsYY/yoDI7MLwIiP40+RJklfcCCBAgAABAgTqArfb/QMCCBAgQIAAgbbAB3z/e0F3js2cAAAAAElFTkSuQmCC', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAN0lEQVRYR+3WQREAMBACsZ5/B5ilMvgEBTt5cW37hjsBBAgQIECAwFwgyfYPCCBAgAABAgTWAh81dWyx0gFwKAAAAABJRU5ErkJggg==', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAOklEQVRYR+3WoREAMAwDsWb/UQtCy9wxTOQJ/oQ8SXKKGwEECBAgQIBAXeDt7f4BAQQIECBAgEBb4AOz8Hzx7WLY4wAAAABJRU5ErkJggg==', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABPUlEQVRYR+1XwW7CMAy1+f9fZOMysSEOEweEOPRNdm3HbdOyIhAcklPrOs/PLy9RygBALxzcCDQFmgJNgaZAU6Ap0BR4PwX8gsRMVLssMRH5HcpzJEaWL7EVg9F1IHRlyqQohgVr4FGUlUcMJSjcUlDw0zvjeun70cLWmneoyf7NgBTQSniBTQQSuJAZsOnnaczjIMb5hCiuHKxokCrJfVnrctyZL0PkJAJe1HMil4nxeyi3Ypfn1kX51jpPvo/JeCNC4PhVdHdJw2XjBR8brF8PEIhNVn12AgP7uHsTBguBn53MUZCqv7Lp07Pn5k1Ro+uWmUNn7D+M57rtk7aG0Vo73xyF/fbFf0bPJjDXngnGocDTdFhygZjwUQrMNrDcmZlQT50VJ/g/UwNyHpu778+yW+/ksOz/BFo54P4AsUXMfRq7XWsAAAAASUVORK5CYII=', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAACMElEQVRYR+2Xv4pTQRTGf2dubhLdICiii2KnYKHVolhauKWPoGAnNr6BD6CvIVaihYuI2i1ia0BY0MZGRHQXjZj/mSPnnskfNWiWZUlzJ5k7M2cm833nO5Mziej2DWWJRUoCpQKlAntSQCqgw39/iUWAGmh37jrRnVsKlgpiqmkoGVABA7E57fvY+pJDdgKqF6HzFCSADkDq+F6AHABtQ+UMVE5D7zXod7fFNhTEckTbj5XQgHzNN+5tQvc5NG7C6BNkp6D3EmpXHDR+dQAjFLchW3VS9rlw3JBh+B7ys5Cf9z0GW1C/7P32AyBAOAz1q4jGliIH3YPuBnSfQX4OGreTIgEYQb/pBDtPnEQ4CivXYPAWBk13oHrB54yA9QuSn2H4AcKRpEILDt0BUzj+RLR1V5EqjD66NPRBVpLcQwjHoHYJOhsQv6U4mnzmrIXJCFr4LDwm/xBUoboG9XX4cc9VKdYoSA2yk5NQLJaKDUjTBoveG3Z2TElTxwjNK4M3LEZgUdDdruvcXzKBpStgp2NPiWi3ks9ZXxIoFVi+AvHLdc9TqtjL3/aYjpPlrzOcEnK62Szhimdd7xX232zFDTgtxezOu3WNMRLjiKgjtOhHVMd1loynVHvOgjuIIJMaELEqhJAV/RCSLbWTcfPFakFgFlALTRRvx+ok6Hlp/Q+v3fmx90bMyUzaEAhmM3KvHlXTL5DxnbGf/1M8RNNACLL5MNtPxP/mypJAqcDSFfgFhpYqWUzhTEAAAAAASUVORK5CYII=', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=', 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAL0lEQVRYR+3QQREAAAzCsOFfNJPBJ1XQS9r2hsUAAQIECBAgQIAAAQIECBAgsBZ4MUx/ofm2I/kAAAAASUVORK5CYII=' ];
  199. const NON_ALPHA_CHANNEL_FORMATS = [ THREE.RGB_S3TC_DXT1_Format, THREE.RGB_PVRTC_4BPPV1_Format, THREE.RGB_PVRTC_2BPPV1_Format, THREE.RGB_ETC1_Format, THREE.RGB_ETC2_Format ]; // Builders. They build Three.js object from Object data parsed by MMDParser.
  200. /**
  201. * @param {THREE.LoadingManager} manager
  202. */
  203. class MeshBuilder {
  204. constructor( manager ) {
  205. this.crossOrigin = 'anonymous';
  206. this.geometryBuilder = new GeometryBuilder();
  207. this.materialBuilder = new MaterialBuilder( manager );
  208. }
  209. /**
  210. * @param {string} crossOrigin
  211. * @return {MeshBuilder}
  212. */
  213. setCrossOrigin( crossOrigin ) {
  214. this.crossOrigin = crossOrigin;
  215. return this;
  216. }
  217. /**
  218. * @param {Object} data - parsed PMD/PMX data
  219. * @param {string} resourcePath
  220. * @param {function} onProgress
  221. * @param {function} onError
  222. * @return {SkinnedMesh}
  223. */
  224. build( data, resourcePath, onProgress, onError ) {
  225. const geometry = this.geometryBuilder.build( data );
  226. const material = this.materialBuilder.setCrossOrigin( this.crossOrigin ).setResourcePath( resourcePath ).build( data, geometry, onProgress, onError );
  227. const mesh = new THREE.SkinnedMesh( geometry, material );
  228. const skeleton = new THREE.Skeleton( initBones( mesh ) );
  229. mesh.bind( skeleton ); // console.log( mesh ); // for console debug
  230. return mesh;
  231. }
  232. } // TODO: Try to remove this function
  233. function initBones( mesh ) {
  234. const geometry = mesh.geometry;
  235. const bones = [];
  236. if ( geometry && geometry.bones !== undefined ) {
  237. // first, create array of 'Bone' objects from geometry data
  238. for ( let i = 0, il = geometry.bones.length; i < il; i ++ ) {
  239. const gbone = geometry.bones[ i ]; // create new 'Bone' object
  240. const bone = new THREE.Bone();
  241. bones.push( bone ); // apply values
  242. bone.name = gbone.name;
  243. bone.position.fromArray( gbone.pos );
  244. bone.quaternion.fromArray( gbone.rotq );
  245. if ( gbone.scl !== undefined ) bone.scale.fromArray( gbone.scl );
  246. } // second, create bone hierarchy
  247. for ( let i = 0, il = geometry.bones.length; i < il; i ++ ) {
  248. const gbone = geometry.bones[ i ];
  249. if ( gbone.parent !== - 1 && gbone.parent !== null && bones[ gbone.parent ] !== undefined ) {
  250. // subsequent bones in the hierarchy
  251. bones[ gbone.parent ].add( bones[ i ] );
  252. } else {
  253. // topmost bone, immediate child of the skinned mesh
  254. mesh.add( bones[ i ] );
  255. }
  256. }
  257. } // now the bones are part of the scene graph and children of the skinned mesh.
  258. // let's update the corresponding matrices
  259. mesh.updateMatrixWorld( true );
  260. return bones;
  261. } //
  262. class GeometryBuilder {
  263. /**
  264. * @param {Object} data - parsed PMD/PMX data
  265. * @return {BufferGeometry}
  266. */
  267. build( data ) {
  268. // for geometry
  269. const positions = [];
  270. const uvs = [];
  271. const normals = [];
  272. const indices = [];
  273. const groups = [];
  274. const bones = [];
  275. const skinIndices = [];
  276. const skinWeights = [];
  277. const morphTargets = [];
  278. const morphPositions = [];
  279. const iks = [];
  280. const grants = [];
  281. const rigidBodies = [];
  282. const constraints = []; // for work
  283. let offset = 0;
  284. const boneTypeTable = {}; // positions, normals, uvs, skinIndices, skinWeights
  285. for ( let i = 0; i < data.metadata.vertexCount; i ++ ) {
  286. const v = data.vertices[ i ];
  287. for ( let j = 0, jl = v.position.length; j < jl; j ++ ) {
  288. positions.push( v.position[ j ] );
  289. }
  290. for ( let j = 0, jl = v.normal.length; j < jl; j ++ ) {
  291. normals.push( v.normal[ j ] );
  292. }
  293. for ( let j = 0, jl = v.uv.length; j < jl; j ++ ) {
  294. uvs.push( v.uv[ j ] );
  295. }
  296. for ( let j = 0; j < 4; j ++ ) {
  297. skinIndices.push( v.skinIndices.length - 1 >= j ? v.skinIndices[ j ] : 0.0 );
  298. }
  299. for ( let j = 0; j < 4; j ++ ) {
  300. skinWeights.push( v.skinWeights.length - 1 >= j ? v.skinWeights[ j ] : 0.0 );
  301. }
  302. } // indices
  303. for ( let i = 0; i < data.metadata.faceCount; i ++ ) {
  304. const face = data.faces[ i ];
  305. for ( let j = 0, jl = face.indices.length; j < jl; j ++ ) {
  306. indices.push( face.indices[ j ] );
  307. }
  308. } // groups
  309. for ( let i = 0; i < data.metadata.materialCount; i ++ ) {
  310. const material = data.materials[ i ];
  311. groups.push( {
  312. offset: offset * 3,
  313. count: material.faceCount * 3
  314. } );
  315. offset += material.faceCount;
  316. } // bones
  317. for ( let i = 0; i < data.metadata.rigidBodyCount; i ++ ) {
  318. const body = data.rigidBodies[ i ];
  319. let value = boneTypeTable[ body.boneIndex ]; // keeps greater number if already value is set without any special reasons
  320. value = value === undefined ? body.type : Math.max( body.type, value );
  321. boneTypeTable[ body.boneIndex ] = value;
  322. }
  323. for ( let i = 0; i < data.metadata.boneCount; i ++ ) {
  324. const boneData = data.bones[ i ];
  325. const bone = {
  326. index: i,
  327. transformationClass: boneData.transformationClass,
  328. parent: boneData.parentIndex,
  329. name: boneData.name,
  330. pos: boneData.position.slice( 0, 3 ),
  331. rotq: [ 0, 0, 0, 1 ],
  332. scl: [ 1, 1, 1 ],
  333. rigidBodyType: boneTypeTable[ i ] !== undefined ? boneTypeTable[ i ] : - 1
  334. };
  335. if ( bone.parent !== - 1 ) {
  336. bone.pos[ 0 ] -= data.bones[ bone.parent ].position[ 0 ];
  337. bone.pos[ 1 ] -= data.bones[ bone.parent ].position[ 1 ];
  338. bone.pos[ 2 ] -= data.bones[ bone.parent ].position[ 2 ];
  339. }
  340. bones.push( bone );
  341. } // iks
  342. // TODO: remove duplicated codes between PMD and PMX
  343. if ( data.metadata.format === 'pmd' ) {
  344. for ( let i = 0; i < data.metadata.ikCount; i ++ ) {
  345. const ik = data.iks[ i ];
  346. const param = {
  347. target: ik.target,
  348. effector: ik.effector,
  349. iteration: ik.iteration,
  350. maxAngle: ik.maxAngle * 4,
  351. links: []
  352. };
  353. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  354. const link = {};
  355. link.index = ik.links[ j ].index;
  356. link.enabled = true;
  357. if ( data.bones[ link.index ].name.indexOf( 'ひざ' ) >= 0 ) {
  358. link.limitation = new THREE.Vector3( 1.0, 0.0, 0.0 );
  359. }
  360. param.links.push( link );
  361. }
  362. iks.push( param );
  363. }
  364. } else {
  365. for ( let i = 0; i < data.metadata.boneCount; i ++ ) {
  366. const ik = data.bones[ i ].ik;
  367. if ( ik === undefined ) continue;
  368. const param = {
  369. target: i,
  370. effector: ik.effector,
  371. iteration: ik.iteration,
  372. maxAngle: ik.maxAngle,
  373. links: []
  374. };
  375. for ( let j = 0, jl = ik.links.length; j < jl; j ++ ) {
  376. const link = {};
  377. link.index = ik.links[ j ].index;
  378. link.enabled = true;
  379. if ( ik.links[ j ].angleLimitation === 1 ) {
  380. // Revert if rotationMin/Max doesn't work well
  381. // link.limitation = new THREE.Vector3( 1.0, 0.0, 0.0 );
  382. const rotationMin = ik.links[ j ].lowerLimitationAngle;
  383. const rotationMax = ik.links[ j ].upperLimitationAngle; // Convert Left to Right coordinate by myself because
  384. // MMDParser doesn't convert. It's a MMDParser's bug
  385. const tmp1 = - rotationMax[ 0 ];
  386. const tmp2 = - rotationMax[ 1 ];
  387. rotationMax[ 0 ] = - rotationMin[ 0 ];
  388. rotationMax[ 1 ] = - rotationMin[ 1 ];
  389. rotationMin[ 0 ] = tmp1;
  390. rotationMin[ 1 ] = tmp2;
  391. link.rotationMin = new THREE.Vector3().fromArray( rotationMin );
  392. link.rotationMax = new THREE.Vector3().fromArray( rotationMax );
  393. }
  394. param.links.push( link );
  395. }
  396. iks.push( param ); // Save the reference even from bone data for efficiently
  397. // simulating PMX animation system
  398. bones[ i ].ik = param;
  399. }
  400. } // grants
  401. if ( data.metadata.format === 'pmx' ) {
  402. // bone index -> grant entry map
  403. const grantEntryMap = {};
  404. for ( let i = 0; i < data.metadata.boneCount; i ++ ) {
  405. const boneData = data.bones[ i ];
  406. const grant = boneData.grant;
  407. if ( grant === undefined ) continue;
  408. const param = {
  409. index: i,
  410. parentIndex: grant.parentIndex,
  411. ratio: grant.ratio,
  412. isLocal: grant.isLocal,
  413. affectRotation: grant.affectRotation,
  414. affectPosition: grant.affectPosition,
  415. transformationClass: boneData.transformationClass
  416. };
  417. grantEntryMap[ i ] = {
  418. parent: null,
  419. children: [],
  420. param: param,
  421. visited: false
  422. };
  423. }
  424. const rootEntry = {
  425. parent: null,
  426. children: [],
  427. param: null,
  428. visited: false
  429. }; // Build a tree representing grant hierarchy
  430. for ( const boneIndex in grantEntryMap ) {
  431. const grantEntry = grantEntryMap[ boneIndex ];
  432. const parentGrantEntry = grantEntryMap[ grantEntry.parentIndex ] || rootEntry;
  433. grantEntry.parent = parentGrantEntry;
  434. parentGrantEntry.children.push( grantEntry );
  435. } // Sort grant parameters from parents to children because
  436. // grant uses parent's transform that parent's grant is already applied
  437. // so grant should be applied in order from parents to children
  438. function traverse( entry ) {
  439. if ( entry.param ) {
  440. grants.push( entry.param ); // Save the reference even from bone data for efficiently
  441. // simulating PMX animation system
  442. bones[ entry.param.index ].grant = entry.param;
  443. }
  444. entry.visited = true;
  445. for ( let i = 0, il = entry.children.length; i < il; i ++ ) {
  446. const child = entry.children[ i ]; // Cut off a loop if exists. (Is a grant loop invalid?)
  447. if ( ! child.visited ) traverse( child );
  448. }
  449. }
  450. traverse( rootEntry );
  451. } // morph
  452. function updateAttributes( attribute, morph, ratio ) {
  453. for ( let i = 0; i < morph.elementCount; i ++ ) {
  454. const element = morph.elements[ i ];
  455. let index;
  456. if ( data.metadata.format === 'pmd' ) {
  457. index = data.morphs[ 0 ].elements[ element.index ].index;
  458. } else {
  459. index = element.index;
  460. }
  461. attribute.array[ index * 3 + 0 ] += element.position[ 0 ] * ratio;
  462. attribute.array[ index * 3 + 1 ] += element.position[ 1 ] * ratio;
  463. attribute.array[ index * 3 + 2 ] += element.position[ 2 ] * ratio;
  464. }
  465. }
  466. for ( let i = 0; i < data.metadata.morphCount; i ++ ) {
  467. const morph = data.morphs[ i ];
  468. const params = {
  469. name: morph.name
  470. };
  471. const attribute = new THREE.Float32BufferAttribute( data.metadata.vertexCount * 3, 3 );
  472. attribute.name = morph.name;
  473. for ( let j = 0; j < data.metadata.vertexCount * 3; j ++ ) {
  474. attribute.array[ j ] = positions[ j ];
  475. }
  476. if ( data.metadata.format === 'pmd' ) {
  477. if ( i !== 0 ) {
  478. updateAttributes( attribute, morph, 1.0 );
  479. }
  480. } else {
  481. if ( morph.type === 0 ) {
  482. // group
  483. for ( let j = 0; j < morph.elementCount; j ++ ) {
  484. const morph2 = data.morphs[ morph.elements[ j ].index ];
  485. const ratio = morph.elements[ j ].ratio;
  486. if ( morph2.type === 1 ) {
  487. updateAttributes( attribute, morph2, ratio );
  488. } else { // TODO: implement
  489. }
  490. }
  491. } else if ( morph.type === 1 ) {
  492. // vertex
  493. updateAttributes( attribute, morph, 1.0 );
  494. } else if ( morph.type === 2 ) { // bone
  495. // TODO: implement
  496. } else if ( morph.type === 3 ) { // uv
  497. // TODO: implement
  498. } else if ( morph.type === 4 ) { // additional uv1
  499. // TODO: implement
  500. } else if ( morph.type === 5 ) { // additional uv2
  501. // TODO: implement
  502. } else if ( morph.type === 6 ) { // additional uv3
  503. // TODO: implement
  504. } else if ( morph.type === 7 ) { // additional uv4
  505. // TODO: implement
  506. } else if ( morph.type === 8 ) { // material
  507. // TODO: implement
  508. }
  509. }
  510. morphTargets.push( params );
  511. morphPositions.push( attribute );
  512. } // rigid bodies from rigidBodies field.
  513. for ( let i = 0; i < data.metadata.rigidBodyCount; i ++ ) {
  514. const rigidBody = data.rigidBodies[ i ];
  515. const params = {};
  516. for ( const key in rigidBody ) {
  517. params[ key ] = rigidBody[ key ];
  518. }
  519. /*
  520. * RigidBody position parameter in PMX seems global position
  521. * while the one in PMD seems offset from corresponding bone.
  522. * So unify being offset.
  523. */
  524. if ( data.metadata.format === 'pmx' ) {
  525. if ( params.boneIndex !== - 1 ) {
  526. const bone = data.bones[ params.boneIndex ];
  527. params.position[ 0 ] -= bone.position[ 0 ];
  528. params.position[ 1 ] -= bone.position[ 1 ];
  529. params.position[ 2 ] -= bone.position[ 2 ];
  530. }
  531. }
  532. rigidBodies.push( params );
  533. } // constraints from constraints field.
  534. for ( let i = 0; i < data.metadata.constraintCount; i ++ ) {
  535. const constraint = data.constraints[ i ];
  536. const params = {};
  537. for ( const key in constraint ) {
  538. params[ key ] = constraint[ key ];
  539. }
  540. const bodyA = rigidBodies[ params.rigidBodyIndex1 ];
  541. const bodyB = rigidBodies[ params.rigidBodyIndex2 ]; // Refer to http://www20.atpages.jp/katwat/wp/?p=4135
  542. if ( bodyA.type !== 0 && bodyB.type === 2 ) {
  543. if ( bodyA.boneIndex !== - 1 && bodyB.boneIndex !== - 1 && data.bones[ bodyB.boneIndex ].parentIndex === bodyA.boneIndex ) {
  544. bodyB.type = 1;
  545. }
  546. }
  547. constraints.push( params );
  548. } // build THREE.BufferGeometry.
  549. const geometry = new THREE.BufferGeometry();
  550. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  551. geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ) );
  552. geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvs, 2 ) );
  553. geometry.setAttribute( 'skinIndex', new THREE.Uint16BufferAttribute( skinIndices, 4 ) );
  554. geometry.setAttribute( 'skinWeight', new THREE.Float32BufferAttribute( skinWeights, 4 ) );
  555. geometry.setIndex( indices );
  556. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  557. geometry.addGroup( groups[ i ].offset, groups[ i ].count, i );
  558. }
  559. geometry.bones = bones;
  560. geometry.morphTargets = morphTargets;
  561. geometry.morphAttributes.position = morphPositions;
  562. geometry.morphTargetsRelative = false;
  563. geometry.userData.MMD = {
  564. bones: bones,
  565. iks: iks,
  566. grants: grants,
  567. rigidBodies: rigidBodies,
  568. constraints: constraints,
  569. format: data.metadata.format
  570. };
  571. geometry.computeBoundingSphere();
  572. return geometry;
  573. }
  574. } //
  575. /**
  576. * @param {THREE.LoadingManager} manager
  577. */
  578. class MaterialBuilder {
  579. constructor( manager ) {
  580. this.manager = manager;
  581. this.textureLoader = new THREE.TextureLoader( this.manager );
  582. this.tgaLoader = null; // lazy generation
  583. this.crossOrigin = 'anonymous';
  584. this.resourcePath = undefined;
  585. }
  586. /**
  587. * @param {string} crossOrigin
  588. * @return {MaterialBuilder}
  589. */
  590. setCrossOrigin( crossOrigin ) {
  591. this.crossOrigin = crossOrigin;
  592. return this;
  593. }
  594. /**
  595. * @param {string} resourcePath
  596. * @return {MaterialBuilder}
  597. */
  598. setResourcePath( resourcePath ) {
  599. this.resourcePath = resourcePath;
  600. return this;
  601. }
  602. /**
  603. * @param {Object} data - parsed PMD/PMX data
  604. * @param {BufferGeometry} geometry - some properties are dependend on geometry
  605. * @param {function} onProgress
  606. * @param {function} onError
  607. * @return {Array<MMDToonMaterial>}
  608. */
  609. build( data, geometry
  610. /*, onProgress, onError */
  611. ) {
  612. const materials = [];
  613. const textures = {};
  614. this.textureLoader.setCrossOrigin( this.crossOrigin ); // materials
  615. for ( let i = 0; i < data.metadata.materialCount; i ++ ) {
  616. const material = data.materials[ i ];
  617. const params = {
  618. userData: {
  619. MMD: {}
  620. }
  621. };
  622. if ( material.name !== undefined ) params.name = material.name;
  623. /*
  624. * THREE.Color
  625. *
  626. * MMD MMDToonMaterial
  627. * ambient - emissive * a
  628. * (a = 1.0 without map texture or 0.2 with map texture)
  629. *
  630. * MMDToonMaterial doesn't have ambient. Set it to emissive instead.
  631. * It'll be too bright if material has map texture so using coef 0.2.
  632. */
  633. params.diffuse = new THREE.Color().fromArray( material.diffuse );
  634. params.opacity = material.diffuse[ 3 ];
  635. params.specular = new THREE.Color().fromArray( material.specular );
  636. params.shininess = material.shininess;
  637. params.emissive = new THREE.Color().fromArray( material.ambient );
  638. params.transparent = params.opacity !== 1.0; //
  639. params.fog = true; // blend
  640. params.blending = THREE.CustomBlending;
  641. params.blendSrc = THREE.SrcAlphaFactor;
  642. params.blendDst = THREE.OneMinusSrcAlphaFactor;
  643. params.blendSrcAlpha = THREE.SrcAlphaFactor;
  644. params.blendDstAlpha = THREE.DstAlphaFactor; // side
  645. if ( data.metadata.format === 'pmx' && ( material.flag & 0x1 ) === 1 ) {
  646. params.side = THREE.DoubleSide;
  647. } else {
  648. params.side = params.opacity === 1.0 ? THREE.FrontSide : THREE.DoubleSide;
  649. }
  650. if ( data.metadata.format === 'pmd' ) {
  651. // map, envMap
  652. if ( material.fileName ) {
  653. const fileName = material.fileName;
  654. const fileNames = fileName.split( '*' ); // fileNames[ 0 ]: mapFileName
  655. // fileNames[ 1 ]: envMapFileName( optional )
  656. params.map = this._loadTexture( fileNames[ 0 ], textures );
  657. if ( fileNames.length > 1 ) {
  658. const extension = fileNames[ 1 ].slice( - 4 ).toLowerCase();
  659. params.envMap = this._loadTexture( fileNames[ 1 ], textures );
  660. params.combine = extension === '.sph' ? THREE.MultiplyOperation : THREE.AddOperation;
  661. }
  662. } // gradientMap
  663. const toonFileName = material.toonIndex === - 1 ? 'toon00.bmp' : data.toonTextures[ material.toonIndex ].fileName;
  664. params.gradientMap = this._loadTexture( toonFileName, textures, {
  665. isToonTexture: true,
  666. isDefaultToonTexture: this._isDefaultToonTexture( toonFileName )
  667. } ); // parameters for OutlineEffect
  668. params.userData.outlineParameters = {
  669. thickness: material.edgeFlag === 1 ? 0.003 : 0.0,
  670. color: [ 0, 0, 0 ],
  671. alpha: 1.0,
  672. visible: material.edgeFlag === 1
  673. };
  674. } else {
  675. // map
  676. if ( material.textureIndex !== - 1 ) {
  677. params.map = this._loadTexture( data.textures[ material.textureIndex ], textures ); // Since PMX spec don't have standard to list map files except color map and env map,
  678. // we need to save file name for further mapping, like matching normal map file names after model loaded.
  679. // ref: https://gist.github.com/felixjones/f8a06bd48f9da9a4539f#texture
  680. params.userData.MMD.mapFileName = data.textures[ material.textureIndex ];
  681. } // envMap TODO: support m.envFlag === 3
  682. if ( material.envTextureIndex !== - 1 && ( material.envFlag === 1 || material.envFlag == 2 ) ) {
  683. params.matcap = this._loadTexture( data.textures[ material.envTextureIndex ], textures ); // Same as color map above, keep file name in userData for further usage.
  684. params.userData.MMD.matcapFileName = data.textures[ material.envTextureIndex ];
  685. params.matcapCombine = material.envFlag === 1 ? THREE.MultiplyOperation : THREE.AddOperation;
  686. } // gradientMap
  687. let toonFileName, isDefaultToon;
  688. if ( material.toonIndex === - 1 || material.toonFlag !== 0 ) {
  689. toonFileName = 'toon' + ( '0' + ( material.toonIndex + 1 ) ).slice( - 2 ) + '.bmp';
  690. isDefaultToon = true;
  691. } else {
  692. toonFileName = data.textures[ material.toonIndex ];
  693. isDefaultToon = false;
  694. }
  695. params.gradientMap = this._loadTexture( toonFileName, textures, {
  696. isToonTexture: true,
  697. isDefaultToonTexture: isDefaultToon
  698. } ); // parameters for OutlineEffect
  699. params.userData.outlineParameters = {
  700. thickness: material.edgeSize / 300,
  701. // TODO: better calculation?
  702. color: material.edgeColor.slice( 0, 3 ),
  703. alpha: material.edgeColor[ 3 ],
  704. visible: ( material.flag & 0x10 ) !== 0 && material.edgeSize > 0.0
  705. };
  706. }
  707. if ( params.map !== undefined ) {
  708. if ( ! params.transparent ) {
  709. this._checkImageTransparency( params.map, geometry, i );
  710. }
  711. params.emissive.multiplyScalar( 0.2 );
  712. }
  713. materials.push( new MMDToonMaterial( params ) );
  714. }
  715. if ( data.metadata.format === 'pmx' ) {
  716. // set transparent true if alpha morph is defined.
  717. function checkAlphaMorph( elements, materials ) {
  718. for ( let i = 0, il = elements.length; i < il; i ++ ) {
  719. const element = elements[ i ];
  720. if ( element.index === - 1 ) continue;
  721. const material = materials[ element.index ];
  722. if ( material.opacity !== element.diffuse[ 3 ] ) {
  723. material.transparent = true;
  724. }
  725. }
  726. }
  727. for ( let i = 0, il = data.morphs.length; i < il; i ++ ) {
  728. const morph = data.morphs[ i ];
  729. const elements = morph.elements;
  730. if ( morph.type === 0 ) {
  731. for ( let j = 0, jl = elements.length; j < jl; j ++ ) {
  732. const morph2 = data.morphs[ elements[ j ].index ];
  733. if ( morph2.type !== 8 ) continue;
  734. checkAlphaMorph( morph2.elements, materials );
  735. }
  736. } else if ( morph.type === 8 ) {
  737. checkAlphaMorph( elements, materials );
  738. }
  739. }
  740. }
  741. return materials;
  742. } // private methods
  743. _getTGALoader() {
  744. if ( this.tgaLoader === null ) {
  745. if ( THREE.TGALoader === undefined ) {
  746. throw new Error( 'THREE.MMDLoader: Import THREE.TGALoader' );
  747. }
  748. this.tgaLoader = new THREE.TGALoader( this.manager );
  749. }
  750. return this.tgaLoader;
  751. }
  752. _isDefaultToonTexture( name ) {
  753. if ( name.length !== 10 ) return false;
  754. return /toon(10|0[0-9])\.bmp/.test( name );
  755. }
  756. _loadTexture( filePath, textures, params, onProgress, onError ) {
  757. params = params || {};
  758. const scope = this;
  759. let fullPath;
  760. if ( params.isDefaultToonTexture === true ) {
  761. let index;
  762. try {
  763. index = parseInt( filePath.match( /toon([0-9]{2})\.bmp$/ )[ 1 ] );
  764. } catch ( e ) {
  765. console.warn( 'THREE.MMDLoader: ' + filePath + ' seems like a ' + 'not right default texture path. Using toon00.bmp instead.' );
  766. index = 0;
  767. }
  768. fullPath = DEFAULT_TOON_TEXTURES[ index ];
  769. } else {
  770. fullPath = this.resourcePath + filePath;
  771. }
  772. if ( textures[ fullPath ] !== undefined ) return textures[ fullPath ];
  773. let loader = this.manager.getHandler( fullPath );
  774. if ( loader === null ) {
  775. loader = filePath.slice( - 4 ).toLowerCase() === '.tga' ? this._getTGALoader() : this.textureLoader;
  776. }
  777. const texture = loader.load( fullPath, function ( t ) {
  778. // MMD toon texture is Axis-Y oriented
  779. // but Three.js gradient map is Axis-X oriented.
  780. // So here replaces the toon texture image with the rotated one.
  781. if ( params.isToonTexture === true ) {
  782. t.image = scope._getRotatedImage( t.image );
  783. t.magFilter = THREE.NearestFilter;
  784. t.minFilter = THREE.NearestFilter;
  785. }
  786. t.flipY = false;
  787. t.wrapS = THREE.RepeatWrapping;
  788. t.wrapT = THREE.RepeatWrapping;
  789. for ( let i = 0; i < texture.readyCallbacks.length; i ++ ) {
  790. texture.readyCallbacks[ i ]( texture );
  791. }
  792. delete texture.readyCallbacks;
  793. }, onProgress, onError );
  794. texture.readyCallbacks = [];
  795. textures[ fullPath ] = texture;
  796. return texture;
  797. }
  798. _getRotatedImage( image ) {
  799. const canvas = document.createElement( 'canvas' );
  800. const context = canvas.getContext( '2d' );
  801. const width = image.width;
  802. const height = image.height;
  803. canvas.width = width;
  804. canvas.height = height;
  805. context.clearRect( 0, 0, width, height );
  806. context.translate( width / 2.0, height / 2.0 );
  807. context.rotate( 0.5 * Math.PI ); // 90.0 * Math.PI / 180.0
  808. context.translate( - width / 2.0, - height / 2.0 );
  809. context.drawImage( image, 0, 0 );
  810. return context.getImageData( 0, 0, width, height );
  811. } // Check if the partial image area used by the texture is transparent.
  812. _checkImageTransparency( map, geometry, groupIndex ) {
  813. map.readyCallbacks.push( function ( texture ) {
  814. // Is there any efficient ways?
  815. function createImageData( image ) {
  816. const canvas = document.createElement( 'canvas' );
  817. canvas.width = image.width;
  818. canvas.height = image.height;
  819. const context = canvas.getContext( '2d' );
  820. context.drawImage( image, 0, 0 );
  821. return context.getImageData( 0, 0, canvas.width, canvas.height );
  822. }
  823. function detectImageTransparency( image, uvs, indices ) {
  824. const width = image.width;
  825. const height = image.height;
  826. const data = image.data;
  827. const threshold = 253;
  828. if ( data.length / ( width * height ) !== 4 ) return false;
  829. for ( let i = 0; i < indices.length; i += 3 ) {
  830. const centerUV = {
  831. x: 0.0,
  832. y: 0.0
  833. };
  834. for ( let j = 0; j < 3; j ++ ) {
  835. const index = indices[ i * 3 + j ];
  836. const uv = {
  837. x: uvs[ index * 2 + 0 ],
  838. y: uvs[ index * 2 + 1 ]
  839. };
  840. if ( getAlphaByUv( image, uv ) < threshold ) return true;
  841. centerUV.x += uv.x;
  842. centerUV.y += uv.y;
  843. }
  844. centerUV.x /= 3;
  845. centerUV.y /= 3;
  846. if ( getAlphaByUv( image, centerUV ) < threshold ) return true;
  847. }
  848. return false;
  849. }
  850. /*
  851. * This method expects
  852. * texture.flipY = false
  853. * texture.wrapS = THREE.RepeatWrapping
  854. * texture.wrapT = THREE.RepeatWrapping
  855. * TODO: more precise
  856. */
  857. function getAlphaByUv( image, uv ) {
  858. const width = image.width;
  859. const height = image.height;
  860. let x = Math.round( uv.x * width ) % width;
  861. let y = Math.round( uv.y * height ) % height;
  862. if ( x < 0 ) x += width;
  863. if ( y < 0 ) y += height;
  864. const index = y * width + x;
  865. return image.data[ index * 4 + 3 ];
  866. }
  867. if ( texture.isCompressedTexture === true ) {
  868. if ( NON_ALPHA_CHANNEL_FORMATS.includes( texture.format ) ) {
  869. map.transparent = false;
  870. } else {
  871. // any other way to check transparency of CompressedTexture?
  872. map.transparent = true;
  873. }
  874. return;
  875. }
  876. const imageData = texture.image.data !== undefined ? texture.image : createImageData( texture.image );
  877. const group = geometry.groups[ groupIndex ];
  878. if ( detectImageTransparency( imageData, geometry.attributes.uv.array, geometry.index.array.slice( group.start, group.start + group.count ) ) ) {
  879. map.transparent = true;
  880. }
  881. } );
  882. }
  883. } //
  884. class AnimationBuilder {
  885. /**
  886. * @param {Object} vmd - parsed VMD data
  887. * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
  888. * @return {AnimationClip}
  889. */
  890. build( vmd, mesh ) {
  891. // combine skeletal and morph animations
  892. const tracks = this.buildSkeletalAnimation( vmd, mesh ).tracks;
  893. const tracks2 = this.buildMorphAnimation( vmd, mesh ).tracks;
  894. for ( let i = 0, il = tracks2.length; i < il; i ++ ) {
  895. tracks.push( tracks2[ i ] );
  896. }
  897. return new THREE.AnimationClip( '', - 1, tracks );
  898. }
  899. /**
  900. * @param {Object} vmd - parsed VMD data
  901. * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
  902. * @return {AnimationClip}
  903. */
  904. buildSkeletalAnimation( vmd, mesh ) {
  905. function pushInterpolation( array, interpolation, index ) {
  906. array.push( interpolation[ index + 0 ] / 127 ); // x1
  907. array.push( interpolation[ index + 8 ] / 127 ); // x2
  908. array.push( interpolation[ index + 4 ] / 127 ); // y1
  909. array.push( interpolation[ index + 12 ] / 127 ); // y2
  910. }
  911. const tracks = [];
  912. const motions = {};
  913. const bones = mesh.skeleton.bones;
  914. const boneNameDictionary = {};
  915. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  916. boneNameDictionary[ bones[ i ].name ] = true;
  917. }
  918. for ( let i = 0; i < vmd.metadata.motionCount; i ++ ) {
  919. const motion = vmd.motions[ i ];
  920. const boneName = motion.boneName;
  921. if ( boneNameDictionary[ boneName ] === undefined ) continue;
  922. motions[ boneName ] = motions[ boneName ] || [];
  923. motions[ boneName ].push( motion );
  924. }
  925. for ( const key in motions ) {
  926. const array = motions[ key ];
  927. array.sort( function ( a, b ) {
  928. return a.frameNum - b.frameNum;
  929. } );
  930. const times = [];
  931. const positions = [];
  932. const rotations = [];
  933. const pInterpolations = [];
  934. const rInterpolations = [];
  935. const basePosition = mesh.skeleton.getBoneByName( key ).position.toArray();
  936. for ( let i = 0, il = array.length; i < il; i ++ ) {
  937. const time = array[ i ].frameNum / 30;
  938. const position = array[ i ].position;
  939. const rotation = array[ i ].rotation;
  940. const interpolation = array[ i ].interpolation;
  941. times.push( time );
  942. for ( let j = 0; j < 3; j ++ ) positions.push( basePosition[ j ] + position[ j ] );
  943. for ( let j = 0; j < 4; j ++ ) rotations.push( rotation[ j ] );
  944. for ( let j = 0; j < 3; j ++ ) pushInterpolation( pInterpolations, interpolation, j );
  945. pushInterpolation( rInterpolations, interpolation, 3 );
  946. }
  947. const targetName = '.bones[' + key + ']';
  948. tracks.push( this._createTrack( targetName + '.position', THREE.VectorKeyframeTrack, times, positions, pInterpolations ) );
  949. tracks.push( this._createTrack( targetName + '.quaternion', THREE.QuaternionKeyframeTrack, times, rotations, rInterpolations ) );
  950. }
  951. return new THREE.AnimationClip( '', - 1, tracks );
  952. }
  953. /**
  954. * @param {Object} vmd - parsed VMD data
  955. * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
  956. * @return {AnimationClip}
  957. */
  958. buildMorphAnimation( vmd, mesh ) {
  959. const tracks = [];
  960. const morphs = {};
  961. const morphTargetDictionary = mesh.morphTargetDictionary;
  962. for ( let i = 0; i < vmd.metadata.morphCount; i ++ ) {
  963. const morph = vmd.morphs[ i ];
  964. const morphName = morph.morphName;
  965. if ( morphTargetDictionary[ morphName ] === undefined ) continue;
  966. morphs[ morphName ] = morphs[ morphName ] || [];
  967. morphs[ morphName ].push( morph );
  968. }
  969. for ( const key in morphs ) {
  970. const array = morphs[ key ];
  971. array.sort( function ( a, b ) {
  972. return a.frameNum - b.frameNum;
  973. } );
  974. const times = [];
  975. const values = [];
  976. for ( let i = 0, il = array.length; i < il; i ++ ) {
  977. times.push( array[ i ].frameNum / 30 );
  978. values.push( array[ i ].weight );
  979. }
  980. tracks.push( new THREE.NumberKeyframeTrack( '.morphTargetInfluences[' + morphTargetDictionary[ key ] + ']', times, values ) );
  981. }
  982. return new THREE.AnimationClip( '', - 1, tracks );
  983. }
  984. /**
  985. * @param {Object} vmd - parsed VMD data
  986. * @return {AnimationClip}
  987. */
  988. buildCameraAnimation( vmd ) {
  989. function pushVector3( array, vec ) {
  990. array.push( vec.x );
  991. array.push( vec.y );
  992. array.push( vec.z );
  993. }
  994. function pushQuaternion( array, q ) {
  995. array.push( q.x );
  996. array.push( q.y );
  997. array.push( q.z );
  998. array.push( q.w );
  999. }
  1000. function pushInterpolation( array, interpolation, index ) {
  1001. array.push( interpolation[ index * 4 + 0 ] / 127 ); // x1
  1002. array.push( interpolation[ index * 4 + 1 ] / 127 ); // x2
  1003. array.push( interpolation[ index * 4 + 2 ] / 127 ); // y1
  1004. array.push( interpolation[ index * 4 + 3 ] / 127 ); // y2
  1005. }
  1006. const cameras = vmd.cameras === undefined ? [] : vmd.cameras.slice();
  1007. cameras.sort( function ( a, b ) {
  1008. return a.frameNum - b.frameNum;
  1009. } );
  1010. const times = [];
  1011. const centers = [];
  1012. const quaternions = [];
  1013. const positions = [];
  1014. const fovs = [];
  1015. const cInterpolations = [];
  1016. const qInterpolations = [];
  1017. const pInterpolations = [];
  1018. const fInterpolations = [];
  1019. const quaternion = new THREE.Quaternion();
  1020. const euler = new THREE.Euler();
  1021. const position = new THREE.Vector3();
  1022. const center = new THREE.Vector3();
  1023. for ( let i = 0, il = cameras.length; i < il; i ++ ) {
  1024. const motion = cameras[ i ];
  1025. const time = motion.frameNum / 30;
  1026. const pos = motion.position;
  1027. const rot = motion.rotation;
  1028. const distance = motion.distance;
  1029. const fov = motion.fov;
  1030. const interpolation = motion.interpolation;
  1031. times.push( time );
  1032. position.set( 0, 0, - distance );
  1033. center.set( pos[ 0 ], pos[ 1 ], pos[ 2 ] );
  1034. euler.set( - rot[ 0 ], - rot[ 1 ], - rot[ 2 ] );
  1035. quaternion.setFromEuler( euler );
  1036. position.add( center );
  1037. position.applyQuaternion( quaternion );
  1038. pushVector3( centers, center );
  1039. pushQuaternion( quaternions, quaternion );
  1040. pushVector3( positions, position );
  1041. fovs.push( fov );
  1042. for ( let j = 0; j < 3; j ++ ) {
  1043. pushInterpolation( cInterpolations, interpolation, j );
  1044. }
  1045. pushInterpolation( qInterpolations, interpolation, 3 ); // use the same parameter for x, y, z axis.
  1046. for ( let j = 0; j < 3; j ++ ) {
  1047. pushInterpolation( pInterpolations, interpolation, 4 );
  1048. }
  1049. pushInterpolation( fInterpolations, interpolation, 5 );
  1050. }
  1051. const tracks = []; // I expect an object whose name 'target' exists under THREE.Camera
  1052. tracks.push( this._createTrack( 'target.position', THREE.VectorKeyframeTrack, times, centers, cInterpolations ) );
  1053. tracks.push( this._createTrack( '.quaternion', THREE.QuaternionKeyframeTrack, times, quaternions, qInterpolations ) );
  1054. tracks.push( this._createTrack( '.position', THREE.VectorKeyframeTrack, times, positions, pInterpolations ) );
  1055. tracks.push( this._createTrack( '.fov', THREE.NumberKeyframeTrack, times, fovs, fInterpolations ) );
  1056. return new THREE.AnimationClip( '', - 1, tracks );
  1057. } // private method
  1058. _createTrack( node, typedKeyframeTrack, times, values, interpolations ) {
  1059. /*
  1060. * optimizes here not to let KeyframeTrackPrototype optimize
  1061. * because KeyframeTrackPrototype optimizes times and values but
  1062. * doesn't optimize interpolations.
  1063. */
  1064. if ( times.length > 2 ) {
  1065. times = times.slice();
  1066. values = values.slice();
  1067. interpolations = interpolations.slice();
  1068. const stride = values.length / times.length;
  1069. const interpolateStride = interpolations.length / times.length;
  1070. let index = 1;
  1071. for ( let aheadIndex = 2, endIndex = times.length; aheadIndex < endIndex; aheadIndex ++ ) {
  1072. for ( let i = 0; i < stride; i ++ ) {
  1073. if ( values[ index * stride + i ] !== values[ ( index - 1 ) * stride + i ] || values[ index * stride + i ] !== values[ aheadIndex * stride + i ] ) {
  1074. index ++;
  1075. break;
  1076. }
  1077. }
  1078. if ( aheadIndex > index ) {
  1079. times[ index ] = times[ aheadIndex ];
  1080. for ( let i = 0; i < stride; i ++ ) {
  1081. values[ index * stride + i ] = values[ aheadIndex * stride + i ];
  1082. }
  1083. for ( let i = 0; i < interpolateStride; i ++ ) {
  1084. interpolations[ index * interpolateStride + i ] = interpolations[ aheadIndex * interpolateStride + i ];
  1085. }
  1086. }
  1087. }
  1088. times.length = index + 1;
  1089. values.length = ( index + 1 ) * stride;
  1090. interpolations.length = ( index + 1 ) * interpolateStride;
  1091. }
  1092. const track = new typedKeyframeTrack( node, times, values );
  1093. track.createInterpolant = function InterpolantFactoryMethodCubicBezier( result ) {
  1094. return new CubicBezierInterpolation( this.times, this.values, this.getValueSize(), result, new Float32Array( interpolations ) );
  1095. };
  1096. return track;
  1097. }
  1098. } // interpolation
  1099. class CubicBezierInterpolation extends THREE.Interpolant {
  1100. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer, params ) {
  1101. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  1102. this.interpolationParams = params;
  1103. }
  1104. interpolate_( i1, t0, t, t1 ) {
  1105. const result = this.resultBuffer;
  1106. const values = this.sampleValues;
  1107. const stride = this.valueSize;
  1108. const params = this.interpolationParams;
  1109. const offset1 = i1 * stride;
  1110. const offset0 = offset1 - stride; // No interpolation if next key frame is in one frame in 30fps.
  1111. // This is from MMD animation spec.
  1112. // '1.5' is for precision loss. times are Float32 in Three.js Animation system.
  1113. const weight1 = t1 - t0 < 1 / 30 * 1.5 ? 0.0 : ( t - t0 ) / ( t1 - t0 );
  1114. if ( stride === 4 ) {
  1115. // THREE.Quaternion
  1116. const x1 = params[ i1 * 4 + 0 ];
  1117. const x2 = params[ i1 * 4 + 1 ];
  1118. const y1 = params[ i1 * 4 + 2 ];
  1119. const y2 = params[ i1 * 4 + 3 ];
  1120. const ratio = this._calculate( x1, x2, y1, y2, weight1 );
  1121. THREE.Quaternion.slerpFlat( result, 0, values, offset0, values, offset1, ratio );
  1122. } else if ( stride === 3 ) {
  1123. // THREE.Vector3
  1124. for ( let i = 0; i !== stride; ++ i ) {
  1125. const x1 = params[ i1 * 12 + i * 4 + 0 ];
  1126. const x2 = params[ i1 * 12 + i * 4 + 1 ];
  1127. const y1 = params[ i1 * 12 + i * 4 + 2 ];
  1128. const y2 = params[ i1 * 12 + i * 4 + 3 ];
  1129. const ratio = this._calculate( x1, x2, y1, y2, weight1 );
  1130. result[ i ] = values[ offset0 + i ] * ( 1 - ratio ) + values[ offset1 + i ] * ratio;
  1131. }
  1132. } else {
  1133. // Number
  1134. const x1 = params[ i1 * 4 + 0 ];
  1135. const x2 = params[ i1 * 4 + 1 ];
  1136. const y1 = params[ i1 * 4 + 2 ];
  1137. const y2 = params[ i1 * 4 + 3 ];
  1138. const ratio = this._calculate( x1, x2, y1, y2, weight1 );
  1139. result[ 0 ] = values[ offset0 ] * ( 1 - ratio ) + values[ offset1 ] * ratio;
  1140. }
  1141. return result;
  1142. }
  1143. _calculate( x1, x2, y1, y2, x ) {
  1144. /*
  1145. * Cubic Bezier curves
  1146. * https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves
  1147. *
  1148. * B(t) = ( 1 - t ) ^ 3 * P0
  1149. * + 3 * ( 1 - t ) ^ 2 * t * P1
  1150. * + 3 * ( 1 - t ) * t^2 * P2
  1151. * + t ^ 3 * P3
  1152. * ( 0 <= t <= 1 )
  1153. *
  1154. * MMD uses Cubic Bezier curves for bone and camera animation interpolation.
  1155. * http://d.hatena.ne.jp/edvakf/20111016/1318716097
  1156. *
  1157. * x = ( 1 - t ) ^ 3 * x0
  1158. * + 3 * ( 1 - t ) ^ 2 * t * x1
  1159. * + 3 * ( 1 - t ) * t^2 * x2
  1160. * + t ^ 3 * x3
  1161. * y = ( 1 - t ) ^ 3 * y0
  1162. * + 3 * ( 1 - t ) ^ 2 * t * y1
  1163. * + 3 * ( 1 - t ) * t^2 * y2
  1164. * + t ^ 3 * y3
  1165. * ( x0 = 0, y0 = 0 )
  1166. * ( x3 = 1, y3 = 1 )
  1167. * ( 0 <= t, x1, x2, y1, y2 <= 1 )
  1168. *
  1169. * Here solves this equation with Bisection method,
  1170. * https://en.wikipedia.org/wiki/Bisection_method
  1171. * gets t, and then calculate y.
  1172. *
  1173. * f(t) = 3 * ( 1 - t ) ^ 2 * t * x1
  1174. * + 3 * ( 1 - t ) * t^2 * x2
  1175. * + t ^ 3 - x = 0
  1176. *
  1177. * (Another option: Newton's method
  1178. * https://en.wikipedia.org/wiki/Newton%27s_method)
  1179. */
  1180. let c = 0.5;
  1181. let t = c;
  1182. let s = 1.0 - t;
  1183. const loop = 15;
  1184. const eps = 1e-5;
  1185. const math = Math;
  1186. let sst3, stt3, ttt;
  1187. for ( let i = 0; i < loop; i ++ ) {
  1188. sst3 = 3.0 * s * s * t;
  1189. stt3 = 3.0 * s * t * t;
  1190. ttt = t * t * t;
  1191. const ft = sst3 * x1 + stt3 * x2 + ttt - x;
  1192. if ( math.abs( ft ) < eps ) break;
  1193. c /= 2.0;
  1194. t += ft < 0 ? c : - c;
  1195. s = 1.0 - t;
  1196. }
  1197. return sst3 * y1 + stt3 * y2 + ttt;
  1198. }
  1199. }
  1200. class MMDToonMaterial extends THREE.ShaderMaterial {
  1201. constructor( parameters ) {
  1202. super();
  1203. this._matcapCombine = THREE.AddOperation;
  1204. this.emissiveIntensity = 1.0;
  1205. this.normalMapType = THREE.TangentSpaceNormalMap;
  1206. this.combine = THREE.MultiplyOperation;
  1207. this.wireframeLinecap = 'round';
  1208. this.wireframeLinejoin = 'round';
  1209. this.flatShading = false;
  1210. this.lights = true;
  1211. this.vertexShader = THREE.MMDToonShader.vertexShader;
  1212. this.fragmentShader = THREE.MMDToonShader.fragmentShader;
  1213. this.defines = Object.assign( {}, THREE.MMDToonShader.defines );
  1214. Object.defineProperty( this, 'matcapCombine', {
  1215. get: function () {
  1216. return this._matcapCombine;
  1217. },
  1218. set: function ( value ) {
  1219. this._matcapCombine = value;
  1220. switch ( value ) {
  1221. case THREE.MultiplyOperation:
  1222. this.defines.MATCAP_BLENDING_MULTIPLY = true;
  1223. delete this.defines.MATCAP_BLENDING_ADD;
  1224. break;
  1225. default:
  1226. case THREE.AddOperation:
  1227. this.defines.MATCAP_BLENDING_ADD = true;
  1228. delete this.defines.MATCAP_BLENDING_MULTIPLY;
  1229. break;
  1230. }
  1231. }
  1232. } );
  1233. this.uniforms = THREE.UniformsUtils.clone( THREE.MMDToonShader.uniforms ); // merged from MeshToon/Phong/MatcapMaterial
  1234. const exposePropertyNames = [ 'specular', 'shininess', 'opacity', 'diffuse', 'map', 'matcap', 'gradientMap', 'lightMap', 'lightMapIntensity', 'aoMap', 'aoMapIntensity', 'emissive', 'emissiveMap', 'bumpMap', 'bumpScale', 'normalMap', 'normalScale', 'displacemantBias', 'displacemantMap', 'displacemantScale', 'specularMap', 'alphaMap', 'envMap', 'reflectivity', 'refractionRatio' ];
  1235. for ( const propertyName of exposePropertyNames ) {
  1236. Object.defineProperty( this, propertyName, {
  1237. get: function () {
  1238. return this.uniforms[ propertyName ].value;
  1239. },
  1240. set: function ( value ) {
  1241. this.uniforms[ propertyName ].value = value;
  1242. }
  1243. } );
  1244. }
  1245. Object.defineProperty( this, 'color', Object.getOwnPropertyDescriptor( this, 'diffuse' ) );
  1246. this.setValues( parameters );
  1247. }
  1248. copy( source ) {
  1249. super.copy( source );
  1250. this.matcapCombine = source.matcapCombine;
  1251. this.emissiveIntensity = source.emissiveIntensity;
  1252. this.normalMapType = source.normalMapType;
  1253. this.combine = source.combine;
  1254. this.wireframeLinecap = source.wireframeLinecap;
  1255. this.wireframeLinejoin = source.wireframeLinejoin;
  1256. this.flatShading = source.flatShading;
  1257. return this;
  1258. }
  1259. }
  1260. MMDToonMaterial.prototype.isMMDToonMaterial = true;
  1261. THREE.MMDLoader = MMDLoader;
  1262. } )();