MMDLoader.js 51 KB

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