NodeMaterialLoader.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { MaterialLoader } from 'three';
  2. import {
  3. NodeMaterial,
  4. LineBasicNodeMaterial,
  5. MeshBasicNodeMaterial,
  6. MeshStandardNodeMaterial,
  7. PointsNodeMaterial,
  8. SpriteNodeMaterial
  9. } from '../materials/Materials.js';
  10. const superFromTypeFunction = MaterialLoader.createMaterialFromType;
  11. MaterialLoader.createMaterialFromType = function ( type ) {
  12. const materialLib = {
  13. NodeMaterial,
  14. LineBasicNodeMaterial,
  15. MeshBasicNodeMaterial,
  16. MeshStandardNodeMaterial,
  17. PointsNodeMaterial,
  18. SpriteNodeMaterial,
  19. };
  20. if ( materialLib[ type ] !== undefined ) {
  21. return new materialLib[ type ]();
  22. }
  23. return superFromTypeFunction.call( this, type );
  24. };
  25. class NodeMaterialLoader extends MaterialLoader {
  26. constructor( manager ) {
  27. super( manager );
  28. this.nodes = {};
  29. }
  30. parse( json ) {
  31. const material = super.parse( json );
  32. const nodes = this.nodes;
  33. const inputNodes = json.inputNodes;
  34. for ( const property in inputNodes ) {
  35. const uuid = inputNodes[ property ];
  36. material[ property ] = nodes[ uuid ];
  37. }
  38. return material;
  39. }
  40. setNodes( value ) {
  41. this.nodes = value;
  42. return this;
  43. }
  44. }
  45. export default NodeMaterialLoader;