MMDLoader.js 51 KB

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