123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- /**
- * @author mrdoob / http://mrdoob.com/
- */
- THREE.MaterialLoader = function ( manager ) {
- this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
- this.textures = {};
- };
- Object.assign( THREE.MaterialLoader.prototype, {
- load: function ( url, onLoad, onProgress, onError ) {
- var scope = this;
- var loader = new THREE.XHRLoader( scope.manager );
- 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;
- if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
- 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 ];
- }
- material.normalScale = new THREE.Vector2().fromArray( normalScale );
- }
- 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 );
- if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
- if ( json.specularMap !== undefined ) material.specularMap = this.getTexture( json.specularMap );
- if ( json.envMap !== undefined ) {
- material.envMap = this.getTexture( json.envMap );
- material.combine = THREE.MultiplyOperation;
- }
- if ( json.reflectivity ) material.reflectivity = json.reflectivity;
- 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;
- // MultiMaterial
- 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;
- }
- } );
|