|
|
@@ -8,6 +8,15 @@ import { nodeObject } from '../tsl/TSLBase.js';
|
|
|
import { uniformArray } from './UniformArrayNode.js';
|
|
|
import ArrayElementNode from '../utils/ArrayElementNode.js';
|
|
|
|
|
|
+/** @module ReferenceNode **/
|
|
|
+
|
|
|
+/**
|
|
|
+ * This class is only relevant if the referenced property is array-like.
|
|
|
+ * In this case, `ReferenceElementNode` allows to refer to a specific
|
|
|
+ * element inside the data structure via an index.
|
|
|
+ *
|
|
|
+ * @augments ArrayElementNode
|
|
|
+ */
|
|
|
class ReferenceElementNode extends ArrayElementNode {
|
|
|
|
|
|
static get type() {
|
|
|
@@ -16,16 +25,43 @@ class ReferenceElementNode extends ArrayElementNode {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new reference element node.
|
|
|
+ *
|
|
|
+ * @param {Node} referenceNode - The reference node.
|
|
|
+ * @param {Node} indexNode - The index node that defines the element access.
|
|
|
+ */
|
|
|
constructor( referenceNode, indexNode ) {
|
|
|
|
|
|
super( referenceNode, indexNode );
|
|
|
|
|
|
+ /**
|
|
|
+ * Similar to {@link ReferenceNode#reference}, an additional
|
|
|
+ * property references to the current node.
|
|
|
+ *
|
|
|
+ * @type {Node}
|
|
|
+ * @default null
|
|
|
+ */
|
|
|
this.referenceNode = referenceNode;
|
|
|
|
|
|
+ /**
|
|
|
+ * This flag can be used for type testing.
|
|
|
+ *
|
|
|
+ * @type {Boolean}
|
|
|
+ * @readonly
|
|
|
+ * @default true
|
|
|
+ */
|
|
|
this.isReferenceElementNode = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This method is overwritten since the node type is inferred from
|
|
|
+ * the uniform type of the reference node.
|
|
|
+ *
|
|
|
+ * @param {NodeBuilder} builder - The current node builder.
|
|
|
+ * @return {String} The node type.
|
|
|
+ */
|
|
|
getNodeType() {
|
|
|
|
|
|
return this.referenceNode.uniformType;
|
|
|
@@ -44,7 +80,14 @@ class ReferenceElementNode extends ArrayElementNode {
|
|
|
|
|
|
}
|
|
|
|
|
|
-// TODO: Extends this from ReferenceBaseNode
|
|
|
+/**
|
|
|
+ * This type of node establishes a reference to a property of another object.
|
|
|
+ * In this way, the value of the node is automatically linked to the value of
|
|
|
+ * referenced object. Reference nodes internally represents the linked value
|
|
|
+ * as a uniform.
|
|
|
+ *
|
|
|
+ * @augments Node
|
|
|
+ */
|
|
|
class ReferenceNode extends Node {
|
|
|
|
|
|
static get type() {
|
|
|
@@ -53,31 +96,118 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Constructs a new reference node.
|
|
|
+ *
|
|
|
+ * @param {String} property - The name of the property the node refers to.
|
|
|
+ * @param {String} uniformType - The uniform type that should be used to represent the property value.
|
|
|
+ * @param {Object?} [object=null] - The object the property belongs to.
|
|
|
+ * @param {Number?} [count=null] - When the linked property is an array-like, this parameter defines its length.
|
|
|
+ */
|
|
|
constructor( property, uniformType, object = null, count = null ) {
|
|
|
|
|
|
super();
|
|
|
|
|
|
+ /**
|
|
|
+ * The name of the property the node refers to.
|
|
|
+ *
|
|
|
+ * @type {String}
|
|
|
+ */
|
|
|
this.property = property;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The uniform type that should be used to represent the property value.
|
|
|
+ *
|
|
|
+ * @type {String}
|
|
|
+ */
|
|
|
this.uniformType = uniformType;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The object the property belongs to.
|
|
|
+ *
|
|
|
+ * @type {Object?}
|
|
|
+ * @default null
|
|
|
+ */
|
|
|
this.object = object;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * When the linked property is an array, this parameter defines its length.
|
|
|
+ *
|
|
|
+ * @type {Number?}
|
|
|
+ * @default null
|
|
|
+ */
|
|
|
this.count = count;
|
|
|
|
|
|
+ /**
|
|
|
+ * The property name might have dots so nested properties can be referred.
|
|
|
+ * The hierarchy of the names is stored inside this array.
|
|
|
+ *
|
|
|
+ * @type {Array<String>}
|
|
|
+ */
|
|
|
this.properties = property.split( '.' );
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Points to the current referred object. This property exists next to {@link ReferenceNode#object}
|
|
|
+ * since the final reference might be updated from calling code.
|
|
|
+ *
|
|
|
+ * @type {Object?}
|
|
|
+ * @default null
|
|
|
+ */
|
|
|
this.reference = object;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The uniform node that holds the value of the reference node.
|
|
|
+ *
|
|
|
+ * @type {UniformNode}
|
|
|
+ * @default null
|
|
|
+ */
|
|
|
this.node = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * The uniform group of the internal uniform.
|
|
|
+ *
|
|
|
+ * @type {UniformGroupNode}
|
|
|
+ * @default null
|
|
|
+ */
|
|
|
this.group = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * An optinal label of the internal uniform node.
|
|
|
+ *
|
|
|
+ * @type {String?}
|
|
|
+ * @default null
|
|
|
+ */
|
|
|
this.name = null;
|
|
|
|
|
|
+ /**
|
|
|
+ * Overwritten since velocity nodes are updated per object.
|
|
|
+ *
|
|
|
+ * @type {String}
|
|
|
+ * @default 'object'
|
|
|
+ */
|
|
|
this.updateType = NodeUpdateType.OBJECT;
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * When the referred property is array-like, this method can be used
|
|
|
+ * to access elements via an index node.
|
|
|
+ *
|
|
|
+ * @param {IndexNode} indexNode - indexNode.
|
|
|
+ * @return {ReferenceElementNode} A reference to an element.
|
|
|
+ */
|
|
|
element( indexNode ) {
|
|
|
|
|
|
return nodeObject( new ReferenceElementNode( this, nodeObject( indexNode ) ) );
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the uniform group for this reference node.
|
|
|
+ *
|
|
|
+ * @param {UniformGroupNode} group - The uniform group to set.
|
|
|
+ * @return {ReferenceNode} A reference to this node.
|
|
|
+ */
|
|
|
setGroup( group ) {
|
|
|
|
|
|
this.group = group;
|
|
|
@@ -86,6 +216,12 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the label for the internal uniform.
|
|
|
+ *
|
|
|
+ * @param {String} name - The label to set.
|
|
|
+ * @return {ReferenceNode} A reference to this node.
|
|
|
+ */
|
|
|
label( name ) {
|
|
|
|
|
|
this.name = name;
|
|
|
@@ -94,6 +230,12 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Sets the node type which automatically defines the internal
|
|
|
+ * uniform type.
|
|
|
+ *
|
|
|
+ * @param {String} uniformType - The type to set.
|
|
|
+ */
|
|
|
setNodeType( uniformType ) {
|
|
|
|
|
|
let node = null;
|
|
|
@@ -132,6 +274,13 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * This method is overwritten since the node type is inferred from
|
|
|
+ * the type of the reference node.
|
|
|
+ *
|
|
|
+ * @param {NodeBuilder} builder - The current node builder.
|
|
|
+ * @return {String} The node type.
|
|
|
+ */
|
|
|
getNodeType( builder ) {
|
|
|
|
|
|
if ( this.node === null ) {
|
|
|
@@ -145,6 +294,12 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns the property value from the given referred object.
|
|
|
+ *
|
|
|
+ * @param {Object} [object=this.reference] - The object to retrieve the property value from.
|
|
|
+ * @return {Any} The value.
|
|
|
+ */
|
|
|
getValueFromReference( object = this.reference ) {
|
|
|
|
|
|
const { properties } = this;
|
|
|
@@ -161,6 +316,13 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Allows to update the reference based on the given state. The state is only
|
|
|
+ * evaluated {@link ReferenceNode#object} is not set.
|
|
|
+ *
|
|
|
+ * @param {(NodeFrame|NodeBuilder)} state - The current state.
|
|
|
+ * @return {Object} The updated reference.
|
|
|
+ */
|
|
|
updateReference( state ) {
|
|
|
|
|
|
this.reference = this.object !== null ? this.object : state.object;
|
|
|
@@ -169,7 +331,13 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
- setup() {
|
|
|
+ /**
|
|
|
+ * The output of the reference node is the internal uniform node.
|
|
|
+ *
|
|
|
+ * @param {NodeBuilder} builder - The current node builder.
|
|
|
+ * @return {UniformNode} The output node.
|
|
|
+ */
|
|
|
+ setup( /* builder */ ) {
|
|
|
|
|
|
this.updateValue();
|
|
|
|
|
|
@@ -177,12 +345,21 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Overwritten to to update the internal uniform value.
|
|
|
+ *
|
|
|
+ * @param {NodeFrame} frame - A reference to the current node frame.
|
|
|
+ */
|
|
|
update( /*frame*/ ) {
|
|
|
|
|
|
this.updateValue();
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Retrieves the value from the referred object property and uses it
|
|
|
+ * to updated the internal uniform.
|
|
|
+ */
|
|
|
updateValue() {
|
|
|
|
|
|
if ( this.node === null ) this.setNodeType( this.uniformType );
|
|
|
@@ -205,5 +382,25 @@ class ReferenceNode extends Node {
|
|
|
|
|
|
export default ReferenceNode;
|
|
|
|
|
|
+/**
|
|
|
+ * TSL function for creating a reference node with the given paramters.
|
|
|
+ *
|
|
|
+ * @function
|
|
|
+ * @param {String} name - The name of the property the node refers to.
|
|
|
+ * @param {String} type - The uniform type that should be used to represent the property value.
|
|
|
+ * @param {Object} object - The object the property belongs to.
|
|
|
+ * @returns {ReferenceNode}
|
|
|
+ */
|
|
|
export const reference = ( name, type, object ) => nodeObject( new ReferenceNode( name, type, object ) );
|
|
|
+
|
|
|
+/**
|
|
|
+ * TSL function for creating a reference node with the given paramters.
|
|
|
+ *
|
|
|
+ * @function
|
|
|
+ * @param {String} name - The name of the property the node refers to.
|
|
|
+ * @param {String} type - The uniform type that should be used to represent the property value.
|
|
|
+ * @param {Number} count - The number of value inside the array-like object.
|
|
|
+ * @param {Object} object - An array-like object the property belongs to.
|
|
|
+ * @returns {ReferenceNode}
|
|
|
+ */
|
|
|
export const referenceBuffer = ( name, type, count, object ) => nodeObject( new ReferenceNode( name, type, object, count ) );
|