Преглед на файлове

Merge pull request #10576 from Mugen87/geometry

Geometries: Some more clean up
Mr.doob преди 8 години
родител
ревизия
fce1ecfcc6

+ 1 - 1
src/geometries/BoxGeometry.js

@@ -89,7 +89,7 @@ function BoxBufferGeometry( width, height, depth, widthSegments, heightSegments,
 
 	function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
 
-		var segmentWidth	= width / gridX;
+		var segmentWidth = width / gridX;
 		var segmentHeight = height / gridY;
 
 		var widthHalf = width / 2;

+ 36 - 21
src/geometries/EdgesGeometry.js

@@ -5,25 +5,32 @@ import { _Math } from '../math/Math';
 
 /**
  * @author WestLangley / http://github.com/WestLangley
+ * @author Mugen87 / https://github.com/Mugen87
  */
 
 function EdgesGeometry( geometry, thresholdAngle ) {
 
 	BufferGeometry.call( this );
 
-	thresholdAngle = ( thresholdAngle !== undefined ) ? thresholdAngle : 1;
+	this.type = 'EdgesGeometry';
 
-	var thresholdDot = Math.cos( _Math.DEG2RAD * thresholdAngle );
+	this.parameters = {
+		thresholdAngle: thresholdAngle
+	};
 
-	var edge = [ 0, 0 ], hash = {};
+	thresholdAngle = ( thresholdAngle !== undefined ) ? thresholdAngle : 1;
 
-	function sortFunction( a, b ) {
+	// buffer
 
-		return a - b;
+	var vertices = [];
 
-	}
+	// helper variables
+
+	var thresholdDot = Math.cos( _Math.DEG2RAD * thresholdAngle );
+	var edge = [ 0, 0 ], hash = {};
+	var key, keys = [ 'a', 'b', 'c' ];
 
-	var keys = [ 'a', 'b', 'c' ];
+	// prepare source geometry
 
 	var geometry2;
 
@@ -41,9 +48,11 @@ function EdgesGeometry( geometry, thresholdAngle ) {
 	geometry2.mergeVertices();
 	geometry2.computeFaceNormals();
 
-	var vertices = geometry2.vertices;
+	var sourceVertices = geometry2.vertices;
 	var faces = geometry2.faces;
 
+	// now create a data structure (hash) where each entry represents an edge with its adjoining faces
+
 	for ( var i = 0, l = faces.length; i < l; i ++ ) {
 
 		var face = faces[ i ];
@@ -54,7 +63,7 @@ function EdgesGeometry( geometry, thresholdAngle ) {
 			edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ];
 			edge.sort( sortFunction );
 
-			var key = edge.toString();
+			key = edge.toString();
 
 			if ( hash[ key ] === undefined ) {
 
@@ -70,31 +79,37 @@ function EdgesGeometry( geometry, thresholdAngle ) {
 
 	}
 
-	var coords = [];
+	// generate vertices
 
-	for ( var key in hash ) {
+	for ( key in hash ) {
 
 		var h = hash[ key ];
 
-		// An edge is only rendered if the angle (in degrees) between the face normals of the adjoining faces exceeds this value. default = 1 degree.
+		// an edge is only rendered if the angle (in degrees) between the face normals of the adjoining faces exceeds this value. default = 1 degree.
 
 		if ( h.face2 === undefined || faces[ h.face1 ].normal.dot( faces[ h.face2 ].normal ) <= thresholdDot ) {
 
-			var vertex = vertices[ h.vert1 ];
-			coords.push( vertex.x );
-			coords.push( vertex.y );
-			coords.push( vertex.z );
+			var vertex = sourceVertices[ h.vert1 ];
+			vertices.push( vertex.x, vertex.y, vertex.z );
 
-			vertex = vertices[ h.vert2 ];
-			coords.push( vertex.x );
-			coords.push( vertex.y );
-			coords.push( vertex.z );
+			vertex = sourceVertices[ h.vert2 ];
+			vertices.push( vertex.x, vertex.y, vertex.z );
 
 		}
 
 	}
 
-	this.addAttribute( 'position', new Float32BufferAttribute( coords, 3 ) );
+	// build geometry
+
+	this.addAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
+
+	// custom array sort function
+
+	function sortFunction( a, b ) {
+
+		return a - b;
+
+	}
 
 }
 

+ 9 - 12
src/geometries/ParametricGeometry.js

@@ -51,11 +51,11 @@ function ParametricBufferGeometry( func, slices, stacks ) {
 
 	// buffers
 
+	var indices = [];
 	var vertices = [];
 	var uvs = [];
 
-	var i, j, p;
-	var u, v;
+	var i, j;
 
 	// generate vertices and uvs
 
@@ -63,13 +63,13 @@ function ParametricBufferGeometry( func, slices, stacks ) {
 
 	for ( i = 0; i <= stacks; i ++ ) {
 
-		v = i / stacks;
+		var v = i / stacks;
 
 		for ( j = 0; j <= slices; j ++ ) {
 
-			u = j / slices;
+			var u = j / slices;
 
-			p = func( u, v );
+			var p = func( u, v );
 			vertices.push( p.x, p.y, p.z );
 
 			uvs.push( u, v );
@@ -80,17 +80,14 @@ function ParametricBufferGeometry( func, slices, stacks ) {
 
 	// generate indices
 
-	var indices = [];
-	var a, b, c, d;
-
 	for ( i = 0; i < stacks; i ++ ) {
 
 		for ( j = 0; j < slices; j ++ ) {
 
-			a = i * sliceCount + j;
-			b = i * sliceCount + j + 1;
-			c = ( i + 1 ) * sliceCount + j + 1;
-			d = ( i + 1 ) * sliceCount + j;
+			var a = i * sliceCount + j;
+			var b = i * sliceCount + j + 1;
+			var c = ( i + 1 ) * sliceCount + j + 1;
+			var d = ( i + 1 ) * sliceCount + j;
 
 			// faces one and two
 

+ 0 - 3
src/geometries/PolyhedronGeometry.js

@@ -35,7 +35,6 @@ import { Float32BufferAttribute } from '../core/BufferAttribute';
 import { BufferGeometry } from '../core/BufferGeometry';
 import { Vector3 } from '../math/Vector3';
 import { Vector2 } from '../math/Vector2';
-import { Sphere } from '../math/Sphere';
 
 function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
 
@@ -77,8 +76,6 @@ function PolyhedronBufferGeometry( vertices, indices, radius, detail ) {
 	this.addAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) );
 	this.normalizeNormals();
 
-	this.boundingSphere = new Sphere( new Vector3(), radius );
-
 	// helper functions
 
 	function subdivide( detail ) {

+ 10 - 6
src/geometries/WireframeGeometry.js

@@ -9,13 +9,9 @@ function WireframeGeometry( geometry ) {
 
 	BufferGeometry.call( this );
 
-	var edge = [ 0, 0 ], hash = {};
-
-	function sortFunction( a, b ) {
+	this.type = 'WireframeGeometry';
 
-		return a - b;
-
-	}
+	var edge = [ 0, 0 ], hash = {};
 
 	var keys = [ 'a', 'b', 'c' ];
 
@@ -179,6 +175,14 @@ function WireframeGeometry( geometry ) {
 
 	}
 
+	// custom array sort function
+
+	function sortFunction( a, b ) {
+
+		return a - b;
+
+	}
+
 }
 
 WireframeGeometry.prototype = Object.create( BufferGeometry.prototype );