Skip to content

Commit a38644b

Browse files
committed
ObjectLoader: Moved material parsing logic to MaterialLoader.
1 parent 72f1fef commit a38644b

File tree

2 files changed

+68
-59
lines changed

2 files changed

+68
-59
lines changed

src/loaders/MaterialLoader.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
THREE.MaterialLoader = function ( manager ) {
66

77
this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
8+
this.textures = {};
89

910
};
1011

@@ -32,10 +33,32 @@ THREE.MaterialLoader.prototype = {
3233

3334
},
3435

36+
setTextures: function ( value ) {
37+
38+
this.textures = value;
39+
40+
},
41+
42+
getTexture: function ( name ) {
43+
44+
var textures = this.textures;
45+
46+
if ( textures[ name ] === undefined ) {
47+
48+
console.warn( 'THREE.MaterialLoader: Undefined texture', name );
49+
50+
}
51+
52+
return textures[ name ];
53+
54+
},
55+
3556
parse: function ( json ) {
3657

3758
var material = new THREE[ json.type ];
59+
material.uuid = json.uuid;
3860

61+
if ( json.name !== undefined ) material.name = json.name;
3962
if ( json.color !== undefined ) material.color.setHex( json.color );
4063
if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
4164
if ( json.specular !== undefined ) material.specular.setHex( json.specular );
@@ -50,13 +73,55 @@ THREE.MaterialLoader.prototype = {
5073
if ( json.opacity !== undefined ) material.opacity = json.opacity;
5174
if ( json.transparent !== undefined ) material.transparent = json.transparent;
5275
if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
76+
if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
77+
if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
5378
if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
5479
if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
5580

5681
// for PointCloudMaterial
5782
if ( json.size !== undefined ) material.size = json.size;
5883
if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
5984

85+
// maps
86+
87+
if ( json.map !== undefined ) material.map = this.getTexture( json.map );
88+
89+
if ( json.alphaMap !== undefined ) {
90+
91+
material.alphaMap = this.getTexture( json.alphaMap );
92+
material.transparent = true;
93+
94+
}
95+
96+
if ( json.bumpMap !== undefined ) material.bumpMap = this.getTexture( json.bumpMap );
97+
if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
98+
99+
if ( json.normalMap !== undefined ) material.normalMap = this.getTexture( json.normalMap );
100+
if ( json.normalScale ) material.normalScale = new THREE.Vector2( json.normalScale, json.normalScale );
101+
102+
if ( json.displacementMap !== undefined ) material.displacementMap = this.getTexture( json.displacementMap );
103+
if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
104+
if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
105+
106+
if ( json.specularMap !== undefined ) material.specularMap = this.getTexture( json.specularMap );
107+
108+
if ( json.envMap !== undefined ) {
109+
110+
material.envMap = this.getTexture( json.envMap );
111+
material.combine = THREE.MultiplyOperation;
112+
113+
}
114+
115+
if ( json.reflectivity ) material.reflectivity = json.reflectivity;
116+
117+
if ( json.lightMap !== undefined ) material.lightMap = this.getTexture( json.lightMap );
118+
if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
119+
120+
if ( json.aoMap !== undefined ) material.aoMap = this.getTexture( json.aoMap );
121+
if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
122+
123+
// MeshFaceMaterial
124+
60125
if ( json.materials !== undefined ) {
61126

62127
for ( var i = 0, l = json.materials.length; i < l; i ++ ) {

src/loaders/ObjectLoader.js

Lines changed: 3 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -300,69 +300,13 @@ THREE.ObjectLoader.prototype = {
300300

301301
if ( json !== undefined ) {
302302

303-
var getTexture = function ( name ) {
304-
305-
if ( textures[ name ] === undefined ) {
306-
307-
console.warn( 'THREE.ObjectLoader: Undefined texture', name );
308-
309-
}
310-
311-
return textures[ name ];
312-
313-
};
314-
315303
var loader = new THREE.MaterialLoader();
304+
loader.setTextures( textures );
316305

317306
for ( var i = 0, l = json.length; i < l; i ++ ) {
318307

319-
var data = json[ i ];
320-
var material = loader.parse( data );
321-
322-
material.uuid = data.uuid;
323-
324-
if ( data.depthTest !== undefined ) material.depthTest = data.depthTest;
325-
if ( data.depthWrite !== undefined ) material.depthWrite = data.depthWrite;
326-
327-
if ( data.name !== undefined ) material.name = data.name;
328-
329-
if ( data.map !== undefined ) material.map = getTexture( data.map );
330-
331-
if ( data.alphaMap !== undefined ) {
332-
333-
material.alphaMap = getTexture( data.alphaMap );
334-
material.transparent = true;
335-
336-
}
337-
338-
if ( data.bumpMap !== undefined ) material.bumpMap = getTexture( data.bumpMap );
339-
if ( data.bumpScale !== undefined ) material.bumpScale = data.bumpScale;
340-
341-
if ( data.normalMap !== undefined ) material.normalMap = getTexture( data.normalMap );
342-
if ( data.normalScale ) material.normalScale = new THREE.Vector2( data.normalScale, data.normalScale );
343-
344-
if ( data.displacementMap !== undefined ) material.displacementMap = getTexture( data.displacementMap );
345-
if ( data.displacementScale !== undefined ) material.displacementScale = data.displacementScale;
346-
if ( data.displacementBias !== undefined ) material.displacementBias = data.displacementBias;
347-
348-
if ( data.specularMap !== undefined ) material.specularMap = getTexture( data.specularMap );
349-
350-
if ( data.envMap !== undefined ) {
351-
352-
material.envMap = getTexture( data.envMap );
353-
material.combine = THREE.MultiplyOperation;
354-
355-
}
356-
357-
if ( data.reflectivity ) material.reflectivity = data.reflectivity;
358-
359-
if ( data.lightMap !== undefined ) material.lightMap = getTexture( data.lightMap );
360-
if ( data.lightMapIntensity !== undefined ) material.lightMapIntensity = data.lightMapIntensity;
361-
362-
if ( data.aoMap !== undefined ) material.aoMap = getTexture( data.aoMap );
363-
if ( data.aoMapIntensity !== undefined ) material.aoMapIntensity = data.aoMapIntensity;
364-
365-
materials[ data.uuid ] = material;
308+
var material = loader.parse( json[ i ] );
309+
materials[ material.uuid ] = material;
366310

367311
}
368312

0 commit comments

Comments
 (0)