Materials.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import NodeMaterial from './NodeMaterial.js';
  2. import LineBasicNodeMaterial from './LineBasicNodeMaterial.js';
  3. import MeshBasicNodeMaterial from './MeshBasicNodeMaterial.js';
  4. import MeshStandardNodeMaterial from './MeshStandardNodeMaterial.js';
  5. import PointsNodeMaterial from './PointsNodeMaterial.js';
  6. import SpriteNodeMaterial from './SpriteNodeMaterial.js';
  7. import { Material } from 'three';
  8. export {
  9. NodeMaterial,
  10. LineBasicNodeMaterial,
  11. MeshBasicNodeMaterial,
  12. MeshStandardNodeMaterial,
  13. PointsNodeMaterial,
  14. SpriteNodeMaterial
  15. };
  16. const materialLib = {
  17. NodeMaterial,
  18. LineBasicNodeMaterial,
  19. MeshBasicNodeMaterial,
  20. MeshStandardNodeMaterial,
  21. PointsNodeMaterial,
  22. SpriteNodeMaterial
  23. };
  24. const fromTypeFunction = Material.fromType;
  25. Material.fromType = function ( type ) {
  26. if ( materialLib[ type ] !== undefined ) {
  27. return new materialLib[ type ]();
  28. }
  29. return fromTypeFunction.call( this, type );
  30. };
  31. NodeMaterial.fromMaterial = function ( material ) {
  32. const type = material.type.replace( 'Material', 'NodeMaterial' );
  33. if ( materialLib[ type ] === undefined ) {
  34. return material; // is already a node material or cannot be converted
  35. }
  36. const nodeMaterial = new materialLib[ type ]( material );
  37. for ( let key in material ) {
  38. if ( nodeMaterial[ key ] === undefined ) {
  39. nodeMaterial[ key ] = material[ key ]; // currently this is needed only for material.alphaTest
  40. }
  41. }
  42. return nodeMaterial;
  43. };