UniformNode.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import InputNode from './InputNode.js';
  2. import { objectGroup } from './UniformGroupNode.js';
  3. import { nodeObject, getConstNodeType } from '../tsl/TSLCore.js';
  4. /**
  5. * Class for representing a uniform.
  6. *
  7. * @augments InputNode
  8. */
  9. class UniformNode extends InputNode {
  10. static get type() {
  11. return 'UniformNode';
  12. }
  13. /**
  14. * Constructs a new uniform node.
  15. *
  16. * @param {any} value - The value of this node. Usually a JS primitive or three.js object (vector, matrix, color, texture).
  17. * @param {?string} nodeType - The node type. If no explicit type is defined, the node tries to derive the type from its value.
  18. */
  19. constructor( value, nodeType = null ) {
  20. super( value, nodeType );
  21. /**
  22. * This flag can be used for type testing.
  23. *
  24. * @type {boolean}
  25. * @readonly
  26. * @default true
  27. */
  28. this.isUniformNode = true;
  29. /**
  30. * The name or label of the uniform.
  31. *
  32. * @type {string}
  33. * @default ''
  34. */
  35. this.name = '';
  36. /**
  37. * The uniform group of this uniform. By default, uniforms are
  38. * managed per object but they might belong to a shared group
  39. * which is updated per frame or render call.
  40. *
  41. * @type {UniformGroupNode}
  42. */
  43. this.groupNode = objectGroup;
  44. }
  45. /**
  46. * Sets the {@link UniformNode#name} property.
  47. *
  48. * @param {string} name - The name of the uniform.
  49. * @return {UniformNode} A reference to this node.
  50. */
  51. label( name ) {
  52. this.name = name;
  53. return this;
  54. }
  55. /**
  56. * Sets the {@link UniformNode#groupNode} property.
  57. *
  58. * @param {UniformGroupNode} group - The uniform group.
  59. * @return {UniformNode} A reference to this node.
  60. */
  61. setGroup( group ) {
  62. this.groupNode = group;
  63. return this;
  64. }
  65. /**
  66. * Returns the {@link UniformNode#groupNode}.
  67. *
  68. * @return {UniformGroupNode} The uniform group.
  69. */
  70. getGroup() {
  71. return this.groupNode;
  72. }
  73. /**
  74. * By default, this method returns the result of {@link Node#getHash} but derived
  75. * classes might overwrite this method with a different implementation.
  76. *
  77. * @param {NodeBuilder} builder - The current node builder.
  78. * @return {string} The uniform hash.
  79. */
  80. getUniformHash( builder ) {
  81. return this.getHash( builder );
  82. }
  83. onUpdate( callback, updateType ) {
  84. const self = this.getSelf();
  85. callback = callback.bind( self );
  86. return super.onUpdate( ( frame ) => {
  87. const value = callback( frame, self );
  88. if ( value !== undefined ) {
  89. this.value = value;
  90. }
  91. }, updateType );
  92. }
  93. generate( builder, output ) {
  94. const type = this.getNodeType( builder );
  95. const hash = this.getUniformHash( builder );
  96. let sharedNode = builder.getNodeFromHash( hash );
  97. if ( sharedNode === undefined ) {
  98. builder.setHashNode( this, hash );
  99. sharedNode = this;
  100. }
  101. const sharedNodeType = sharedNode.getInputType( builder );
  102. const nodeUniform = builder.getUniformFromNode( sharedNode, sharedNodeType, builder.shaderStage, this.name || builder.context.label );
  103. const propertyName = builder.getPropertyName( nodeUniform );
  104. if ( builder.context.label !== undefined ) delete builder.context.label;
  105. return builder.format( propertyName, type, output );
  106. }
  107. }
  108. export default UniformNode;
  109. /**
  110. * TSL function for creating a uniform node.
  111. *
  112. * @tsl
  113. * @function
  114. * @param {any} arg1 - The value of this node. Usually a JS primitive or three.js object (vector, matrix, color, texture).
  115. * @param {?string} arg2 - The node type. If no explicit type is defined, the node tries to derive the type from its value.
  116. * @returns {UniformNode}
  117. */
  118. export const uniform = ( arg1, arg2 ) => {
  119. const nodeType = getConstNodeType( arg2 || arg1 );
  120. // @TODO: get ConstNode from .traverse() in the future
  121. const value = ( arg1 && arg1.isNode === true ) ? ( arg1.node && arg1.node.value ) || arg1.value : arg1;
  122. return nodeObject( new UniformNode( value, nodeType ) );
  123. };
粤ICP备19079148号