Skip to content

Node: Document more modules. #30055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/nodes/core/NodeBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,7 @@ class NodeBuilder {
*
* @param {Node} node - Node that the flow was started.
* @param {('vertex'|'fragment'|'compute'|'any')} shaderStage - The shader stage.
* @return {Object}
* @return {Object} The flow data.
*/
getFlowData( node/*, shaderStage*/ ) {

Expand All @@ -1831,7 +1831,7 @@ class NodeBuilder {
* Executes the node flow based on a root node to generate the final shader code.
*
* @param {Node} node - The node to execute.
* @return {Object}
* @return {Object} The code flow.
*/
flowNode( node ) {

Expand Down Expand Up @@ -1976,7 +1976,7 @@ class NodeBuilder {
*
* @param {Node} node - The node to execute.
* @param {String?} output - Expected output type. For example 'vec3'.
* @return {Object}
* @return {Object} The code flow.
*/
flowChildNode( node, output = null ) {

Expand Down
40 changes: 38 additions & 2 deletions src/nodes/utils/ArrayElementNode.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,60 @@
import Node from '../core/Node.js';

class ArrayElementNode extends Node {
/**
* Base class for representing element access on an array-like
* node data structures.
*
* @augments Node
*/
class ArrayElementNode extends Node { // @TODO: If extending from TempNode it breaks webgpu_compute

static get type() {

return 'ArrayElementNode';

} // @TODO: If extending from TempNode it breaks webgpu_compute
}

/**
* Constructs array element node.
*
* @param {Node} node - The array-like node.
* @param {Node} indexNode - The index node that defines the element access.
*/
constructor( node, indexNode ) {

super();

/**
* The array-like node.
*
* @type {Node}
*/
this.node = node;

/**
* The index node that defines the element access.
*
* @type {Node}
*/
this.indexNode = indexNode;

/**
* This flag can be used for type testing.
*
* @type {Boolean}
* @readonly
* @default true
*/
this.isArrayElementNode = true;

}

/**
* This method is overwritten since the node type is inferred from the array-like node.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {String} The node type.
*/
getNodeType( builder ) {

return this.node.getElementType( builder );
Expand Down
16 changes: 16 additions & 0 deletions src/nodes/utils/Discard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,23 @@ import { select } from '../math/ConditionalNode.js';
import { expression } from '../code/ExpressionNode.js';
import { addMethodChaining } from '../tsl/TSLCore.js';

/** @module Discard **/

/**
* Represents a `discard` shader operation in TSL.
*
* @method
* @param {ConditionalNode?} conditional - An optional conditional node. It allows to decide whether the discard should be executed or not.
* @return {Node} The `discard` expression.
*/
export const Discard = ( conditional ) => ( conditional ? select( conditional, expression( 'discard' ) ) : expression( 'discard' ) ).append();

/**
* Represents a `return` shader operation in TSL.
*
* @method
* @return {ExpressionNode} The `return` expression.
*/
export const Return = () => expression( 'return' ).append();

addMethodChaining( 'discard', Discard );
38 changes: 38 additions & 0 deletions src/nodes/utils/FlipNode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import TempNode from '../core/TempNode.js';
import { vectorComponents } from '../core/constants.js';

/**
* This module is part of the TSL core and usually not used in app level code.
* It represents a flip operation during the shader generation process
* meaning it flips normalized values with the following formula:
* ```
* x = 1 - x;
* ```
* `FlipNode` is internally used to implement any `flipXYZW()`, `flipRGBA()` and
* `flipSTPQ()` method invocations on node objects. For example:
* ```js
* uvNode = uvNode.flipY();
* ```
*
* @augments TempNode
*/
class FlipNode extends TempNode {

static get type() {
Expand All @@ -9,15 +24,38 @@ class FlipNode extends TempNode {

}

/**
* Constructs a new flip node.
*
* @param {Node} sourceNode - The node which component(s) should be flipped.
* @param {String} components - The components that should be flipped e.g. `'x'` or `'xy'`.
*/
constructor( sourceNode, components ) {

super();

/**
* The node which component(s) should be flipped.
*
* @type {Node}
*/
this.sourceNode = sourceNode;

/**
* The components that should be flipped e.g. `'x'` or `'xy'`.
*
* @type {String}
*/
this.components = components;

}

/**
* This method is overwritten since the node type is inferred from the source node.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {String} The node type.
*/
getNodeType( builder ) {

return this.sourceNode.getNodeType( builder );
Expand Down
50 changes: 50 additions & 0 deletions src/nodes/utils/LoopNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ import Node from '../core/Node.js';
import { expression } from '../code/ExpressionNode.js';
import { nodeObject, nodeArray } from '../tsl/TSLBase.js';

/**
* This module offers a variety of ways to implement loops in TSL. In it's basic form it's:
* ```js
* Loop( count, ( { i } ) => {
*
* } );
* ```
* However, it is also possible to define a start and end ranges, data types and loop conditions:
* ```js
* Loop( { start: int( 0 ), end: int( 10 ), type: 'int', condition: '<' }, ( { i } ) => {
*
* } );
*```
* Nested loops can be definde in a compacted form:
* ```js
* Loop( 10, 5, ( { i, j } ) => {
*
* } );
* ```
* Loops that should run backwards can be defined like so:
* ```js
* Loop( { start: 10 }, () => {} );
* ```
* The module also provides `Break()` and `Continue()` TSL expression for loop control.
* @augments Node
*/
class LoopNode extends Node {

static get type() {
Expand All @@ -10,6 +36,11 @@ class LoopNode extends Node {

}

/**
* Constructs a new loop node.
*
* @param {Array<Any>} params - Depending on the loop type, array holds different parameterization values for the loop.
*/
constructor( params = [] ) {

super();
Expand All @@ -18,12 +49,25 @@ class LoopNode extends Node {

}

/**
* Returns a loop variable name based on an index. The pattern is
* `0` = `i`, `1`= `j`, `2`= `k` and so on.
*
* @param {Number} index - The index.
* @return {String} The loop variable name.
*/
getVarName( index ) {

return String.fromCharCode( 'i'.charCodeAt() + index );

}

/**
* Returns properties about this node.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {Object} The node properties.
*/
getProperties( builder ) {

const properties = builder.getNodeProperties( this );
Expand Down Expand Up @@ -56,6 +100,12 @@ class LoopNode extends Node {

}

/**
* This method is overwritten since the node type is inferred based on the loop configuration.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {String} The node type.
*/
getNodeType( builder ) {

const { returnsNode } = this.getProperties( builder );
Expand Down
9 changes: 9 additions & 0 deletions src/nodes/utils/MatcapUVNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ import { transformedNormalView } from '../accessors/Normal.js';
import { positionViewDirection } from '../accessors/Position.js';
import { nodeImmutable, vec2, vec3 } from '../tsl/TSLBase.js';

/**
* Can be used to compute texture coordinates for projecting a
* matcap onto a mesh. Used by {@link MeshMatcapNodeMaterial}.
*
* @augments TempNode
*/
class MatcapUVNode extends TempNode {

static get type() {
Expand All @@ -11,6 +17,9 @@ class MatcapUVNode extends TempNode {

}

/**
* Constructs a new matcap uv node.
*/
constructor() {

super( 'vec2' );
Expand Down
40 changes: 40 additions & 0 deletions src/nodes/utils/MaxMipLevelNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import UniformNode from '../core/UniformNode.js';
import { NodeUpdateType } from '../core/constants.js';
import { nodeProxy } from '../tsl/TSLBase.js';

/**
* A special type of uniform node that computes the
* maximum mipmap level for a given texture node.
*
* ```js
* const level = maxMipLevel( textureNode );
* ```
*
* @augments UniformNode
*/
class MaxMipLevelNode extends UniformNode {

static get type() {
Expand All @@ -10,22 +20,52 @@ class MaxMipLevelNode extends UniformNode {

}

/**
* Constructs a new max mip level node.
*
* @param {TextureNode} node - The texture node to compute the max mip level for.
*/
constructor( textureNode ) {

super( 0 );

/**
* The texture node to compute the max mip level for.
*
* @private
* @type {TextureNode}
*/
this._textureNode = textureNode;

/**
* The `updateType` is set to `NodeUpdateType.FRAME` since the node updates
* the texture once per frame in its {@link MaxMipLevelNode#update} method.
*
* @type {String}
* @default 'frame'
*/
this.updateType = NodeUpdateType.FRAME;

}

/**
* The texture node to compute the max mip level for.
*
* @readonly
* @type {TextureNode}
*/
get textureNode() {

return this._textureNode;

}

/**
* The texture.
*
* @readonly
* @type {Texture}
*/
get texture() {

return this._textureNode.value;
Expand Down
Loading
Loading