Node.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { NodeUpdateType } from './constants.js';
  2. import { MathUtils } from 'three';
  3. class Node {
  4. constructor( nodeType = null ) {
  5. this.nodeType = nodeType;
  6. this.updateType = NodeUpdateType.None;
  7. this.uuid = MathUtils.generateUUID();
  8. }
  9. get type() {
  10. return this.constructor.name;
  11. }
  12. getHash( /*builder*/ ) {
  13. return this.uuid;
  14. }
  15. getUpdateType( /*builder*/ ) {
  16. return this.updateType;
  17. }
  18. getNodeType( /*builder*/ ) {
  19. return this.nodeType;
  20. }
  21. update( /*frame*/ ) {
  22. console.warn( 'Abstract function.' );
  23. }
  24. generate( /*builder, output*/ ) {
  25. console.warn( 'Abstract function.' );
  26. }
  27. build( builder, output = null ) {
  28. const hash = this.getHash( builder );
  29. const sharedNode = builder.getNodeFromHash( hash );
  30. if ( sharedNode !== undefined && this !== sharedNode ) {
  31. return sharedNode.build( builder, output );
  32. }
  33. builder.addNode( this );
  34. builder.addStack( this );
  35. const isGenerateOnce = this.generate.length === 1;
  36. let snippet = null;
  37. if ( isGenerateOnce ) {
  38. const type = this.getNodeType( builder );
  39. const nodeData = builder.getDataFromNode( this );
  40. snippet = nodeData.snippet;
  41. if ( snippet === undefined ) {
  42. snippet = this.generate( builder ) || '';
  43. nodeData.snippet = snippet;
  44. }
  45. snippet = builder.format( snippet, type, output );
  46. } else {
  47. snippet = this.generate( builder, output ) || '';
  48. }
  49. builder.removeStack( this );
  50. return snippet;
  51. }
  52. }
  53. Node.prototype.isNode = true;
  54. export default Node;