فهرست منبع

Added maxIterations property to TessellateModifier

Aki Rodic 4 سال پیش
والد
کامیت
ba5ce531f1

+ 10 - 4
examples/js/modifiers/TessellateModifier.js

@@ -2,9 +2,11 @@
  * Break faces with edges longer than maxEdgeLength
  */
 
-THREE.TessellateModifier = function ( maxEdgeLength ) {
+THREE.TessellateModifier = function ( maxEdgeLength = 0.1, maxIterations = 6, maxFaces = 1000000 ) {
 
-	this.maxEdgeLength = ( maxEdgeLength === undefined ) ? 0.1 : maxEdgeLength;
+	this.maxEdgeLength = maxEdgeLength;
+	this.maxIterations = maxIterations;
+	this.maxFaces = maxFaces;
 
 };
 
@@ -26,16 +28,18 @@ THREE.TessellateModifier.prototype.modify = function ( geometry ) {
 	geometry.mergeVertices( 6 );
 
 	let finalized = false;
+	let iteration = 0;
 	const maxEdgeLengthSquared = this.maxEdgeLength * this.maxEdgeLength;
 
 	let edge;
 
-	while ( ! finalized ) {
+	while ( ! finalized && iteration < this.maxIterations && geometry.faces.length < this.maxFace ) {
 
 		const faces = [];
 		const faceVertexUvs = [];
 
 		finalized = true;
+		iteration ++;
 
 		for ( var i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
 
@@ -61,7 +65,9 @@ THREE.TessellateModifier.prototype.modify = function ( geometry ) {
 				const dbc = vb.distanceToSquared( vc );
 				const dac = va.distanceToSquared( vc );
 
-				if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
+				const limitReached = ( faces.length + il - i ) >= this.maxFaces;
+
+				if ( ! limitReached && ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) ) {
 
 					finalized = false;
 

+ 3 - 1
examples/jsm/modifiers/TessellateModifier.d.ts

@@ -5,8 +5,10 @@ import {
 
 export class TessellateModifier {
 
-	constructor( maxEdgeLength: number );
+	constructor( maxEdgeLength: number = 0.1, maxIterations: number = 6, maxFaces: number = 100000 );
 	maxEdgeLength: number;
+	maxIterations: number;
+	maxFaces: number;
 
 	modify( geometry: Geometry | BufferGeometry ): Geometry | BufferGeometry;
 

+ 10 - 4
examples/jsm/modifiers/TessellateModifier.js

@@ -8,9 +8,11 @@ import {
  * Break faces with edges longer than maxEdgeLength
  */
 
-var TessellateModifier = function ( maxEdgeLength ) {
+const TessellateModifier = function ( maxEdgeLength = 0.1, maxIterations = 6, maxFaces = 1000000 ) {
 
-	this.maxEdgeLength = ( maxEdgeLength === undefined ) ? 0.1 : maxEdgeLength;
+	this.maxEdgeLength = maxEdgeLength;
+	this.maxIterations = maxIterations;
+	this.maxFaces = maxFaces;
 
 };
 
@@ -32,16 +34,18 @@ TessellateModifier.prototype.modify = function ( geometry ) {
 	geometry.mergeVertices( 6 );
 
 	let finalized = false;
+	let iteration = 0;
 	const maxEdgeLengthSquared = this.maxEdgeLength * this.maxEdgeLength;
 
 	let edge;
 
-	while ( ! finalized ) {
+	while ( ! finalized && iteration < this.maxIterations && geometry.faces.length < this.maxFaces ) {
 
 		const faces = [];
 		const faceVertexUvs = [];
 
 		finalized = true;
+		iteration ++;
 
 		for ( var i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) {
 
@@ -67,7 +71,9 @@ TessellateModifier.prototype.modify = function ( geometry ) {
 				const dbc = vb.distanceToSquared( vc );
 				const dac = va.distanceToSquared( vc );
 
-				if ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) {
+				const limitReached = ( faces.length + il - i ) >= this.maxFaces;
+
+				if ( ! limitReached && ( dab > maxEdgeLengthSquared || dbc > maxEdgeLengthSquared || dac > maxEdgeLengthSquared ) ) {
 
 					finalized = false;
 

+ 1 - 1
src/core/Geometry.d.ts

@@ -245,7 +245,7 @@ export class Geometry extends EventDispatcher {
 	 * Checks for duplicate vertices using hashmap for specified number of decimal points, e.g. 4 for epsilon of 0.0001
 	 * Duplicated vertices are removed and faces' vertices are updated.
 	 */
-	mergeVertices( precisionPoints: number ): number;
+	mergeVertices( precisionPoints?: number = 4 ): number;
 
 	setFromPoints( points: Array<Vector2> | Array<Vector3> ): this;
 

+ 1 - 2
src/core/Geometry.js

@@ -769,12 +769,11 @@ Geometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ),
 	 * and faces' vertices are updated.
 	 */
 
-	mergeVertices: function ( precisionPoints ) {
+	mergeVertices: function ( precisionPoints = 4 ) {
 
 		const verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique)
 		const unique = [], changes = [];
 
-		precisionPoints = precisionPoints || 4;
 		const precision = Math.pow( 10, precisionPoints );
 
 		for ( let i = 0, il = this.vertices.length; i < il; i ++ ) {