Materials.js 1.3 KB

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