InputNode.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import Node from './Node.js';
  2. import { getValueType, getValueFromType, arrayBufferToBase64 } from './NodeUtils.js';
  3. import { warn } from '../../utils.js';
  4. /**
  5. * Base class for representing data input nodes.
  6. *
  7. * @augments Node
  8. */
  9. class InputNode extends Node {
  10. static get type() {
  11. return 'InputNode';
  12. }
  13. /**
  14. * Constructs a new input node.
  15. *
  16. * @param {any} value - The value of this node. This can be any JS primitive, functions, array buffers or even three.js objects (vector, matrices, colors).
  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( nodeType );
  21. /**
  22. * This flag can be used for type testing.
  23. *
  24. * @type {boolean}
  25. * @readonly
  26. * @default true
  27. */
  28. this.isInputNode = true;
  29. /**
  30. * The value of this node. This can be any JS primitive, functions, array buffers or even three.js objects (vector, matrices, colors).
  31. *
  32. * @type {any}
  33. */
  34. this.value = value;
  35. /**
  36. * The precision of the value in the shader.
  37. *
  38. * @type {?('low'|'medium'|'high')}
  39. * @default null
  40. */
  41. this.precision = null;
  42. }
  43. getNodeType( /*builder*/ ) {
  44. if ( this.nodeType === null ) {
  45. return getValueType( this.value );
  46. }
  47. return this.nodeType;
  48. }
  49. /**
  50. * Returns the input type of the node which is by default the node type. Derived modules
  51. * might overwrite this method and use a fixed type or compute one analytically.
  52. *
  53. * A typical example for different input and node types are textures. The input type of a
  54. * normal RGBA texture is `texture` whereas its node type is `vec4`.
  55. *
  56. * @param {NodeBuilder} builder - The current node builder.
  57. * @return {string} The input type.
  58. */
  59. getInputType( builder ) {
  60. return this.getNodeType( builder );
  61. }
  62. /**
  63. * Sets the precision to the given value. The method can be
  64. * overwritten in derived classes if the final precision must be computed
  65. * analytically.
  66. *
  67. * @param {('low'|'medium'|'high')} precision - The precision of the input value in the shader.
  68. * @return {InputNode} A reference to this node.
  69. */
  70. setPrecision( precision ) {
  71. this.precision = precision;
  72. return this;
  73. }
  74. serialize( data ) {
  75. super.serialize( data );
  76. data.value = this.value;
  77. if ( this.value && this.value.toArray ) data.value = this.value.toArray();
  78. data.valueType = getValueType( this.value );
  79. data.nodeType = this.nodeType;
  80. if ( data.valueType === 'ArrayBuffer' ) data.value = arrayBufferToBase64( data.value );
  81. data.precision = this.precision;
  82. }
  83. deserialize( data ) {
  84. super.deserialize( data );
  85. this.nodeType = data.nodeType;
  86. this.value = Array.isArray( data.value ) ? getValueFromType( data.valueType, ...data.value ) : data.value;
  87. this.precision = data.precision || null;
  88. if ( this.value && this.value.fromArray ) this.value = this.value.fromArray( data.value );
  89. }
  90. generate( /*builder, output*/ ) {
  91. warn( 'Abstract function.' );
  92. }
  93. }
  94. export default InputNode;
粤ICP备19079148号