Skip to content

Node: Add more docs. #30046

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 1 commit into from
Dec 6, 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
9 changes: 9 additions & 0 deletions src/nodes/procedural/Checker.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { uv } from '../accessors/UV.js';
import { Fn } from '../tsl/TSLBase.js';

/** @module Procedural **/

/**
* Creates a 2x2 checkerboard pattern that can be used as procedural texture data.
*
* @method
* @param {Node<vec2>} uv - The uv coordinates.
* @return {Node<float>} The result data.
*/
export const checker = /*@__PURE__*/ Fn( ( [ coord = uv() ] ) => {

const uv = coord.mul( 2.0 );
Expand Down
31 changes: 31 additions & 0 deletions src/nodes/utils/ConvertNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import Node from '../core/Node.js';

/**
* This module is part of the TSL core and usually not used in app level code.
* It represents a convert operation during the shader generation process
* meaning it converts the data type of a node to a target data type.
*
* @augments Node
*/
class ConvertNode extends Node {

static get type() {
Expand All @@ -8,15 +15,39 @@ class ConvertNode extends Node {

}

/**
* Constructs a new convert node.
*
* @param {Node} node - The node which type should be converted.
* @param {String} convertTo - The target node type. Multiple types can be defined by separating them with a `|` sign.
*/
constructor( node, convertTo ) {

super();

/**
* The node which type should be converted.
*
* @type {Node}
*/
this.node = node;

/**
* The target node type. Multiple types can be defined by separating them with a `|` sign.
*
* @type {String}
*/
this.convertTo = convertTo;

}

/**
* This method is overwritten since the implementation tries to infere the best
* matching type from the {@link ConvertNode#convertTo} property.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {String} The node type.
*/
getNodeType( builder ) {

const requestType = this.node.getNodeType( builder );
Expand Down
69 changes: 69 additions & 0 deletions src/nodes/utils/CubeMapNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { CubeReflectionMapping, CubeRefractionMapping, EquirectangularReflection

const _cache = new WeakMap();

/**
* This node can be used to automatically convert environment maps in the
* equirectangular format into the cube map format.
*
* @augments TempNode
*/
class CubeMapNode extends TempNode {

static get type() {
Expand All @@ -16,20 +22,59 @@ class CubeMapNode extends TempNode {

}

/**
* Constructs a new cube map node.
*
* @param {Node} envNode - The node representing the environment map.
*/
constructor( envNode ) {

super( 'vec3' );

/**
* The node representing the environment map.
*
* @type {Node}
*/
this.envNode = envNode;

/**
* A reference to the internal cube texture.
*
* @private
* @type {CubeTexture}
* @default null
*/
this._cubeTexture = null;

/**
* A reference to the internal cube texture node.
*
* @private
* @type {CubeTextureNode}
*/
this._cubeTextureNode = cubeTexture();

const defaultTexture = new CubeTexture();
defaultTexture.isRenderTargetTexture = true;

/**
* A default cube texture that acts as a placeholder.
* It is used when the conversion from equirectangular to cube
* map has not finished yet for a givent texture.
*
* @private
* @type {CubeTexture}
*/
this._defaultTexture = defaultTexture;

/**
* The `updateBeforeType` is set to `NodeUpdateType.RENDER` since the node updates
* the texture once per render in its {@link CubeMapNode#updateBefore} method.
*
* @type {String}
* @default 'render'
*/
this.updateBeforeType = NodeUpdateType.RENDER;

}
Expand Down Expand Up @@ -117,6 +162,14 @@ class CubeMapNode extends TempNode {

export default CubeMapNode;

/**
* Returns true if the given equirectangular image has been fully loaded
* and is ready for further processing.
*
* @private
* @param {Image} image - The equirectangular image to check.
* @return {Boolean} Whether the image is ready or not.
*/
function isEquirectangularMapReady( image ) {

if ( image === null || image === undefined ) return false;
Expand All @@ -125,6 +178,14 @@ function isEquirectangularMapReady( image ) {

}

/**
* This function is executed when `dispose()` is called on the equirectangular
* texture. In this case, the generated cube map with its render target
* is deleted as well.
*
* @private
* @param {Object} event - The event object.
*/
function onTextureDispose( event ) {

const texture = event.target;
Expand All @@ -143,6 +204,14 @@ function onTextureDispose( event ) {

}

/**
* This function makes sure the generated cube map uses the correct
* texture mapping that corresponds to the equirectangular original.
*
* @private
* @param {Texture} texture - The cube texture.
* @param {Number} mapping - The original texture mapping.
*/
function mapTextureMapping( texture, mapping ) {

if ( mapping === EquirectangularReflectionMapping ) {
Expand Down
21 changes: 21 additions & 0 deletions src/nodes/utils/EquirectUVNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ import TempNode from '../core/TempNode.js';
import { positionWorldDirection } from '../accessors/Position.js';
import { nodeProxy, vec2 } from '../tsl/TSLBase.js';

/**
* Can be used to compute texture coordinates for projecting an
* equirectangular texture onto a mesh for using it as the scene's
* background.
*
* ```js
* scene.backgroundNode = texture( equirectTexture, equirectUV() );
* ```
*
* @augments TempNode
*/
class EquirectUVNode extends TempNode {

static get type() {
Expand All @@ -10,10 +21,20 @@ class EquirectUVNode extends TempNode {

}

/**
* Constructs a new equirect uv node.
*
* @param {Node<vec3>} [dirNode=positionWorldDirection] - A direction vector for sampling why is by default `positionWorldDirection`.
*/
constructor( dirNode = positionWorldDirection ) {

super( 'vec2' );

/**
* A direction vector for sampling why is by default `positionWorldDirection`.
*
* @type {Node<vec3>}
*/
this.dirNode = dirNode;

}
Expand Down
25 changes: 25 additions & 0 deletions src/nodes/utils/JoinNode.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import TempNode from '../core/TempNode.js';

/**
* This module is part of the TSL core and usually not used in app level code.
* It represents a join operation during the shader generation process.
* For example in can compose/join two single floats into a `vec2` type.
*
* @augments TempNode
*/
class JoinNode extends TempNode {

static get type() {
Expand All @@ -8,14 +15,32 @@ class JoinNode extends TempNode {

}

/**
* Constructs a new join node.
*
* @param {Array<Node>} nodes - An array of nodes that should be joined.
* @param {String?} [nodeType=null] - The node type.
*/
constructor( nodes = [], nodeType = null ) {

super( nodeType );

/**
* An array of nodes that should be joined.
*
* @type {Array<Node>}
*/
this.nodes = nodes;

}

/**
* This method is overwritten since the node type must be inferred from the
* joined data length if not explicitely defined.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {String} The node type.
*/
getNodeType( builder ) {

if ( this.nodeType !== null ) {
Expand Down
33 changes: 33 additions & 0 deletions src/nodes/utils/Oscillators.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
import { time } from './Timer.js';

/** @module Oscillators **/

/**
* Generates a sine wave oscillation based on a timer.
*
* @method
* @param {Node<float>} time - The timer to generate the oscillation with.
* @return {Node<float>} The oscillation node.
*/
export const oscSine = ( t = time ) => t.add( 0.75 ).mul( Math.PI * 2 ).sin().mul( 0.5 ).add( 0.5 );

/**
* Generates a square wave oscillation based on a timer.
*
* @method
* @param {Node<float>} time - The timer to generate the oscillation with.
* @return {Node<float>} The oscillation node.
*/
export const oscSquare = ( t = time ) => t.fract().round();

/**
* Generates a triangle wave oscillation based on a timer.
*
* @method
* @param {Node<float>} time - The timer to generate the oscillation with.
* @return {Node<float>} The oscillation node.
*/
export const oscTriangle = ( t = time ) => t.add( 0.5 ).fract().mul( 2 ).sub( 1 ).abs();

/**
* Generates a sawtooth wave oscillation based on a timer.
*
* @method
* @param {Node<float>} time - The timer to generate the oscillation with.
* @return {Node<float>} The oscillation node.
*/
export const oscSawtooth = ( t = time ) => t.fract();
17 changes: 17 additions & 0 deletions src/nodes/utils/Packing.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import { nodeObject } from '../tsl/TSLBase.js';

/** @module Packing **/

/**
* Packs a direction vector into a color value.
*
* @method
* @param {Node<vec3>} node - The direction to pack.
* @return {Node<vec3>} The color.
*/
export const directionToColor = ( node ) => nodeObject( node ).mul( 0.5 ).add( 0.5 );

/**
* Unpacks a color value into a direction vector.
*
* @method
* @param {Node<vec3>} color - The color to unpack.
* @return {Node<vec3>} The direction.
*/
export const colorToDirection = ( node ) => nodeObject( node ).mul( 2.0 ).sub( 1 );
20 changes: 10 additions & 10 deletions src/nodes/utils/PostProcessingUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { WebGPUCoordinateSystem } from '../../constants.js';
* depth value and the camera's inverse projection matrix.
*
* @method
* @param {vec2} screenPosition - The fragment's screen position expressed as uv coordinates.
* @param {float} depth - The fragment's depth value.
* @param {mat4} projectionMatrixInverse - The camera's inverse projection matrix.
* @return {vec3} The fragments position in view space.
* @param {Node<vec2>} screenPosition - The fragment's screen position expressed as uv coordinates.
* @param {Node<float>} depth - The fragment's depth value.
* @param {Node<mat4>} projectionMatrixInverse - The camera's inverse projection matrix.
* @return {Node<vec3>} The fragments position in view space.
*/
export const getViewPosition = /*@__PURE__*/ Fn( ( [ screenPosition, depth, projectionMatrixInverse ], builder ) => {

Expand Down Expand Up @@ -41,9 +41,9 @@ export const getViewPosition = /*@__PURE__*/ Fn( ( [ screenPosition, depth, proj
* and the camera's projection matrix
*
* @method
* @param {vec3} viewPosition - The fragments position in view space.
* @param {mat4} projectionMatrix - The camera's projection matrix.
* @return {vec2} The fragment's screen position expressed as uv coordinates.
* @param {Node<vec3>} viewPosition - The fragments position in view space.
* @param {Node<mat4>} projectionMatrix - The camera's projection matrix.
* @return {Node<vec2>} The fragment's screen position expressed as uv coordinates.
*/
export const getScreenPosition = /*@__PURE__*/ Fn( ( [ viewPosition, projectionMatrix ] ) => {

Expand All @@ -58,10 +58,10 @@ export const getScreenPosition = /*@__PURE__*/ Fn( ( [ viewPosition, projectionM
* target is available or if flat surface normals are required.
*
* @method
* @param {vec2} uv - The texture coordinate.
* @param {Node<vec2>} uv - The texture coordinate.
* @param {DepthTexture} depthTexture - The depth texture.
* @param {mat4} projectionMatrixInverse - The camera's inverse projection matrix.
* @return {vec3} The computed normal vector.
* @param {Node<mat4>} projectionMatrixInverse - The camera's inverse projection matrix.
* @return {Node<vec3>} The computed normal vector.
*/
export const getNormalFromDepth = /*@__PURE__*/ Fn( ( [ uv, depthTexture, projectionMatrixInverse ] ) => {

Expand Down
Loading
Loading