ReferenceNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { NodeUpdateType } from '../core/constants.js';
  3. import { uniform } from '../core/UniformNode.js';
  4. import { texture } from './TextureNode.js';
  5. import { nodeObject } from '../shadernode/ShaderNode.js';
  6. class ReferenceNode extends Node {
  7. constructor( property, uniformType, object = null ) {
  8. super();
  9. this.property = property;
  10. this.index = null;
  11. this.uniformType = uniformType;
  12. this.object = object;
  13. this.reference = null;
  14. this.node = null;
  15. this.updateType = NodeUpdateType.OBJECT;
  16. this.setNodeType( uniformType );
  17. }
  18. updateReference( frame ) {
  19. this.reference = this.object !== null ? this.object : frame.object;
  20. return this.reference;
  21. }
  22. setIndex( index ) {
  23. this.index = index;
  24. return this;
  25. }
  26. getIndex() {
  27. return this.index;
  28. }
  29. setNodeType( uniformType ) {
  30. let node = null;
  31. if ( uniformType === 'texture' ) {
  32. node = texture( null );
  33. } else {
  34. node = uniform( uniformType );
  35. }
  36. this.node = node;
  37. }
  38. getNodeType( builder ) {
  39. return this.node.getNodeType( builder );
  40. }
  41. update( /*frame*/ ) {
  42. let value = this.reference[ this.property ];
  43. if ( this.index !== null ) {
  44. value = value[ this.index ];
  45. }
  46. this.node.value = value;
  47. }
  48. setup( /*builder*/ ) {
  49. return this.node;
  50. }
  51. }
  52. export default ReferenceNode;
  53. export const reference = ( name, type, object ) => nodeObject( new ReferenceNode( name, type, object ) );
  54. export const referenceIndex = ( name, index, type, object ) => nodeObject( new ReferenceNode( name, type, object ).setIndex( index ) );
  55. addNodeClass( 'ReferenceNode', ReferenceNode );