Parcourir la source

Added IndexBufferAttribute.

Mr.doob il y a 10 ans
Parent
commit
d74af727b7

+ 1 - 1
examples/js/loaders/AWDLoader.js

@@ -763,7 +763,7 @@
 					else if ( str_type === 2 ) {
 
 						buffer = new Uint16Array( str_len / 2 );
-						attrib = new THREE.BufferAttribute( buffer, 1 );
+						attrib = new THREE.IndexBufferAttribute( buffer, 1 );
 						geom.addAttribute( 'index', attrib );
 
 						geom.addDrawCall( 0, str_len / 2 );

+ 1 - 1
examples/js/loaders/BabylonLoader.js

@@ -87,7 +87,7 @@ THREE.BabylonLoader.prototype = {
 
 		var indices = new Uint16Array( json.indices );
 
-		geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+		geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 
 		// positions
 

+ 1 - 1
examples/js/loaders/UTF8Loader.js

@@ -94,7 +94,7 @@ THREE.UTF8Loader.BufferGeometryCreator.prototype.create = function ( attribArray
 
 	}
 
-	geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+	geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 	geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
 	geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
 	geometry.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );

+ 1 - 1
examples/js/loaders/VTKLoader.js

@@ -101,7 +101,7 @@ THREE.VTKLoader.prototype = {
 		}
 
 		var geometry = new THREE.BufferGeometry();
-		geometry.addAttribute( 'index', new THREE.BufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
+		geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( new ( indices.length > 65535 ? Uint32Array : Uint16Array )( indices ), 1 ) );
 		geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
 
 		return geometry;

+ 2 - 2
examples/js/loaders/ctm/CTMLoader.js

@@ -219,7 +219,7 @@ THREE.CTMLoader.prototype.createModel = function ( file, callback ) {
 
 		}
 
-		this.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+		this.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 		this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
 
 		if ( normals !== undefined ) {
@@ -246,7 +246,7 @@ THREE.CTMLoader.prototype.createModel = function ( file, callback ) {
 	Model.prototype.constructor = Model;
 
 	var geometry = new Model();
-	
+
 	// compute vertex normals if not present in the CTM model
 	if ( geometry.attributes.normal === undefined ) {
 		geometry.computeVertexNormals();

+ 5 - 5
examples/webgl_buffergeometry_instancing_dynamic.html

@@ -54,9 +54,9 @@
 
     <script id="vertexShader" type="x-shader/x-vertex">
         precision highp float;
-        
-        uniform mat4 modelViewMatrix; 
-        uniform mat4 projectionMatrix; 
+
+        uniform mat4 modelViewMatrix;
+        uniform mat4 projectionMatrix;
 
         attribute vec3 position;
         attribute vec3 offset;
@@ -207,7 +207,7 @@
                 22, 21, 23
             ] );
 
-            geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+            geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 
             // per instance data
             var offsets = new THREE.InstancedBufferAttribute( new Float32Array( instances * 3 ), 3, 1, false );
@@ -322,7 +322,7 @@
                 var index = i * 4;
                 currentQ.set( orientations.array[index], orientations.array[index + 1], orientations.array[index + 2], orientations.array[index + 3] );
                 currentQ.multiply( tmpQ );
-                
+
                 orientations.setXYZW( i, currentQ.x, currentQ.y, currentQ.z, currentQ.w );
 
             }

+ 4 - 4
examples/webgl_buffergeometry_instancing_interleaved_dynamic.html

@@ -56,8 +56,8 @@
     <script id="vertexShader" type="x-shader/x-vertex">
         precision highp float;
 
-        uniform mat4 modelViewMatrix; 
-        uniform mat4 projectionMatrix; 
+        uniform mat4 modelViewMatrix;
+        uniform mat4 projectionMatrix;
 
         attribute vec3 position;
         attribute vec3 offset;
@@ -178,7 +178,7 @@
             22, 21, 23
         ] );
 
-        geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+        geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 
         // per instance data
         instanceBuffer = new THREE.InstancedInterleavedBuffer( new Float32Array( instances * 8 ), 8, true, 1 );
@@ -310,4 +310,4 @@
 
 
 
-</html>
+</html>

+ 1 - 1
examples/webgl_buffergeometry_lines_indexed.html

@@ -182,7 +182,7 @@
 				);
 				// --------------------------------
 
-				geometry.addAttribute( 'index', new THREE.BufferAttribute( new Uint16Array( indices_array ), 1 ) );
+				geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( new Uint16Array( indices_array ), 1 ) );
 				geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( positions ), 3 ) );
 				geometry.addAttribute( 'color', new THREE.BufferAttribute( new Float32Array( colors ), 3 ) );
 				geometry.computeBoundingSphere();

+ 2 - 4
examples/webgl_buffergeometry_uint.html

@@ -78,12 +78,10 @@
 
 				//
 
-				var triangles = 160000;
+				var triangles = 500000;
 
 				var geometry = new THREE.BufferGeometry();
 
-
-
 				var indices = new Uint32Array( triangles * 3 );
 
 				for ( var i = 0; i < indices.length; i ++ ) {
@@ -190,7 +188,7 @@
 
 				}
 
-				geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+				geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 				geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
 				geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
 				geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );

+ 2 - 2
examples/webgl_interactive_raycasting_pointcloud.html

@@ -131,7 +131,7 @@
 
 				}
 
-				geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+				geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 
 				var material = new THREE.PointCloudMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
 				var pointcloud = new THREE.PointCloud( geometry, material );
@@ -159,7 +159,7 @@
 
 				}
 
-				geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+				geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 				geometry.addDrawCall( 0, indices.length );
 
 				var material = new THREE.PointCloudMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );

+ 10 - 2
src/core/BufferGeometry.js

@@ -39,6 +39,13 @@ THREE.BufferGeometry.prototype = {
 
 		}
 
+		if ( name === 'index' && attribute instanceof THREE.IndexBufferAttribute === false ) {
+
+			console.warn( 'THREE.BufferGeometry.addAttribute: Use THREE.IndexBufferAttribute for index attribute.' );
+			attribute = new THREE.IndexBufferAttribute( attribute.array, attribute.itemSize );
+
+		}
+
 		this.attributes[ name ] = attribute;
 
 	},
@@ -458,8 +465,9 @@ THREE.BufferGeometry.prototype = {
 
 		if ( geometry.indices.length > 0 ) {
 
-			var indices = new Uint16Array( geometry.indices.length * 3 );
-			this.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
+			var TypeArray = geometry.vertices.length > 65535 ? Uint32Array : Uint16Array;
+			var indices = new TypeArray( geometry.indices.length * 3 );
+			this.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ).copyIndicesArray( geometry.indices ) );
 
 		}
 

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

@@ -56,7 +56,7 @@ THREE.CircleBufferGeometry = function ( radius, segments, thetaStart, thetaLengt
 
 	}
 
-	this.addAttribute( 'index', new THREE.BufferAttribute( new Uint16Array( indices ), 1 ) );
+	this.addAttribute( 'index', new THREE.IndexBufferAttribute( new Uint16Array( indices ), 1 ) );
 	this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
 	this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
 	this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );

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

@@ -85,7 +85,7 @@ THREE.PlaneBufferGeometry = function ( width, height, widthSegments, heightSegme
 
 	}
 
-	this.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+	this.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 	this.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
 	this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
 	this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );

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

@@ -88,7 +88,7 @@ THREE.SphereBufferGeometry = function ( radius, widthSegments, heightSegments, p
 
 	}
 
-	this.addAttribute( 'index', new THREE.BufferAttribute( new Uint16Array( indices ), 1 ) );
+	this.addAttribute( 'index', new THREE.IndexBufferAttribute( new Uint16Array( indices ), 1 ) );
 	this.addAttribute( 'position', positions );
 	this.addAttribute( 'normal', normals );
 	this.addAttribute( 'uv', uvs );

+ 1 - 1
src/objects/Sprite.js

@@ -10,7 +10,7 @@ THREE.Sprite = ( function () {
 	var uvs = new Float32Array( [ 0, 0,   1, 0,   1, 1,   0, 1 ] );
 
 	var geometry = new THREE.BufferGeometry();
-	geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
+	geometry.addAttribute( 'index', new THREE.IndexBufferAttribute( indices, 1 ) );
 	geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
 	geometry.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
 

+ 16 - 7
src/renderers/webgl/WebGLObjects.js

@@ -92,7 +92,7 @@ THREE.WebGLObjects = function ( gl, properties, info ) {
 
 		for ( var name in attributes ) {
 
-			updateAttribute( attributes[ name ], name );
+			updateAttribute( attributes[ name ] );
 
 		}
 
@@ -106,7 +106,7 @@ THREE.WebGLObjects = function ( gl, properties, info ) {
 
 			for ( var i = 0, l = array.length; i < l; i ++ ) {
 
-				updateAttribute( array[ i ], i );
+				updateAttribute( array[ i ] );
 
 			}
 
@@ -116,9 +116,19 @@ THREE.WebGLObjects = function ( gl, properties, info ) {
 
 	}
 
-	function updateAttribute( attribute, name ) {
+	function updateAttribute( attribute ) {
 
-		var bufferType = name === 'index' ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
+		var bufferType;
+
+		if ( attribute instanceof THREE.IndexBufferAttribute ) {
+
+			bufferType = gl.ELEMENT_ARRAY_BUFFER;
+
+		} else {
+
+			bufferType = gl.ARRAY_BUFFER;
+
+		}
 
 		var data = ( attribute instanceof THREE.InterleavedBufferAttribute ) ? attribute.data : attribute;
 
@@ -251,9 +261,9 @@ THREE.WebGLObjects = function ( gl, properties, info ) {
 		console.timeEnd( 'wireframe' );
 
 		var TypeArray = position.count > 65535 ? Uint32Array : Uint16Array;
-		var attribute = new THREE.BufferAttribute( new TypeArray( indices ), 1 );
+		var attribute = new THREE.IndexBufferAttribute( new TypeArray( indices ), 1 );
 
-		updateAttribute( attribute, 'index' );
+		updateAttribute( attribute );
 
 		property.wireframe = attribute;
 
@@ -277,7 +287,6 @@ THREE.WebGLObjects = function ( gl, properties, info ) {
 	this.getWireframeAttribute = getWireframeAttribute;
 
 	this.update = update;
-	this.updateAttribute = updateAttribute;
 
 	this.clear = function () {
 

+ 1 - 0
utils/build/includes/common.json

@@ -26,6 +26,7 @@
 	"src/core/Face4.js",
 	"src/core/BufferAttribute.js",
 	"src/core/DynamicBufferAttribute.js",
+	"src/core/IndexBufferAttribute.js",
 	"src/core/InstancedBufferAttribute.js",
 	"src/core/InterleavedBuffer.js",
 	"src/core/InstancedInterleavedBuffer.js",