Skip to content

Commit 42795bf

Browse files
authored
Node: Document more modules. (#30142)
* Node: Document more modules. * Update ScriptableNode.js
1 parent 5f4914a commit 42795bf

File tree

6 files changed

+468
-2
lines changed

6 files changed

+468
-2
lines changed

examples/webgpu_materials.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@
241241

242242
// Scriptable
243243

244-
ScriptableNodeResources.set( 'THREE', THREE );
245244
ScriptableNodeResources.set( 'TSL', TSL );
246245

247246
const asyncNode = scriptable( js( `
@@ -420,7 +419,7 @@
420419
function testSerialization( mesh ) {
421420

422421
const json = mesh.toJSON();
423-
const loader = new THREE.NodeObjectLoader().setNodes( moduleToLib( TSL ) ).setNodeMaterials( moduleToLib( THREE ) );
422+
const loader = new THREE.NodeObjectLoader().setNodes( moduleToLib( THREE ) ).setNodeMaterials( moduleToLib( THREE ) );
424423
const serializedMesh = loader.parse( json );
425424

426425
serializedMesh.position.x = ( objects.length % 4 ) * 200 - 400;

src/loaders/nodes/NodeLoader.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,46 @@ import { nodeObject, float } from '../../nodes/tsl/TSLBase.js';
33
import { Loader } from '../Loader.js';
44
import { FileLoader } from '../../loaders/FileLoader.js';
55

6+
/**
7+
* A loader for loading node objects in the three.js JSON Object/Scene format.
8+
*
9+
* @augments Loader
10+
*/
611
class NodeLoader extends Loader {
712

13+
/**
14+
* Constructs a new node loader.
15+
*
16+
* @param {LoadingManager?} manager - A reference to a loading manager.
17+
*/
818
constructor( manager ) {
919

1020
super( manager );
1121

22+
/**
23+
* Represents a dictionary of textures.
24+
*
25+
* @type {Object<String,Texture>}
26+
*/
1227
this.textures = {};
28+
29+
/**
30+
* Represents a dictionary of node types.
31+
*
32+
* @type {Object<String,Node.constructor>}
33+
*/
1334
this.nodes = {};
1435

1536
}
1637

38+
/**
39+
* Loads the node definitions from the given URL.
40+
*
41+
* @param {String} url - The path/URL of the file to be loaded.
42+
* @param {Function} onLoad - Will be called when load completes.
43+
* @param {Function} onProgress - Will be called while load progresses.
44+
* @param {Function} onError - Will be called when errors are thrown during the loading process.
45+
*/
1746
load( url, onLoad, onProgress, onError ) {
1847

1948
const loader = new FileLoader( this.manager );
@@ -46,6 +75,12 @@ class NodeLoader extends Loader {
4675

4776
}
4877

78+
/**
79+
* Parse the node dependencies for the loaded node.
80+
*
81+
* @param {Object} json - The JSON definition
82+
* @return {Object<String,Node>} A dictionary with node dependencies.
83+
*/
4984
parseNodes( json ) {
5085

5186
const nodes = {};
@@ -80,6 +115,12 @@ class NodeLoader extends Loader {
80115

81116
}
82117

118+
/**
119+
* Parses the node from the given JSON.
120+
*
121+
* @param {Object} json - The JSON definition
122+
* @return {Node} The parsed node.
123+
*/
83124
parse( json ) {
84125

85126
const node = this.createNodeFromType( json.type );
@@ -98,20 +139,38 @@ class NodeLoader extends Loader {
98139

99140
}
100141

142+
/**
143+
* Defines the dictionary of textures.
144+
*
145+
* @param {Object<String,Texture>} value - The texture library defines as `<uuid,texture>`.
146+
* @return {NodeLoader} A reference to this loader.
147+
*/
101148
setTextures( value ) {
102149

103150
this.textures = value;
104151
return this;
105152

106153
}
107154

155+
/**
156+
* Defines the dictionary of node types.
157+
*
158+
* @param {Object<String,Node.constructor>} value - The node library defined as `<classname,class>`.
159+
* @return {NodeLoader} A reference to this loader.
160+
*/
108161
setNodes( value ) {
109162

110163
this.nodes = value;
111164
return this;
112165

113166
}
114167

168+
/**
169+
* Creates a node object from the given type.
170+
*
171+
* @param {String} type - The node type.
172+
* @return {Node} The created node instance.
173+
*/
115174
createNodeFromType( type ) {
116175

117176
if ( this.nodes[ type ] === undefined ) {

src/loaders/nodes/NodeMaterialLoader.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,43 @@
11
import { MaterialLoader } from '../../loaders/MaterialLoader.js';
22

3+
/**
4+
* A special type of material loader for loading node materials.
5+
*
6+
* @augments MaterialLoader
7+
*/
38
class NodeMaterialLoader extends MaterialLoader {
49

10+
/**
11+
* Constructs a new node material loader.
12+
*
13+
* @param {LoadingManager?} manager - A reference to a loading manager.
14+
*/
515
constructor( manager ) {
616

717
super( manager );
818

19+
/**
20+
* Represents a dictionary of node types.
21+
*
22+
* @type {Object<String,Node.constructor>}
23+
*/
924
this.nodes = {};
25+
26+
/**
27+
* Represents a dictionary of node material types.
28+
*
29+
* @type {Object<String,NodeMaterial.constructor>}
30+
*/
1031
this.nodeMaterials = {};
1132

1233
}
1334

35+
/**
36+
* Parses the node material from the given JSON.
37+
*
38+
* @param {Object} json - The JSON definition
39+
* @return {NodeMaterial}. The parsed material.
40+
*/
1441
parse( json ) {
1542

1643
const material = super.parse( json );
@@ -30,20 +57,38 @@ class NodeMaterialLoader extends MaterialLoader {
3057

3158
}
3259

60+
/**
61+
* Defines the dictionary of node types.
62+
*
63+
* @param {Object<String,Node.constructor>} value - The node library defined as `<classname,class>`.
64+
* @return {NodeLoader} A reference to this loader.
65+
*/
3366
setNodes( value ) {
3467

3568
this.nodes = value;
3669
return this;
3770

3871
}
3972

73+
/**
74+
* Defines the dictionary of node material types.
75+
*
76+
* @param {Object<String,NodeMaterial.constructor>} value - The node material library defined as `<classname,class>`.
77+
* @return {NodeLoader} A reference to this loader.
78+
*/
4079
setNodeMaterials( value ) {
4180

4281
this.nodeMaterials = value;
4382
return this;
4483

4584
}
4685

86+
/**
87+
* Creates a node material from the given type.
88+
*
89+
* @param {String} type - The node material type.
90+
* @return {Node} The created node material instance.
91+
*/
4792
createMaterialFromType( type ) {
4893

4994
const materialClass = this.nodeMaterials[ type ];

src/loaders/nodes/NodeObjectLoader.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,80 @@ import NodeMaterialLoader from './NodeMaterialLoader.js';
33

44
import { ObjectLoader } from '../../loaders/ObjectLoader.js';
55

6+
/**
7+
* A special type of object loader for loading 3D objects using
8+
* node materials.
9+
*
10+
* @augments ObjectLoader
11+
*/
612
class NodeObjectLoader extends ObjectLoader {
713

14+
/**
15+
* Constructs a new node object loader.
16+
*
17+
* @param {LoadingManager?} manager - A reference to a loading manager.
18+
*/
819
constructor( manager ) {
920

1021
super( manager );
1122

23+
/**
24+
* Represents a dictionary of node types.
25+
*
26+
* @type {Object<String,Node.constructor>}
27+
*/
1228
this.nodes = {};
29+
30+
/**
31+
* Represents a dictionary of node material types.
32+
*
33+
* @type {Object<String,NodeMaterial.constructor>}
34+
*/
1335
this.nodeMaterials = {};
1436

37+
/**
38+
* A reference for holdng the `nodes` JSON property.
39+
*
40+
* @private
41+
* @type {Object?}
42+
*/
1543
this._nodesJSON = null;
1644

1745
}
1846

47+
/**
48+
* Defines the dictionary of node types.
49+
*
50+
* @param {Object<String,Node.constructor>} value - The node library defined as `<classname,class>`.
51+
* @return {NodeLoader} A reference to this loader.
52+
*/
1953
setNodes( value ) {
2054

2155
this.nodes = value;
2256
return this;
2357

2458
}
2559

60+
/**
61+
* Defines the dictionary of node material types.
62+
*
63+
* @param {Object<String,NodeMaterial.constructor>} value - The node material library defined as `<classname,class>`.
64+
* @return {NodeLoader} A reference to this loader.
65+
*/
2666
setNodeMaterials( value ) {
2767

2868
this.nodeMaterials = value;
2969
return this;
3070

3171
}
3272

73+
/**
74+
* Parses the node objects from the given JSON.
75+
*
76+
* @param {Object} json - The JSON definition
77+
* @param {Function} onLoad - The onLoad callback function.
78+
* @return {Object3D}. The parsed 3D object.
79+
*/
3380
parse( json, onLoad ) {
3481

3582
this._nodesJSON = json.nodes;
@@ -42,6 +89,13 @@ class NodeObjectLoader extends ObjectLoader {
4289

4390
}
4491

92+
/**
93+
* Parses the node objects from the given JSON and textures.
94+
*
95+
* @param {Object} json - The JSON definition
96+
* @param {Object<String,Texture>} textures - The texture library.
97+
* @return {Object<String,Node>}. The parsed nodes.
98+
*/
4599
parseNodes( json, textures ) {
46100

47101
if ( json !== undefined ) {
@@ -58,6 +112,13 @@ class NodeObjectLoader extends ObjectLoader {
58112

59113
}
60114

115+
/**
116+
* Parses the node objects from the given JSON and textures.
117+
*
118+
* @param {Object} json - The JSON definition
119+
* @param {Object<String,Texture>} textures - The texture library.
120+
* @return {Object<String,NodeMaterial>}. The parsed materials.
121+
*/
61122
parseMaterials( json, textures ) {
62123

63124
const materials = {};

0 commit comments

Comments
 (0)