Skip to content
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

Node: Document more modules. #30067

Merged
merged 1 commit into from
Dec 8, 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
42 changes: 21 additions & 21 deletions src/nodes/display/ColorAdjustment.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { LinearSRGBColorSpace } from '../../constants.js';
* Computes a grayscale value for the given RGB color value.
*
* @method
* @param {vec3} color - The color value to compute the grayscale for.
* @return {vec3} The grayscale color.
* @param {Node<vec3>} color - The color value to compute the grayscale for.
* @return {Node<vec3>} The grayscale color.
*/
export const grayscale = /*@__PURE__*/ Fn( ( [ color ] ) => {

Expand All @@ -24,9 +24,9 @@ export const grayscale = /*@__PURE__*/ Fn( ( [ color ] ) => {
* Super-saturates or desaturates the given RGB color.
*
* @method
* @param {vec3} color - The input color.
* @param {float} [adjustment=1] - Specifies the amount of the conversion. A value under `1` desaturates the color, a value over `1` super-saturates it.
* @return {vec3} The saturated color.
* @param {Node<vec3>} color - The input color.
* @param {Node<float>} [adjustment=1] - Specifies the amount of the conversion. A value under `1` desaturates the color, a value over `1` super-saturates it.
* @return {Node<vec3>} The saturated color.
*/
export const saturation = /*@__PURE__*/ Fn( ( [ color, adjustment = float( 1 ) ] ) => {

Expand All @@ -40,9 +40,9 @@ export const saturation = /*@__PURE__*/ Fn( ( [ color, adjustment = float( 1 ) ]
* compared to {@link ColorAdjustment#saturation}.
*
* @method
* @param {vec3} color - The input color.
* @param {float} [adjustment=1] - Controls the intensity of the vibrance effect.
* @return {vec3} The updated color.
* @param {Node<vec3>} color - The input color.
* @param {Node<float>} [adjustment=1] - Controls the intensity of the vibrance effect.
* @return {Node<vec3>} The updated color.
*/
export const vibrance = /*@__PURE__*/ Fn( ( [ color, adjustment = float( 1 ) ] ) => {

Expand All @@ -59,9 +59,9 @@ export const vibrance = /*@__PURE__*/ Fn( ( [ color, adjustment = float( 1 ) ] )
* Updates the hue component of the given RGB color while preserving its luminance and saturation.
*
* @method
* @param {vec3} color - The input color.
* @param {float} [adjustment=1] - Defines the degree of hue rotation in radians. A positive value rotates the hue clockwise, while a negative value rotates it counterclockwise.
* @return {vec3} The updated color.
* @param {Node<vec3>} color - The input color.
* @param {Node<float>} [adjustment=1] - Defines the degree of hue rotation in radians. A positive value rotates the hue clockwise, while a negative value rotates it counterclockwise.
* @return {Node<vec3>} The updated color.
*/
export const hue = /*@__PURE__*/ Fn( ( [ color, adjustment = float( 1 ) ] ) => {

Expand All @@ -77,9 +77,9 @@ export const hue = /*@__PURE__*/ Fn( ( [ color, adjustment = float( 1 ) ] ) => {
* Computes the luminance for the given RGB color value.
*
* @method
* @param {vec3} color - The color value to compute the luminance for.
* @param {vec3?} luminanceCoefficients - The luminance coefficients. By default predefined values of the current working color space are used.
* @return {vec3} The luminance.
* @param {Node<vec3>} color - The color value to compute the luminance for.
* @param {Node<vec3>?} luminanceCoefficients - The luminance coefficients. By default predefined values of the current working color space are used.
* @return {Node<vec3>} The luminance.
*/
export const luminance = (
color,
Expand All @@ -94,13 +94,13 @@ export const luminance = (
* or AgX Log), and will return output in the same space. Output may require clamping >=0.
*
* @method
* @param {vec4} color Input (-Infinity < input < +Infinity)
* @param {number | vec3} slope Slope (0 ≤ slope < +Infinity)
* @param {number | vec3} offset Offset (-Infinity < offset < +Infinity; typically -1 < offset < 1)
* @param {number | vec3} power Power (0 < power < +Infinity)
* @param {number} saturation Saturation (0 ≤ saturation < +Infinity; typically 0 ≤ saturation < 4)
* @param {vec3} luminanceCoefficients Luminance coefficients for saturation term, typically Rec. 709
* @return Output, -Infinity < output < +Infinity
* @param {Node<vec4>} color Input (-Infinity < input < +Infinity)
* @param {Node<vec3>} slope Slope (0 ≤ slope < +Infinity)
* @param {Node<vec3>} offset Offset (-Infinity < offset < +Infinity; typically -1 < offset < 1)
* @param {Node<vec3>} power Power (0 < power < +Infinity)
* @param {Node<float>} saturation Saturation (0 ≤ saturation < +Infinity; typically 0 ≤ saturation < 4)
* @param {Node<vec3>} luminanceCoefficients Luminance coefficients for saturation term, typically Rec. 709
* @return {Node<vec4>} Output, -Infinity < output < +Infinity
*
* References:
* - ASC CDL v1.2
Expand Down
8 changes: 4 additions & 4 deletions src/nodes/display/ColorSpaceFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { Fn } from '../tsl/TSLCore.js';
* Converts the given color value from sRGB to linear-sRGB color space.
*
* @method
* @param {vec3} color - The sRGB color.
* @return {vec3} The linear-sRGB color.
* @param {Node<vec3>} color - The sRGB color.
* @return {Node<vec3>} The linear-sRGB color.
*/
export const sRGBTransferEOTF = /*@__PURE__*/ Fn( ( [ color ] ) => {

Expand All @@ -32,8 +32,8 @@ export const sRGBTransferEOTF = /*@__PURE__*/ Fn( ( [ color ] ) => {
* Converts the given color value from linear-sRGB to sRGB color space.
*
* @method
* @param {vec3} color - The linear-sRGB color.
* @return {vec3} The sRGB color.
* @param {Node<vec3>} color - The linear-sRGB color.
* @return {Node<vec3>} The sRGB color.
*/
export const sRGBTransferOETF = /*@__PURE__*/ Fn( ( [ color ] ) => {

Expand Down
36 changes: 18 additions & 18 deletions src/nodes/display/ToneMappingFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { mul, sub, div } from '../math/OperatorNode.js';
* Linear tone mapping, exposure only.
*
* @method
* @param {vec3} color - The color that should be tone mapped.
* @param {float} exposure - The exposure.
* @return {vec3} The tone mapped color.
* @param {Node<vec3>} color - The color that should be tone mapped.
* @param {Node<float>} exposure - The exposure.
* @return {Node<vec3>} The tone mapped color.
*/
export const linearToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {

Expand All @@ -32,9 +32,9 @@ export const linearToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {
* Reference: {@link https://www.cs.utah.edu/docs/techreports/2002/pdf/UUCS-02-001.pdf}
*
* @method
* @param {vec3} color - The color that should be tone mapped.
* @param {float} exposure - The exposure.
* @return {vec3} The tone mapped color.
* @param {Node<vec3>} color - The color that should be tone mapped.
* @param {Node<float>} exposure - The exposure.
* @return {Node<vec3>} The tone mapped color.
*/
export const reinhardToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {

Expand All @@ -57,9 +57,9 @@ export const reinhardToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) =>
* Reference: {@link http://filmicworlds.com/blog/filmic-tonemapping-operators/}
*
* @method
* @param {vec3} color - The color that should be tone mapped.
* @param {float} exposure - The exposure.
* @return {vec3} The tone mapped color.
* @param {Node<vec3>} color - The color that should be tone mapped.
* @param {Node<float>} exposure - The exposure.
* @return {Node<vec3>} The tone mapped color.
*/
export const cineonToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {

Expand Down Expand Up @@ -98,9 +98,9 @@ const RRTAndODTFit = /*@__PURE__*/ Fn( ( [ color ] ) => {
* Reference: {@link https://github.com/selfshadow/ltc_code/blob/master/webgl/shaders/ltc/ltc_blit.fs}
*
* @method
* @param {vec3} color - The color that should be tone mapped.
* @param {float} exposure - The exposure.
* @return {vec3} The tone mapped color.
* @param {Node<vec3>} color - The color that should be tone mapped.
* @param {Node<float>} exposure - The exposure.
* @return {Node<vec3>} The tone mapped color.
*/
export const acesFilmicToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {

Expand Down Expand Up @@ -156,9 +156,9 @@ const agxDefaultContrastApprox = /*@__PURE__*/ Fn( ( [ x_immutable ] ) => {
* AgX tone mapping.
*
* @method
* @param {vec3} color - The color that should be tone mapped.
* @param {float} exposure - The exposure.
* @return {vec3} The tone mapped color.
* @param {Node<vec3>} color - The color that should be tone mapped.
* @param {Node<float>} exposure - The exposure.
* @return {Node<vec3>} The tone mapped color.
*/
export const agxToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {

Expand Down Expand Up @@ -197,9 +197,9 @@ export const agxToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {
* Reference: {@link https://modelviewer.dev/examples/tone-mapping}
*
* @method
* @param {vec3} color - The color that should be tone mapped.
* @param {float} exposure - The exposure.
* @return {vec3} The tone mapped color.
* @param {Node<vec3>} color - The color that should be tone mapped.
* @param {Node<float>} exposure - The exposure.
* @return {Node<vec3>} The tone mapped color.
*/
export const neutralToneMapping = /*@__PURE__*/ Fn( ( [ color, exposure ] ) => {

Expand Down
45 changes: 43 additions & 2 deletions src/nodes/geometry/RangeNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import { InstancedBufferAttribute } from '../../core/InstancedBufferAttribute.js
let min = null;
let max = null;

/**
* `RangeNode` generates random instanced attribute data in a defined range.
* An exemplary use case for this utility node is to generate random per-instance
* colors:
* ```js
* const material = new MeshBasicNodeMaterial();
* material.colorNode = range( new Color( 0x000000 ), new Color( 0xFFFFFF ) );
* const mesh = new InstancedMesh( geometry, material, count );
* ```
* @augments Node
*/
class RangeNode extends Node {

static get type() {
Expand All @@ -20,15 +31,39 @@ class RangeNode extends Node {

}

/**
* Constructs a new range node.
*
* @param {Node<any>} [minNode=float()] - A node defining the lower bound of the range.
* @param {Node<any>} [maxNode=float()] - A node defining the upper bound of the range.
*/
constructor( minNode = float(), maxNode = float() ) {

super();

/**
* A node defining the lower bound of the range.
*
* @type {Node<any>}
* @default float()
*/
this.minNode = minNode;

/**
* A node defining the upper bound of the range.
*
* @type {Node<any>}
* @default float()
*/
this.maxNode = maxNode;

}

/**
* Returns the vector length which is computed based on the range definition.
*
* @return {Number} The vector length.
*/
getVectorLength( builder ) {

const minLength = builder.getTypeLength( getValueType( this.minNode.value ) );
Expand All @@ -38,6 +73,12 @@ class RangeNode extends Node {

}

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

return builder.object.count > 1 ? builder.getTypeFromLength( this.getVectorLength( builder ) ) : 'float';
Expand Down Expand Up @@ -65,11 +106,11 @@ class RangeNode extends Node {
max.setScalar( 0 );

if ( minLength === 1 ) min.setScalar( minValue );
else if ( minValue.isColor ) min.set( minValue.r, minValue.g, minValue.b );
else if ( minValue.isColor ) min.set( minValue.r, minValue.g, minValue.b, 1 );
Copy link
Collaborator Author

@Mugen87 Mugen87 Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sunag This is a minor fix to prevent NaN values in the attribute. Vector4.set() should always be used with four arguments otherwise properties end up undefined and then produce garbage values in subsequent computations.

Since the code assumes a four-component color value, 1 is used so alpha gets 1.

else min.set( minValue.x, minValue.y, minValue.z || 0, minValue.w || 0 );

if ( maxLength === 1 ) max.setScalar( maxValue );
else if ( maxValue.isColor ) max.set( maxValue.r, maxValue.g, maxValue.b );
else if ( maxValue.isColor ) max.set( maxValue.r, maxValue.g, maxValue.b, 1 );
else max.set( maxValue.x, maxValue.y, maxValue.z || 0, maxValue.w || 0 );

const stride = 4;
Expand Down
43 changes: 43 additions & 0 deletions src/nodes/math/ConditionalNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import Node from '../core/Node.js';
import { property } from '../core/PropertyNode.js';
import { addMethodChaining, nodeProxy } from '../tsl/TSLCore.js';

/**
* Represents a logical `if/else` statement. Can be used as an alternative
* to the `If()`/`Else()` syntax.
*
* The corresponding TSL `select()` looks like so:
* ```js
* velocity = position.greaterThanEqual( limit ).select( velocity.negate(), velocity );
* ```
* The `select()` method is called in a chaining fashion on a codition. The parameter nodes of `select()`
* determine the outcome of the entire statement.
*
* @augments Node
*/
class ConditionalNode extends Node {

static get type() {
Expand All @@ -10,17 +23,47 @@ class ConditionalNode extends Node {

}

/**
* Constructs a new conditional node.
*
* @param {Node} condNode - The node that defines the condition.
* @param {Node} ifNode - The node that is evaluate when the condition ends up `true`.
* @param {Node?} [elseNode=null] - The node that is evaluate when the condition ends up `false`.
*/
constructor( condNode, ifNode, elseNode = null ) {

super();

/**
* The node that defines the condition.
*
* @type {Node}
*/
this.condNode = condNode;

/**
* The node that is evaluate when the condition ends up `true`.
*
* @type {Node}
*/
this.ifNode = ifNode;

/**
* The node that is evaluate when the condition ends up `false`.
*
* @type {Node}
*/
this.elseNode = elseNode;

}

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

const ifType = this.ifNode.getNodeType( builder );
Expand Down
9 changes: 9 additions & 0 deletions src/nodes/math/Hash.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { Fn } from '../tsl/TSLBase.js';

/** @module Hash **/

/**
* Generates a hash value in the range `[0, 1]` from the given seed.
*
* @method
* @param {Node<float>} seed - The seed.
* @return {Node<float>} The hash value.
*/
export const hash = /*@__PURE__*/ Fn( ( [ seed ] ) => {

// Taken from https://www.shadertoy.com/view/XlGcRh, originally from pcg-random.org
Expand Down
Loading
Loading