Browse Source

Extras geometries: clone

Provide a clone method to the specialized geometries in
src/extras/geometry, otherwise:
- the `.parameters` are not copied
- the `.type` of the clone is "Geometry" or "BufferGeometry"
- the cloned object is not an instance of the initial type. It loses its
specialization and becomes a plain `Geometry` or `BufferGeometry`.

Class deriving from `PolyhedronGeometry` defined  `this.parameters` all
over the place. Standardize to defining the `.type` and `.parameters`
after the `PolyhedronGeometry` constructor is called.

Also fix prototype inheritance chain for classes deriving from
`PolyhedronGeometry`.
dubejf 10 years ago
parent
commit
8785ae9c57

+ 17 - 0
src/extras/geometries/BoxGeometry.js

@@ -124,4 +124,21 @@ THREE.BoxGeometry = function ( width, height, depth, widthSegments, heightSegmen
 THREE.BoxGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.BoxGeometry.prototype.constructor = THREE.BoxGeometry;
 
+THREE.BoxGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.BoxGeometry(
+		this.parameters.width,
+		this.parameters.height,
+		this.parameters.depth,
+		this.parameters.widthSegments,
+		this.parameters.heightSegments,
+		this.parameters.depthSegments
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};
+
 THREE.CubeGeometry = THREE.BoxGeometry; // backwards compatibility

+ 15 - 0
src/extras/geometries/CircleBufferGeometry.js

@@ -67,3 +67,18 @@ THREE.CircleBufferGeometry = function ( radius, segments, thetaStart, thetaLengt
 
 THREE.CircleBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
 THREE.CircleBufferGeometry.prototype.constructor = THREE.CircleBufferGeometry;
+
+THREE.CircleBufferGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.CircleBufferGeometry(
+		this.parameters.radius,
+		this.parameters.segments,
+		this.parameters.thetaStart,
+		this.parameters.thetaLength
+	);
+
+	THREE.BufferGeometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 15 - 0
src/extras/geometries/CircleGeometry.js

@@ -57,3 +57,18 @@ THREE.CircleGeometry = function ( radius, segments, thetaStart, thetaLength ) {
 
 THREE.CircleGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.CircleGeometry.prototype.constructor = THREE.CircleGeometry;
+
+THREE.CircleGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.CircleGeometry(
+		this.parameters.radius,
+		this.parameters.segments,
+		this.parameters.thetaStart,
+		this.parameters.thetaLength
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 19 - 0
src/extras/geometries/CylinderGeometry.js

@@ -170,3 +170,22 @@ THREE.CylinderGeometry = function ( radiusTop, radiusBottom, height, radialSegme
 
 THREE.CylinderGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.CylinderGeometry.prototype.constructor = THREE.CylinderGeometry;
+
+THREE.CylinderGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.CylinderGeometry(
+		this.parameters.radiusTop,
+		this.parameters.radiusBottom,
+		this.parameters.height,
+		this.parameters.radialSegments,
+		this.parameters.heightSegments,
+		this.parameters.openEnded,
+		this.parameters.thetaStart,
+		this.parameters.thetaLength
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 21 - 6
src/extras/geometries/DodecahedronGeometry.js

@@ -4,11 +4,6 @@
 
 THREE.DodecahedronGeometry = function ( radius, detail ) {
 
-	this.parameters = {
-		radius: radius,
-		detail: detail
-	};
-
 	var t = ( 1 + Math.sqrt( 5 ) ) / 2;
 	var r = 1 / t;
 
@@ -50,7 +45,27 @@ THREE.DodecahedronGeometry = function ( radius, detail ) {
 
 	THREE.PolyhedronGeometry.call( this, vertices, indices, radius, detail );
 
+	this.type = 'DodecahedronGeometry';
+
+	this.parameters = {
+		radius: radius,
+		detail: detail
+	};
+
 };
 
-THREE.DodecahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
+THREE.DodecahedronGeometry.prototype = Object.create( THREE.PolyhedronGeometry.prototype );
 THREE.DodecahedronGeometry.prototype.constructor = THREE.DodecahedronGeometry;
+
+THREE.DodecahedronGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.DodecahedronGeometry(
+		this.parameters.radius,
+		this.parameters.detail
+	);
+
+	THREE.PolyhedronGeometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 14 - 1
src/extras/geometries/IcosahedronGeometry.js

@@ -29,5 +29,18 @@ THREE.IcosahedronGeometry = function ( radius, detail ) {
 	};
 };
 
-THREE.IcosahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
+THREE.IcosahedronGeometry.prototype = Object.create( THREE.PolyhedronGeometry.prototype );
 THREE.IcosahedronGeometry.prototype.constructor = THREE.IcosahedronGeometry;
+
+THREE.IcosahedronGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.IcosahedronGeometry(
+		this.parameters.radius,
+		this.parameters.detail
+	);
+
+	THREE.PolyhedronGeometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 14 - 6
src/extras/geometries/OctahedronGeometry.js

@@ -4,11 +4,6 @@
 
 THREE.OctahedronGeometry = function ( radius, detail ) {
 
-	this.parameters = {
-		radius: radius,
-		detail: detail
-	};
-
 	var vertices = [
 		1, 0, 0,   - 1, 0, 0,    0, 1, 0,    0,- 1, 0,    0, 0, 1,    0, 0,- 1
 	];
@@ -27,5 +22,18 @@ THREE.OctahedronGeometry = function ( radius, detail ) {
 	};
 };
 
-THREE.OctahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
+THREE.OctahedronGeometry.prototype = Object.create( THREE.PolyhedronGeometry.prototype );
 THREE.OctahedronGeometry.prototype.constructor = THREE.OctahedronGeometry;
+
+THREE.OctahedronGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.OctahedronGeometry(
+		this.parameters.radius,
+		this.parameters.detail
+	);
+
+	THREE.PolyhedronGeometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 15 - 0
src/extras/geometries/PlaneBufferGeometry.js

@@ -94,3 +94,18 @@ THREE.PlaneBufferGeometry = function ( width, height, widthSegments, heightSegme
 
 THREE.PlaneBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
 THREE.PlaneBufferGeometry.prototype.constructor = THREE.PlaneBufferGeometry;
+
+THREE.PlaneBufferGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.PlaneBufferGeometry(
+		this.parameters.width,
+		this.parameters.height,
+		this.parameters.widthSegments,
+		this.parameters.heightSegments
+	);
+
+	THREE.BufferGeometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 15 - 0
src/extras/geometries/PlaneGeometry.js

@@ -22,3 +22,18 @@ THREE.PlaneGeometry = function ( width, height, widthSegments, heightSegments )
 
 THREE.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.PlaneGeometry.prototype.constructor = THREE.PlaneGeometry;
+
+THREE.PlaneGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.PlaneGeometry(
+		this.parameters.width,
+		this.parameters.height,
+		this.parameters.widthSegments,
+		this.parameters.heightSegments
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 19 - 0
src/extras/geometries/PolyhedronGeometry.js

@@ -233,3 +233,22 @@ THREE.PolyhedronGeometry = function ( vertices, indices, radius, detail ) {
 
 THREE.PolyhedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.PolyhedronGeometry.prototype.constructor = THREE.PolyhedronGeometry;
+
+THREE.PolyhedronGeometry.prototype.clone = function ( geometry ) {
+
+	if ( geometry === undefined ) {
+
+		geometry = new THREE.PolyhedronGeometry(
+			this.parameters.vertices,
+			this.parameters.indices,
+			this.parameters.radius,
+			this.parameters.detail
+		);
+
+	}
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 16 - 0
src/extras/geometries/RingGeometry.js

@@ -81,3 +81,19 @@ THREE.RingGeometry = function ( innerRadius, outerRadius, thetaSegments, phiSegm
 THREE.RingGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.RingGeometry.prototype.constructor = THREE.RingGeometry;
 
+THREE.RingGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.RingGeometry(
+		this.parameters.innerRadius,
+		this.parameters.outerRadius,
+		this.parameters.thetaSegments,
+		this.parameters.phiSegments,
+		this.parameters.thetaStart,
+		this.parameters.thetaLength
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 18 - 0
src/extras/geometries/SphereBufferGeometry.js

@@ -99,3 +99,21 @@ THREE.SphereBufferGeometry = function ( radius, widthSegments, heightSegments, p
 
 THREE.SphereBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
 THREE.SphereBufferGeometry.prototype.constructor = THREE.SphereBufferGeometry;
+
+THREE.SphereBufferGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.SphereBufferGeometry(
+		this.parameters.radius,
+		this.parameters.widthSegments,
+		this.parameters.heightSegments,
+		this.parameters.phiStart,
+		this.parameters.phiLength,
+		this.parameters.thetaStart,
+		this.parameters.thetaLength
+	);
+
+	THREE.BufferGeometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 18 - 0
src/extras/geometries/SphereGeometry.js

@@ -113,3 +113,21 @@ THREE.SphereGeometry = function ( radius, widthSegments, heightSegments, phiStar
 
 THREE.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.SphereGeometry.prototype.constructor = THREE.SphereGeometry;
+
+THREE.SphereGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.SphereGeometry(
+		this.parameters.radius,
+		this.parameters.widthSegments,
+		this.parameters.heightSegments,
+		this.parameters.phiStart,
+		this.parameters.phiLength,
+		this.parameters.thetaStart,
+		this.parameters.thetaLength
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 14 - 1
src/extras/geometries/TetrahedronGeometry.js

@@ -23,5 +23,18 @@ THREE.TetrahedronGeometry = function ( radius, detail ) {
 
 };
 
-THREE.TetrahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
+THREE.TetrahedronGeometry.prototype = Object.create( THREE.PolyhedronGeometry.prototype );
 THREE.TetrahedronGeometry.prototype.constructor = THREE.TetrahedronGeometry;
+
+THREE.TetrahedronGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.TetrahedronGeometry(
+		this.parameters.radius,
+		this.parameters.detail
+	);
+
+	THREE.PolyhedronGeometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 16 - 0
src/extras/geometries/TorusGeometry.js

@@ -77,3 +77,19 @@ THREE.TorusGeometry = function ( radius, tube, radialSegments, tubularSegments,
 
 THREE.TorusGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.TorusGeometry.prototype.constructor = THREE.TorusGeometry;
+
+THREE.TorusGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.TorusGeometry(
+		this.parameters.radius,
+		this.parameters.tube,
+		this.parameters.radialSegments,
+		this.parameters.tubularSegments,
+		this.parameters.arc
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 19 - 1
src/extras/geometries/TorusKnotGeometry.js

@@ -26,7 +26,7 @@ THREE.TorusKnotGeometry = function ( radius, tube, radialSegments, tubularSegmen
 	p = p || 2;
 	q = q || 3;
 	heightScale = heightScale || 1;
-	
+
 	var grid = new Array( radialSegments );
 	var tang = new THREE.Vector3();
 	var n = new THREE.Vector3();
@@ -111,3 +111,21 @@ THREE.TorusKnotGeometry = function ( radius, tube, radialSegments, tubularSegmen
 
 THREE.TorusKnotGeometry.prototype = Object.create( THREE.Geometry.prototype );
 THREE.TorusKnotGeometry.prototype.constructor = THREE.TorusKnotGeometry;
+
+THREE.TorusKnotGeometry.prototype.clone = function () {
+
+	var geometry = new THREE.TorusKnotGeometry(
+		this.parameters.radius,
+		this.parameters.tube,
+		this.parameters.radialSegments,
+		this.parameters.tubularSegments,
+		this.parameters.p,
+		this.parameters.q,
+		this.parameters.heightScale
+	);
+
+	THREE.Geometry.prototype.clone.call( this, geometry );
+
+	return geometry;
+
+};

+ 1 - 1
src/extras/geometries/WireframeGeometry.js

@@ -7,7 +7,7 @@ THREE.WireframeGeometry = function ( geometry ) {
 	THREE.BufferGeometry.call( this );
 
 	var edge = [ 0, 0 ], hash = {};
-	var sortFunction = function ( a, b ) { return a - b };
+	var sortFunction = function ( a, b ) { return a - b; };
 
 	var keys = [ 'a', 'b', 'c' ];