-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
NodeLoader.js
189 lines (130 loc) · 3.56 KB
/
NodeLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { nodeObject, float } from '../../nodes/tsl/TSLBase.js';
import { Loader } from '../Loader.js';
import { FileLoader } from '../../loaders/FileLoader.js';
/**
* A loader for loading node objects in the three.js JSON Object/Scene format.
*
* @augments Loader
*/
class NodeLoader extends Loader {
/**
* Constructs a new node loader.
*
* @param {LoadingManager?} manager - A reference to a loading manager.
*/
constructor( manager ) {
super( manager );
/**
* Represents a dictionary of textures.
*
* @type {Object<String,Texture>}
*/
this.textures = {};
/**
* Represents a dictionary of node types.
*
* @type {Object<String,Node.constructor>}
*/
this.nodes = {};
}
/**
* Loads the node definitions from the given URL.
*
* @param {String} url - The path/URL of the file to be loaded.
* @param {Function} onLoad - Will be called when load completes.
* @param {Function} onProgress - Will be called while load progresses.
* @param {Function} onError - Will be called when errors are thrown during the loading process.
*/
load( url, onLoad, onProgress, onError ) {
const loader = new FileLoader( this.manager );
loader.setPath( this.path );
loader.setRequestHeader( this.requestHeader );
loader.setWithCredentials( this.withCredentials );
loader.load( url, ( text ) => {
try {
onLoad( this.parse( JSON.parse( text ) ) );
} catch ( e ) {
if ( onError ) {
onError( e );
} else {
console.error( e );
}
this.manager.itemError( url );
}
}, onProgress, onError );
}
/**
* Parse the node dependencies for the loaded node.
*
* @param {Object} json - The JSON definition
* @return {Object<String,Node>} A dictionary with node dependencies.
*/
parseNodes( json ) {
const nodes = {};
if ( json !== undefined ) {
for ( const nodeJSON of json ) {
const { uuid, type } = nodeJSON;
nodes[ uuid ] = this.createNodeFromType( type );
nodes[ uuid ].uuid = uuid;
}
const meta = { nodes, textures: this.textures };
for ( const nodeJSON of json ) {
nodeJSON.meta = meta;
const node = nodes[ nodeJSON.uuid ];
node.deserialize( nodeJSON );
delete nodeJSON.meta;
}
}
return nodes;
}
/**
* Parses the node from the given JSON.
*
* @param {Object} json - The JSON definition
* @return {Node} The parsed node.
*/
parse( json ) {
const node = this.createNodeFromType( json.type );
node.uuid = json.uuid;
const nodes = this.parseNodes( json.nodes );
const meta = { nodes, textures: this.textures };
json.meta = meta;
node.deserialize( json );
delete json.meta;
return node;
}
/**
* Defines the dictionary of textures.
*
* @param {Object<String,Texture>} value - The texture library defines as `<uuid,texture>`.
* @return {NodeLoader} A reference to this loader.
*/
setTextures( value ) {
this.textures = value;
return this;
}
/**
* Defines the dictionary of node types.
*
* @param {Object<String,Node.constructor>} value - The node library defined as `<classname,class>`.
* @return {NodeLoader} A reference to this loader.
*/
setNodes( value ) {
this.nodes = value;
return this;
}
/**
* Creates a node object from the given type.
*
* @param {String} type - The node type.
* @return {Node} The created node instance.
*/
createNodeFromType( type ) {
if ( this.nodes[ type ] === undefined ) {
console.error( 'THREE.NodeLoader: Node type not found:', type );
return float();
}
return nodeObject( new this.nodes[ type ]() );
}
}
export default NodeLoader;