1
0

MaterialXLoader.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  1. import { FileLoader, Loader, TextureLoader, MeshBasicNodeMaterial, MeshPhysicalNodeMaterial, RepeatWrapping } from 'three';
  2. import {
  3. float, bool, int, vec2, vec3, vec4, color, texture,
  4. positionLocal, positionWorld, uv, vertexColor,
  5. normalLocal, normalWorld, tangentLocal, tangentWorld,
  6. add, sub, mul, div, mod, abs, sign, floor, ceil, round, pow, sin, cos, tan,
  7. asin, acos, atan2, sqrt, exp, clamp, min, max, normalize, length, dot, cross, normalMap,
  8. remap, smoothstep, luminance, mx_rgbtohsv, mx_hsvtorgb,
  9. mix, split,
  10. mx_ramplr, mx_ramptb, mx_splitlr, mx_splittb,
  11. mx_fractal_noise_float, mx_noise_float, mx_cell_noise_float, mx_worley_noise_float,
  12. mx_transform_uv,
  13. mx_safepower, mx_contrast,
  14. mx_srgb_texture_to_lin_rec709,
  15. saturation,
  16. timerLocal, frameId
  17. } from 'three/tsl';
  18. const colorSpaceLib = {
  19. mx_srgb_texture_to_lin_rec709
  20. };
  21. class MXElement {
  22. constructor( name, nodeFunc, params = [] ) {
  23. this.name = name;
  24. this.nodeFunc = nodeFunc;
  25. this.params = params;
  26. }
  27. }
  28. // Ref: https://github.com/mrdoob/three.js/issues/24674
  29. const mx_add = ( in1, in2 = float( 0 ) ) => add( in1, in2 );
  30. const mx_subtract = ( in1, in2 = float( 0 ) ) => sub( in1, in2 );
  31. const mx_multiply = ( in1, in2 = float( 1 ) ) => mul( in1, in2 );
  32. const mx_divide = ( in1, in2 = float( 1 ) ) => div( in1, in2 );
  33. const mx_modulo = ( in1, in2 = float( 1 ) ) => mod( in1, in2 );
  34. const mx_power = ( in1, in2 = float( 1 ) ) => pow( in1, in2 );
  35. const mx_atan2 = ( in1 = float( 0 ), in2 = float( 1 ) ) => atan2( in1, in2 );
  36. const mx_timer = () => timerLocal();
  37. const mx_frame = () => frameId;
  38. const mx_invert = ( in1, amount = float( 1 ) ) => sub( amount, in1 );
  39. const separate = ( in1, channel ) => split( in1, channel.at( - 1 ) );
  40. const extract = ( in1, index ) => in1.element( index );
  41. const MXElements = [
  42. // << Math >>
  43. new MXElement( 'add', mx_add, [ 'in1', 'in2' ] ),
  44. new MXElement( 'subtract', mx_subtract, [ 'in1', 'in2' ] ),
  45. new MXElement( 'multiply', mx_multiply, [ 'in1', 'in2' ] ),
  46. new MXElement( 'divide', mx_divide, [ 'in1', 'in2' ] ),
  47. new MXElement( 'modulo', mx_modulo, [ 'in1', 'in2' ] ),
  48. new MXElement( 'absval', abs, [ 'in1', 'in2' ] ),
  49. new MXElement( 'sign', sign, [ 'in1', 'in2' ] ),
  50. new MXElement( 'floor', floor, [ 'in1', 'in2' ] ),
  51. new MXElement( 'ceil', ceil, [ 'in1', 'in2' ] ),
  52. new MXElement( 'round', round, [ 'in1', 'in2' ] ),
  53. new MXElement( 'power', mx_power, [ 'in1', 'in2' ] ),
  54. new MXElement( 'sin', sin, [ 'in' ] ),
  55. new MXElement( 'cos', cos, [ 'in' ] ),
  56. new MXElement( 'tan', tan, [ 'in' ] ),
  57. new MXElement( 'asin', asin, [ 'in' ] ),
  58. new MXElement( 'acos', acos, [ 'in' ] ),
  59. new MXElement( 'atan2', mx_atan2, [ 'in1', 'in2' ] ),
  60. new MXElement( 'sqrt', sqrt, [ 'in' ] ),
  61. //new MtlXElement( 'ln', ... ),
  62. new MXElement( 'exp', exp, [ 'in' ] ),
  63. new MXElement( 'clamp', clamp, [ 'in', 'low', 'high' ] ),
  64. new MXElement( 'min', min, [ 'in1', 'in2' ] ),
  65. new MXElement( 'max', max, [ 'in1', 'in2' ] ),
  66. new MXElement( 'normalize', normalize, [ 'in' ] ),
  67. new MXElement( 'magnitude', length, [ 'in1', 'in2' ] ),
  68. new MXElement( 'dotproduct', dot, [ 'in1', 'in2' ] ),
  69. new MXElement( 'crossproduct', cross, [ 'in' ] ),
  70. new MXElement( 'invert', mx_invert, [ 'in', 'amount' ] ),
  71. //new MtlXElement( 'transformpoint', ... ),
  72. //new MtlXElement( 'transformvector', ... ),
  73. //new MtlXElement( 'transformnormal', ... ),
  74. //new MtlXElement( 'transformmatrix', ... ),
  75. new MXElement( 'normalmap', normalMap, [ 'in', 'scale' ] ),
  76. //new MtlXElement( 'transpose', ... ),
  77. //new MtlXElement( 'determinant', ... ),
  78. //new MtlXElement( 'invertmatrix', ... ),
  79. //new MtlXElement( 'rotate2d', rotateUV, [ 'in', radians( 'amount' )** ] ),
  80. //new MtlXElement( 'rotate3d', ... ),
  81. //new MtlXElement( 'arrayappend', ... ),
  82. //new MtlXElement( 'dot', ... ),
  83. // << Adjustment >>
  84. new MXElement( 'remap', remap, [ 'in', 'inlow', 'inhigh', 'outlow', 'outhigh' ] ),
  85. new MXElement( 'smoothstep', smoothstep, [ 'in', 'low', 'high' ] ),
  86. //new MtlXElement( 'curveadjust', ... ),
  87. //new MtlXElement( 'curvelookup', ... ),
  88. new MXElement( 'luminance', luminance, [ 'in', 'lumacoeffs' ] ),
  89. new MXElement( 'rgbtohsv', mx_rgbtohsv, [ 'in' ] ),
  90. new MXElement( 'hsvtorgb', mx_hsvtorgb, [ 'in' ] ),
  91. // << Mix >>
  92. new MXElement( 'mix', mix, [ 'bg', 'fg', 'mix' ] ),
  93. // << Channel >>
  94. new MXElement( 'combine2', vec2, [ 'in1', 'in2' ] ),
  95. new MXElement( 'combine3', vec3, [ 'in1', 'in2', 'in3' ] ),
  96. new MXElement( 'combine4', vec4, [ 'in1', 'in2', 'in3', 'in4' ] ),
  97. // << Procedural >>
  98. new MXElement( 'ramplr', mx_ramplr, [ 'valuel', 'valuer', 'texcoord' ] ),
  99. new MXElement( 'ramptb', mx_ramptb, [ 'valuet', 'valueb', 'texcoord' ] ),
  100. new MXElement( 'splitlr', mx_splitlr, [ 'valuel', 'valuer', 'texcoord' ] ),
  101. new MXElement( 'splittb', mx_splittb, [ 'valuet', 'valueb', 'texcoord' ] ),
  102. new MXElement( 'noise2d', mx_noise_float, [ 'texcoord', 'amplitude', 'pivot' ] ),
  103. new MXElement( 'noise3d', mx_noise_float, [ 'texcoord', 'amplitude', 'pivot' ] ),
  104. new MXElement( 'fractal3d', mx_fractal_noise_float, [ 'position', 'octaves', 'lacunarity', 'diminish', 'amplitude' ] ),
  105. new MXElement( 'cellnoise2d', mx_cell_noise_float, [ 'texcoord' ] ),
  106. new MXElement( 'cellnoise3d', mx_cell_noise_float, [ 'texcoord' ] ),
  107. new MXElement( 'worleynoise2d', mx_worley_noise_float, [ 'texcoord', 'jitter' ] ),
  108. new MXElement( 'worleynoise3d', mx_worley_noise_float, [ 'texcoord', 'jitter' ] ),
  109. // << Supplemental >>
  110. //new MtlXElement( 'tiledimage', ... ),
  111. //new MtlXElement( 'triplanarprojection', triplanarTextures, [ 'filex', 'filey', 'filez' ] ),
  112. //new MtlXElement( 'ramp4', ... ),
  113. //new MtlXElement( 'place2d', mx_place2d, [ 'texcoord', 'pivot', 'scale', 'rotate', 'offset' ] ),
  114. new MXElement( 'safepower', mx_safepower, [ 'in1', 'in2' ] ),
  115. new MXElement( 'contrast', mx_contrast, [ 'in', 'amount', 'pivot' ] ),
  116. //new MtlXElement( 'hsvadjust', ... ),
  117. new MXElement( 'saturate', saturation, [ 'in', 'amount' ] ),
  118. new MXElement( 'extract', extract, [ 'in', 'index' ] ),
  119. new MXElement( 'separate2', separate, [ 'in' ] ),
  120. new MXElement( 'separate3', separate, [ 'in' ] ),
  121. new MXElement( 'separate4', separate, [ 'in' ] ),
  122. new MXElement( 'time', mx_timer ),
  123. new MXElement( 'frame', mx_frame )
  124. ];
  125. const MtlXLibrary = {};
  126. MXElements.forEach( element => MtlXLibrary[ element.name ] = element );
  127. class MaterialXLoader extends Loader {
  128. constructor( manager ) {
  129. super( manager );
  130. }
  131. load( url, onLoad, onProgress, onError ) {
  132. const _onError = function ( e ) {
  133. if ( onError ) {
  134. onError( e );
  135. } else {
  136. console.error( e );
  137. }
  138. };
  139. new FileLoader( this.manager )
  140. .setPath( this.path )
  141. .load( url, async ( text ) => {
  142. try {
  143. onLoad( this.parse( text ) );
  144. } catch ( e ) {
  145. _onError( e );
  146. }
  147. }, onProgress, _onError );
  148. return this;
  149. }
  150. parse( text ) {
  151. return new MaterialX( this.manager, this.path ).parse( text );
  152. }
  153. }
  154. class MaterialXNode {
  155. constructor( materialX, nodeXML, nodePath = '' ) {
  156. this.materialX = materialX;
  157. this.nodeXML = nodeXML;
  158. this.nodePath = nodePath ? nodePath + '/' + this.name : this.name;
  159. this.parent = null;
  160. this.node = null;
  161. this.children = [];
  162. }
  163. get element() {
  164. return this.nodeXML.nodeName;
  165. }
  166. get nodeGraph() {
  167. return this.getAttribute( 'nodegraph' );
  168. }
  169. get nodeName() {
  170. return this.getAttribute( 'nodename' );
  171. }
  172. get interfaceName() {
  173. return this.getAttribute( 'interfacename' );
  174. }
  175. get output() {
  176. return this.getAttribute( 'output' );
  177. }
  178. get name() {
  179. return this.getAttribute( 'name' );
  180. }
  181. get type() {
  182. return this.getAttribute( 'type' );
  183. }
  184. get value() {
  185. return this.getAttribute( 'value' );
  186. }
  187. getNodeGraph() {
  188. let nodeX = this;
  189. while ( nodeX !== null ) {
  190. if ( nodeX.element === 'nodegraph' ) {
  191. break;
  192. }
  193. nodeX = nodeX.parent;
  194. }
  195. return nodeX;
  196. }
  197. getRoot() {
  198. let nodeX = this;
  199. while ( nodeX.parent !== null ) {
  200. nodeX = nodeX.parent;
  201. }
  202. return nodeX;
  203. }
  204. get referencePath() {
  205. let referencePath = null;
  206. if ( this.nodeGraph !== null && this.output !== null ) {
  207. referencePath = this.nodeGraph + '/' + this.output;
  208. } else if ( this.nodeName !== null || this.interfaceName !== null ) {
  209. referencePath = this.getNodeGraph().nodePath + '/' + ( this.nodeName || this.interfaceName );
  210. }
  211. return referencePath;
  212. }
  213. get hasReference() {
  214. return this.referencePath !== null;
  215. }
  216. get isConst() {
  217. return this.element === 'input' && this.value !== null && this.type !== 'filename';
  218. }
  219. getColorSpaceNode() {
  220. const csSource = this.getAttribute( 'colorspace' );
  221. const csTarget = this.getRoot().getAttribute( 'colorspace' );
  222. const nodeName = `mx_${ csSource }_to_${ csTarget }`;
  223. return colorSpaceLib[ nodeName ];
  224. }
  225. getTexture() {
  226. const filePrefix = this.getRecursiveAttribute( 'fileprefix' ) || '';
  227. let loader = this.materialX.textureLoader;
  228. const uri = filePrefix + this.value;
  229. if ( uri ) {
  230. const handler = this.materialX.manager.getHandler( uri );
  231. if ( handler !== null ) loader = handler;
  232. }
  233. const texture = loader.load( uri );
  234. texture.wrapS = texture.wrapT = RepeatWrapping;
  235. texture.flipY = false;
  236. return texture;
  237. }
  238. getClassFromType( type ) {
  239. let nodeClass = null;
  240. if ( type === 'integer' ) nodeClass = int;
  241. else if ( type === 'float' ) nodeClass = float;
  242. else if ( type === 'vector2' ) nodeClass = vec2;
  243. else if ( type === 'vector3' ) nodeClass = vec3;
  244. else if ( type === 'vector4' || type === 'color4' ) nodeClass = vec4;
  245. else if ( type === 'color3' ) nodeClass = color;
  246. else if ( type === 'boolean' ) nodeClass = bool;
  247. return nodeClass;
  248. }
  249. getNode( out = null ) {
  250. let node = this.node;
  251. if ( node !== null && out === null ) {
  252. return node;
  253. }
  254. //
  255. const type = this.type;
  256. if ( this.isConst ) {
  257. const nodeClass = this.getClassFromType( type );
  258. node = nodeClass( ...this.getVector() );
  259. } else if ( this.hasReference ) {
  260. if ( this.element === 'output' && this.output && out === null ) {
  261. out = this.output;
  262. }
  263. node = this.materialX.getMaterialXNode( this.referencePath ).getNode( out );
  264. } else {
  265. const element = this.element;
  266. if ( element === 'convert' ) {
  267. const nodeClass = this.getClassFromType( type );
  268. node = nodeClass( this.getNodeByName( 'in' ) );
  269. } else if ( element === 'constant' ) {
  270. node = this.getNodeByName( 'value' );
  271. } else if ( element === 'position' ) {
  272. const space = this.getAttribute( 'space' );
  273. node = space === 'world' ? positionWorld : positionLocal;
  274. } else if ( element === 'normal' ) {
  275. const space = this.getAttribute( 'space' );
  276. node = space === 'world' ? normalWorld : normalLocal;
  277. } else if ( element === 'tangent' ) {
  278. const space = this.getAttribute( 'space' );
  279. node = space === 'world' ? tangentWorld : tangentLocal;
  280. } else if ( element === 'texcoord' ) {
  281. const indexNode = this.getChildByName( 'index' );
  282. const index = indexNode ? parseInt( indexNode.value ) : 0;
  283. node = uv( index );
  284. } else if ( element === 'geomcolor' ) {
  285. const indexNode = this.getChildByName( 'index' );
  286. const index = indexNode ? parseInt( indexNode.value ) : 0;
  287. node = vertexColor( index );
  288. } else if ( element === 'tiledimage' ) {
  289. const file = this.getChildByName( 'file' );
  290. const textureFile = file.getTexture();
  291. const uvTiling = mx_transform_uv( ...this.getNodesByNames( [ 'uvtiling', 'uvoffset' ] ) );
  292. node = texture( textureFile, uvTiling );
  293. const colorSpaceNode = file.getColorSpaceNode();
  294. if ( colorSpaceNode ) {
  295. node = colorSpaceNode( node );
  296. }
  297. } else if ( element === 'image' ) {
  298. const file = this.getChildByName( 'file' );
  299. const uvNode = this.getNodeByName( 'texcoord' );
  300. const textureFile = file.getTexture();
  301. node = texture( textureFile, uvNode );
  302. const colorSpaceNode = file.getColorSpaceNode();
  303. if ( colorSpaceNode ) {
  304. node = colorSpaceNode( node );
  305. }
  306. } else if ( MtlXLibrary[ element ] !== undefined ) {
  307. const nodeElement = MtlXLibrary[ element ];
  308. if ( out !== null ) {
  309. node = nodeElement.nodeFunc( ...this.getNodesByNames( ...nodeElement.params ), out );
  310. } else {
  311. node = nodeElement.nodeFunc( ...this.getNodesByNames( ...nodeElement.params ) );
  312. }
  313. }
  314. }
  315. //
  316. if ( node === null ) {
  317. console.warn( `THREE.MaterialXLoader: Unexpected node ${ new XMLSerializer().serializeToString( this.nodeXML ) }.` );
  318. node = float( 0 );
  319. }
  320. //
  321. const nodeToTypeClass = this.getClassFromType( type );
  322. if ( nodeToTypeClass !== null ) {
  323. node = nodeToTypeClass( node );
  324. }
  325. node.name = this.name;
  326. this.node = node;
  327. return node;
  328. }
  329. getChildByName( name ) {
  330. for ( const input of this.children ) {
  331. if ( input.name === name ) {
  332. return input;
  333. }
  334. }
  335. }
  336. getNodes() {
  337. const nodes = {};
  338. for ( const input of this.children ) {
  339. const node = input.getNode();
  340. nodes[ node.name ] = node;
  341. }
  342. return nodes;
  343. }
  344. getNodeByName( name ) {
  345. const child = this.getChildByName( name );
  346. return child ? child.getNode( child.output ) : undefined;
  347. }
  348. getNodesByNames( ...names ) {
  349. const nodes = [];
  350. for ( const name of names ) {
  351. const node = this.getNodeByName( name );
  352. if ( node ) nodes.push( node );
  353. }
  354. return nodes;
  355. }
  356. getValue() {
  357. return this.value.trim();
  358. }
  359. getVector() {
  360. const vector = [];
  361. for ( const val of this.getValue().split( /[,|\s]/ ) ) {
  362. if ( val !== '' ) {
  363. vector.push( Number( val.trim() ) );
  364. }
  365. }
  366. return vector;
  367. }
  368. getAttribute( name ) {
  369. return this.nodeXML.getAttribute( name );
  370. }
  371. getRecursiveAttribute( name ) {
  372. let attribute = this.nodeXML.getAttribute( name );
  373. if ( attribute === null && this.parent !== null ) {
  374. attribute = this.parent.getRecursiveAttribute( name );
  375. }
  376. return attribute;
  377. }
  378. setStandardSurfaceToGltfPBR( material ) {
  379. const inputs = this.getNodes();
  380. //
  381. let colorNode = null;
  382. if ( inputs.base && inputs.base_color ) colorNode = mul( inputs.base, inputs.base_color );
  383. else if ( inputs.base ) colorNode = inputs.base;
  384. else if ( inputs.base_color ) colorNode = inputs.base_color;
  385. //
  386. let roughnessNode = null;
  387. if ( inputs.specular_roughness ) roughnessNode = inputs.specular_roughness;
  388. //
  389. let metalnessNode = null;
  390. if ( inputs.metalness ) metalnessNode = inputs.metalness;
  391. //
  392. let clearcoatNode = null;
  393. let clearcoatRoughnessNode = null;
  394. if ( inputs.coat ) clearcoatNode = inputs.coat;
  395. if ( inputs.coat_roughness ) clearcoatRoughnessNode = inputs.coat_roughness;
  396. if ( inputs.coat_color ) {
  397. colorNode = colorNode ? mul( colorNode, inputs.coat_color ) : colorNode;
  398. }
  399. //
  400. let normalNode = null;
  401. if ( inputs.normal ) normalNode = inputs.normal;
  402. //
  403. let emissiveNode = null;
  404. if ( inputs.emission ) emissiveNode = inputs.emission;
  405. if ( inputs.emissionColor ) {
  406. emissiveNode = emissiveNode ? mul( emissiveNode, inputs.emissionColor ) : emissiveNode;
  407. }
  408. //
  409. material.colorNode = colorNode || color( 0.8, 0.8, 0.8 );
  410. material.roughnessNode = roughnessNode || float( 0.2 );
  411. material.metalnessNode = metalnessNode || float( 0 );
  412. material.clearcoatNode = clearcoatNode || float( 0 );
  413. material.clearcoatRoughnessNode = clearcoatRoughnessNode || float( 0 );
  414. if ( normalNode ) material.normalNode = normalNode;
  415. if ( emissiveNode ) material.emissiveNode = emissiveNode;
  416. }
  417. /*setGltfPBR( material ) {
  418. const inputs = this.getNodes();
  419. console.log( inputs );
  420. }*/
  421. setMaterial( material ) {
  422. const element = this.element;
  423. if ( element === 'gltf_pbr' ) {
  424. //this.setGltfPBR( material );
  425. } else if ( element === 'standard_surface' ) {
  426. this.setStandardSurfaceToGltfPBR( material );
  427. }
  428. }
  429. toBasicMaterial() {
  430. const material = new MeshBasicNodeMaterial();
  431. material.name = this.name;
  432. for ( const nodeX of this.children.toReversed() ) {
  433. if ( nodeX.name === 'out' ) {
  434. material.colorNode = nodeX.getNode();
  435. break;
  436. }
  437. }
  438. return material;
  439. }
  440. toPhysicalMaterial() {
  441. const material = new MeshPhysicalNodeMaterial();
  442. material.name = this.name;
  443. for ( const nodeX of this.children ) {
  444. const shaderProperties = this.materialX.getMaterialXNode( nodeX.nodeName );
  445. shaderProperties.setMaterial( material );
  446. }
  447. return material;
  448. }
  449. toMaterials() {
  450. const materials = {};
  451. let isUnlit = true;
  452. for ( const nodeX of this.children ) {
  453. if ( nodeX.element === 'surfacematerial' ) {
  454. const material = nodeX.toPhysicalMaterial();
  455. materials[ material.name ] = material;
  456. isUnlit = false;
  457. }
  458. }
  459. if ( isUnlit ) {
  460. for ( const nodeX of this.children ) {
  461. if ( nodeX.element === 'nodegraph' ) {
  462. const material = nodeX.toBasicMaterial();
  463. materials[ material.name ] = material;
  464. }
  465. }
  466. }
  467. return materials;
  468. }
  469. add( materialXNode ) {
  470. materialXNode.parent = this;
  471. this.children.push( materialXNode );
  472. }
  473. }
  474. class MaterialX {
  475. constructor( manager, path ) {
  476. this.manager = manager;
  477. this.path = path;
  478. this.resourcePath = '';
  479. this.nodesXLib = new Map();
  480. //this.nodesXRefLib = new WeakMap();
  481. this.textureLoader = new TextureLoader( manager );
  482. }
  483. addMaterialXNode( materialXNode ) {
  484. this.nodesXLib.set( materialXNode.nodePath, materialXNode );
  485. }
  486. /*getMaterialXNodeFromXML( xmlNode ) {
  487. return this.nodesXRefLib.get( xmlNode );
  488. }*/
  489. getMaterialXNode( ...names ) {
  490. return this.nodesXLib.get( names.join( '/' ) );
  491. }
  492. parseNode( nodeXML, nodePath = '' ) {
  493. const materialXNode = new MaterialXNode( this, nodeXML, nodePath );
  494. if ( materialXNode.nodePath ) this.addMaterialXNode( materialXNode );
  495. for ( const childNodeXML of nodeXML.children ) {
  496. const childMXNode = this.parseNode( childNodeXML, materialXNode.nodePath );
  497. materialXNode.add( childMXNode );
  498. }
  499. return materialXNode;
  500. }
  501. parse( text ) {
  502. const rootXML = new DOMParser().parseFromString( text, 'application/xml' ).documentElement;
  503. this.textureLoader.setPath( this.path );
  504. //
  505. const materials = this.parseNode( rootXML ).toMaterials();
  506. return { materials };
  507. }
  508. }
  509. export { MaterialXLoader };