MaterialLoader.js 5.3 KB
Newer Older
R
Rich Harris 已提交
1 2 3 4 5
import { MultiplyOperation } from '../constants';
import { Vector2 } from '../math/Vector2';
import { XHRLoader } from './XHRLoader';
import { DefaultLoadingManager } from './LoadingManager';

M
Mr.doob 已提交
6 7 8 9
/**
 * @author mrdoob / http://mrdoob.com/
 */

R
Rich Harris 已提交
10 11
function MaterialLoader ( manager ) {
	this.isMaterialLoader = true;
M
Mr.doob 已提交
12

R
Rich Harris 已提交
13
	this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
M
Mr.doob 已提交
14 15 16 17
	this.textures = {};

};

R
Rich Harris 已提交
18
Object.assign( MaterialLoader.prototype, {
M
Mr.doob 已提交
19 20 21 22 23

	load: function ( url, onLoad, onProgress, onError ) {

		var scope = this;

R
Rich Harris 已提交
24
		var loader = new XHRLoader( scope.manager );
M
Mr.doob 已提交
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
		loader.load( url, function ( text ) {

			onLoad( scope.parse( JSON.parse( text ) ) );

		}, onProgress, onError );

	},

	setTextures: function ( value ) {

		this.textures = value;

	},

	getTexture: function ( name ) {

		var textures = this.textures;

		if ( textures[ name ] === undefined ) {

			console.warn( 'THREE.MaterialLoader: Undefined texture', name );

		}

		return textures[ name ];

	},

	parse: function ( json ) {

		var material = new THREE[ json.type ];

		if ( json.uuid !== undefined ) material.uuid = json.uuid;
		if ( json.name !== undefined ) material.name = json.name;
		if ( json.color !== undefined ) material.color.setHex( json.color );
		if ( json.roughness !== undefined ) material.roughness = json.roughness;
		if ( json.metalness !== undefined ) material.metalness = json.metalness;
		if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
		if ( json.specular !== undefined ) material.specular.setHex( json.specular );
		if ( json.shininess !== undefined ) material.shininess = json.shininess;
		if ( json.uniforms !== undefined ) material.uniforms = json.uniforms;
		if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
		if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
		if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
		if ( json.shading !== undefined ) material.shading = json.shading;
		if ( json.blending !== undefined ) material.blending = json.blending;
		if ( json.side !== undefined ) material.side = json.side;
		if ( json.opacity !== undefined ) material.opacity = json.opacity;
		if ( json.transparent !== undefined ) material.transparent = json.transparent;
		if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
		if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
		if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
M
Mugen87 已提交
77
		if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
M
Mr.doob 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
		if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
		if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;

		// for PointsMaterial
		if ( json.size !== undefined ) material.size = json.size;
		if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;

		// maps

		if ( json.map !== undefined ) material.map = this.getTexture( json.map );

		if ( json.alphaMap !== undefined ) {

			material.alphaMap = this.getTexture( json.alphaMap );
			material.transparent = true;

		}

		if ( json.bumpMap !== undefined ) material.bumpMap = this.getTexture( json.bumpMap );
		if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;

		if ( json.normalMap !== undefined ) material.normalMap = this.getTexture( json.normalMap );
		if ( json.normalScale !== undefined ) {

			var normalScale = json.normalScale;

			if ( Array.isArray( normalScale ) === false ) {

				// Blender exporter used to export a scalar. See #7459

				normalScale = [ normalScale, normalScale ];

			}

R
Rich Harris 已提交
112
			material.normalScale = new Vector2().fromArray( normalScale );
M
Mr.doob 已提交
113 114 115 116 117 118 119 120 121 122 123

		}

		if ( json.displacementMap !== undefined ) material.displacementMap = this.getTexture( json.displacementMap );
		if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
		if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;

		if ( json.roughnessMap !== undefined ) material.roughnessMap = this.getTexture( json.roughnessMap );
		if ( json.metalnessMap !== undefined ) material.metalnessMap = this.getTexture( json.metalnessMap );

		if ( json.emissiveMap !== undefined ) material.emissiveMap = this.getTexture( json.emissiveMap );
124
		if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
W
WestLangley 已提交
125

M
Mr.doob 已提交
126 127 128 129 130
		if ( json.specularMap !== undefined ) material.specularMap = this.getTexture( json.specularMap );

		if ( json.envMap !== undefined ) {

			material.envMap = this.getTexture( json.envMap );
R
Rich Harris 已提交
131
			material.combine = MultiplyOperation;
M
Mr.doob 已提交
132 133 134

		}

135
		if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
M
Mr.doob 已提交
136 137 138 139 140 141 142

		if ( json.lightMap !== undefined ) material.lightMap = this.getTexture( json.lightMap );
		if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;

		if ( json.aoMap !== undefined ) material.aoMap = this.getTexture( json.aoMap );
		if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;

M
Mugen87 已提交
143
		// MultiMaterial
M
Mr.doob 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

		if ( json.materials !== undefined ) {

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

				material.materials.push( this.parse( json.materials[ i ] ) );

			}

		}

		return material;

	}

M
Mr.doob 已提交
159
} );
R
Rich Harris 已提交
160 161 162


export { MaterialLoader };