Node.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. getTypeLength( builder ) {
  22. return builder.getTypeLength( this.getNodeType( builder ) );
  23. }
  24. update( /*frame*/ ) {
  25. console.warn( 'Abstract function.' );
  26. }
  27. generate( /*builder, output*/ ) {
  28. console.warn( 'Abstract function.' );
  29. }
  30. build( builder, output = null ) {
  31. const hash = this.getHash( builder );
  32. const sharedNode = builder.getNodeFromHash( hash );
  33. if ( sharedNode !== undefined && this !== sharedNode ) {
  34. return sharedNode.build( builder, output );
  35. }
  36. builder.addNode( this );
  37. builder.addStack( this );
  38. const isGenerateOnce = this.generate.length === 1;
  39. let snippet = null;
  40. if ( isGenerateOnce ) {
  41. const type = this.getNodeType( builder );
  42. const nodeData = builder.getDataFromNode( this );
  43. snippet = nodeData.snippet;
  44. if ( snippet === undefined ) {
  45. snippet = this.generate( builder );
  46. nodeData.snippet = snippet;
  47. }
  48. snippet = builder.format( snippet, type, output );
  49. } else {
  50. snippet = this.generate( builder, output );
  51. }
  52. builder.removeStack( this );
  53. return snippet;
  54. }
  55. }
  56. Node.prototype.isNode = true;
  57. export default Node;