ScriptableValueNode.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import Node from '../core/Node.js';
  2. import { arrayBufferToBase64, base64ToArrayBuffer } from '../core/NodeUtils.js';
  3. import { nodeProxy, float } from '../tsl/TSLBase.js';
  4. import { EventDispatcher } from '../../core/EventDispatcher.js';
  5. /**
  6. * `ScriptableNode` uses this class to manage script inputs and outputs.
  7. *
  8. * @augments Node
  9. */
  10. class ScriptableValueNode extends Node {
  11. static get type() {
  12. return 'ScriptableValueNode';
  13. }
  14. /**
  15. * Constructs a new scriptable node.
  16. *
  17. * @param {any} [value=null] - The value.
  18. */
  19. constructor( value = null ) {
  20. super();
  21. /**
  22. * A reference to the value.
  23. *
  24. * @private
  25. * @default null
  26. */
  27. this._value = value;
  28. /**
  29. * Depending on the type of `_value`, this property might cache parsed data.
  30. *
  31. * @private
  32. * @default null
  33. */
  34. this._cache = null;
  35. /**
  36. * If this node represents an input, this property represents the input type.
  37. *
  38. * @type {?string}
  39. * @default null
  40. */
  41. this.inputType = null;
  42. /**
  43. * If this node represents an output, this property represents the output type.
  44. *
  45. * @type {?string}
  46. * @default null
  47. */
  48. this.outputType = null;
  49. /**
  50. * An event dispatcher for managing events.
  51. *
  52. * @type {EventDispatcher}
  53. */
  54. this.events = new EventDispatcher();
  55. /**
  56. * This flag can be used for type testing.
  57. *
  58. * @type {boolean}
  59. * @readonly
  60. * @default true
  61. */
  62. this.isScriptableValueNode = true;
  63. }
  64. /**
  65. * Whether this node represents an output or not.
  66. *
  67. * @type {boolean}
  68. * @readonly
  69. * @default true
  70. */
  71. get isScriptableOutputNode() {
  72. return this.outputType !== null;
  73. }
  74. set value( val ) {
  75. if ( this._value === val ) return;
  76. if ( this._cache && this.inputType === 'URL' && this.value.value instanceof ArrayBuffer ) {
  77. URL.revokeObjectURL( this._cache );
  78. this._cache = null;
  79. }
  80. this._value = val;
  81. this.events.dispatchEvent( { type: 'change' } );
  82. this.refresh();
  83. }
  84. /**
  85. * The node's value.
  86. *
  87. * @type {any}
  88. */
  89. get value() {
  90. return this._value;
  91. }
  92. /**
  93. * Dispatches the `refresh` event.
  94. */
  95. refresh() {
  96. this.events.dispatchEvent( { type: 'refresh' } );
  97. }
  98. /**
  99. * The `value` property usually represents a node or even binary data in form of array buffers.
  100. * In this case, this method tries to return the actual value behind the complex type.
  101. *
  102. * @return {any} The value.
  103. */
  104. getValue() {
  105. const value = this.value;
  106. if ( value && this._cache === null && this.inputType === 'URL' && value.value instanceof ArrayBuffer ) {
  107. this._cache = URL.createObjectURL( new Blob( [ value.value ] ) );
  108. } else if ( value && value.value !== null && value.value !== undefined && (
  109. ( ( this.inputType === 'URL' || this.inputType === 'String' ) && typeof value.value === 'string' ) ||
  110. ( this.inputType === 'Number' && typeof value.value === 'number' ) ||
  111. ( this.inputType === 'Vector2' && value.value.isVector2 ) ||
  112. ( this.inputType === 'Vector3' && value.value.isVector3 ) ||
  113. ( this.inputType === 'Vector4' && value.value.isVector4 ) ||
  114. ( this.inputType === 'Color' && value.value.isColor ) ||
  115. ( this.inputType === 'Matrix3' && value.value.isMatrix3 ) ||
  116. ( this.inputType === 'Matrix4' && value.value.isMatrix4 )
  117. ) ) {
  118. return value.value;
  119. }
  120. return this._cache || value;
  121. }
  122. /**
  123. * Overwritten since the node type is inferred from the value.
  124. *
  125. * @param {NodeBuilder} builder - The current node builder.
  126. * @return {string} The node type.
  127. */
  128. getNodeType( builder ) {
  129. return this.value && this.value.isNode ? this.value.getNodeType( builder ) : 'float';
  130. }
  131. setup() {
  132. return this.value && this.value.isNode ? this.value : float();
  133. }
  134. serialize( data ) {
  135. super.serialize( data );
  136. if ( this.value !== null ) {
  137. if ( this.inputType === 'ArrayBuffer' ) {
  138. data.value = arrayBufferToBase64( this.value );
  139. } else {
  140. data.value = this.value ? this.value.toJSON( data.meta ).uuid : null;
  141. }
  142. } else {
  143. data.value = null;
  144. }
  145. data.inputType = this.inputType;
  146. data.outputType = this.outputType;
  147. }
  148. deserialize( data ) {
  149. super.deserialize( data );
  150. let value = null;
  151. if ( data.value !== null ) {
  152. if ( data.inputType === 'ArrayBuffer' ) {
  153. value = base64ToArrayBuffer( data.value );
  154. } else if ( data.inputType === 'Texture' ) {
  155. value = data.meta.textures[ data.value ];
  156. } else {
  157. value = data.meta.nodes[ data.value ] || null;
  158. }
  159. }
  160. this.value = value;
  161. this.inputType = data.inputType;
  162. this.outputType = data.outputType;
  163. }
  164. }
  165. export default ScriptableValueNode;
  166. /**
  167. * TSL function for creating a scriptable value node.
  168. *
  169. * @tsl
  170. * @function
  171. * @param {any} [value] - The value.
  172. * @returns {ScriptableValueNode}
  173. */
  174. export const scriptableValue = /*@__PURE__*/ nodeProxy( ScriptableValueNode ).setParameterLength( 1 );
粤ICP备19079148号