UNPKG

6.8 kBJavaScriptView Raw
1import { Vector2 } from '../math/Vector2.js';
2import { FileLoader } from './FileLoader.js';
3import { DefaultLoadingManager } from './LoadingManager.js';
4import * as Materials from '../materials/Materials.js';
5
6/**
7 * @author mrdoob / http://mrdoob.com/
8 */
9
10function MaterialLoader( manager ) {
11
12 this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
13 this.textures = {};
14
15}
16
17Object.assign( MaterialLoader.prototype, {
18
19 load: function ( url, onLoad, onProgress, onError ) {
20
21 var scope = this;
22
23 var loader = new FileLoader( scope.manager );
24 loader.load( url, function ( text ) {
25
26 onLoad( scope.parse( JSON.parse( text ) ) );
27
28 }, onProgress, onError );
29
30 },
31
32 setTextures: function ( value ) {
33
34 this.textures = value;
35
36 },
37
38 parse: function ( json ) {
39
40 var textures = this.textures;
41
42 function getTexture( name ) {
43
44 if ( textures[ name ] === undefined ) {
45
46 console.warn( 'THREE.MaterialLoader: Undefined texture', name );
47
48 }
49
50 return textures[ name ];
51
52 }
53
54 var material = new Materials[ json.type ]();
55
56 if ( json.uuid !== undefined ) material.uuid = json.uuid;
57 if ( json.name !== undefined ) material.name = json.name;
58 if ( json.color !== undefined ) material.color.setHex( json.color );
59 if ( json.roughness !== undefined ) material.roughness = json.roughness;
60 if ( json.metalness !== undefined ) material.metalness = json.metalness;
61 if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
62 if ( json.specular !== undefined ) material.specular.setHex( json.specular );
63 if ( json.shininess !== undefined ) material.shininess = json.shininess;
64 if ( json.clearCoat !== undefined ) material.clearCoat = json.clearCoat;
65 if ( json.clearCoatRoughness !== undefined ) material.clearCoatRoughness = json.clearCoatRoughness;
66 if ( json.uniforms !== undefined ) material.uniforms = json.uniforms;
67 if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
68 if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
69 if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
70 if ( json.fog !== undefined ) material.fog = json.fog;
71 if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
72 if ( json.blending !== undefined ) material.blending = json.blending;
73 if ( json.side !== undefined ) material.side = json.side;
74 if ( json.opacity !== undefined ) material.opacity = json.opacity;
75 if ( json.transparent !== undefined ) material.transparent = json.transparent;
76 if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
77 if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
78 if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
79 if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
80 if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
81 if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
82 if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
83 if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
84
85 if ( json.rotation !== undefined ) material.rotation = json.rotation;
86
87 if ( json.linewidth !== 1 ) material.linewidth = json.linewidth;
88 if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
89 if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
90 if ( json.scale !== undefined ) material.scale = json.scale;
91
92 if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
93 if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
94 if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
95
96 if ( json.skinning !== undefined ) material.skinning = json.skinning;
97 if ( json.morphTargets !== undefined ) material.morphTargets = json.morphTargets;
98 if ( json.dithering !== undefined ) material.dithering = json.dithering;
99
100 if ( json.visible !== undefined ) material.visible = json.visible;
101 if ( json.userData !== undefined ) material.userData = json.userData;
102
103 // Deprecated
104
105 if ( json.shading !== undefined ) material.flatShading = json.shading === 1; // THREE.FlatShading
106
107 // for PointsMaterial
108
109 if ( json.size !== undefined ) material.size = json.size;
110 if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
111
112 // maps
113
114 if ( json.map !== undefined ) material.map = getTexture( json.map );
115
116 if ( json.alphaMap !== undefined ) {
117
118 material.alphaMap = getTexture( json.alphaMap );
119 material.transparent = true;
120
121 }
122
123 if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
124 if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
125
126 if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
127 if ( json.normalScale !== undefined ) {
128
129 var normalScale = json.normalScale;
130
131 if ( Array.isArray( normalScale ) === false ) {
132
133 // Blender exporter used to export a scalar. See #7459
134
135 normalScale = [ normalScale, normalScale ];
136
137 }
138
139 material.normalScale = new Vector2().fromArray( normalScale );
140
141 }
142
143 if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
144 if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
145 if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
146
147 if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
148 if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
149
150 if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
151 if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
152
153 if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
154
155 if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
156
157 if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
158
159 if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
160 if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
161
162 if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
163 if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
164
165 if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
166
167 return material;
168
169 }
170
171} );
172
173
174export { MaterialLoader };