@@ -2,6 +2,8 @@ import Node from '../core/Node.js';
22import { nodeObject , vec3 } from '../tsl/TSLBase.js' ;
33import { hashArray } from '../core/NodeUtils.js' ;
44
5+ /** @module LightsNode **/
6+
57const sortLights = ( lights ) => {
68
79 return lights . sort ( ( a , b ) => a . id - b . id ) ;
@@ -26,6 +28,13 @@ const getLightNodeById = ( id, lightNodes ) => {
2628
2729const _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+ */
2938class 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
281382export 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+ */
283392export const lights = ( lights = [ ] ) => nodeObject ( new LightsNode ( ) ) . setLights ( lights ) ;
0 commit comments