UniformNode.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import InputNode from './InputNode.js';
  2. import { objectGroup } from './UniformGroupNode.js';
  3. import { addNodeClass } from './Node.js';
  4. import { nodeObject, getConstNodeType } from '../shadernode/ShaderNode.js';
  5. class UniformNode extends InputNode {
  6. constructor( value, nodeType = null ) {
  7. super( value, nodeType );
  8. this.isUniformNode = true;
  9. this.name = '';
  10. this.groupNode = objectGroup;
  11. }
  12. label( name ) {
  13. this.name = name;
  14. return this;
  15. }
  16. setGroup( group ) {
  17. this.groupNode = group;
  18. return this;
  19. }
  20. getGroup() {
  21. return this.groupNode;
  22. }
  23. getUniformHash( builder ) {
  24. return this.getHash( builder );
  25. }
  26. onUpdate( callback, updateType ) {
  27. const self = this.getSelf();
  28. callback = callback.bind( self );
  29. return super.onUpdate( ( frame ) => {
  30. const value = callback( frame, self );
  31. if ( value !== undefined ) {
  32. this.value = value;
  33. }
  34. }, updateType );
  35. }
  36. generate( builder, output ) {
  37. const type = this.getNodeType( builder );
  38. const hash = this.getUniformHash( builder );
  39. let sharedNode = builder.getNodeFromHash( hash );
  40. if ( sharedNode === undefined ) {
  41. builder.setHashNode( this, hash );
  42. sharedNode = this;
  43. }
  44. const sharedNodeType = sharedNode.getInputType( builder );
  45. const nodeUniform = builder.getUniformFromNode( sharedNode, sharedNodeType, builder.shaderStage, this.name || builder.context.label );
  46. const propertyName = builder.getPropertyName( nodeUniform );
  47. if ( builder.context.label !== undefined ) delete builder.context.label;
  48. return builder.format( propertyName, type, output );
  49. }
  50. }
  51. export default UniformNode;
  52. export const uniform = ( arg1, arg2 ) => {
  53. const nodeType = getConstNodeType( arg2 || arg1 );
  54. // @TODO: get ConstNode from .traverse() in the future
  55. const value = ( arg1 && arg1.isNode === true ) ? ( arg1.node && arg1.node.value ) || arg1.value : arg1;
  56. return nodeObject( new UniformNode( value, nodeType ) );
  57. };
  58. addNodeClass( 'UniformNode', UniformNode );