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

	FaceColors,
	VertexColors,

	DoubleSide,
	BackSide,

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

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

	crossOrigin: undefined,

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

88 89 90 91 92 93 94 95 96
		var BlendingMode = {
			NoBlending: NoBlending,
			NormalBlending: NormalBlending,
			AdditiveBlending: AdditiveBlending,
			SubtractiveBlending: SubtractiveBlending,
			MultiplyBlending: MultiplyBlending,
			CustomBlending: CustomBlending
		};

97 98 99
		var color = new Color();
		var textureLoader = new TextureLoader();
		var materialLoader = new MaterialLoader();
100

W
WestLangley 已提交
101
		return function createMaterial( m, texturePath, crossOrigin ) {
102

103
			// convert from old material format
104

105
			var textures = {};
106

107
			function loadTexture( path, repeat, offset, wrap, anisotropy ) {
108

109
				var fullPath = texturePath + path;
R
Rich Harris 已提交
110
				var loader = Loader.Handlers.get( fullPath );
111 112 113 114 115 116 117 118 119

				var texture;

				if ( loader !== null ) {

					texture = loader.load( fullPath );

				} else {

120 121
					textureLoader.setCrossOrigin( crossOrigin );
					texture = textureLoader.load( fullPath );
122 123 124

				}

125
				if ( repeat !== undefined ) {
126

M
Mr.doob 已提交
127
					texture.repeat.fromArray( repeat );
128

R
Rich Harris 已提交
129 130
					if ( repeat[ 0 ] !== 1 ) texture.wrapS = RepeatWrapping;
					if ( repeat[ 1 ] !== 1 ) texture.wrapT = RepeatWrapping;
131 132 133

				}

134
				if ( offset !== undefined ) {
135

M
Mr.doob 已提交
136
					texture.offset.fromArray( offset );
137 138 139

				}

140
				if ( wrap !== undefined ) {
141

R
Rich Harris 已提交
142 143
					if ( wrap[ 0 ] === 'repeat' ) texture.wrapS = RepeatWrapping;
					if ( wrap[ 0 ] === 'mirror' ) texture.wrapS = MirroredRepeatWrapping;
144

R
Rich Harris 已提交
145 146
					if ( wrap[ 1 ] === 'repeat' ) texture.wrapT = RepeatWrapping;
					if ( wrap[ 1 ] === 'mirror' ) texture.wrapT = MirroredRepeatWrapping;
147 148 149

				}

150
				if ( anisotropy !== undefined ) {
151 152 153 154 155

					texture.anisotropy = anisotropy;

				}

R
Rich Harris 已提交
156
				var uuid = _Math.generateUUID();
157

158
				textures[ uuid ] = texture;
159

160
				return uuid;
161 162 163 164 165

			}

			//

166
			var json = {
R
Rich Harris 已提交
167
				uuid: _Math.generateUUID(),
168 169 170 171 172 173 174 175
				type: 'MeshLambertMaterial'
			};

			for ( var name in m ) {

				var value = m[ name ];

				switch ( name ) {
176

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

323
				}
324 325 326

			}

327
			if ( json.type === 'MeshBasicMaterial' ) delete json.emissive;
328
			if ( json.type !== 'MeshPhongMaterial' ) delete json.specular;
329

330
			if ( json.opacity < 1 ) json.transparent = true;
331

332
			materialLoader.setTextures( textures );
333

334
			return materialLoader.parse( json );
335 336 337 338 339

		};

	} )()

340
} );
R
Rich Harris 已提交
341

342
export { Loader };