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

BufferGeometry: Faster setIndex from Array (#23290)

* BufferGeometry: Faster setIndex from Array

* Rename arrayContainsOver arrayNeedsUint32
Toru Higuruma преди 3 години
родител
ревизия
84f3e57c53
променени са 3 файла, в които са добавени 19 реда и са изтрити 5 реда
  1. 2 2
      src/core/BufferGeometry.js
  2. 2 2
      src/renderers/webgl/WebGLGeometries.js
  3. 15 1
      src/utils.js

+ 2 - 2
src/core/BufferGeometry.js

@@ -8,7 +8,7 @@ import { Object3D } from './Object3D.js';
 import { Matrix4 } from '../math/Matrix4.js';
 import { Matrix3 } from '../math/Matrix3.js';
 import * as MathUtils from '../math/MathUtils.js';
-import { arrayMax } from '../utils.js';
+import { arrayNeedsUint32 } from '../utils.js';
 
 let _id = 0;
 
@@ -59,7 +59,7 @@ class BufferGeometry extends EventDispatcher {
 
 		if ( Array.isArray( index ) ) {
 
-			this.index = new ( arrayMax( index ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
+			this.index = new ( arrayNeedsUint32( index ) ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
 
 		} else {
 

+ 2 - 2
src/renderers/webgl/WebGLGeometries.js

@@ -1,5 +1,5 @@
 import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute.js';
-import { arrayMax } from '../../utils.js';
+import { arrayNeedsUint32 } from '../../utils.js';
 
 function WebGLGeometries( gl, attributes, info, bindingStates ) {
 
@@ -133,7 +133,7 @@ function WebGLGeometries( gl, attributes, info, bindingStates ) {
 
 		}
 
-		const attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
+		const attribute = new ( arrayNeedsUint32( indices ) ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 );
 		attribute.version = version;
 
 		// Updating index buffer in VAO now. See WebGLBindingStates

+ 15 - 1
src/utils.js

@@ -30,6 +30,20 @@ function arrayMax( array ) {
 
 }
 
+function arrayNeedsUint32( array ) {
+
+	// assumes larger values usually on last
+
+	for ( let i = array.length - 1; i >= 0; -- i ) {
+
+		if ( array[ i ] > 65535 ) return true;
+
+	}
+
+	return false;
+
+}
+
 const TYPED_ARRAYS = {
 	Int8Array: Int8Array,
 	Uint8Array: Uint8Array,
@@ -54,4 +68,4 @@ function createElementNS( name ) {
 
 }
 
-export { arrayMin, arrayMax, getTypedArray, createElementNS };
+export { arrayMin, arrayMax, arrayNeedsUint32, getTypedArray, createElementNS };