NodeLibrary.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. class NodeLibrary {
  2. constructor() {
  3. this.lightNodes = new WeakMap();
  4. this.materialNodes = new Map();
  5. this.toneMappingNodes = new Map();
  6. }
  7. fromMaterial( material ) {
  8. if ( material.isNodeMaterial ) return material;
  9. let nodeMaterial = null;
  10. const nodeMaterialClass = this.getMaterialNodeClass( material.type );
  11. if ( nodeMaterialClass !== null ) {
  12. nodeMaterial = new nodeMaterialClass();
  13. for ( const key in material ) {
  14. nodeMaterial[ key ] = material[ key ];
  15. }
  16. }
  17. return nodeMaterial;
  18. }
  19. addToneMapping( toneMappingNode, toneMapping ) {
  20. this.addType( toneMappingNode, toneMapping, this.toneMappingNodes );
  21. }
  22. getToneMappingFunction( toneMapping ) {
  23. return this.toneMappingNodes.get( toneMapping ) || null;
  24. }
  25. getMaterialNodeClass( materialType ) {
  26. return this.materialNodes.get( materialType ) || null;
  27. }
  28. addMaterial( materialNodeClass, materialClass ) {
  29. this.addType( materialNodeClass, materialClass.type, this.materialNodes );
  30. }
  31. getLightNodeClass( light ) {
  32. return this.lightNodes.get( light ) || null;
  33. }
  34. addLight( lightNodeClass, lightClass ) {
  35. this.addClass( lightNodeClass, lightClass, this.lightNodes );
  36. }
  37. addType( nodeClass, type, library ) {
  38. if ( library.has( type ) ) {
  39. console.warn( `Redefinition of node ${ type }` );
  40. return;
  41. }
  42. if ( typeof nodeClass !== 'function' ) throw new Error( `Node class ${ nodeClass.name } is not a class.` );
  43. if ( typeof type === 'function' || typeof type === 'object' ) throw new Error( `Base class ${ type } is not a class.` );
  44. library.set( type, nodeClass );
  45. }
  46. addClass( nodeClass, baseClass, library ) {
  47. if ( library.has( baseClass ) ) {
  48. console.warn( `Redefinition of node ${ baseClass.name }` );
  49. return;
  50. }
  51. if ( typeof nodeClass !== 'function' ) throw new Error( `Node class ${ nodeClass.name } is not a class.` );
  52. if ( typeof baseClass !== 'function' ) throw new Error( `Base class ${ baseClass.name } is not a class.` );
  53. library.set( baseClass, nodeClass );
  54. }
  55. }
  56. export default NodeLibrary;
粤ICP备19079148号