Browse Source

Unified *Material.toJSON

Mr.doob 10 years ago
parent
commit
df09504cc4

+ 32 - 2
src/materials/Material.js

@@ -123,9 +123,39 @@ THREE.Material.prototype = {
 		data.type = this.type;
 		if ( this.name !== '' ) data.name = this.name;
 
+		if ( this.color instanceof THREE.Color ) data.color = this.color.getHex();
+		if ( this.emissive instanceof THREE.Color ) data.emissive = this.emissive.getHex();
+		if ( this.specular instanceof THREE.Color ) data.specular = this.specular.getHex();
+		if ( this.shininess !== undefined ) data.shininess = this.shininess;
+
+		if ( this.map instanceof THREE.Texture ) data.map = this.map.toJSON( meta ).uuid;
+		if ( this.alphaMap instanceof THREE.Texture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
+		if ( this.lightMap instanceof THREE.Texture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
+		if ( this.bumpMap instanceof THREE.Texture ) {
+			data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
+			data.bumpScale = this.bumpScale;
+		}
+		if ( this.normalMap instanceof THREE.Texture ) {
+			data.normalMap = this.normalMap.toJSON( meta ).uuid;
+			data.normalScale = this.normalScale; // Removed for now, causes issue in editor ui.js
+		}
+		if ( this.specularMap instanceof THREE.Texture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
+		if ( this.envMap instanceof THREE.Texture ) {
+			data.envMap = this.envMap.toJSON( meta ).uuid;
+			data.reflectivity = this.reflectivity; // Scale behind envMap
+		}
+
+		if ( this.size !== undefined ) data.size = this.size;
+		if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
+
+		if ( this.vertexColors !== undefined && this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
+		if ( this.shading !== undefined && this.shading !== THREE.SmoothShading ) data.shading = this.shading;
+		if ( this.blending !== undefined && this.blending !== THREE.NormalBlending ) data.blending = this.blending;
+		if ( this.side !== undefined && this.side !== THREE.FrontSide ) data.side = this.side;
+
 		if ( this.opacity < 1 ) data.opacity = this.opacity;
-		if ( this.transparent !== false ) data.transparent = this.transparent;
-		if ( this.wireframe !== false ) data.wireframe = this.wireframe;
+		if ( this.transparent === true ) data.transparent = this.transparent;
+		if ( this.wireframe === true ) data.wireframe = this.wireframe;
 
 		return data;
 

+ 0 - 13
src/materials/MeshBasicMaterial.js

@@ -109,16 +109,3 @@ THREE.MeshBasicMaterial.prototype.clone = function () {
 	return material;
 
 };
-
-THREE.MeshBasicMaterial.prototype.toJSON = function () {
-
-	var data = THREE.Material.prototype.toJSON.call( this );
-
-	data.color = this.color.getHex();
-	if ( this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
-	if ( this.blending !== THREE.NormalBlending ) data.blending = this.blending;
-	if ( this.side !== THREE.FrontSide ) data.side = this.side;
-
-	return data;
-
-};

+ 0 - 11
src/materials/MeshDepthMaterial.js

@@ -43,14 +43,3 @@ THREE.MeshDepthMaterial.prototype.clone = function () {
 	return material;
 
 };
-
-THREE.MeshDepthMaterial.prototype.toJSON = function () {
-
-	var data = THREE.Material.prototype.toJSON.call( this );
-
-	if ( this.blending !== THREE.NormalBlending ) data.blending = this.blending;
-	if ( this.side !== THREE.FrontSide ) data.side = this.side;
-
-	return data;
-
-};

+ 0 - 15
src/materials/MeshLambertMaterial.js

@@ -116,18 +116,3 @@ THREE.MeshLambertMaterial.prototype.clone = function () {
 	return material;
 
 };
-
-THREE.MeshLambertMaterial.prototype.toJSON = function () {
-
-	var data = THREE.Material.prototype.toJSON.call( this );
-
-	data.color = this.color.getHex();
-	data.emissive = this.emissive.getHex();
-	if ( this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
-	if ( this.shading !== THREE.SmoothShading ) data.shading = this.shading;
-	if ( this.blending !== THREE.NormalBlending ) data.blending = this.blending;
-	if ( this.side !== THREE.FrontSide ) data.side = this.side;
-
-	return data;
-
-};

+ 0 - 11
src/materials/MeshNormalMaterial.js

@@ -44,14 +44,3 @@ THREE.MeshNormalMaterial.prototype.clone = function () {
 	return material;
 
 };
-
-THREE.MeshNormalMaterial.prototype.toJSON = function () {
-
-	var data = THREE.Material.prototype.toJSON.call( this );
-
-	if ( this.blending !== THREE.NormalBlending ) data.blending = this.blending;
-	if ( this.side !== THREE.FrontSide ) data.side = this.side;
-
-	return data;
-
-};

+ 0 - 35
src/materials/MeshPhongMaterial.js

@@ -162,38 +162,3 @@ THREE.MeshPhongMaterial.prototype.clone = function () {
 	return material;
 
 };
-
-THREE.MeshPhongMaterial.prototype.toJSON = function ( meta ) {
-
-	var data = THREE.Material.prototype.toJSON.call( this, meta );
-
-	data.color = this.color.getHex();
-	data.emissive = this.emissive.getHex();
-	data.specular = this.specular.getHex();
-	data.shininess = this.shininess;
-
-	if ( this.map instanceof THREE.Texture ) data.map = this.map.toJSON( meta ).uuid;
-	if ( this.alphaMap instanceof THREE.Texture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
-	if ( this.lightMap instanceof THREE.Texture ) data.lightMap = this.lightMap.toJSON( meta ).uuid;
-	if ( this.bumpMap instanceof THREE.Texture ) {
-		data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
-		data.bumpScale = this.bumpScale;
-	}
-	if ( this.normalMap instanceof THREE.Texture ) {
-		data.normalMap = this.normalMap.toJSON( meta ).uuid;
-		data.normalScale = this.normalScale; // Removed for now, causes issue in editor ui.js
-	}
-	if ( this.specularMap instanceof THREE.Texture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
-	if ( this.envMap instanceof THREE.Texture ) {
-		data.envMap = this.envMap.toJSON( meta ).uuid;
-		data.reflectivity = this.reflectivity; // Scale behind envMap
-	}
-
-	if ( this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
-	if ( this.shading !== THREE.SmoothShading ) data.shading = this.shading;
-	if ( this.blending !== THREE.NormalBlending ) data.blending = this.blending;
-	if ( this.side !== THREE.FrontSide ) data.side = this.side;
-
-	return data;
-
-};

+ 0 - 15
src/materials/PointCloudMaterial.js

@@ -65,21 +65,6 @@ THREE.PointCloudMaterial.prototype.clone = function () {
 
 };
 
-THREE.PointCloudMaterial.prototype.toJSON = function () {
-
-	var data = THREE.Material.prototype.toJSON.call( this );
-
-	data.size	= this.size;
-	data.sizeAttenuation = this.sizeAttenuation;
-	data.color = this.color.getHex();
-
-	if ( this.vertexColors !== THREE.NoColors ) data.vertexColors = this.vertexColors;
-	if ( this.blending !== THREE.NormalBlending ) data.blending = this.blending;
-
-	return data;
-
-};
-
 // backwards compatibility
 
 THREE.ParticleBasicMaterial = function ( parameters ) {

+ 0 - 10
src/materials/SpriteMaterial.js

@@ -55,13 +55,3 @@ THREE.SpriteMaterial.prototype.clone = function () {
 	return material;
 
 };
-
-THREE.SpriteMaterial.prototype.toJSON = function () {
-
-	var data = THREE.Material.prototype.toJSON.call( this );
-
-	data.color = this.color.getHex();
-
-	return data;
-
-};