Переглянути джерело

Serialize and load JSON Light .shadow property (#9416)

* Serialize and load JSON Light .shadow property

* Add .toJSON() method to LightShadow
* Light .toJSON() writes .shadow property
* Parse .shadow property in DirectionLight and Spotlight JSON data

* Remove .castShadow condition when exporting .shadow property to JSON
satori99 9 роки тому
батько
коміт
df9a924279
3 змінених файлів з 36 додано та 0 видалено
  1. 2 0
      src/lights/Light.js
  2. 16 0
      src/lights/LightShadow.js
  3. 18 0
      src/loaders/ObjectLoader.js

+ 2 - 0
src/lights/Light.js

@@ -50,6 +50,8 @@ Light.prototype = Object.assign( Object.create( Object3D.prototype ), {
 		if ( this.decay !== undefined ) data.object.decay = this.decay;
 		if ( this.penumbra !== undefined ) data.object.penumbra = this.penumbra;
 
+		if ( this.shadow !== undefined ) data.object.shadow = this.shadow.toJSON();
+
 		return data;
 
 	}

+ 16 - 0
src/lights/LightShadow.js

@@ -38,6 +38,22 @@ Object.assign( LightShadow.prototype, {
 
 		return new this.constructor().copy( this );
 
+	},
+
+	toJSON: function () {
+
+		var object = {};
+
+		if ( this.bias !== 0 ) object.bias = this.bias;
+		if ( this.radius !== 1 ) object.radius = this.radius;
+		if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();
+
+		var camera = this.camera.toJSON( false ).object;
+		delete camera.matrix;
+		object.camera = camera;
+
+		return object;
+
 	}
 
 } );

+ 18 - 0
src/loaders/ObjectLoader.js

@@ -529,6 +529,15 @@ Object.assign( ObjectLoader.prototype, {
 
 					object = new DirectionalLight( data.color, data.intensity );
 
+					if ( data.shadow ) {
+
+						if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
+						if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
+						if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
+						if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
+
+					}
+
 					break;
 
 				case 'PointLight':
@@ -541,6 +550,15 @@ Object.assign( ObjectLoader.prototype, {
 
 					object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
 
+					if ( data.shadow ) {
+
+						if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
+						if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
+						if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
+						if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
+
+					}
+
 					break;
 
 				case 'HemisphereLight':