Skip to content

Commit 4de68d2

Browse files
authored
StructNode: Add some docs (#30426)
1 parent 8319d7c commit 4de68d2

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

examples/webgpu_compute_particles.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@
227227
// events
228228

229229
renderer.domElement.addEventListener( 'pointermove', onMove );
230+
230231
//
231232

232233
controls = new OrbitControls( camera, renderer.domElement );

src/nodes/core/StructNode.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,26 @@ import { nodeObject } from '../tsl/TSLCore.js';
44

55
/** @module StructNode **/
66

7+
/**
8+
* StructNode allows to create custom structures with multiple members.
9+
* This can also be used to define structures in attribute and uniform data.
10+
*
11+
* ```js
12+
* // Define a custom struct
13+
* const BoundingBox = struct( { min: 'vec3', max: 'vec3' } );
14+
*
15+
* // Create a new instance of the struct
16+
* const bb = BoundingBox( vec3( 0 ), vec3( 1 ) ); // style 1
17+
* const bb = BoundingBox( { min: vec3( 0 ), max: vec3( 1 ) } ); // style 2
18+
*
19+
* // Access the struct members
20+
* const min = bb.get( 'min' );
21+
*
22+
* // Assign a new value to a member
23+
* min.assign( vec3() );
24+
* ```
25+
* @augments Node
26+
*/
727
class StructNode extends Node {
828

929
static get type() {
@@ -51,6 +71,14 @@ class StructNode extends Node {
5171

5272
export default StructNode;
5373

74+
/**
75+
* TSL function for creating a struct node.
76+
*
77+
* @function
78+
* @param {Object} membersLayout - The layout of the struct members.
79+
* @param {string} [name=null] - The name of the struct.
80+
* @returns {Function} The struct function.
81+
*/
5482
export const struct = ( membersLayout, name = null ) => {
5583

5684
const structLayout = new StructTypeNode( membersLayout, name );

src/nodes/core/StructTypeNode.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1+
12
import Node from './Node.js';
23
import { getLengthFromType } from './NodeUtils.js';
34

45
/** @module StructTypeNode **/
56

7+
/**
8+
* Generates a layout for struct members.
9+
* This function takes an object representing struct members and returns an array of member layouts.
10+
* Each member layout includes the member's name, type, and whether it is atomic.
11+
*
12+
* @param {Object.<string, string|Object>} members - An object where keys are member names and values are either types (as strings) or objects with type and atomic properties.
13+
* @returns {Array.<{name: string, type: string, atomic: boolean}>} An array of member layouts.
14+
*/
615
function getMembersLayout( members ) {
716

817
return Object.entries( members ).map( ( [ name, value ] ) => {
@@ -19,6 +28,14 @@ function getMembersLayout( members ) {
1928

2029
}
2130

31+
/**
32+
* Represents a struct type node in the node-based system.
33+
* This class is used to define and manage the layout and types of struct members.
34+
* It extends the base Node class and provides methods to get the length of the struct,
35+
* retrieve member types, and generate the struct type for a builder.
36+
*
37+
* @augments Node
38+
*/
2239
class StructTypeNode extends Node {
2340

2441
static get type() {
@@ -27,17 +44,48 @@ class StructTypeNode extends Node {
2744

2845
}
2946

47+
/**
48+
* Creates an instance of StructTypeNode.
49+
*
50+
* @param {Object} membersLayout - The layout of the members for the struct.
51+
* @param {string} [name=null] - The optional name of the struct.
52+
*/
3053
constructor( membersLayout, name = null ) {
3154

3255
super( 'struct' );
3356

57+
/**
58+
* The layout of the members for the struct
59+
*
60+
* @type {Array.<{name: string, type: string, atomic: boolean}>}
61+
*/
3462
this.membersLayout = getMembersLayout( membersLayout );
63+
64+
/**
65+
* The name of the struct.
66+
*
67+
* @type {String}
68+
* @default null
69+
*/
3570
this.name = name;
3671

72+
/**
73+
* This flag can be used for type testing.
74+
*
75+
* @type {Boolean}
76+
* @readonly
77+
* @default true
78+
*/
3779
this.isStructLayoutNode = true;
3880

3981
}
4082

83+
/**
84+
* Returns the length of the struct.
85+
* The length is calculated by summing the lengths of the struct's members.
86+
*
87+
* @returns {Number} The length of the struct.
88+
*/
4189
getLength() {
4290

4391
let length = 0;

0 commit comments

Comments
 (0)