MMDLoader.js 50 KB

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