Loader.js 8.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import {
	NoBlending,
	NormalBlending,
	AdditiveBlending,
	SubtractiveBlending,
	MultiplyBlending,
	CustomBlending,

	FaceColors,
	VertexColors,

	DoubleSide,
	BackSide,

	MirroredRepeatWrapping,
	RepeatWrapping
} from '../constants';
R
Rich Harris 已提交
18 19 20 21 22
import { _Math } from '../math/Math';
import { MaterialLoader } from './MaterialLoader';
import { TextureLoader } from './TextureLoader';
import { Color } from '../math/Color';

23 24 25 26
/**
 * @author alteredq / http://alteredqualia.com/
 */

M
Mr.doob 已提交
27
function Loader() {
28 29 30 31 32

	this.onLoadStart = function () {};
	this.onLoadProgress = function () {};
	this.onLoadComplete = function () {};

M
Mr.doob 已提交
33
}
34

R
Rich Harris 已提交
35
Loader.prototype = {
36

R
Rich Harris 已提交
37
	constructor: Loader,
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

	crossOrigin: undefined,

	extractUrlBase: function ( url ) {

		var parts = url.split( '/' );

		if ( parts.length === 1 ) return './';

		parts.pop();

		return parts.join( '/' ) + '/';

	},

	initMaterials: function ( materials, texturePath, crossOrigin ) {

		var array = [];

		for ( var i = 0; i < materials.length; ++ i ) {

			array[ i ] = this.createMaterial( materials[ i ], texturePath, crossOrigin );

		}

		return array;

	},

	createMaterial: ( function () {

69 70 71 72 73 74 75 76 77
		var BlendingMode = {
			NoBlending: NoBlending,
			NormalBlending: NormalBlending,
			AdditiveBlending: AdditiveBlending,
			SubtractiveBlending: SubtractiveBlending,
			MultiplyBlending: MultiplyBlending,
			CustomBlending: CustomBlending
		};

78
		var color, textureLoader, materialLoader;
79

W
WestLangley 已提交
80
		return function createMaterial( m, texturePath, crossOrigin ) {
81

R
Rich Harris 已提交
82 83 84
			if ( color === undefined ) color = new Color();
			if ( textureLoader === undefined ) textureLoader = new TextureLoader();
			if ( materialLoader === undefined ) materialLoader = new MaterialLoader();
85

86
			// convert from old material format
87

88
			var textures = {};
89

90
			function loadTexture( path, repeat, offset, wrap, anisotropy ) {
91

92
				var fullPath = texturePath + path;
R
Rich Harris 已提交
93
				var loader = Loader.Handlers.get( fullPath );
94 95 96 97 98 99 100 101 102

				var texture;

				if ( loader !== null ) {

					texture = loader.load( fullPath );

				} else {

103 104
					textureLoader.setCrossOrigin( crossOrigin );
					texture = textureLoader.load( fullPath );
105 106 107

				}

108
				if ( repeat !== undefined ) {
109

M
Mr.doob 已提交
110
					texture.repeat.fromArray( repeat );
111

R
Rich Harris 已提交
112 113
					if ( repeat[ 0 ] !== 1 ) texture.wrapS = RepeatWrapping;
					if ( repeat[ 1 ] !== 1 ) texture.wrapT = RepeatWrapping;
114 115 116

				}

117
				if ( offset !== undefined ) {
118

M
Mr.doob 已提交
119
					texture.offset.fromArray( offset );
120 121 122

				}

123
				if ( wrap !== undefined ) {
124

R
Rich Harris 已提交
125 126
					if ( wrap[ 0 ] === 'repeat' ) texture.wrapS = RepeatWrapping;
					if ( wrap[ 0 ] === 'mirror' ) texture.wrapS = MirroredRepeatWrapping;
127

R
Rich Harris 已提交
128 129
					if ( wrap[ 1 ] === 'repeat' ) texture.wrapT = RepeatWrapping;
					if ( wrap[ 1 ] === 'mirror' ) texture.wrapT = MirroredRepeatWrapping;
130 131 132

				}

133
				if ( anisotropy !== undefined ) {
134 135 136 137 138

					texture.anisotropy = anisotropy;

				}

R
Rich Harris 已提交
139
				var uuid = _Math.generateUUID();
140

141
				textures[ uuid ] = texture;
142

143
				return uuid;
144 145 146 147 148

			}

			//

149
			var json = {
R
Rich Harris 已提交
150
				uuid: _Math.generateUUID(),
151 152 153 154 155 156 157 158
				type: 'MeshLambertMaterial'
			};

			for ( var name in m ) {

				var value = m[ name ];

				switch ( name ) {
159

160 161 162 163 164 165 166 167 168
					case 'DbgColor':
					case 'DbgIndex':
					case 'opticalDensity':
					case 'illumination':
						break;
					case 'DbgName':
						json.name = value;
						break;
					case 'blending':
169
						json.blending = BlendingMode[ value ];
170
						break;
171
					case 'colorAmbient':
172 173
					case 'mapAmbient':
						console.warn( 'THREE.Loader.createMaterial:', name, 'is no longer supported.' );
174
						break;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
					case 'colorDiffuse':
						json.color = color.fromArray( value ).getHex();
						break;
					case 'colorSpecular':
						json.specular = color.fromArray( value ).getHex();
						break;
					case 'colorEmissive':
						json.emissive = color.fromArray( value ).getHex();
						break;
					case 'specularCoef':
						json.shininess = value;
						break;
					case 'shading':
						if ( value.toLowerCase() === 'basic' ) json.type = 'MeshBasicMaterial';
						if ( value.toLowerCase() === 'phong' ) json.type = 'MeshPhongMaterial';
A
Alex Goldring 已提交
190
						if ( value.toLowerCase() === 'standard' ) json.type = 'MeshStandardMaterial';
191 192
						break;
					case 'mapDiffuse':
M
Mr.doob 已提交
193
						json.map = loadTexture( value, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
194 195 196 197 198 199
						break;
					case 'mapDiffuseRepeat':
					case 'mapDiffuseOffset':
					case 'mapDiffuseWrap':
					case 'mapDiffuseAnisotropy':
						break;
200 201 202 203 204 205 206 207
					case 'mapEmissive':
						json.emissiveMap = loadTexture( value, m.mapEmissiveRepeat, m.mapEmissiveOffset, m.mapEmissiveWrap, m.mapEmissiveAnisotropy );
						break;
					case 'mapEmissiveRepeat':
					case 'mapEmissiveOffset':
					case 'mapEmissiveWrap':
					case 'mapEmissiveAnisotropy':
						break;
208
					case 'mapLight':
M
Mr.doob 已提交
209
						json.lightMap = loadTexture( value, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
210 211 212 213 214 215 216
						break;
					case 'mapLightRepeat':
					case 'mapLightOffset':
					case 'mapLightWrap':
					case 'mapLightAnisotropy':
						break;
					case 'mapAO':
M
Mr.doob 已提交
217
						json.aoMap = loadTexture( value, m.mapAORepeat, m.mapAOOffset, m.mapAOWrap, m.mapAOAnisotropy );
218 219 220 221 222 223 224
						break;
					case 'mapAORepeat':
					case 'mapAOOffset':
					case 'mapAOWrap':
					case 'mapAOAnisotropy':
						break;
					case 'mapBump':
M
Mr.doob 已提交
225
						json.bumpMap = loadTexture( value, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
226 227 228 229 230 231 232 233 234 235
						break;
					case 'mapBumpScale':
						json.bumpScale = value;
						break;
					case 'mapBumpRepeat':
					case 'mapBumpOffset':
					case 'mapBumpWrap':
					case 'mapBumpAnisotropy':
						break;
					case 'mapNormal':
M
Mr.doob 已提交
236
						json.normalMap = loadTexture( value, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
237 238 239 240 241 242 243 244 245 246
						break;
					case 'mapNormalFactor':
						json.normalScale = [ value, value ];
						break;
					case 'mapNormalRepeat':
					case 'mapNormalOffset':
					case 'mapNormalWrap':
					case 'mapNormalAnisotropy':
						break;
					case 'mapSpecular':
M
Mr.doob 已提交
247
						json.specularMap = loadTexture( value, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
248 249 250 251 252 253
						break;
					case 'mapSpecularRepeat':
					case 'mapSpecularOffset':
					case 'mapSpecularWrap':
					case 'mapSpecularAnisotropy':
						break;
A
Alex Goldring 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
					case 'mapMetalness':
						json.metalnessMap = loadTexture( value, m.mapMetalnessRepeat, m.mapMetalnessOffset, m.mapMetalnessWrap, m.mapMetalnessAnisotropy );
						break;
					case 'mapMetalnessRepeat':
					case 'mapMetalnessOffset':
					case 'mapMetalnessWrap':
					case 'mapMetalnessAnisotropy':
						break;
					case 'mapRoughness':
						json.roughnessMap = loadTexture( value, m.mapRoughnessRepeat, m.mapRoughnessOffset, m.mapRoughnessWrap, m.mapRoughnessAnisotropy );
						break;
					case 'mapRoughnessRepeat':
					case 'mapRoughnessOffset':
					case 'mapRoughnessWrap':
					case 'mapRoughnessAnisotropy':
						break;
270
					case 'mapAlpha':
M
Mr.doob 已提交
271
						json.alphaMap = loadTexture( value, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
272 273 274 275 276 277 278
						break;
					case 'mapAlphaRepeat':
					case 'mapAlphaOffset':
					case 'mapAlphaWrap':
					case 'mapAlphaAnisotropy':
						break;
					case 'flipSided':
R
Rich Harris 已提交
279
						json.side = BackSide;
280 281
						break;
					case 'doubleSided':
R
Rich Harris 已提交
282
						json.side = DoubleSide;
283 284
						break;
					case 'transparency':
285
						console.warn( 'THREE.Loader.createMaterial: transparency has been renamed to opacity' );
286 287 288 289
						json.opacity = value;
						break;
					case 'depthTest':
					case 'depthWrite':
M
Mugen87 已提交
290
					case 'colorWrite':
M
Mr.doob 已提交
291 292 293
					case 'opacity':
					case 'reflectivity':
					case 'transparent':
294 295 296 297 298
					case 'visible':
					case 'wireframe':
						json[ name ] = value;
						break;
					case 'vertexColors':
R
Rich Harris 已提交
299 300
						if ( value === true ) json.vertexColors = VertexColors;
						if ( value === 'face' ) json.vertexColors = FaceColors;
301 302
						break;
					default:
303
						console.error( 'THREE.Loader.createMaterial: Unsupported', name, value );
304
						break;
305

306
				}
307 308 309

			}

310
			if ( json.type === 'MeshBasicMaterial' ) delete json.emissive;
311
			if ( json.type !== 'MeshPhongMaterial' ) delete json.specular;
312

313
			if ( json.opacity < 1 ) json.transparent = true;
314

315
			materialLoader.setTextures( textures );
316

317
			return materialLoader.parse( json );
318 319 320 321 322 323 324

		};

	} )()

};

R
Rich Harris 已提交
325
Loader.Handlers = {
326 327 328 329 330 331 332 333 334 335 336

	handlers: [],

	add: function ( regex, loader ) {

		this.handlers.push( regex, loader );

	},

	get: function ( file ) {

337 338 339
		var handlers = this.handlers;

		for ( var i = 0, l = handlers.length; i < l; i += 2 ) {
340

341
			var regex = handlers[ i ];
342
			var loader = handlers[ i + 1 ];
343 344 345 346 347 348 349 350 351 352 353 354 355 356

			if ( regex.test( file ) ) {

				return loader;

			}

		}

		return null;

	}

};
R
Rich Harris 已提交
357 358


359
export { Loader };