Loader.js 8.4 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

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
Loader.Handlers = {

	handlers: [],

	add: function ( regex, loader ) {

		this.handlers.push( regex, loader );

	},

	get: function ( file ) {

		var handlers = this.handlers;

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

			var regex = handlers[ i ];
			var loader = handlers[ i + 1 ];

			if ( regex.test( file ) ) {

				return loader;

			}

		}

		return null;

	}

};

Object.assign( Loader.prototype, {
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

	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 () {

100 101 102 103 104 105 106 107 108
		var BlendingMode = {
			NoBlending: NoBlending,
			NormalBlending: NormalBlending,
			AdditiveBlending: AdditiveBlending,
			SubtractiveBlending: SubtractiveBlending,
			MultiplyBlending: MultiplyBlending,
			CustomBlending: CustomBlending
		};

109 110 111
		var color = new Color();
		var textureLoader = new TextureLoader();
		var materialLoader = new MaterialLoader();
112

W
WestLangley 已提交
113
		return function createMaterial( m, texturePath, crossOrigin ) {
114

115
			// convert from old material format
116

117
			var textures = {};
118

119
			function loadTexture( path, repeat, offset, wrap, anisotropy ) {
120

121
				var fullPath = texturePath + path;
R
Rich Harris 已提交
122
				var loader = Loader.Handlers.get( fullPath );
123 124 125 126 127 128 129 130 131

				var texture;

				if ( loader !== null ) {

					texture = loader.load( fullPath );

				} else {

132 133
					textureLoader.setCrossOrigin( crossOrigin );
					texture = textureLoader.load( fullPath );
134 135 136

				}

137
				if ( repeat !== undefined ) {
138

M
Mr.doob 已提交
139
					texture.repeat.fromArray( repeat );
140

R
Rich Harris 已提交
141 142
					if ( repeat[ 0 ] !== 1 ) texture.wrapS = RepeatWrapping;
					if ( repeat[ 1 ] !== 1 ) texture.wrapT = RepeatWrapping;
143 144 145

				}

146
				if ( offset !== undefined ) {
147

M
Mr.doob 已提交
148
					texture.offset.fromArray( offset );
149 150 151

				}

152
				if ( wrap !== undefined ) {
153

R
Rich Harris 已提交
154 155
					if ( wrap[ 0 ] === 'repeat' ) texture.wrapS = RepeatWrapping;
					if ( wrap[ 0 ] === 'mirror' ) texture.wrapS = MirroredRepeatWrapping;
156

R
Rich Harris 已提交
157 158
					if ( wrap[ 1 ] === 'repeat' ) texture.wrapT = RepeatWrapping;
					if ( wrap[ 1 ] === 'mirror' ) texture.wrapT = MirroredRepeatWrapping;
159 160 161

				}

162
				if ( anisotropy !== undefined ) {
163 164 165 166 167

					texture.anisotropy = anisotropy;

				}

R
Rich Harris 已提交
168
				var uuid = _Math.generateUUID();
169

170
				textures[ uuid ] = texture;
171

172
				return uuid;
173 174 175 176 177

			}

			//

178
			var json = {
R
Rich Harris 已提交
179
				uuid: _Math.generateUUID(),
180 181 182 183 184 185 186 187
				type: 'MeshLambertMaterial'
			};

			for ( var name in m ) {

				var value = m[ name ];

				switch ( name ) {
188

189 190 191 192 193 194 195 196 197
					case 'DbgColor':
					case 'DbgIndex':
					case 'opticalDensity':
					case 'illumination':
						break;
					case 'DbgName':
						json.name = value;
						break;
					case 'blending':
198
						json.blending = BlendingMode[ value ];
199
						break;
200
					case 'colorAmbient':
201 202
					case 'mapAmbient':
						console.warn( 'THREE.Loader.createMaterial:', name, 'is no longer supported.' );
203
						break;
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
					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 已提交
219
						if ( value.toLowerCase() === 'standard' ) json.type = 'MeshStandardMaterial';
220 221
						break;
					case 'mapDiffuse':
M
Mr.doob 已提交
222
						json.map = loadTexture( value, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
223 224 225 226 227 228
						break;
					case 'mapDiffuseRepeat':
					case 'mapDiffuseOffset':
					case 'mapDiffuseWrap':
					case 'mapDiffuseAnisotropy':
						break;
229 230 231 232 233 234 235 236
					case 'mapEmissive':
						json.emissiveMap = loadTexture( value, m.mapEmissiveRepeat, m.mapEmissiveOffset, m.mapEmissiveWrap, m.mapEmissiveAnisotropy );
						break;
					case 'mapEmissiveRepeat':
					case 'mapEmissiveOffset':
					case 'mapEmissiveWrap':
					case 'mapEmissiveAnisotropy':
						break;
237
					case 'mapLight':
M
Mr.doob 已提交
238
						json.lightMap = loadTexture( value, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
239 240 241 242 243 244 245
						break;
					case 'mapLightRepeat':
					case 'mapLightOffset':
					case 'mapLightWrap':
					case 'mapLightAnisotropy':
						break;
					case 'mapAO':
M
Mr.doob 已提交
246
						json.aoMap = loadTexture( value, m.mapAORepeat, m.mapAOOffset, m.mapAOWrap, m.mapAOAnisotropy );
247 248 249 250 251 252 253
						break;
					case 'mapAORepeat':
					case 'mapAOOffset':
					case 'mapAOWrap':
					case 'mapAOAnisotropy':
						break;
					case 'mapBump':
M
Mr.doob 已提交
254
						json.bumpMap = loadTexture( value, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
255 256 257 258 259 260 261 262 263 264
						break;
					case 'mapBumpScale':
						json.bumpScale = value;
						break;
					case 'mapBumpRepeat':
					case 'mapBumpOffset':
					case 'mapBumpWrap':
					case 'mapBumpAnisotropy':
						break;
					case 'mapNormal':
M
Mr.doob 已提交
265
						json.normalMap = loadTexture( value, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
266 267 268 269 270 271 272 273 274 275
						break;
					case 'mapNormalFactor':
						json.normalScale = [ value, value ];
						break;
					case 'mapNormalRepeat':
					case 'mapNormalOffset':
					case 'mapNormalWrap':
					case 'mapNormalAnisotropy':
						break;
					case 'mapSpecular':
M
Mr.doob 已提交
276
						json.specularMap = loadTexture( value, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
277 278 279 280 281 282
						break;
					case 'mapSpecularRepeat':
					case 'mapSpecularOffset':
					case 'mapSpecularWrap':
					case 'mapSpecularAnisotropy':
						break;
A
Alex Goldring 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
					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;
299
					case 'mapAlpha':
M
Mr.doob 已提交
300
						json.alphaMap = loadTexture( value, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
301 302 303 304 305 306 307
						break;
					case 'mapAlphaRepeat':
					case 'mapAlphaOffset':
					case 'mapAlphaWrap':
					case 'mapAlphaAnisotropy':
						break;
					case 'flipSided':
R
Rich Harris 已提交
308
						json.side = BackSide;
309 310
						break;
					case 'doubleSided':
R
Rich Harris 已提交
311
						json.side = DoubleSide;
312 313
						break;
					case 'transparency':
314
						console.warn( 'THREE.Loader.createMaterial: transparency has been renamed to opacity' );
315 316 317 318
						json.opacity = value;
						break;
					case 'depthTest':
					case 'depthWrite':
M
Mugen87 已提交
319
					case 'colorWrite':
M
Mr.doob 已提交
320 321 322
					case 'opacity':
					case 'reflectivity':
					case 'transparent':
323 324 325 326 327
					case 'visible':
					case 'wireframe':
						json[ name ] = value;
						break;
					case 'vertexColors':
R
Rich Harris 已提交
328 329
						if ( value === true ) json.vertexColors = VertexColors;
						if ( value === 'face' ) json.vertexColors = FaceColors;
330 331
						break;
					default:
332
						console.error( 'THREE.Loader.createMaterial: Unsupported', name, value );
333
						break;
334

335
				}
336 337 338

			}

339
			if ( json.type === 'MeshBasicMaterial' ) delete json.emissive;
340
			if ( json.type !== 'MeshPhongMaterial' ) delete json.specular;
341

342
			if ( json.opacity < 1 ) json.transparent = true;
343

344
			materialLoader.setTextures( textures );
345

346
			return materialLoader.parse( json );
347 348 349 350 351

		};

	} )()

352
} );
R
Rich Harris 已提交
353 354


355
export { Loader };