-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
/
NodeObjectLoader.js
151 lines (110 loc) · 3.07 KB
/
NodeObjectLoader.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
import NodeLoader from './NodeLoader.js';
import NodeMaterialLoader from './NodeMaterialLoader.js';
import { ObjectLoader } from '../../loaders/ObjectLoader.js';
/**
* A special type of object loader for loading 3D objects using
* node materials.
*
* @augments ObjectLoader
*/
class NodeObjectLoader extends ObjectLoader {
/**
* Constructs a new node object loader.
*
* @param {LoadingManager?} manager - A reference to a loading manager.
*/
constructor( manager ) {
super( manager );
/**
* Represents a dictionary of node types.
*
* @type {Object<String,Node.constructor>}
*/
this.nodes = {};
/**
* Represents a dictionary of node material types.
*
* @type {Object<String,NodeMaterial.constructor>}
*/
this.nodeMaterials = {};
/**
* A reference for holdng the `nodes` JSON property.
*
* @private
* @type {Object?}
*/
this._nodesJSON = null;
}
/**
* 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;
}
/**
* Defines the dictionary of node material types.
*
* @param {Object<String,NodeMaterial.constructor>} value - The node material library defined as `<classname,class>`.
* @return {NodeLoader} A reference to this loader.
*/
setNodeMaterials( value ) {
this.nodeMaterials = value;
return this;
}
/**
* Parses the node objects from the given JSON.
*
* @param {Object} json - The JSON definition
* @param {Function} onLoad - The onLoad callback function.
* @return {Object3D}. The parsed 3D object.
*/
parse( json, onLoad ) {
this._nodesJSON = json.nodes;
const data = super.parse( json, onLoad );
this._nodesJSON = null; // dispose
return data;
}
/**
* Parses the node objects from the given JSON and textures.
*
* @param {Object} json - The JSON definition
* @param {Object<String,Texture>} textures - The texture library.
* @return {Object<String,Node>}. The parsed nodes.
*/
parseNodes( json, textures ) {
if ( json !== undefined ) {
const loader = new NodeLoader();
loader.setNodes( this.nodes );
loader.setTextures( textures );
return loader.parseNodes( json );
}
return {};
}
/**
* Parses the node objects from the given JSON and textures.
*
* @param {Object} json - The JSON definition
* @param {Object<String,Texture>} textures - The texture library.
* @return {Object<String,NodeMaterial>}. The parsed materials.
*/
parseMaterials( json, textures ) {
const materials = {};
if ( json !== undefined ) {
const nodes = this.parseNodes( this._nodesJSON, textures );
const loader = new NodeMaterialLoader();
loader.setTextures( textures );
loader.setNodes( nodes );
loader.setNodeMaterials( this.nodeMaterials );
for ( let i = 0, l = json.length; i < l; i ++ ) {
const data = json[ i ];
materials[ data.uuid ] = loader.parse( data );
}
}
return materials;
}
}
export default NodeObjectLoader;