Loader.js 8.1 KB
Newer Older
R
Rich Harris 已提交
1 2 3 4 5 6
import { FaceColors, VertexColors, DoubleSide, BackSide, MirroredRepeatWrapping, RepeatWrapping } from '../constants';
import { _Math } from '../math/Math';
import { MaterialLoader } from './MaterialLoader';
import { TextureLoader } from './TextureLoader';
import { Color } from '../math/Color';

7 8 9 10
/**
 * @author alteredq / http://alteredqualia.com/
 */

M
Mr.doob 已提交
11
function Loader() {
12 13 14 15 16

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

M
Mr.doob 已提交
17
}
18

R
Rich Harris 已提交
19
Loader.prototype = {
20

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

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

53
		var color, textureLoader, materialLoader;
54

W
WestLangley 已提交
55
		return function createMaterial( m, texturePath, crossOrigin ) {
56

R
Rich Harris 已提交
57 58 59
			if ( color === undefined ) color = new Color();
			if ( textureLoader === undefined ) textureLoader = new TextureLoader();
			if ( materialLoader === undefined ) materialLoader = new MaterialLoader();
60

61
			// convert from old material format
62

63
			var textures = {};
64

65
			function loadTexture( path, repeat, offset, wrap, anisotropy ) {
66

67
				var fullPath = texturePath + path;
R
Rich Harris 已提交
68
				var loader = Loader.Handlers.get( fullPath );
69 70 71 72 73 74 75 76 77

				var texture;

				if ( loader !== null ) {

					texture = loader.load( fullPath );

				} else {

78 79
					textureLoader.setCrossOrigin( crossOrigin );
					texture = textureLoader.load( fullPath );
80 81 82

				}

83
				if ( repeat !== undefined ) {
84

M
Mr.doob 已提交
85
					texture.repeat.fromArray( repeat );
86

R
Rich Harris 已提交
87 88
					if ( repeat[ 0 ] !== 1 ) texture.wrapS = RepeatWrapping;
					if ( repeat[ 1 ] !== 1 ) texture.wrapT = RepeatWrapping;
89 90 91

				}

92
				if ( offset !== undefined ) {
93

M
Mr.doob 已提交
94
					texture.offset.fromArray( offset );
95 96 97

				}

98
				if ( wrap !== undefined ) {
99

R
Rich Harris 已提交
100 101
					if ( wrap[ 0 ] === 'repeat' ) texture.wrapS = RepeatWrapping;
					if ( wrap[ 0 ] === 'mirror' ) texture.wrapS = MirroredRepeatWrapping;
102

R
Rich Harris 已提交
103 104
					if ( wrap[ 1 ] === 'repeat' ) texture.wrapT = RepeatWrapping;
					if ( wrap[ 1 ] === 'mirror' ) texture.wrapT = MirroredRepeatWrapping;
105 106 107

				}

108
				if ( anisotropy !== undefined ) {
109 110 111 112 113

					texture.anisotropy = anisotropy;

				}

R
Rich Harris 已提交
114
				var uuid = _Math.generateUUID();
115

116
				textures[ uuid ] = texture;
117

118
				return uuid;
119 120 121 122 123

			}

			//

124
			var json = {
R
Rich Harris 已提交
125
				uuid: _Math.generateUUID(),
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
				type: 'MeshLambertMaterial'
			};

			for ( var name in m ) {

				var value = m[ name ];

				switch ( name ) {
					case 'DbgColor':
					case 'DbgIndex':
					case 'opticalDensity':
					case 'illumination':
						break;
					case 'DbgName':
						json.name = value;
						break;
					case 'blending':
						json.blending = THREE[ value ];
						break;
145
					case 'colorAmbient':
146 147
					case 'mapAmbient':
						console.warn( 'THREE.Loader.createMaterial:', name, 'is no longer supported.' );
148
						break;
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
					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 已提交
164
						if ( value.toLowerCase() === 'standard' ) json.type = 'MeshStandardMaterial';
165 166
						break;
					case 'mapDiffuse':
M
Mr.doob 已提交
167
						json.map = loadTexture( value, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
168 169 170 171 172 173
						break;
					case 'mapDiffuseRepeat':
					case 'mapDiffuseOffset':
					case 'mapDiffuseWrap':
					case 'mapDiffuseAnisotropy':
						break;
174 175 176 177 178 179 180 181
					case 'mapEmissive':
						json.emissiveMap = loadTexture( value, m.mapEmissiveRepeat, m.mapEmissiveOffset, m.mapEmissiveWrap, m.mapEmissiveAnisotropy );
						break;
					case 'mapEmissiveRepeat':
					case 'mapEmissiveOffset':
					case 'mapEmissiveWrap':
					case 'mapEmissiveAnisotropy':
						break;
182
					case 'mapLight':
M
Mr.doob 已提交
183
						json.lightMap = loadTexture( value, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
184 185 186 187 188 189 190
						break;
					case 'mapLightRepeat':
					case 'mapLightOffset':
					case 'mapLightWrap':
					case 'mapLightAnisotropy':
						break;
					case 'mapAO':
M
Mr.doob 已提交
191
						json.aoMap = loadTexture( value, m.mapAORepeat, m.mapAOOffset, m.mapAOWrap, m.mapAOAnisotropy );
192 193 194 195 196 197 198
						break;
					case 'mapAORepeat':
					case 'mapAOOffset':
					case 'mapAOWrap':
					case 'mapAOAnisotropy':
						break;
					case 'mapBump':
M
Mr.doob 已提交
199
						json.bumpMap = loadTexture( value, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
200 201 202 203 204 205 206 207 208 209
						break;
					case 'mapBumpScale':
						json.bumpScale = value;
						break;
					case 'mapBumpRepeat':
					case 'mapBumpOffset':
					case 'mapBumpWrap':
					case 'mapBumpAnisotropy':
						break;
					case 'mapNormal':
M
Mr.doob 已提交
210
						json.normalMap = loadTexture( value, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
211 212 213 214 215 216 217 218 219 220
						break;
					case 'mapNormalFactor':
						json.normalScale = [ value, value ];
						break;
					case 'mapNormalRepeat':
					case 'mapNormalOffset':
					case 'mapNormalWrap':
					case 'mapNormalAnisotropy':
						break;
					case 'mapSpecular':
M
Mr.doob 已提交
221
						json.specularMap = loadTexture( value, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
222 223 224 225 226 227
						break;
					case 'mapSpecularRepeat':
					case 'mapSpecularOffset':
					case 'mapSpecularWrap':
					case 'mapSpecularAnisotropy':
						break;
A
Alex Goldring 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
					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;
244
					case 'mapAlpha':
M
Mr.doob 已提交
245
						json.alphaMap = loadTexture( value, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
246 247 248 249 250 251 252
						break;
					case 'mapAlphaRepeat':
					case 'mapAlphaOffset':
					case 'mapAlphaWrap':
					case 'mapAlphaAnisotropy':
						break;
					case 'flipSided':
R
Rich Harris 已提交
253
						json.side = BackSide;
254 255
						break;
					case 'doubleSided':
R
Rich Harris 已提交
256
						json.side = DoubleSide;
257 258
						break;
					case 'transparency':
259
						console.warn( 'THREE.Loader.createMaterial: transparency has been renamed to opacity' );
260 261 262 263
						json.opacity = value;
						break;
					case 'depthTest':
					case 'depthWrite':
M
Mugen87 已提交
264
					case 'colorWrite':
M
Mr.doob 已提交
265 266 267
					case 'opacity':
					case 'reflectivity':
					case 'transparent':
268 269 270 271 272
					case 'visible':
					case 'wireframe':
						json[ name ] = value;
						break;
					case 'vertexColors':
R
Rich Harris 已提交
273 274
						if ( value === true ) json.vertexColors = VertexColors;
						if ( value === 'face' ) json.vertexColors = FaceColors;
275 276
						break;
					default:
277
						console.error( 'THREE.Loader.createMaterial: Unsupported', name, value );
278 279
						break;
				}
280 281 282

			}

283
			if ( json.type === 'MeshBasicMaterial' ) delete json.emissive;
284
			if ( json.type !== 'MeshPhongMaterial' ) delete json.specular;
285

286
			if ( json.opacity < 1 ) json.transparent = true;
287

288
			materialLoader.setTextures( textures );
289

290
			return materialLoader.parse( json );
291 292 293 294 295 296 297

		};

	} )()

};

R
Rich Harris 已提交
298
Loader.Handlers = {
299 300 301 302 303 304 305 306 307 308 309

	handlers: [],

	add: function ( regex, loader ) {

		this.handlers.push( regex, loader );

	},

	get: function ( file ) {

310 311 312
		var handlers = this.handlers;

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

314 315
			var regex = handlers[ i ];
			var loader  = handlers[ i + 1 ];
316 317 318 319 320 321 322 323 324 325 326 327 328 329

			if ( regex.test( file ) ) {

				return loader;

			}

		}

		return null;

	}

};
R
Rich Harris 已提交
330 331


332
export { Loader };