NodeMaterialLoader.js 1.2 KB

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