浏览代码

Change precision to tolerance, update memory function name

Garrett Johnson 7 年之前
父节点
当前提交
814e782ac4
共有 2 个文件被更改,包括 15 次插入7 次删除
  1. 8 4
      examples/js/BufferGeometryUtils.js
  2. 7 3
      src/core/BufferGeometry.js

+ 8 - 4
examples/js/BufferGeometryUtils.js

@@ -462,17 +462,21 @@ THREE.BufferGeometryUtils = {
 	 * @param {Array<THREE.BufferGeometry>} geometry
 	 * @return {number}
 	 */
-	getMemoryUse: function ( geometry ) {
+	estimateBytesUsed: function ( geometry ) {
 
-		// Return the estimated memory used by this geometry
+		// Return the estimated memory used by this geometry in bytes
+		// Calculate using itemSize, count, and BYTES_PER_ELEMENT to account
+		// for InterleavedBufferAttributes.
 		var mem = 0;
 		for ( var name in geometry.attributes ) {
 
-			mem += geometry.getAttribute( name ).array.byteLength;
+			var attr = geometry.getAttribute( name );
+			mem += attr.count * attr.itemSize * attr.array.BYTES_PER_ELEMENT;
 
 		}
 
-		mem += geometry.getIndex() ? geometry.getIndex().array.byteLength : 0;
+		var indices = geometry.getIndex();
+		mem += indices ? indices.count * indices.itemSize * indices.array.BYTES_PER_ELEMENT : 0;
 		return mem;
 
 	}

+ 7 - 3
src/core/BufferGeometry.js

@@ -915,7 +915,9 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 	},
 
-	mergeVertices: function ( precision = 4 ) {
+	mergeVertices: function ( tolerance = 1e-4 ) {
+
+        tolerance = Math.max( tolerance, Number.EPSILON );
 
 		// Generate an index buffer if the geometry doesn't have one, or optimize it
 		// if it's already available.
@@ -933,7 +935,9 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 		var newIndices = [];
 		var getters = [ 'getX', 'getY', 'getZ', 'getW' ];
 
-		var precisionMultiplier = Math.pow( 10, precision + 1 );
+		// convert the error tolerance to an amount of decimal places to truncate to
+		var decimalShift = Math.log10( 1 / tolerance );
+		var shiftMultiplier = Math.pow( 10, decimalShift );
 		for ( var i = 0; i < vertexCount; i ++ ) {
 
 			// Generate a hash for the vertex attributes at the current index 'i'
@@ -947,7 +951,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 				for ( var k = 0; k < itemSize; k ++ ) {
 
 					// double tilde truncates the decimal value
-					hash += `${ ~ ~ ( attribute[ getters[ k ] ]( i ) * precisionMultiplier ) },`;
+					hash += `${ ~ ~ ( attribute[ getters[ k ] ]( i ) * shiftMultiplier ) },`;
 
 				}