MaterialXLoader.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. import { FileLoader, Loader, TextureLoader, RepeatWrapping, MeshBasicNodeMaterial, MeshPhysicalNodeMaterial } from 'three/webgpu';
  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. /**
  128. * A loader for the MaterialX format.
  129. *
  130. * The node materials loaded with this loader can only be used with {@link WebGPURenderer}.
  131. *
  132. * ```js
  133. * const loader = new MaterialXLoader().setPath( SAMPLE_PATH );
  134. * const materials = await loader.loadAsync( 'standard_surface_brass_tiled.mtlx' );
  135. * ```
  136. *
  137. * @augments Loader
  138. */
  139. class MaterialXLoader extends Loader {
  140. /**
  141. * Constructs a new MaterialX loader.
  142. *
  143. * @param {LoadingManager} [manager] - The loading manager.
  144. */
  145. constructor( manager ) {
  146. super( manager );
  147. }
  148. /**
  149. * Starts loading from the given URL and passes the loaded MaterialX asset
  150. * to the `onLoad()` callback.
  151. *
  152. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  153. * @param {function(Object<string,NodeMaterial>)} onLoad - Executed when the loading process has been finished.
  154. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  155. * @param {onErrorCallback} onError - Executed when errors occur.
  156. * @return {MaterialXLoader} A reference to this loader.
  157. */
  158. load( url, onLoad, onProgress, onError ) {
  159. const _onError = function ( e ) {
  160. if ( onError ) {
  161. onError( e );
  162. } else {
  163. console.error( e );
  164. }
  165. };
  166. new FileLoader( this.manager )
  167. .setPath( this.path )
  168. .load( url, async ( text ) => {
  169. try {
  170. onLoad( this.parse( text ) );
  171. } catch ( e ) {
  172. _onError( e );
  173. }
  174. }, onProgress, _onError );
  175. return this;
  176. }
  177. /**
  178. * Parses the given MaterialX data and returns the resulting materials.
  179. *
  180. * @param {string} text - The raw MaterialX data as a string.
  181. * @return {Object<string,NodeMaterial>} A dictionary holding the parse node materials.
  182. */
  183. parse( text ) {
  184. return new MaterialX( this.manager, this.path ).parse( text );
  185. }
  186. }
  187. class MaterialXNode {
  188. constructor( materialX, nodeXML, nodePath = '' ) {
  189. this.materialX = materialX;
  190. this.nodeXML = nodeXML;
  191. this.nodePath = nodePath ? nodePath + '/' + this.name : this.name;
  192. this.parent = null;
  193. this.node = null;
  194. this.children = [];
  195. }
  196. get element() {
  197. return this.nodeXML.nodeName;
  198. }
  199. get nodeGraph() {
  200. return this.getAttribute( 'nodegraph' );
  201. }
  202. get nodeName() {
  203. return this.getAttribute( 'nodename' );
  204. }
  205. get interfaceName() {
  206. return this.getAttribute( 'interfacename' );
  207. }
  208. get output() {
  209. return this.getAttribute( 'output' );
  210. }
  211. get name() {
  212. return this.getAttribute( 'name' );
  213. }
  214. get type() {
  215. return this.getAttribute( 'type' );
  216. }
  217. get value() {
  218. return this.getAttribute( 'value' );
  219. }
  220. getNodeGraph() {
  221. let nodeX = this;
  222. while ( nodeX !== null ) {
  223. if ( nodeX.element === 'nodegraph' ) {
  224. break;
  225. }
  226. nodeX = nodeX.parent;
  227. }
  228. return nodeX;
  229. }
  230. getRoot() {
  231. let nodeX = this;
  232. while ( nodeX.parent !== null ) {
  233. nodeX = nodeX.parent;
  234. }
  235. return nodeX;
  236. }
  237. get referencePath() {
  238. let referencePath = null;
  239. if ( this.nodeGraph !== null && this.output !== null ) {
  240. referencePath = this.nodeGraph + '/' + this.output;
  241. } else if ( this.nodeName !== null || this.interfaceName !== null ) {
  242. referencePath = this.getNodeGraph().nodePath + '/' + ( this.nodeName || this.interfaceName );
  243. }
  244. return referencePath;
  245. }
  246. get hasReference() {
  247. return this.referencePath !== null;
  248. }
  249. get isConst() {
  250. return this.element === 'input' && this.value !== null && this.type !== 'filename';
  251. }
  252. getColorSpaceNode() {
  253. const csSource = this.getAttribute( 'colorspace' );
  254. const csTarget = this.getRoot().getAttribute( 'colorspace' );
  255. const nodeName = `mx_${ csSource }_to_${ csTarget }`;
  256. return colorSpaceLib[ nodeName ];
  257. }
  258. getTexture() {
  259. const filePrefix = this.getRecursiveAttribute( 'fileprefix' ) || '';
  260. let loader = this.materialX.textureLoader;
  261. const uri = filePrefix + this.value;
  262. if ( uri ) {
  263. const handler = this.materialX.manager.getHandler( uri );
  264. if ( handler !== null ) loader = handler;
  265. }
  266. const texture = loader.load( uri );
  267. texture.wrapS = texture.wrapT = RepeatWrapping;
  268. texture.flipY = false;
  269. return texture;
  270. }
  271. getClassFromType( type ) {
  272. let nodeClass = null;
  273. if ( type === 'integer' ) nodeClass = int;
  274. else if ( type === 'float' ) nodeClass = float;
  275. else if ( type === 'vector2' ) nodeClass = vec2;
  276. else if ( type === 'vector3' ) nodeClass = vec3;
  277. else if ( type === 'vector4' || type === 'color4' ) nodeClass = vec4;
  278. else if ( type === 'color3' ) nodeClass = color;
  279. else if ( type === 'boolean' ) nodeClass = bool;
  280. return nodeClass;
  281. }
  282. getNode( out = null ) {
  283. let node = this.node;
  284. if ( node !== null && out === null ) {
  285. return node;
  286. }
  287. //
  288. const type = this.type;
  289. if ( this.isConst ) {
  290. const nodeClass = this.getClassFromType( type );
  291. node = nodeClass( ...this.getVector() );
  292. } else if ( this.hasReference ) {
  293. if ( this.element === 'output' && this.output && out === null ) {
  294. out = this.output;
  295. }
  296. node = this.materialX.getMaterialXNode( this.referencePath ).getNode( out );
  297. } else {
  298. const element = this.element;
  299. if ( element === 'convert' ) {
  300. const nodeClass = this.getClassFromType( type );
  301. node = nodeClass( this.getNodeByName( 'in' ) );
  302. } else if ( element === 'constant' ) {
  303. node = this.getNodeByName( 'value' );
  304. } else if ( element === 'position' ) {
  305. const space = this.getAttribute( 'space' );
  306. node = space === 'world' ? positionWorld : positionLocal;
  307. } else if ( element === 'normal' ) {
  308. const space = this.getAttribute( 'space' );
  309. node = space === 'world' ? normalWorld : normalLocal;
  310. } else if ( element === 'tangent' ) {
  311. const space = this.getAttribute( 'space' );
  312. node = space === 'world' ? tangentWorld : tangentLocal;
  313. } else if ( element === 'texcoord' ) {
  314. const indexNode = this.getChildByName( 'index' );
  315. const index = indexNode ? parseInt( indexNode.value ) : 0;
  316. node = uv( index );
  317. } else if ( element === 'geomcolor' ) {
  318. const indexNode = this.getChildByName( 'index' );
  319. const index = indexNode ? parseInt( indexNode.value ) : 0;
  320. node = vertexColor( index );
  321. } else if ( element === 'tiledimage' ) {
  322. const file = this.getChildByName( 'file' );
  323. const textureFile = file.getTexture();
  324. const uvTiling = mx_transform_uv( ...this.getNodesByNames( [ 'uvtiling', 'uvoffset' ] ) );
  325. node = texture( textureFile, uvTiling );
  326. const colorSpaceNode = file.getColorSpaceNode();
  327. if ( colorSpaceNode ) {
  328. node = colorSpaceNode( node );
  329. }
  330. } else if ( element === 'image' ) {
  331. const file = this.getChildByName( 'file' );
  332. const uvNode = this.getNodeByName( 'texcoord' );
  333. const textureFile = file.getTexture();
  334. node = texture( textureFile, uvNode );
  335. const colorSpaceNode = file.getColorSpaceNode();
  336. if ( colorSpaceNode ) {
  337. node = colorSpaceNode( node );
  338. }
  339. } else if ( MtlXLibrary[ element ] !== undefined ) {
  340. const nodeElement = MtlXLibrary[ element ];
  341. if ( out !== null ) {
  342. node = nodeElement.nodeFunc( ...this.getNodesByNames( ...nodeElement.params ), out );
  343. } else {
  344. node = nodeElement.nodeFunc( ...this.getNodesByNames( ...nodeElement.params ) );
  345. }
  346. }
  347. }
  348. //
  349. if ( node === null ) {
  350. console.warn( `THREE.MaterialXLoader: Unexpected node ${ new XMLSerializer().serializeToString( this.nodeXML ) }.` );
  351. node = float( 0 );
  352. }
  353. //
  354. const nodeToTypeClass = this.getClassFromType( type );
  355. if ( nodeToTypeClass !== null ) {
  356. node = nodeToTypeClass( node );
  357. }
  358. node.name = this.name;
  359. this.node = node;
  360. return node;
  361. }
  362. getChildByName( name ) {
  363. for ( const input of this.children ) {
  364. if ( input.name === name ) {
  365. return input;
  366. }
  367. }
  368. }
  369. getNodes() {
  370. const nodes = {};
  371. for ( const input of this.children ) {
  372. const node = input.getNode();
  373. nodes[ node.name ] = node;
  374. }
  375. return nodes;
  376. }
  377. getNodeByName( name ) {
  378. const child = this.getChildByName( name );
  379. return child ? child.getNode( child.output ) : undefined;
  380. }
  381. getNodesByNames( ...names ) {
  382. const nodes = [];
  383. for ( const name of names ) {
  384. const node = this.getNodeByName( name );
  385. if ( node ) nodes.push( node );
  386. }
  387. return nodes;
  388. }
  389. getValue() {
  390. return this.value.trim();
  391. }
  392. getVector() {
  393. const vector = [];
  394. for ( const val of this.getValue().split( /[,|\s]/ ) ) {
  395. if ( val !== '' ) {
  396. vector.push( Number( val.trim() ) );
  397. }
  398. }
  399. return vector;
  400. }
  401. getAttribute( name ) {
  402. return this.nodeXML.getAttribute( name );
  403. }
  404. getRecursiveAttribute( name ) {
  405. let attribute = this.nodeXML.getAttribute( name );
  406. if ( attribute === null && this.parent !== null ) {
  407. attribute = this.parent.getRecursiveAttribute( name );
  408. }
  409. return attribute;
  410. }
  411. setStandardSurfaceToGltfPBR( material ) {
  412. const inputs = this.getNodes();
  413. //
  414. let colorNode = null;
  415. if ( inputs.base && inputs.base_color ) colorNode = mul( inputs.base, inputs.base_color );
  416. else if ( inputs.base ) colorNode = inputs.base;
  417. else if ( inputs.base_color ) colorNode = inputs.base_color;
  418. //
  419. let roughnessNode = null;
  420. if ( inputs.specular_roughness ) roughnessNode = inputs.specular_roughness;
  421. //
  422. let metalnessNode = null;
  423. if ( inputs.metalness ) metalnessNode = inputs.metalness;
  424. //
  425. let clearcoatNode = null;
  426. let clearcoatRoughnessNode = null;
  427. if ( inputs.coat ) clearcoatNode = inputs.coat;
  428. if ( inputs.coat_roughness ) clearcoatRoughnessNode = inputs.coat_roughness;
  429. if ( inputs.coat_color ) {
  430. colorNode = colorNode ? mul( colorNode, inputs.coat_color ) : colorNode;
  431. }
  432. //
  433. let normalNode = null;
  434. if ( inputs.normal ) normalNode = inputs.normal;
  435. //
  436. let emissiveNode = null;
  437. if ( inputs.emission ) emissiveNode = inputs.emission;
  438. if ( inputs.emissionColor ) {
  439. emissiveNode = emissiveNode ? mul( emissiveNode, inputs.emissionColor ) : emissiveNode;
  440. }
  441. //
  442. material.colorNode = colorNode || color( 0.8, 0.8, 0.8 );
  443. material.roughnessNode = roughnessNode || float( 0.2 );
  444. material.metalnessNode = metalnessNode || float( 0 );
  445. material.clearcoatNode = clearcoatNode || float( 0 );
  446. material.clearcoatRoughnessNode = clearcoatRoughnessNode || float( 0 );
  447. if ( normalNode ) material.normalNode = normalNode;
  448. if ( emissiveNode ) material.emissiveNode = emissiveNode;
  449. }
  450. /*setGltfPBR( material ) {
  451. const inputs = this.getNodes();
  452. console.log( inputs );
  453. }*/
  454. setMaterial( material ) {
  455. const element = this.element;
  456. if ( element === 'gltf_pbr' ) {
  457. //this.setGltfPBR( material );
  458. } else if ( element === 'standard_surface' ) {
  459. this.setStandardSurfaceToGltfPBR( material );
  460. }
  461. }
  462. toBasicMaterial() {
  463. const material = new MeshBasicNodeMaterial();
  464. material.name = this.name;
  465. for ( const nodeX of this.children.toReversed() ) {
  466. if ( nodeX.name === 'out' ) {
  467. material.colorNode = nodeX.getNode();
  468. break;
  469. }
  470. }
  471. return material;
  472. }
  473. toPhysicalMaterial() {
  474. const material = new MeshPhysicalNodeMaterial();
  475. material.name = this.name;
  476. for ( const nodeX of this.children ) {
  477. const shaderProperties = this.materialX.getMaterialXNode( nodeX.nodeName );
  478. shaderProperties.setMaterial( material );
  479. }
  480. return material;
  481. }
  482. toMaterials() {
  483. const materials = {};
  484. let isUnlit = true;
  485. for ( const nodeX of this.children ) {
  486. if ( nodeX.element === 'surfacematerial' ) {
  487. const material = nodeX.toPhysicalMaterial();
  488. materials[ material.name ] = material;
  489. isUnlit = false;
  490. }
  491. }
  492. if ( isUnlit ) {
  493. for ( const nodeX of this.children ) {
  494. if ( nodeX.element === 'nodegraph' ) {
  495. const material = nodeX.toBasicMaterial();
  496. materials[ material.name ] = material;
  497. }
  498. }
  499. }
  500. return materials;
  501. }
  502. add( materialXNode ) {
  503. materialXNode.parent = this;
  504. this.children.push( materialXNode );
  505. }
  506. }
  507. class MaterialX {
  508. constructor( manager, path ) {
  509. this.manager = manager;
  510. this.path = path;
  511. this.resourcePath = '';
  512. this.nodesXLib = new Map();
  513. //this.nodesXRefLib = new WeakMap();
  514. this.textureLoader = new TextureLoader( manager );
  515. }
  516. addMaterialXNode( materialXNode ) {
  517. this.nodesXLib.set( materialXNode.nodePath, materialXNode );
  518. }
  519. /*getMaterialXNodeFromXML( xmlNode ) {
  520. return this.nodesXRefLib.get( xmlNode );
  521. }*/
  522. getMaterialXNode( ...names ) {
  523. return this.nodesXLib.get( names.join( '/' ) );
  524. }
  525. parseNode( nodeXML, nodePath = '' ) {
  526. const materialXNode = new MaterialXNode( this, nodeXML, nodePath );
  527. if ( materialXNode.nodePath ) this.addMaterialXNode( materialXNode );
  528. for ( const childNodeXML of nodeXML.children ) {
  529. const childMXNode = this.parseNode( childNodeXML, materialXNode.nodePath );
  530. materialXNode.add( childMXNode );
  531. }
  532. return materialXNode;
  533. }
  534. parse( text ) {
  535. const rootXML = new DOMParser().parseFromString( text, 'application/xml' ).documentElement;
  536. this.textureLoader.setPath( this.path );
  537. //
  538. const materials = this.parseNode( rootXML ).toMaterials();
  539. return { materials };
  540. }
  541. }
  542. export { MaterialXLoader };
粤ICP备19079148号