3MFLoader.js 34 KB

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