Skip to content

Commit 0eae292

Browse files
authored
Nodes: Document more modules. (#30087)
1 parent 8ac75e9 commit 0eae292

File tree

6 files changed

+369
-8
lines changed

6 files changed

+369
-8
lines changed

src/nodes/fog/Fog.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import { positionView } from '../accessors/Position.js';
22
import { smoothstep } from '../math/MathNode.js';
33
import { Fn, vec4 } from '../tsl/TSLBase.js';
44

5+
/** @module Fog **/
6+
57
/**
68
* Returns a node that represents the `z` coordinate in view space
79
* for the current fragment. It's a different representation of the
@@ -75,7 +77,7 @@ export const fog = Fn( ( [ color, factor ] ) => {
7577

7678
export function rangeFog( color, near, far ) { // @deprecated, r171
7779

78-
console.warn( 'THREE.TSL: "rangeFog( color, near, far )" is deprecated. Use "fog( color, rangeFog( near, far ) )" instead.' );
80+
console.warn( 'THREE.TSL: "rangeFog( color, near, far )" is deprecated. Use "fog( color, rangeFogFactor( near, far ) )" instead.' );
7981
return fog( color, rangeFogFactor( near, far ) );
8082

8183
}

src/nodes/lighting/AnalyticLightNode.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ class AnalyticLightNode extends LightingNode {
126126

127127
/**
128128
* Setups the shadow for this light. This method is only executed if the light
129-
* cast shadows and the current build object receives shadows.
129+
* cast shadows and the current build object receives shadows. It incorporates
130+
* shadows into the lighting computation.
130131
*
131132
* @param {NodeBuilder} builder - The current node builder.
132133
*/

src/nodes/lighting/LightsNode.js

Lines changed: 111 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import Node from '../core/Node.js';
22
import { nodeObject, vec3 } from '../tsl/TSLBase.js';
33
import { hashArray } from '../core/NodeUtils.js';
44

5+
/** @module LightsNode **/
6+
57
const sortLights = ( lights ) => {
68

79
return lights.sort( ( a, b ) => a.id - b.id );
@@ -26,6 +28,13 @@ const getLightNodeById = ( id, lightNodes ) => {
2628

2729
const _lightsNodeRef = /*@__PURE__*/ new WeakMap();
2830

31+
/**
32+
* This node represents the scene's lighting and manages the lighting model's life cycle
33+
* for the current build 3D object. It is responsible for computing the total outgoing
34+
* light in a given lighting context.
35+
*
36+
* @augments Node
37+
*/
2938
class LightsNode extends Node {
3039

3140
static get type() {
@@ -34,29 +43,76 @@ class LightsNode extends Node {
3443

3544
}
3645

46+
/**
47+
* Constructs a new lights node.
48+
*/
3749
constructor() {
3850

3951
super( 'vec3' );
4052

53+
/**
54+
* A node representing the total diffuse light.
55+
*
56+
* @type {Node<vec3>}
57+
*/
4158
this.totalDiffuseNode = vec3().toVar( 'totalDiffuse' );
59+
60+
/**
61+
* A node representing the total specular light.
62+
*
63+
* @type {Node<vec3>}
64+
*/
4265
this.totalSpecularNode = vec3().toVar( 'totalSpecular' );
4366

67+
/**
68+
* A node representing the outgoing light.
69+
*
70+
* @type {Node<vec3>}
71+
*/
4472
this.outgoingLightNode = vec3().toVar( 'outgoingLight' );
4573

74+
/**
75+
* An array representing the lights in the scene.
76+
*
77+
* @private
78+
* @type {Array<Light>}
79+
*/
4680
this._lights = [];
4781

82+
/**
83+
* For each light in the scene, this node will create a
84+
* corresponding light node.
85+
*
86+
* @private
87+
* @type {Array<LightingNode>?}
88+
* @default null
89+
*/
4890
this._lightNodes = null;
91+
92+
/**
93+
* A hash for identifying the current light nodes setup.
94+
*
95+
* @private
96+
* @type {String?}
97+
* @default null
98+
*/
4999
this._lightNodesHash = null;
50100

101+
/**
102+
* `LightsNode` sets this property to `true` by default.
103+
*
104+
* @type {Boolean}
105+
* @default true
106+
*/
51107
this.global = true;
52108

53109
}
54110

55111
/**
56-
* Overwrites the default `customCacheKey()` implementation by including the
112+
* Overwrites the default {@link Node#customCacheKey} implementation by including the
57113
* light IDs into the cache key.
58114
*
59-
* @return {Number} The hash.
115+
* @return {Number} The custom cache key.
60116
*/
61117
customCacheKey() {
62118

@@ -72,6 +128,12 @@ class LightsNode extends Node {
72128

73129
}
74130

131+
/**
132+
* Computes a hash value for identifying the current light nodes setup.
133+
*
134+
* @param {NodeBuilder} builder - A reference to the current node builder.
135+
* @return {String} The computed hash.
136+
*/
75137
getHash( builder ) {
76138

77139
if ( this._lightNodesHash === null ) {
@@ -106,6 +168,12 @@ class LightsNode extends Node {
106168

107169
}
108170

171+
/**
172+
* Creates lighting nodes for each scene light. This makes it possible to further
173+
* process lights in the node system.
174+
*
175+
* @param {NodeBuilder} builder - A reference to the current node builder.
176+
*/
109177
setupLightsNode( builder ) {
110178

111179
const lightNodes = [];
@@ -133,6 +201,8 @@ class LightsNode extends Node {
133201

134202
if ( lightNode === null ) {
135203

204+
// find the corresponding node type for a given light
205+
136206
const lightNodeClass = nodeLibrary.getLightNodeClass( light.constructor );
137207

138208
if ( lightNodeClass === null ) {
@@ -167,6 +237,13 @@ class LightsNode extends Node {
167237

168238
}
169239

240+
/**
241+
* Setups the internal lights by building all respective
242+
* light nodes.
243+
*
244+
* @param {NodeBuilder} builder - A reference to the current node builder.
245+
* @param {Array<LightingNode>} lightNodes - An array of lighting nodes.
246+
*/
170247
setupLights( builder, lightNodes ) {
171248

172249
for ( const lightNode of lightNodes ) {
@@ -177,6 +254,14 @@ class LightsNode extends Node {
177254

178255
}
179256

257+
/**
258+
* The implementation makes sure that for each light in the scene
259+
* there is a corresponding light node. By building the light nodes
260+
* and evaluating the lighting model the outgoing light is computed.
261+
*
262+
* @param {NodeBuilder} builder - A reference to the current node builder.
263+
* @return {Node<vec3>} A node representing the outgoing light.
264+
*/
180265
setup( builder ) {
181266

182267
if ( this._lightNodes === null ) this.setupLightsNode( builder );
@@ -253,6 +338,12 @@ class LightsNode extends Node {
253338

254339
}
255340

341+
/**
342+
* Configures this node with an array of lights.
343+
*
344+
* @param {Array<Light>} lights - An array of lights.
345+
* @return {LightsNode} A reference to this node.
346+
*/
256347
setLights( lights ) {
257348

258349
this._lights = lights;
@@ -264,12 +355,22 @@ class LightsNode extends Node {
264355

265356
}
266357

358+
/**
359+
* Returns an array of the scene's lights.
360+
*
361+
* @return {Array<Light>} The scene's lights.
362+
*/
267363
getLights() {
268364

269365
return this._lights;
270366

271367
}
272368

369+
/**
370+
* Whether the scene has lights or not.
371+
*
372+
* @type {Boolean}
373+
*/
273374
get hasLights() {
274375

275376
return this._lights.length > 0;
@@ -280,4 +381,12 @@ class LightsNode extends Node {
280381

281382
export default LightsNode;
282383

384+
/**
385+
* Factory method for creating an instance of `LightsNode` and configuring
386+
* it with the given array of lights.
387+
*
388+
* @method
389+
* @param {Array<Light>} lights - An array of lights.
390+
* @return {LightsNode} The created lights node.
391+
*/
283392
export const lights = ( lights = [] ) => nodeObject( new LightsNode() ).setLights( lights );

src/nodes/lighting/ShadowBaseNode.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ import { NodeUpdateType } from '../core/constants.js';
33
import { vec3 } from '../tsl/TSLBase.js';
44
import { positionWorld } from '../accessors/Position.js';
55

6+
/** @module ShadowBaseNode **/
7+
8+
/**
9+
* Base class for all shadow nodes.
10+
*
11+
* Shadow nodes encapsulate shadow related logic and are always coupled to lighting nodes.
12+
* Lighting nodes might share the same shadow node type or use specific ones depending on
13+
* their requirements.
14+
*
15+
* @augments Node
16+
*/
617
class ShadowBaseNode extends Node {
718

819
static get type() {
@@ -11,17 +22,46 @@ class ShadowBaseNode extends Node {
1122

1223
}
1324

25+
/**
26+
* Constructs a new shadow base node.
27+
*
28+
* @param {Light} light - The shadow casting light.
29+
*/
1430
constructor( light ) {
1531

1632
super();
1733

34+
/**
35+
* The shadow casting light.
36+
*
37+
* @type {Light}
38+
*/
1839
this.light = light;
40+
41+
/**
42+
* Overwritten since shadows are updated by default per render.
43+
*
44+
* @type {String}
45+
* @default 'render'
46+
*/
1947
this.updateBeforeType = NodeUpdateType.RENDER;
2048

49+
/**
50+
* This flag can be used for type testing.
51+
*
52+
* @type {Boolean}
53+
* @readonly
54+
* @default true
55+
*/
2156
this.isShadowBaseNode = true;
2257

2358
}
2459

60+
/**
61+
* Setups the shadow position node which is by default the predefined TSL node object `shadowWorldPosition`.
62+
*
63+
* @param {(NodeBuilder|{material})} object - A configuration object that must at least hold a material reference.
64+
*/
2565
setupShadowPosition( { material } ) {
2666

2767
// Use assign inside an Fn()
@@ -30,6 +70,11 @@ class ShadowBaseNode extends Node {
3070

3171
}
3272

73+
/**
74+
* Can be called when the shadow isn't required anymore. That can happen when
75+
* a lighting node stops casting shadows by setting {@link Object3D#castShadow}
76+
* to `false`.
77+
*/
3378
dispose() {
3479

3580
this.updateBeforeType = NodeUpdateType.NONE;
@@ -38,6 +83,11 @@ class ShadowBaseNode extends Node {
3883

3984
}
4085

86+
/**
87+
* Represents the vertex position in world space during the shadow pass.
88+
*
89+
* @type {Node<vec3>}
90+
*/
4191
export const shadowWorldPosition = /*@__PURE__*/ vec3().toVar( 'shadowWorldPosition' );
4292

4393
export default ShadowBaseNode;

0 commit comments

Comments
 (0)