Browse Source

Shape: Refactor serialization/ deserialization

Mugen87 7 years ago
parent
commit
edb3d28e53

+ 5 - 0
docs/api/extras/core/Shape.html

@@ -62,6 +62,11 @@
 		<h2>Properties</h2>
 		<div>See the base [page:Path] class for common properties.</div>
 
+		<h3>[property:String uuid]</h3>
+		<div>
+		[link:http://en.wikipedia.org/wiki/Universally_unique_identifier UUID] of this instance. This gets automatically assigned, so this shouldn't be edited.
+		</div>
+
 		<h3>[property:array holes]</h3>
 		<div>An array of [page:Path paths] that define the holes in the shape.</div>
 

+ 2 - 2
src/core/BufferGeometry.js

@@ -933,7 +933,7 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 				if ( Array.isArray( shapes ) === false ) {
 
-					data.shapes.push( shapes.toJSON() );
+					data.shapes.push( shapes.uuid );
 
 				} else {
 
@@ -941,7 +941,7 @@ Object.assign( BufferGeometry.prototype, EventDispatcher.prototype, {
 
 						var shape = shapes[ i ];
 
-						data.shapes.push( shape.toJSON() );
+						data.shapes.push( shape.uuid );
 
 					}
 

+ 2 - 2
src/core/Geometry.js

@@ -1019,7 +1019,7 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 				if ( Array.isArray( shapes ) === false ) {
 
-					data.shapes.push( shapes.toJSON() );
+					data.shapes.push( shapes.uuid );
 
 				} else {
 
@@ -1027,7 +1027,7 @@ Object.assign( Geometry.prototype, EventDispatcher.prototype, {
 
 						var shape = shapes[ i ];
 
-						data.shapes.push( shape.toJSON() );
+						data.shapes.push( shape.uuid );
 
 					}
 

+ 29 - 1
src/core/Object3D.js

@@ -626,7 +626,8 @@ Object.assign( Object3D.prototype, EventDispatcher.prototype, {
 				geometries: {},
 				materials: {},
 				textures: {},
-				images: {}
+				images: {},
+				shapes: {}
 			};
 
 			output.metadata = {
@@ -670,6 +671,31 @@ Object.assign( Object3D.prototype, EventDispatcher.prototype, {
 
 			object.geometry = serialize( meta.geometries, this.geometry );
 
+			var parameters = this.geometry.parameters;
+
+			if ( parameters !== undefined && parameters.shapes !== undefined ) {
+
+				var shapes = parameters.shapes;
+				var uuids = [];
+
+				if ( Array.isArray( shapes ) ) {
+
+					for ( var i = 0, l = shapes.length; i < l; i ++ ) {
+
+						var shape = shapes[ i ];
+
+						serialize( meta.shapes, shape );
+
+					}
+
+				} else {
+
+					serialize( meta.shapes, shapes );
+
+				}
+
+			}
+
 		}
 
 		if ( this.material !== undefined ) {
@@ -714,11 +740,13 @@ Object.assign( Object3D.prototype, EventDispatcher.prototype, {
 			var materials = extractFromCache( meta.materials );
 			var textures = extractFromCache( meta.textures );
 			var images = extractFromCache( meta.images );
+			var shapes = extractFromCache( meta.shapes );
 
 			if ( geometries.length > 0 ) output.geometries = geometries;
 			if ( materials.length > 0 ) output.materials = materials;
 			if ( textures.length > 0 ) output.textures = textures;
 			if ( images.length > 0 ) output.images = images;
+			if ( shapes.length > 0 ) output.shapes = shapes;
 
 		}
 

+ 5 - 0
src/extras/core/Shape.js

@@ -1,4 +1,5 @@
 import { Path } from './Path.js';
+import { _Math } from '../../math/Math.js';
 
 /**
  * @author zz85 / http://www.lab4games.net/zz85/blog
@@ -15,6 +16,8 @@ function Shape( points ) {
 
 	Path.call( this, points );
 
+	this.uuid = _Math.generateUUID();
+
 	this.type = 'Shape';
 
 	this.holes = [];
@@ -74,6 +77,7 @@ Shape.prototype = Object.assign( Object.create( Path.prototype ), {
 
 		var data = Path.prototype.toJSON.call( this );
 
+		data.uuid = this.uuid;
 		data.holes = [];
 
 		for ( var i = 0, l = this.holes.length; i < l; i ++ ) {
@@ -91,6 +95,7 @@ Shape.prototype = Object.assign( Object.create( Path.prototype ), {
 
 		Path.prototype.fromJSON.call( this, json );
 
+		this.uuid = json.uuid;
 		this.holes = [];
 
 		for ( var i = 0, l = json.holes.length; i < l; i ++ ) {

+ 27 - 6
src/loaders/ObjectLoader.js

@@ -124,7 +124,8 @@ Object.assign( ObjectLoader.prototype, {
 
 	parse: function ( json, onLoad ) {
 
-		var geometries = this.parseGeometries( json.geometries );
+		var shapes = this.parseShape( json.shapes );
+		var geometries = this.parseGeometries( json.geometries, shapes );
 
 		var images = this.parseImages( json.images, function () {
 
@@ -153,7 +154,27 @@ Object.assign( ObjectLoader.prototype, {
 
 	},
 
-	parseGeometries: function ( json ) {
+	parseShape: function ( json ) {
+
+		var shapes = {};
+
+		if ( json !== undefined ) {
+
+			for ( var i = 0, l = json.length; i < l; i ++ ) {
+
+				var shape = new Shape().fromJSON( json[ i ] );
+
+				shapes[ shape.uuid ] = shape;
+
+			}
+
+		}
+
+		return shapes;
+
+	},
+
+	parseGeometries: function ( json, shapes ) {
 
 		var geometries = {};
 
@@ -338,18 +359,18 @@ Object.assign( ObjectLoader.prototype, {
 					case 'ShapeGeometry':
 					case 'ShapeBufferGeometry':
 
-						var shapes = [];
+						var geometryShapes = [];
 
 						for ( var i = 0, l = data.shapes.length; i < l; i ++ ) {
 
-							var shape = new Shape().fromJSON( data.shapes[ i ] );
+							var shape = shapes[ data.shapes[ i ] ];
 
-							shapes.push( shape );
+							geometryShapes.push( shape );
 
 						}
 
 						geometry = new Geometries[ data.type ](
-							shapes,
+							geometryShapes,
 							data.curveSegments
 						);