3MFLoader.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. ( function () {
  2. /**
  3. *
  4. * 3D Manufacturing Format (3MF) specification: https://3mf.io/specification/
  5. *
  6. * The following features from the core specification are supported:
  7. *
  8. * - 3D Models
  9. * - Object Resources (Meshes and Components)
  10. * - Material Resources (Base Materials)
  11. *
  12. * 3MF Materials and Properties Extension are only partially supported.
  13. *
  14. * - Texture 2D
  15. * - Texture 2D Groups
  16. * - THREE.Color Groups (Vertex Colors)
  17. * - Metallic Display Properties (PBR)
  18. */
  19. class ThreeMFLoader extends THREE.Loader {
  20. constructor( manager ) {
  21. super( manager );
  22. this.availableExtensions = [];
  23. }
  24. load( url, onLoad, onProgress, onError ) {
  25. const scope = this;
  26. const loader = new THREE.FileLoader( scope.manager );
  27. loader.setPath( scope.path );
  28. loader.setResponseType( 'arraybuffer' );
  29. loader.setRequestHeader( scope.requestHeader );
  30. loader.setWithCredentials( scope.withCredentials );
  31. loader.load( url, function ( buffer ) {
  32. try {
  33. onLoad( scope.parse( buffer ) );
  34. } catch ( e ) {
  35. if ( onError ) {
  36. onError( e );
  37. } else {
  38. console.error( e );
  39. }
  40. scope.manager.itemError( url );
  41. }
  42. }, onProgress, onError );
  43. }
  44. parse( data ) {
  45. const scope = this;
  46. const textureLoader = new THREE.TextureLoader( this.manager );
  47. function loadDocument( data ) {
  48. let zip = null;
  49. let file = null;
  50. let relsName;
  51. let modelRelsName;
  52. const modelPartNames = [];
  53. const printTicketPartNames = [];
  54. const texturesPartNames = [];
  55. const otherPartNames = [];
  56. let modelRels;
  57. const modelParts = {};
  58. const printTicketParts = {};
  59. const texturesParts = {};
  60. const otherParts = {};
  61. try {
  62. zip = fflate.unzipSync( new Uint8Array( data ) ); // eslint-disable-line no-undef
  63. } catch ( e ) {
  64. if ( e instanceof ReferenceError ) {
  65. console.error( 'THREE.3MFLoader: fflate missing and file is compressed.' );
  66. return null;
  67. }
  68. }
  69. for ( file in zip ) {
  70. if ( file.match( /\_rels\/.rels$/ ) ) {
  71. relsName = file;
  72. } else if ( file.match( /3D\/_rels\/.*\.model\.rels$/ ) ) {
  73. modelRelsName = file;
  74. } else if ( file.match( /^3D\/.*\.model$/ ) ) {
  75. modelPartNames.push( file );
  76. } else if ( file.match( /^3D\/Metadata\/.*\.xml$/ ) ) {
  77. printTicketPartNames.push( file );
  78. } else if ( file.match( /^3D\/Textures?\/.*/ ) ) {
  79. texturesPartNames.push( file );
  80. } else if ( file.match( /^3D\/Other\/.*/ ) ) {
  81. otherPartNames.push( file );
  82. }
  83. } //
  84. const relsView = zip[ relsName ];
  85. const relsFileText = THREE.LoaderUtils.decodeText( relsView );
  86. const rels = parseRelsXml( relsFileText ); //
  87. if ( modelRelsName ) {
  88. const relsView = zip[ modelRelsName ];
  89. const relsFileText = THREE.LoaderUtils.decodeText( relsView );
  90. modelRels = parseRelsXml( relsFileText );
  91. } //
  92. for ( let i = 0; i < modelPartNames.length; i ++ ) {
  93. const modelPart = modelPartNames[ i ];
  94. const view = zip[ modelPart ];
  95. const fileText = THREE.LoaderUtils.decodeText( view );
  96. const xmlData = new DOMParser().parseFromString( fileText, 'application/xml' );
  97. if ( xmlData.documentElement.nodeName.toLowerCase() !== 'model' ) {
  98. console.error( 'THREE.3MFLoader: Error loading 3MF - no 3MF document found: ', modelPart );
  99. }
  100. const modelNode = xmlData.querySelector( 'model' );
  101. const extensions = {};
  102. for ( let i = 0; i < modelNode.attributes.length; i ++ ) {
  103. const attr = modelNode.attributes[ i ];
  104. if ( attr.name.match( /^xmlns:(.+)$/ ) ) {
  105. extensions[ attr.value ] = RegExp.$1;
  106. }
  107. }
  108. const modelData = parseModelNode( modelNode );
  109. modelData[ 'xml' ] = modelNode;
  110. if ( 0 < Object.keys( extensions ).length ) {
  111. modelData[ 'extensions' ] = extensions;
  112. }
  113. modelParts[ modelPart ] = modelData;
  114. } //
  115. for ( let i = 0; i < texturesPartNames.length; i ++ ) {
  116. const texturesPartName = texturesPartNames[ i ];
  117. texturesParts[ texturesPartName ] = zip[ texturesPartName ].buffer;
  118. }
  119. return {
  120. rels: rels,
  121. modelRels: modelRels,
  122. model: modelParts,
  123. printTicket: printTicketParts,
  124. texture: texturesParts,
  125. other: otherParts
  126. };
  127. }
  128. function parseRelsXml( relsFileText ) {
  129. const relationships = [];
  130. const relsXmlData = new DOMParser().parseFromString( relsFileText, 'application/xml' );
  131. const relsNodes = relsXmlData.querySelectorAll( 'Relationship' );
  132. for ( let i = 0; i < relsNodes.length; i ++ ) {
  133. const relsNode = relsNodes[ i ];
  134. const relationship = {
  135. target: relsNode.getAttribute( 'Target' ),
  136. //required
  137. id: relsNode.getAttribute( 'Id' ),
  138. //required
  139. type: relsNode.getAttribute( 'Type' ) //required
  140. };
  141. relationships.push( relationship );
  142. }
  143. return relationships;
  144. }
  145. function parseMetadataNodes( metadataNodes ) {
  146. const metadataData = {};
  147. for ( let i = 0; i < metadataNodes.length; i ++ ) {
  148. const metadataNode = metadataNodes[ i ];
  149. const name = metadataNode.getAttribute( 'name' );
  150. const validNames = [ 'Title', 'Designer', 'Description', 'Copyright', 'LicenseTerms', 'Rating', 'CreationDate', 'ModificationDate' ];
  151. if ( 0 <= validNames.indexOf( name ) ) {
  152. metadataData[ name ] = metadataNode.textContent;
  153. }
  154. }
  155. return metadataData;
  156. }
  157. function parseBasematerialsNode( basematerialsNode ) {
  158. const basematerialsData = {
  159. id: basematerialsNode.getAttribute( 'id' ),
  160. // required
  161. basematerials: []
  162. };
  163. const basematerialNodes = basematerialsNode.querySelectorAll( 'base' );
  164. for ( let i = 0; i < basematerialNodes.length; i ++ ) {
  165. const basematerialNode = basematerialNodes[ i ];
  166. const basematerialData = parseBasematerialNode( basematerialNode );
  167. basematerialData.index = i; // the order and count of the material nodes form an implicit 0-based index
  168. basematerialsData.basematerials.push( basematerialData );
  169. }
  170. return basematerialsData;
  171. }
  172. function parseTexture2DNode( texture2DNode ) {
  173. const texture2dData = {
  174. id: texture2DNode.getAttribute( 'id' ),
  175. // required
  176. path: texture2DNode.getAttribute( 'path' ),
  177. // required
  178. contenttype: texture2DNode.getAttribute( 'contenttype' ),
  179. // required
  180. tilestyleu: texture2DNode.getAttribute( 'tilestyleu' ),
  181. tilestylev: texture2DNode.getAttribute( 'tilestylev' ),
  182. filter: texture2DNode.getAttribute( 'filter' )
  183. };
  184. return texture2dData;
  185. }
  186. function parseTextures2DGroupNode( texture2DGroupNode ) {
  187. const texture2DGroupData = {
  188. id: texture2DGroupNode.getAttribute( 'id' ),
  189. // required
  190. texid: texture2DGroupNode.getAttribute( 'texid' ),
  191. // required
  192. displaypropertiesid: texture2DGroupNode.getAttribute( 'displaypropertiesid' )
  193. };
  194. const tex2coordNodes = texture2DGroupNode.querySelectorAll( 'tex2coord' );
  195. const uvs = [];
  196. for ( let i = 0; i < tex2coordNodes.length; i ++ ) {
  197. const tex2coordNode = tex2coordNodes[ i ];
  198. const u = tex2coordNode.getAttribute( 'u' );
  199. const v = tex2coordNode.getAttribute( 'v' );
  200. uvs.push( parseFloat( u ), parseFloat( v ) );
  201. }
  202. texture2DGroupData[ 'uvs' ] = new Float32Array( uvs );
  203. return texture2DGroupData;
  204. }
  205. function parseColorGroupNode( colorGroupNode ) {
  206. const colorGroupData = {
  207. id: colorGroupNode.getAttribute( 'id' ),
  208. // required
  209. displaypropertiesid: colorGroupNode.getAttribute( 'displaypropertiesid' )
  210. };
  211. const colorNodes = colorGroupNode.querySelectorAll( 'color' );
  212. const colors = [];
  213. const colorObject = new THREE.Color();
  214. for ( let i = 0; i < colorNodes.length; i ++ ) {
  215. const colorNode = colorNodes[ i ];
  216. const color = colorNode.getAttribute( 'color' );
  217. colorObject.setStyle( color.substring( 0, 7 ) );
  218. colorObject.convertSRGBToLinear(); // color is in sRGB
  219. colors.push( colorObject.r, colorObject.g, colorObject.b );
  220. }
  221. colorGroupData[ 'colors' ] = new Float32Array( colors );
  222. return colorGroupData;
  223. }
  224. function parseMetallicDisplaypropertiesNode( metallicDisplaypropetiesNode ) {
  225. const metallicDisplaypropertiesData = {
  226. id: metallicDisplaypropetiesNode.getAttribute( 'id' ) // required
  227. };
  228. const metallicNodes = metallicDisplaypropetiesNode.querySelectorAll( 'pbmetallic' );
  229. const metallicData = [];
  230. for ( let i = 0; i < metallicNodes.length; i ++ ) {
  231. const metallicNode = metallicNodes[ i ];
  232. metallicData.push( {
  233. name: metallicNode.getAttribute( 'name' ),
  234. // required
  235. metallicness: parseFloat( metallicNode.getAttribute( 'metallicness' ) ),
  236. // required
  237. roughness: parseFloat( metallicNode.getAttribute( 'roughness' ) ) // required
  238. } );
  239. }
  240. metallicDisplaypropertiesData.data = metallicData;
  241. return metallicDisplaypropertiesData;
  242. }
  243. function parseBasematerialNode( basematerialNode ) {
  244. const basematerialData = {};
  245. basematerialData[ 'name' ] = basematerialNode.getAttribute( 'name' ); // required
  246. basematerialData[ 'displaycolor' ] = basematerialNode.getAttribute( 'displaycolor' ); // required
  247. basematerialData[ 'displaypropertiesid' ] = basematerialNode.getAttribute( 'displaypropertiesid' );
  248. return basematerialData;
  249. }
  250. function parseMeshNode( meshNode ) {
  251. const meshData = {};
  252. const vertices = [];
  253. const vertexNodes = meshNode.querySelectorAll( 'vertices vertex' );
  254. for ( let i = 0; i < vertexNodes.length; i ++ ) {
  255. const vertexNode = vertexNodes[ i ];
  256. const x = vertexNode.getAttribute( 'x' );
  257. const y = vertexNode.getAttribute( 'y' );
  258. const z = vertexNode.getAttribute( 'z' );
  259. vertices.push( parseFloat( x ), parseFloat( y ), parseFloat( z ) );
  260. }
  261. meshData[ 'vertices' ] = new Float32Array( vertices );
  262. const triangleProperties = [];
  263. const triangles = [];
  264. const triangleNodes = meshNode.querySelectorAll( 'triangles triangle' );
  265. for ( let i = 0; i < triangleNodes.length; i ++ ) {
  266. const triangleNode = triangleNodes[ i ];
  267. const v1 = triangleNode.getAttribute( 'v1' );
  268. const v2 = triangleNode.getAttribute( 'v2' );
  269. const v3 = triangleNode.getAttribute( 'v3' );
  270. const p1 = triangleNode.getAttribute( 'p1' );
  271. const p2 = triangleNode.getAttribute( 'p2' );
  272. const p3 = triangleNode.getAttribute( 'p3' );
  273. const pid = triangleNode.getAttribute( 'pid' );
  274. const triangleProperty = {};
  275. triangleProperty[ 'v1' ] = parseInt( v1, 10 );
  276. triangleProperty[ 'v2' ] = parseInt( v2, 10 );
  277. triangleProperty[ 'v3' ] = parseInt( v3, 10 );
  278. triangles.push( triangleProperty[ 'v1' ], triangleProperty[ 'v2' ], triangleProperty[ 'v3' ] ); // optional
  279. if ( p1 ) {
  280. triangleProperty[ 'p1' ] = parseInt( p1, 10 );
  281. }
  282. if ( p2 ) {
  283. triangleProperty[ 'p2' ] = parseInt( p2, 10 );
  284. }
  285. if ( p3 ) {
  286. triangleProperty[ 'p3' ] = parseInt( p3, 10 );
  287. }
  288. if ( pid ) {
  289. triangleProperty[ 'pid' ] = pid;
  290. }
  291. if ( 0 < Object.keys( triangleProperty ).length ) {
  292. triangleProperties.push( triangleProperty );
  293. }
  294. }
  295. meshData[ 'triangleProperties' ] = triangleProperties;
  296. meshData[ 'triangles' ] = new Uint32Array( triangles );
  297. return meshData;
  298. }
  299. function parseComponentsNode( componentsNode ) {
  300. const components = [];
  301. const componentNodes = componentsNode.querySelectorAll( 'component' );
  302. for ( let i = 0; i < componentNodes.length; i ++ ) {
  303. const componentNode = componentNodes[ i ];
  304. const componentData = parseComponentNode( componentNode );
  305. components.push( componentData );
  306. }
  307. return components;
  308. }
  309. function parseComponentNode( componentNode ) {
  310. const componentData = {};
  311. componentData[ 'objectId' ] = componentNode.getAttribute( 'objectid' ); // required
  312. const transform = componentNode.getAttribute( 'transform' );
  313. if ( transform ) {
  314. componentData[ 'transform' ] = parseTransform( transform );
  315. }
  316. return componentData;
  317. }
  318. function parseTransform( transform ) {
  319. const t = [];
  320. transform.split( ' ' ).forEach( function ( s ) {
  321. t.push( parseFloat( s ) );
  322. } );
  323. const matrix = new THREE.Matrix4();
  324. matrix.set( t[ 0 ], t[ 3 ], t[ 6 ], t[ 9 ], t[ 1 ], t[ 4 ], t[ 7 ], t[ 10 ], t[ 2 ], t[ 5 ], t[ 8 ], t[ 11 ], 0.0, 0.0, 0.0, 1.0 );
  325. return matrix;
  326. }
  327. function parseObjectNode( objectNode ) {
  328. const objectData = {
  329. type: objectNode.getAttribute( 'type' )
  330. };
  331. const id = objectNode.getAttribute( 'id' );
  332. if ( id ) {
  333. objectData[ 'id' ] = id;
  334. }
  335. const pid = objectNode.getAttribute( 'pid' );
  336. if ( pid ) {
  337. objectData[ 'pid' ] = pid;
  338. }
  339. const pindex = objectNode.getAttribute( 'pindex' );
  340. if ( pindex ) {
  341. objectData[ 'pindex' ] = pindex;
  342. }
  343. const thumbnail = objectNode.getAttribute( 'thumbnail' );
  344. if ( thumbnail ) {
  345. objectData[ 'thumbnail' ] = thumbnail;
  346. }
  347. const partnumber = objectNode.getAttribute( 'partnumber' );
  348. if ( partnumber ) {
  349. objectData[ 'partnumber' ] = partnumber;
  350. }
  351. const name = objectNode.getAttribute( 'name' );
  352. if ( name ) {
  353. objectData[ 'name' ] = name;
  354. }
  355. const meshNode = objectNode.querySelector( 'mesh' );
  356. if ( meshNode ) {
  357. objectData[ 'mesh' ] = parseMeshNode( meshNode );
  358. }
  359. const componentsNode = objectNode.querySelector( 'components' );
  360. if ( componentsNode ) {
  361. objectData[ 'components' ] = parseComponentsNode( componentsNode );
  362. }
  363. return objectData;
  364. }
  365. function parseResourcesNode( resourcesNode ) {
  366. const resourcesData = {};
  367. resourcesData[ 'basematerials' ] = {};
  368. const basematerialsNodes = resourcesNode.querySelectorAll( 'basematerials' );
  369. for ( let i = 0; i < basematerialsNodes.length; i ++ ) {
  370. const basematerialsNode = basematerialsNodes[ i ];
  371. const basematerialsData = parseBasematerialsNode( basematerialsNode );
  372. resourcesData[ 'basematerials' ][ basematerialsData[ 'id' ] ] = basematerialsData;
  373. } //
  374. resourcesData[ 'texture2d' ] = {};
  375. const textures2DNodes = resourcesNode.querySelectorAll( 'texture2d' );
  376. for ( let i = 0; i < textures2DNodes.length; i ++ ) {
  377. const textures2DNode = textures2DNodes[ i ];
  378. const texture2DData = parseTexture2DNode( textures2DNode );
  379. resourcesData[ 'texture2d' ][ texture2DData[ 'id' ] ] = texture2DData;
  380. } //
  381. resourcesData[ 'colorgroup' ] = {};
  382. const colorGroupNodes = resourcesNode.querySelectorAll( 'colorgroup' );
  383. for ( let i = 0; i < colorGroupNodes.length; i ++ ) {
  384. const colorGroupNode = colorGroupNodes[ i ];
  385. const colorGroupData = parseColorGroupNode( colorGroupNode );
  386. resourcesData[ 'colorgroup' ][ colorGroupData[ 'id' ] ] = colorGroupData;
  387. } //
  388. resourcesData[ 'pbmetallicdisplayproperties' ] = {};
  389. const pbmetallicdisplaypropertiesNodes = resourcesNode.querySelectorAll( 'pbmetallicdisplayproperties' );
  390. for ( let i = 0; i < pbmetallicdisplaypropertiesNodes.length; i ++ ) {
  391. const pbmetallicdisplaypropertiesNode = pbmetallicdisplaypropertiesNodes[ i ];
  392. const pbmetallicdisplaypropertiesData = parseMetallicDisplaypropertiesNode( pbmetallicdisplaypropertiesNode );
  393. resourcesData[ 'pbmetallicdisplayproperties' ][ pbmetallicdisplaypropertiesData[ 'id' ] ] = pbmetallicdisplaypropertiesData;
  394. } //
  395. resourcesData[ 'texture2dgroup' ] = {};
  396. const textures2DGroupNodes = resourcesNode.querySelectorAll( 'texture2dgroup' );
  397. for ( let i = 0; i < textures2DGroupNodes.length; i ++ ) {
  398. const textures2DGroupNode = textures2DGroupNodes[ i ];
  399. const textures2DGroupData = parseTextures2DGroupNode( textures2DGroupNode );
  400. resourcesData[ 'texture2dgroup' ][ textures2DGroupData[ 'id' ] ] = textures2DGroupData;
  401. } //
  402. resourcesData[ 'object' ] = {};
  403. const objectNodes = resourcesNode.querySelectorAll( 'object' );
  404. for ( let i = 0; i < objectNodes.length; i ++ ) {
  405. const objectNode = objectNodes[ i ];
  406. const objectData = parseObjectNode( objectNode );
  407. resourcesData[ 'object' ][ objectData[ 'id' ] ] = objectData;
  408. }
  409. return resourcesData;
  410. }
  411. function parseBuildNode( buildNode ) {
  412. const buildData = [];
  413. const itemNodes = buildNode.querySelectorAll( 'item' );
  414. for ( let i = 0; i < itemNodes.length; i ++ ) {
  415. const itemNode = itemNodes[ i ];
  416. const buildItem = {
  417. objectId: itemNode.getAttribute( 'objectid' )
  418. };
  419. const transform = itemNode.getAttribute( 'transform' );
  420. if ( transform ) {
  421. buildItem[ 'transform' ] = parseTransform( transform );
  422. }
  423. buildData.push( buildItem );
  424. }
  425. return buildData;
  426. }
  427. function parseModelNode( modelNode ) {
  428. const modelData = {
  429. unit: modelNode.getAttribute( 'unit' ) || 'millimeter'
  430. };
  431. const metadataNodes = modelNode.querySelectorAll( 'metadata' );
  432. if ( metadataNodes ) {
  433. modelData[ 'metadata' ] = parseMetadataNodes( metadataNodes );
  434. }
  435. const resourcesNode = modelNode.querySelector( 'resources' );
  436. if ( resourcesNode ) {
  437. modelData[ 'resources' ] = parseResourcesNode( resourcesNode );
  438. }
  439. const buildNode = modelNode.querySelector( 'build' );
  440. if ( buildNode ) {
  441. modelData[ 'build' ] = parseBuildNode( buildNode );
  442. }
  443. return modelData;
  444. }
  445. function buildTexture( texture2dgroup, objects, modelData, textureData ) {
  446. const texid = texture2dgroup.texid;
  447. const texture2ds = modelData.resources.texture2d;
  448. const texture2d = texture2ds[ texid ];
  449. if ( texture2d ) {
  450. const data = textureData[ texture2d.path ];
  451. const type = texture2d.contenttype;
  452. const blob = new Blob( [ data ], {
  453. type: type
  454. } );
  455. const sourceURI = URL.createObjectURL( blob );
  456. const texture = textureLoader.load( sourceURI, function () {
  457. URL.revokeObjectURL( sourceURI );
  458. } );
  459. texture.encoding = THREE.sRGBEncoding; // texture parameters
  460. switch ( texture2d.tilestyleu ) {
  461. case 'wrap':
  462. texture.wrapS = THREE.RepeatWrapping;
  463. break;
  464. case 'mirror':
  465. texture.wrapS = THREE.MirroredRepeatWrapping;
  466. break;
  467. case 'none':
  468. case 'clamp':
  469. texture.wrapS = THREE.ClampToEdgeWrapping;
  470. break;
  471. default:
  472. texture.wrapS = THREE.RepeatWrapping;
  473. }
  474. switch ( texture2d.tilestylev ) {
  475. case 'wrap':
  476. texture.wrapT = THREE.RepeatWrapping;
  477. break;
  478. case 'mirror':
  479. texture.wrapT = THREE.MirroredRepeatWrapping;
  480. break;
  481. case 'none':
  482. case 'clamp':
  483. texture.wrapT = THREE.ClampToEdgeWrapping;
  484. break;
  485. default:
  486. texture.wrapT = THREE.RepeatWrapping;
  487. }
  488. switch ( texture2d.filter ) {
  489. case 'auto':
  490. texture.magFilter = THREE.LinearFilter;
  491. texture.minFilter = THREE.LinearMipmapLinearFilter;
  492. break;
  493. case 'linear':
  494. texture.magFilter = THREE.LinearFilter;
  495. texture.minFilter = THREE.LinearFilter;
  496. break;
  497. case 'nearest':
  498. texture.magFilter = THREE.NearestFilter;
  499. texture.minFilter = THREE.NearestFilter;
  500. break;
  501. default:
  502. texture.magFilter = THREE.LinearFilter;
  503. texture.minFilter = THREE.LinearMipmapLinearFilter;
  504. }
  505. return texture;
  506. } else {
  507. return null;
  508. }
  509. }
  510. function buildBasematerialsMeshes( basematerials, triangleProperties, meshData, objects, modelData, textureData, objectData ) {
  511. const objectPindex = objectData.pindex;
  512. const materialMap = {};
  513. for ( let i = 0, l = triangleProperties.length; i < l; i ++ ) {
  514. const triangleProperty = triangleProperties[ i ];
  515. const pindex = triangleProperty.p1 !== undefined ? triangleProperty.p1 : objectPindex;
  516. if ( materialMap[ pindex ] === undefined ) materialMap[ pindex ] = [];
  517. materialMap[ pindex ].push( triangleProperty );
  518. } //
  519. const keys = Object.keys( materialMap );
  520. const meshes = [];
  521. for ( let i = 0, l = keys.length; i < l; i ++ ) {
  522. const materialIndex = keys[ i ];
  523. const trianglePropertiesProps = materialMap[ materialIndex ];
  524. const basematerialData = basematerials.basematerials[ materialIndex ];
  525. const material = getBuild( basematerialData, objects, modelData, textureData, objectData, buildBasematerial ); //
  526. const geometry = new THREE.BufferGeometry();
  527. const positionData = [];
  528. const vertices = meshData.vertices;
  529. for ( let j = 0, jl = trianglePropertiesProps.length; j < jl; j ++ ) {
  530. const triangleProperty = trianglePropertiesProps[ j ];
  531. positionData.push( vertices[ triangleProperty.v1 * 3 + 0 ] );
  532. positionData.push( vertices[ triangleProperty.v1 * 3 + 1 ] );
  533. positionData.push( vertices[ triangleProperty.v1 * 3 + 2 ] );
  534. positionData.push( vertices[ triangleProperty.v2 * 3 + 0 ] );
  535. positionData.push( vertices[ triangleProperty.v2 * 3 + 1 ] );
  536. positionData.push( vertices[ triangleProperty.v2 * 3 + 2 ] );
  537. positionData.push( vertices[ triangleProperty.v3 * 3 + 0 ] );
  538. positionData.push( vertices[ triangleProperty.v3 * 3 + 1 ] );
  539. positionData.push( vertices[ triangleProperty.v3 * 3 + 2 ] );
  540. }
  541. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positionData, 3 ) ); //
  542. const mesh = new THREE.Mesh( geometry, material );
  543. meshes.push( mesh );
  544. }
  545. return meshes;
  546. }
  547. function buildTexturedMesh( texture2dgroup, triangleProperties, meshData, objects, modelData, textureData, objectData ) {
  548. // geometry
  549. const geometry = new THREE.BufferGeometry();
  550. const positionData = [];
  551. const uvData = [];
  552. const vertices = meshData.vertices;
  553. const uvs = texture2dgroup.uvs;
  554. for ( let i = 0, l = triangleProperties.length; i < l; i ++ ) {
  555. const triangleProperty = triangleProperties[ i ];
  556. positionData.push( vertices[ triangleProperty.v1 * 3 + 0 ] );
  557. positionData.push( vertices[ triangleProperty.v1 * 3 + 1 ] );
  558. positionData.push( vertices[ triangleProperty.v1 * 3 + 2 ] );
  559. positionData.push( vertices[ triangleProperty.v2 * 3 + 0 ] );
  560. positionData.push( vertices[ triangleProperty.v2 * 3 + 1 ] );
  561. positionData.push( vertices[ triangleProperty.v2 * 3 + 2 ] );
  562. positionData.push( vertices[ triangleProperty.v3 * 3 + 0 ] );
  563. positionData.push( vertices[ triangleProperty.v3 * 3 + 1 ] );
  564. positionData.push( vertices[ triangleProperty.v3 * 3 + 2 ] ); //
  565. uvData.push( uvs[ triangleProperty.p1 * 2 + 0 ] );
  566. uvData.push( uvs[ triangleProperty.p1 * 2 + 1 ] );
  567. uvData.push( uvs[ triangleProperty.p2 * 2 + 0 ] );
  568. uvData.push( uvs[ triangleProperty.p2 * 2 + 1 ] );
  569. uvData.push( uvs[ triangleProperty.p3 * 2 + 0 ] );
  570. uvData.push( uvs[ triangleProperty.p3 * 2 + 1 ] );
  571. }
  572. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positionData, 3 ) );
  573. geometry.setAttribute( 'uv', new THREE.Float32BufferAttribute( uvData, 2 ) ); // material
  574. const texture = getBuild( texture2dgroup, objects, modelData, textureData, objectData, buildTexture );
  575. const material = new THREE.MeshPhongMaterial( {
  576. map: texture,
  577. flatShading: true
  578. } ); // mesh
  579. const mesh = new THREE.Mesh( geometry, material );
  580. return mesh;
  581. }
  582. function buildVertexColorMesh( colorgroup, triangleProperties, meshData, objects, modelData, objectData ) {
  583. // geometry
  584. const geometry = new THREE.BufferGeometry();
  585. const positionData = [];
  586. const colorData = [];
  587. const vertices = meshData.vertices;
  588. const colors = colorgroup.colors;
  589. for ( let i = 0, l = triangleProperties.length; i < l; i ++ ) {
  590. const triangleProperty = triangleProperties[ i ];
  591. const v1 = triangleProperty.v1;
  592. const v2 = triangleProperty.v2;
  593. const v3 = triangleProperty.v3;
  594. positionData.push( vertices[ v1 * 3 + 0 ] );
  595. positionData.push( vertices[ v1 * 3 + 1 ] );
  596. positionData.push( vertices[ v1 * 3 + 2 ] );
  597. positionData.push( vertices[ v2 * 3 + 0 ] );
  598. positionData.push( vertices[ v2 * 3 + 1 ] );
  599. positionData.push( vertices[ v2 * 3 + 2 ] );
  600. positionData.push( vertices[ v3 * 3 + 0 ] );
  601. positionData.push( vertices[ v3 * 3 + 1 ] );
  602. positionData.push( vertices[ v3 * 3 + 2 ] ); //
  603. const p1 = triangleProperty.p1 !== undefined ? triangleProperty.p1 : objectData.pindex;
  604. const p2 = triangleProperty.p2 !== undefined ? triangleProperty.p2 : p1;
  605. const p3 = triangleProperty.p3 !== undefined ? triangleProperty.p3 : p1;
  606. colorData.push( colors[ p1 * 3 + 0 ] );
  607. colorData.push( colors[ p1 * 3 + 1 ] );
  608. colorData.push( colors[ p1 * 3 + 2 ] );
  609. colorData.push( colors[ p2 * 3 + 0 ] );
  610. colorData.push( colors[ p2 * 3 + 1 ] );
  611. colorData.push( colors[ p2 * 3 + 2 ] );
  612. colorData.push( colors[ p3 * 3 + 0 ] );
  613. colorData.push( colors[ p3 * 3 + 1 ] );
  614. colorData.push( colors[ p3 * 3 + 2 ] );
  615. }
  616. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positionData, 3 ) );
  617. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colorData, 3 ) ); // material
  618. const material = new THREE.MeshPhongMaterial( {
  619. vertexColors: true,
  620. flatShading: true
  621. } ); // mesh
  622. const mesh = new THREE.Mesh( geometry, material );
  623. return mesh;
  624. }
  625. function buildDefaultMesh( meshData ) {
  626. const geometry = new THREE.BufferGeometry();
  627. geometry.setIndex( new THREE.BufferAttribute( meshData[ 'triangles' ], 1 ) );
  628. geometry.setAttribute( 'position', new THREE.BufferAttribute( meshData[ 'vertices' ], 3 ) );
  629. const material = new THREE.MeshPhongMaterial( {
  630. color: 0xffffff,
  631. flatShading: true
  632. } );
  633. const mesh = new THREE.Mesh( geometry, material );
  634. return mesh;
  635. }
  636. function buildMeshes( resourceMap, meshData, objects, modelData, textureData, objectData ) {
  637. const keys = Object.keys( resourceMap );
  638. const meshes = [];
  639. for ( let i = 0, il = keys.length; i < il; i ++ ) {
  640. const resourceId = keys[ i ];
  641. const triangleProperties = resourceMap[ resourceId ];
  642. const resourceType = getResourceType( resourceId, modelData );
  643. switch ( resourceType ) {
  644. case 'material':
  645. const basematerials = modelData.resources.basematerials[ resourceId ];
  646. const newMeshes = buildBasematerialsMeshes( basematerials, triangleProperties, meshData, objects, modelData, textureData, objectData );
  647. for ( let j = 0, jl = newMeshes.length; j < jl; j ++ ) {
  648. meshes.push( newMeshes[ j ] );
  649. }
  650. break;
  651. case 'texture':
  652. const texture2dgroup = modelData.resources.texture2dgroup[ resourceId ];
  653. meshes.push( buildTexturedMesh( texture2dgroup, triangleProperties, meshData, objects, modelData, textureData, objectData ) );
  654. break;
  655. case 'vertexColors':
  656. const colorgroup = modelData.resources.colorgroup[ resourceId ];
  657. meshes.push( buildVertexColorMesh( colorgroup, triangleProperties, meshData, objects, modelData, objectData ) );
  658. break;
  659. case 'default':
  660. meshes.push( buildDefaultMesh( meshData ) );
  661. break;
  662. default:
  663. console.error( 'THREE.3MFLoader: Unsupported resource type.' );
  664. }
  665. }
  666. return meshes;
  667. }
  668. function getResourceType( pid, modelData ) {
  669. if ( modelData.resources.texture2dgroup[ pid ] !== undefined ) {
  670. return 'texture';
  671. } else if ( modelData.resources.basematerials[ pid ] !== undefined ) {
  672. return 'material';
  673. } else if ( modelData.resources.colorgroup[ pid ] !== undefined ) {
  674. return 'vertexColors';
  675. } else if ( pid === 'default' ) {
  676. return 'default';
  677. } else {
  678. return undefined;
  679. }
  680. }
  681. function analyzeObject( modelData, meshData, objectData ) {
  682. const resourceMap = {};
  683. const triangleProperties = meshData[ 'triangleProperties' ];
  684. const objectPid = objectData.pid;
  685. for ( let i = 0, l = triangleProperties.length; i < l; i ++ ) {
  686. const triangleProperty = triangleProperties[ i ];
  687. let pid = triangleProperty.pid !== undefined ? triangleProperty.pid : objectPid;
  688. if ( pid === undefined ) pid = 'default';
  689. if ( resourceMap[ pid ] === undefined ) resourceMap[ pid ] = [];
  690. resourceMap[ pid ].push( triangleProperty );
  691. }
  692. return resourceMap;
  693. }
  694. function buildGroup( meshData, objects, modelData, textureData, objectData ) {
  695. const group = new THREE.Group();
  696. const resourceMap = analyzeObject( modelData, meshData, objectData );
  697. const meshes = buildMeshes( resourceMap, meshData, objects, modelData, textureData, objectData );
  698. for ( let i = 0, l = meshes.length; i < l; i ++ ) {
  699. group.add( meshes[ i ] );
  700. }
  701. return group;
  702. }
  703. function applyExtensions( extensions, meshData, modelXml ) {
  704. if ( ! extensions ) {
  705. return;
  706. }
  707. const availableExtensions = [];
  708. const keys = Object.keys( extensions );
  709. for ( let i = 0; i < keys.length; i ++ ) {
  710. const ns = keys[ i ];
  711. for ( let j = 0; j < scope.availableExtensions.length; j ++ ) {
  712. const extension = scope.availableExtensions[ j ];
  713. if ( extension.ns === ns ) {
  714. availableExtensions.push( extension );
  715. }
  716. }
  717. }
  718. for ( let i = 0; i < availableExtensions.length; i ++ ) {
  719. const extension = availableExtensions[ i ];
  720. extension.apply( modelXml, extensions[ extension[ 'ns' ] ], meshData );
  721. }
  722. }
  723. function getBuild( data, objects, modelData, textureData, objectData, builder ) {
  724. if ( data.build !== undefined ) return data.build;
  725. data.build = builder( data, objects, modelData, textureData, objectData );
  726. return data.build;
  727. }
  728. function buildBasematerial( materialData, objects, modelData ) {
  729. let material;
  730. const displaypropertiesid = materialData.displaypropertiesid;
  731. const pbmetallicdisplayproperties = modelData.resources.pbmetallicdisplayproperties;
  732. if ( displaypropertiesid !== null && pbmetallicdisplayproperties[ displaypropertiesid ] !== undefined ) {
  733. // metallic display property, use StandardMaterial
  734. const pbmetallicdisplayproperty = pbmetallicdisplayproperties[ displaypropertiesid ];
  735. const metallicData = pbmetallicdisplayproperty.data[ materialData.index ];
  736. material = new THREE.MeshStandardMaterial( {
  737. flatShading: true,
  738. roughness: metallicData.roughness,
  739. metalness: metallicData.metallicness
  740. } );
  741. } else {
  742. // otherwise use PhongMaterial
  743. material = new THREE.MeshPhongMaterial( {
  744. flatShading: true
  745. } );
  746. }
  747. material.name = materialData.name; // displaycolor MUST be specified with a value of a 6 or 8 digit hexadecimal number, e.g. "#RRGGBB" or "#RRGGBBAA"
  748. const displaycolor = materialData.displaycolor;
  749. const color = displaycolor.substring( 0, 7 );
  750. material.color.setStyle( color );
  751. material.color.convertSRGBToLinear(); // displaycolor is in sRGB
  752. // process alpha if set
  753. if ( displaycolor.length === 9 ) {
  754. material.opacity = parseInt( displaycolor.charAt( 7 ) + displaycolor.charAt( 8 ), 16 ) / 255;
  755. }
  756. return material;
  757. }
  758. function buildComposite( compositeData, objects, modelData, textureData ) {
  759. const composite = new THREE.Group();
  760. for ( let j = 0; j < compositeData.length; j ++ ) {
  761. const component = compositeData[ j ];
  762. let build = objects[ component.objectId ];
  763. if ( build === undefined ) {
  764. buildObject( component.objectId, objects, modelData, textureData );
  765. build = objects[ component.objectId ];
  766. }
  767. const object3D = build.clone(); // apply component transform
  768. const transform = component.transform;
  769. if ( transform ) {
  770. object3D.applyMatrix4( transform );
  771. }
  772. composite.add( object3D );
  773. }
  774. return composite;
  775. }
  776. function buildObject( objectId, objects, modelData, textureData ) {
  777. const objectData = modelData[ 'resources' ][ 'object' ][ objectId ];
  778. if ( objectData[ 'mesh' ] ) {
  779. const meshData = objectData[ 'mesh' ];
  780. const extensions = modelData[ 'extensions' ];
  781. const modelXml = modelData[ 'xml' ];
  782. applyExtensions( extensions, meshData, modelXml );
  783. objects[ objectData.id ] = getBuild( meshData, objects, modelData, textureData, objectData, buildGroup );
  784. } else {
  785. const compositeData = objectData[ 'components' ];
  786. objects[ objectData.id ] = getBuild( compositeData, objects, modelData, textureData, objectData, buildComposite );
  787. }
  788. }
  789. function buildObjects( data3mf ) {
  790. const modelsData = data3mf.model;
  791. const modelRels = data3mf.modelRels;
  792. const objects = {};
  793. const modelsKeys = Object.keys( modelsData );
  794. const textureData = {}; // evaluate model relationships to textures
  795. if ( modelRels ) {
  796. for ( let i = 0, l = modelRels.length; i < l; i ++ ) {
  797. const modelRel = modelRels[ i ];
  798. const textureKey = modelRel.target.substring( 1 );
  799. if ( data3mf.texture[ textureKey ] ) {
  800. textureData[ modelRel.target ] = data3mf.texture[ textureKey ];
  801. }
  802. }
  803. } // start build
  804. for ( let i = 0; i < modelsKeys.length; i ++ ) {
  805. const modelsKey = modelsKeys[ i ];
  806. const modelData = modelsData[ modelsKey ];
  807. const objectIds = Object.keys( modelData[ 'resources' ][ 'object' ] );
  808. for ( let j = 0; j < objectIds.length; j ++ ) {
  809. const objectId = objectIds[ j ];
  810. buildObject( objectId, objects, modelData, textureData );
  811. }
  812. }
  813. return objects;
  814. }
  815. function fetch3DModelPart( rels ) {
  816. for ( let i = 0; i < rels.length; i ++ ) {
  817. const rel = rels[ i ];
  818. const extension = rel.target.split( '.' ).pop();
  819. if ( extension.toLowerCase() === 'model' ) return rel;
  820. }
  821. }
  822. function build( objects, data3mf ) {
  823. const group = new THREE.Group();
  824. const relationship = fetch3DModelPart( data3mf[ 'rels' ] );
  825. const buildData = data3mf.model[ relationship[ 'target' ].substring( 1 ) ][ 'build' ];
  826. for ( let i = 0; i < buildData.length; i ++ ) {
  827. const buildItem = buildData[ i ];
  828. const object3D = objects[ buildItem[ 'objectId' ] ].clone(); // apply transform
  829. const transform = buildItem[ 'transform' ];
  830. if ( transform ) {
  831. object3D.applyMatrix4( transform );
  832. }
  833. group.add( object3D );
  834. }
  835. return group;
  836. }
  837. const data3mf = loadDocument( data );
  838. const objects = buildObjects( data3mf );
  839. return build( objects, data3mf );
  840. }
  841. addExtension( extension ) {
  842. this.availableExtensions.push( extension );
  843. }
  844. }
  845. THREE.ThreeMFLoader = ThreeMFLoader;
  846. } )();