MMDLoader.js 51 KB

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