MMDLoader.js 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278
  1. import {
  2. AddOperation,
  3. AnimationClip,
  4. Bone,
  5. BufferGeometry,
  6. Color,
  7. ColorManagement,
  8. CustomBlending,
  9. TangentSpaceNormalMap,
  10. DoubleSide,
  11. DstAlphaFactor,
  12. Euler,
  13. FileLoader,
  14. Float32BufferAttribute,
  15. FrontSide,
  16. Interpolant,
  17. Loader,
  18. LoaderUtils,
  19. UniformsUtils,
  20. ShaderMaterial,
  21. MultiplyOperation,
  22. NearestFilter,
  23. NumberKeyframeTrack,
  24. OneMinusSrcAlphaFactor,
  25. Quaternion,
  26. QuaternionKeyframeTrack,
  27. RepeatWrapping,
  28. Skeleton,
  29. SkinnedMesh,
  30. SrcAlphaFactor,
  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().fromArray( material.diffuse );
  751. params.opacity = material.diffuse[ 3 ];
  752. params.specular = new Color().fromArray( material.specular );
  753. params.shininess = material.shininess;
  754. params.emissive = new Color().fromArray( material.ambient );
  755. params.transparent = params.opacity !== 1.0;
  756. if ( ColorManagement.enabled === true ) {
  757. params.diffuse.convertSRGBToLinear();
  758. params.specular.convertSRGBToLinear();
  759. params.emissive.convertSRGBToLinear();
  760. }
  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. for ( let i = 0; i < texture.readyCallbacks.length; i ++ ) {
  946. texture.readyCallbacks[ i ]( texture );
  947. }
  948. delete texture.readyCallbacks;
  949. }, onProgress, onError );
  950. texture.readyCallbacks = [];
  951. textures[ fullPath ] = texture;
  952. return texture;
  953. }
  954. _getRotatedImage( image ) {
  955. const canvas = document.createElement( 'canvas' );
  956. const context = canvas.getContext( '2d' );
  957. const width = image.width;
  958. const height = image.height;
  959. canvas.width = width;
  960. canvas.height = height;
  961. context.clearRect( 0, 0, width, height );
  962. context.translate( width / 2.0, height / 2.0 );
  963. context.rotate( 0.5 * Math.PI ); // 90.0 * Math.PI / 180.0
  964. context.translate( - width / 2.0, - height / 2.0 );
  965. context.drawImage( image, 0, 0 );
  966. return context.getImageData( 0, 0, width, height );
  967. }
  968. // Check if the partial image area used by the texture is transparent.
  969. _checkImageTransparency( map, geometry, groupIndex ) {
  970. map.readyCallbacks.push( function ( texture ) {
  971. // Is there any efficient ways?
  972. function createImageData( image ) {
  973. const canvas = document.createElement( 'canvas' );
  974. canvas.width = image.width;
  975. canvas.height = image.height;
  976. const context = canvas.getContext( '2d' );
  977. context.drawImage( image, 0, 0 );
  978. return context.getImageData( 0, 0, canvas.width, canvas.height );
  979. }
  980. function detectImageTransparency( image, uvs, indices ) {
  981. const width = image.width;
  982. const height = image.height;
  983. const data = image.data;
  984. const threshold = 253;
  985. if ( data.length / ( width * height ) !== 4 ) return false;
  986. for ( let i = 0; i < indices.length; i += 3 ) {
  987. const centerUV = { x: 0.0, y: 0.0 };
  988. for ( let j = 0; j < 3; j ++ ) {
  989. const index = indices[ i * 3 + j ];
  990. const uv = { x: uvs[ index * 2 + 0 ], y: uvs[ index * 2 + 1 ] };
  991. if ( getAlphaByUv( image, uv ) < threshold ) return true;
  992. centerUV.x += uv.x;
  993. centerUV.y += uv.y;
  994. }
  995. centerUV.x /= 3;
  996. centerUV.y /= 3;
  997. if ( getAlphaByUv( image, centerUV ) < threshold ) return true;
  998. }
  999. return false;
  1000. }
  1001. /*
  1002. * This method expects
  1003. * texture.flipY = false
  1004. * texture.wrapS = RepeatWrapping
  1005. * texture.wrapT = RepeatWrapping
  1006. * TODO: more precise
  1007. */
  1008. function getAlphaByUv( image, uv ) {
  1009. const width = image.width;
  1010. const height = image.height;
  1011. let x = Math.round( uv.x * width ) % width;
  1012. let y = Math.round( uv.y * height ) % height;
  1013. if ( x < 0 ) x += width;
  1014. if ( y < 0 ) y += height;
  1015. const index = y * width + x;
  1016. return image.data[ index * 4 + 3 ];
  1017. }
  1018. if ( texture.isCompressedTexture === true ) {
  1019. if ( NON_ALPHA_CHANNEL_FORMATS.includes( texture.format ) ) {
  1020. map.transparent = false;
  1021. } else {
  1022. // any other way to check transparency of CompressedTexture?
  1023. map.transparent = true;
  1024. }
  1025. return;
  1026. }
  1027. const imageData = texture.image.data !== undefined
  1028. ? texture.image
  1029. : createImageData( texture.image );
  1030. const group = geometry.groups[ groupIndex ];
  1031. if ( detectImageTransparency(
  1032. imageData,
  1033. geometry.attributes.uv.array,
  1034. geometry.index.array.slice( group.start, group.start + group.count ) ) ) {
  1035. map.transparent = true;
  1036. }
  1037. } );
  1038. }
  1039. }
  1040. //
  1041. class AnimationBuilder {
  1042. /**
  1043. * @param {Object} vmd - parsed VMD data
  1044. * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
  1045. * @return {AnimationClip}
  1046. */
  1047. build( vmd, mesh ) {
  1048. // combine skeletal and morph animations
  1049. const tracks = this.buildSkeletalAnimation( vmd, mesh ).tracks;
  1050. const tracks2 = this.buildMorphAnimation( vmd, mesh ).tracks;
  1051. for ( let i = 0, il = tracks2.length; i < il; i ++ ) {
  1052. tracks.push( tracks2[ i ] );
  1053. }
  1054. return new AnimationClip( '', - 1, tracks );
  1055. }
  1056. /**
  1057. * @param {Object} vmd - parsed VMD data
  1058. * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
  1059. * @return {AnimationClip}
  1060. */
  1061. buildSkeletalAnimation( vmd, mesh ) {
  1062. function pushInterpolation( array, interpolation, index ) {
  1063. array.push( interpolation[ index + 0 ] / 127 ); // x1
  1064. array.push( interpolation[ index + 8 ] / 127 ); // x2
  1065. array.push( interpolation[ index + 4 ] / 127 ); // y1
  1066. array.push( interpolation[ index + 12 ] / 127 ); // y2
  1067. }
  1068. const tracks = [];
  1069. const motions = {};
  1070. const bones = mesh.skeleton.bones;
  1071. const boneNameDictionary = {};
  1072. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  1073. boneNameDictionary[ bones[ i ].name ] = true;
  1074. }
  1075. for ( let i = 0; i < vmd.metadata.motionCount; i ++ ) {
  1076. const motion = vmd.motions[ i ];
  1077. const boneName = motion.boneName;
  1078. if ( boneNameDictionary[ boneName ] === undefined ) continue;
  1079. motions[ boneName ] = motions[ boneName ] || [];
  1080. motions[ boneName ].push( motion );
  1081. }
  1082. for ( const key in motions ) {
  1083. const array = motions[ key ];
  1084. array.sort( function ( a, b ) {
  1085. return a.frameNum - b.frameNum;
  1086. } );
  1087. const times = [];
  1088. const positions = [];
  1089. const rotations = [];
  1090. const pInterpolations = [];
  1091. const rInterpolations = [];
  1092. const basePosition = mesh.skeleton.getBoneByName( key ).position.toArray();
  1093. for ( let i = 0, il = array.length; i < il; i ++ ) {
  1094. const time = array[ i ].frameNum / 30;
  1095. const position = array[ i ].position;
  1096. const rotation = array[ i ].rotation;
  1097. const interpolation = array[ i ].interpolation;
  1098. times.push( time );
  1099. for ( let j = 0; j < 3; j ++ ) positions.push( basePosition[ j ] + position[ j ] );
  1100. for ( let j = 0; j < 4; j ++ ) rotations.push( rotation[ j ] );
  1101. for ( let j = 0; j < 3; j ++ ) pushInterpolation( pInterpolations, interpolation, j );
  1102. pushInterpolation( rInterpolations, interpolation, 3 );
  1103. }
  1104. const targetName = '.bones[' + key + ']';
  1105. tracks.push( this._createTrack( targetName + '.position', VectorKeyframeTrack, times, positions, pInterpolations ) );
  1106. tracks.push( this._createTrack( targetName + '.quaternion', QuaternionKeyframeTrack, times, rotations, rInterpolations ) );
  1107. }
  1108. return new AnimationClip( '', - 1, tracks );
  1109. }
  1110. /**
  1111. * @param {Object} vmd - parsed VMD data
  1112. * @param {SkinnedMesh} mesh - tracks will be fitting to mesh
  1113. * @return {AnimationClip}
  1114. */
  1115. buildMorphAnimation( vmd, mesh ) {
  1116. const tracks = [];
  1117. const morphs = {};
  1118. const morphTargetDictionary = mesh.morphTargetDictionary;
  1119. for ( let i = 0; i < vmd.metadata.morphCount; i ++ ) {
  1120. const morph = vmd.morphs[ i ];
  1121. const morphName = morph.morphName;
  1122. if ( morphTargetDictionary[ morphName ] === undefined ) continue;
  1123. morphs[ morphName ] = morphs[ morphName ] || [];
  1124. morphs[ morphName ].push( morph );
  1125. }
  1126. for ( const key in morphs ) {
  1127. const array = morphs[ key ];
  1128. array.sort( function ( a, b ) {
  1129. return a.frameNum - b.frameNum;
  1130. } );
  1131. const times = [];
  1132. const values = [];
  1133. for ( let i = 0, il = array.length; i < il; i ++ ) {
  1134. times.push( array[ i ].frameNum / 30 );
  1135. values.push( array[ i ].weight );
  1136. }
  1137. tracks.push( new NumberKeyframeTrack( '.morphTargetInfluences[' + morphTargetDictionary[ key ] + ']', times, values ) );
  1138. }
  1139. return new AnimationClip( '', - 1, tracks );
  1140. }
  1141. /**
  1142. * @param {Object} vmd - parsed VMD data
  1143. * @return {AnimationClip}
  1144. */
  1145. buildCameraAnimation( vmd ) {
  1146. function pushVector3( array, vec ) {
  1147. array.push( vec.x );
  1148. array.push( vec.y );
  1149. array.push( vec.z );
  1150. }
  1151. function pushQuaternion( array, q ) {
  1152. array.push( q.x );
  1153. array.push( q.y );
  1154. array.push( q.z );
  1155. array.push( q.w );
  1156. }
  1157. function pushInterpolation( array, interpolation, index ) {
  1158. array.push( interpolation[ index * 4 + 0 ] / 127 ); // x1
  1159. array.push( interpolation[ index * 4 + 1 ] / 127 ); // x2
  1160. array.push( interpolation[ index * 4 + 2 ] / 127 ); // y1
  1161. array.push( interpolation[ index * 4 + 3 ] / 127 ); // y2
  1162. }
  1163. const cameras = vmd.cameras === undefined ? [] : vmd.cameras.slice();
  1164. cameras.sort( function ( a, b ) {
  1165. return a.frameNum - b.frameNum;
  1166. } );
  1167. const times = [];
  1168. const centers = [];
  1169. const quaternions = [];
  1170. const positions = [];
  1171. const fovs = [];
  1172. const cInterpolations = [];
  1173. const qInterpolations = [];
  1174. const pInterpolations = [];
  1175. const fInterpolations = [];
  1176. const quaternion = new Quaternion();
  1177. const euler = new Euler();
  1178. const position = new Vector3();
  1179. const center = new Vector3();
  1180. for ( let i = 0, il = cameras.length; i < il; i ++ ) {
  1181. const motion = cameras[ i ];
  1182. const time = motion.frameNum / 30;
  1183. const pos = motion.position;
  1184. const rot = motion.rotation;
  1185. const distance = motion.distance;
  1186. const fov = motion.fov;
  1187. const interpolation = motion.interpolation;
  1188. times.push( time );
  1189. position.set( 0, 0, - distance );
  1190. center.set( pos[ 0 ], pos[ 1 ], pos[ 2 ] );
  1191. euler.set( - rot[ 0 ], - rot[ 1 ], - rot[ 2 ] );
  1192. quaternion.setFromEuler( euler );
  1193. position.add( center );
  1194. position.applyQuaternion( quaternion );
  1195. pushVector3( centers, center );
  1196. pushQuaternion( quaternions, quaternion );
  1197. pushVector3( positions, position );
  1198. fovs.push( fov );
  1199. for ( let j = 0; j < 3; j ++ ) {
  1200. pushInterpolation( cInterpolations, interpolation, j );
  1201. }
  1202. pushInterpolation( qInterpolations, interpolation, 3 );
  1203. // use the same parameter for x, y, z axis.
  1204. for ( let j = 0; j < 3; j ++ ) {
  1205. pushInterpolation( pInterpolations, interpolation, 4 );
  1206. }
  1207. pushInterpolation( fInterpolations, interpolation, 5 );
  1208. }
  1209. const tracks = [];
  1210. // I expect an object whose name 'target' exists under THREE.Camera
  1211. tracks.push( this._createTrack( 'target.position', VectorKeyframeTrack, times, centers, cInterpolations ) );
  1212. tracks.push( this._createTrack( '.quaternion', QuaternionKeyframeTrack, times, quaternions, qInterpolations ) );
  1213. tracks.push( this._createTrack( '.position', VectorKeyframeTrack, times, positions, pInterpolations ) );
  1214. tracks.push( this._createTrack( '.fov', NumberKeyframeTrack, times, fovs, fInterpolations ) );
  1215. return new AnimationClip( '', - 1, tracks );
  1216. }
  1217. // private method
  1218. _createTrack( node, typedKeyframeTrack, times, values, interpolations ) {
  1219. /*
  1220. * optimizes here not to let KeyframeTrackPrototype optimize
  1221. * because KeyframeTrackPrototype optimizes times and values but
  1222. * doesn't optimize interpolations.
  1223. */
  1224. if ( times.length > 2 ) {
  1225. times = times.slice();
  1226. values = values.slice();
  1227. interpolations = interpolations.slice();
  1228. const stride = values.length / times.length;
  1229. const interpolateStride = interpolations.length / times.length;
  1230. let index = 1;
  1231. for ( let aheadIndex = 2, endIndex = times.length; aheadIndex < endIndex; aheadIndex ++ ) {
  1232. for ( let i = 0; i < stride; i ++ ) {
  1233. if ( values[ index * stride + i ] !== values[ ( index - 1 ) * stride + i ] ||
  1234. values[ index * stride + i ] !== values[ aheadIndex * stride + i ] ) {
  1235. index ++;
  1236. break;
  1237. }
  1238. }
  1239. if ( aheadIndex > index ) {
  1240. times[ index ] = times[ aheadIndex ];
  1241. for ( let i = 0; i < stride; i ++ ) {
  1242. values[ index * stride + i ] = values[ aheadIndex * stride + i ];
  1243. }
  1244. for ( let i = 0; i < interpolateStride; i ++ ) {
  1245. interpolations[ index * interpolateStride + i ] = interpolations[ aheadIndex * interpolateStride + i ];
  1246. }
  1247. }
  1248. }
  1249. times.length = index + 1;
  1250. values.length = ( index + 1 ) * stride;
  1251. interpolations.length = ( index + 1 ) * interpolateStride;
  1252. }
  1253. const track = new typedKeyframeTrack( node, times, values );
  1254. track.createInterpolant = function InterpolantFactoryMethodCubicBezier( result ) {
  1255. return new CubicBezierInterpolation( this.times, this.values, this.getValueSize(), result, new Float32Array( interpolations ) );
  1256. };
  1257. return track;
  1258. }
  1259. }
  1260. // interpolation
  1261. class CubicBezierInterpolation extends Interpolant {
  1262. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer, params ) {
  1263. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  1264. this.interpolationParams = params;
  1265. }
  1266. interpolate_( i1, t0, t, t1 ) {
  1267. const result = this.resultBuffer;
  1268. const values = this.sampleValues;
  1269. const stride = this.valueSize;
  1270. const params = this.interpolationParams;
  1271. const offset1 = i1 * stride;
  1272. const offset0 = offset1 - stride;
  1273. // No interpolation if next key frame is in one frame in 30fps.
  1274. // This is from MMD animation spec.
  1275. // '1.5' is for precision loss. times are Float32 in Three.js Animation system.
  1276. const weight1 = ( ( t1 - t0 ) < 1 / 30 * 1.5 ) ? 0.0 : ( t - t0 ) / ( t1 - t0 );
  1277. if ( stride === 4 ) { // Quaternion
  1278. const x1 = params[ i1 * 4 + 0 ];
  1279. const x2 = params[ i1 * 4 + 1 ];
  1280. const y1 = params[ i1 * 4 + 2 ];
  1281. const y2 = params[ i1 * 4 + 3 ];
  1282. const ratio = this._calculate( x1, x2, y1, y2, weight1 );
  1283. Quaternion.slerpFlat( result, 0, values, offset0, values, offset1, ratio );
  1284. } else if ( stride === 3 ) { // Vector3
  1285. for ( let i = 0; i !== stride; ++ i ) {
  1286. const x1 = params[ i1 * 12 + i * 4 + 0 ];
  1287. const x2 = params[ i1 * 12 + i * 4 + 1 ];
  1288. const y1 = params[ i1 * 12 + i * 4 + 2 ];
  1289. const y2 = params[ i1 * 12 + i * 4 + 3 ];
  1290. const ratio = this._calculate( x1, x2, y1, y2, weight1 );
  1291. result[ i ] = values[ offset0 + i ] * ( 1 - ratio ) + values[ offset1 + i ] * ratio;
  1292. }
  1293. } else { // Number
  1294. const x1 = params[ i1 * 4 + 0 ];
  1295. const x2 = params[ i1 * 4 + 1 ];
  1296. const y1 = params[ i1 * 4 + 2 ];
  1297. const y2 = params[ i1 * 4 + 3 ];
  1298. const ratio = this._calculate( x1, x2, y1, y2, weight1 );
  1299. result[ 0 ] = values[ offset0 ] * ( 1 - ratio ) + values[ offset1 ] * ratio;
  1300. }
  1301. return result;
  1302. }
  1303. _calculate( x1, x2, y1, y2, x ) {
  1304. /*
  1305. * Cubic Bezier curves
  1306. * https://en.wikipedia.org/wiki/B%C3%A9zier_curve#Cubic_B.C3.A9zier_curves
  1307. *
  1308. * B(t) = ( 1 - t ) ^ 3 * P0
  1309. * + 3 * ( 1 - t ) ^ 2 * t * P1
  1310. * + 3 * ( 1 - t ) * t^2 * P2
  1311. * + t ^ 3 * P3
  1312. * ( 0 <= t <= 1 )
  1313. *
  1314. * MMD uses Cubic Bezier curves for bone and camera animation interpolation.
  1315. * http://d.hatena.ne.jp/edvakf/20111016/1318716097
  1316. *
  1317. * x = ( 1 - t ) ^ 3 * x0
  1318. * + 3 * ( 1 - t ) ^ 2 * t * x1
  1319. * + 3 * ( 1 - t ) * t^2 * x2
  1320. * + t ^ 3 * x3
  1321. * y = ( 1 - t ) ^ 3 * y0
  1322. * + 3 * ( 1 - t ) ^ 2 * t * y1
  1323. * + 3 * ( 1 - t ) * t^2 * y2
  1324. * + t ^ 3 * y3
  1325. * ( x0 = 0, y0 = 0 )
  1326. * ( x3 = 1, y3 = 1 )
  1327. * ( 0 <= t, x1, x2, y1, y2 <= 1 )
  1328. *
  1329. * Here solves this equation with Bisection method,
  1330. * https://en.wikipedia.org/wiki/Bisection_method
  1331. * gets t, and then calculate y.
  1332. *
  1333. * f(t) = 3 * ( 1 - t ) ^ 2 * t * x1
  1334. * + 3 * ( 1 - t ) * t^2 * x2
  1335. * + t ^ 3 - x = 0
  1336. *
  1337. * (Another option: Newton's method
  1338. * https://en.wikipedia.org/wiki/Newton%27s_method)
  1339. */
  1340. let c = 0.5;
  1341. let t = c;
  1342. let s = 1.0 - t;
  1343. const loop = 15;
  1344. const eps = 1e-5;
  1345. const math = Math;
  1346. let sst3, stt3, ttt;
  1347. for ( let i = 0; i < loop; i ++ ) {
  1348. sst3 = 3.0 * s * s * t;
  1349. stt3 = 3.0 * s * t * t;
  1350. ttt = t * t * t;
  1351. const ft = ( sst3 * x1 ) + ( stt3 * x2 ) + ( ttt ) - x;
  1352. if ( math.abs( ft ) < eps ) break;
  1353. c /= 2.0;
  1354. t += ( ft < 0 ) ? c : - c;
  1355. s = 1.0 - t;
  1356. }
  1357. return ( sst3 * y1 ) + ( stt3 * y2 ) + ttt;
  1358. }
  1359. }
  1360. class MMDToonMaterial extends ShaderMaterial {
  1361. constructor( parameters ) {
  1362. super();
  1363. this.isMMDToonMaterial = true;
  1364. this.type = 'MMDToonMaterial';
  1365. this._matcapCombine = AddOperation;
  1366. this.emissiveIntensity = 1.0;
  1367. this.normalMapType = TangentSpaceNormalMap;
  1368. this.combine = MultiplyOperation;
  1369. this.wireframeLinecap = 'round';
  1370. this.wireframeLinejoin = 'round';
  1371. this.flatShading = false;
  1372. this.lights = true;
  1373. this.vertexShader = MMDToonShader.vertexShader;
  1374. this.fragmentShader = MMDToonShader.fragmentShader;
  1375. this.defines = Object.assign( {}, MMDToonShader.defines );
  1376. Object.defineProperty( this, 'matcapCombine', {
  1377. get: function () {
  1378. return this._matcapCombine;
  1379. },
  1380. set: function ( value ) {
  1381. this._matcapCombine = value;
  1382. switch ( value ) {
  1383. case MultiplyOperation:
  1384. this.defines.MATCAP_BLENDING_MULTIPLY = true;
  1385. delete this.defines.MATCAP_BLENDING_ADD;
  1386. break;
  1387. default:
  1388. case AddOperation:
  1389. this.defines.MATCAP_BLENDING_ADD = true;
  1390. delete this.defines.MATCAP_BLENDING_MULTIPLY;
  1391. break;
  1392. }
  1393. },
  1394. } );
  1395. this.uniforms = UniformsUtils.clone( MMDToonShader.uniforms );
  1396. // merged from MeshToon/Phong/MatcapMaterial
  1397. const exposePropertyNames = [
  1398. 'specular',
  1399. 'opacity',
  1400. 'diffuse',
  1401. 'map',
  1402. 'matcap',
  1403. 'gradientMap',
  1404. 'lightMap',
  1405. 'lightMapIntensity',
  1406. 'aoMap',
  1407. 'aoMapIntensity',
  1408. 'emissive',
  1409. 'emissiveMap',
  1410. 'bumpMap',
  1411. 'bumpScale',
  1412. 'normalMap',
  1413. 'normalScale',
  1414. 'displacemantBias',
  1415. 'displacemantMap',
  1416. 'displacemantScale',
  1417. 'specularMap',
  1418. 'alphaMap',
  1419. 'reflectivity',
  1420. 'refractionRatio',
  1421. ];
  1422. for ( const propertyName of exposePropertyNames ) {
  1423. Object.defineProperty( this, propertyName, {
  1424. get: function () {
  1425. return this.uniforms[ propertyName ].value;
  1426. },
  1427. set: function ( value ) {
  1428. this.uniforms[ propertyName ].value = value;
  1429. },
  1430. } );
  1431. }
  1432. // Special path for shininess to handle zero shininess properly
  1433. this._shininess = 30;
  1434. Object.defineProperty( this, 'shininess', {
  1435. get: function () {
  1436. return this._shininess;
  1437. },
  1438. set: function ( value ) {
  1439. this._shininess = value;
  1440. this.uniforms.shininess.value = Math.max( this._shininess, 1e-4 ); // To prevent pow( 0.0, 0.0 )
  1441. },
  1442. } );
  1443. Object.defineProperty(
  1444. this,
  1445. 'color',
  1446. Object.getOwnPropertyDescriptor( this, 'diffuse' )
  1447. );
  1448. this.setValues( parameters );
  1449. }
  1450. copy( source ) {
  1451. super.copy( source );
  1452. this.matcapCombine = source.matcapCombine;
  1453. this.emissiveIntensity = source.emissiveIntensity;
  1454. this.normalMapType = source.normalMapType;
  1455. this.combine = source.combine;
  1456. this.wireframeLinecap = source.wireframeLinecap;
  1457. this.wireframeLinejoin = source.wireframeLinejoin;
  1458. this.flatShading = source.flatShading;
  1459. return this;
  1460. }
  1461. }
  1462. export { MMDLoader };