瀏覽代碼

Encapsulate drawcalls/offsets

Replace `geom.offsets.push` / `geom.drawcalls.push` with
`geom.addDrawCall()` everywhere.
dubejf 10 年之前
父節點
當前提交
d2ca5091f6

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

@@ -759,11 +759,7 @@ THREE.AWDLoader = (function () {
 	attrib = new THREE.BufferAttribute( buffer, 1 );
 	geom.addAttribute( 'index', attrib );
 
-	geom.offsets.push({
-            start: 0,
-            index: 0,
-            count: str_len / 2
-          });
+	geom.addDrawCall( 0, str_len / 2, 0 );
 
 	idx = 0;
 

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

@@ -99,7 +99,7 @@ THREE.UTF8Loader.BufferGeometryCreator.prototype.create = function ( attribArray
 	geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
 	geometry.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
 
-	geometry.offsets.push( { start: 0, count: indices.length, index: 0 } );
+	geometry.addDrawCall( 0, indices.length, 0 );
 
 	geometry.computeBoundingSphere();
 

+ 4 - 6
examples/webgl_buffergeometry.html

@@ -204,13 +204,11 @@
 
 				for ( var i = 0; i < offsets; i ++ ) {
 
-					var offset = {
-						start: i * chunkSize * 3,
-						index: i * chunkSize * 3,
-						count: Math.min( triangles - ( i * chunkSize ), chunkSize ) * 3
-					};
+					var indexStart = i * chunkSize * 3;
+					var indexCount = Math.min( triangles - ( i * chunkSize ), chunkSize ) * 3;
+					var indexOffset = i * chunkSize * 3;
 
-					geometry.offsets.push( offset );
+					geometry.addDrawCall( indexStart, indexCount, indexOffset );
 
 				}
 

+ 5 - 13
examples/webgl_buffergeometry_drawcalls.html

@@ -44,10 +44,11 @@
 		<script>
 
 			var group;
-			var container, stats;
+			var container, controls, stats;
 			var particlesData = [];
 			var camera, scene, renderer;
-			var positions,colors;
+			var positions, colors;
+			var particles;
 			var pointCloud;
 			var particlePositions;
 			var linesMesh;
@@ -146,12 +147,7 @@
 
 				}
 
-				particles.drawcalls.push( {
-					start: 0,
-					count: particleCount,
-					index: 0
-				} );
-
+				particles.addDrawCall( 0, particleCount, 0 );
 				particles.addAttribute( 'position', new THREE.DynamicBufferAttribute( particlePositions, 3 ) );
 
 				// create the particle system
@@ -165,11 +161,7 @@
 
 				geometry.computeBoundingSphere();
 
-				geometry.drawcalls.push( {
-					start: 0,
-					count: 0,
-					index: 0
-				} );
+				geometry.addDrawCall( 0, 0, 0 );
 
 				var material = new THREE.LineBasicMaterial( {
 					vertexColors: THREE.VertexColors,

+ 4 - 7
examples/webgl_interactive_raycasting_pointcloud.html

@@ -42,7 +42,7 @@
 
 			var renderer, scene, camera, stats;
 			var pointclouds;
-			var raycaster, intersects;
+			var raycaster;
 			var mouse = new THREE.Vector2();
 			var intersection = null;
 			var spheres = [];
@@ -160,9 +160,7 @@
 				}
 
 				geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
-
-				var offset = { start: 0, count: indices.length, index: 0 };
-				geometry.offsets.push( offset );
+				geometry.addDrawCall( 0, indices.length, 0 );
 
 				var material = new THREE.PointCloudMaterial( { size: pointSize, vertexColors: THREE.VertexColors } );
 				var pointcloud = new THREE.PointCloud( geometry, material );
@@ -174,7 +172,6 @@
 			function generateRegularPointcloud( color, width, length ) {
 
 				var geometry = new THREE.Geometry();
-				var numPoints = width * length;
 
 				var colors = [];
 
@@ -217,7 +214,7 @@
 
 			function init() {
 
-				container = document.getElementById( 'container' );
+				var container = document.getElementById( 'container' );
 
 				scene = new THREE.Scene();
 
@@ -229,7 +226,7 @@
 
 				//
 
-				pcBuffer = generatePointcloud( new THREE.Color( 1,0,0 ), width, length );
+				var pcBuffer = generatePointcloud( new THREE.Color( 1,0,0 ), width, length );
 				pcBuffer.scale.set( 10,10,10 );
 				pcBuffer.position.set( -5,0,5 );
 				scene.add( pcBuffer );

+ 32 - 28
src/core/BufferGeometry.js

@@ -62,6 +62,13 @@ THREE.BufferGeometry.prototype = {
 
 	},
 
+	clearDrawCalls: function () {
+
+		this.drawcalls = [];
+		this.offsets = this.drawcalls;
+
+	},
+
 	applyMatrix: function ( matrix ) {
 
 		var position = this.attributes.position;
@@ -100,6 +107,8 @@ THREE.BufferGeometry.prototype = {
 
 	copy: function ( geometry ) {
 
+		// TODO Clear attributes? Clear drawcalls? Copy morphTargets?
+
 		var attributes = geometry.attributes;
 		var offsets = geometry.offsets;
 
@@ -114,14 +123,7 @@ THREE.BufferGeometry.prototype = {
 		for ( var i = 0, il = offsets.length; i < il; i ++ ) {
 
 			var offset = offsets[ i ];
-
-			this.offsets.push( {
-
-				start: offset.start,
-				index: offset.index,
-				count: offset.count
-
-			} );
+			this.addDrawCall( offset.start, offset.count, offset.index );
 
 		}
 
@@ -552,13 +554,17 @@ THREE.BufferGeometry.prototype = {
 
 				var indices = attributes.index.array;
 
-				var offsets = ( this.offsets.length > 0 ? this.offsets : [ { start: 0, count: indices.length, index: 0 } ] );
+				if ( this.drawcalls.length === 0 ) {
+
+					this.addDrawCall( 0, indices.length, 0 );
 
-				for ( var j = 0, jl = offsets.length; j < jl; ++ j ) {
+				}
+
+				for ( var j = 0, jl = this.drawcalls.length; j < jl; ++ j ) {
 
-					var start = offsets[ j ].start;
-					var count = offsets[ j ].count;
-					var index = offsets[ j ].index;
+					var start = this.drawcalls[ j ].start;
+					var count = this.drawcalls[ j ].count;
+					var index = this.drawcalls[ j ].index;
 
 					for ( var i = start, il = start + count; i < il; i += 3 ) {
 
@@ -838,8 +844,8 @@ THREE.BufferGeometry.prototype = {
 		var indexPtr = 0;
 		var vertexPtr = 0;
 
-		var offsets = [ { start:0, count:0, index:0 } ];
-		var offset = offsets[ 0 ];
+		var tmpOffsets = [ { start:0, count:0, index:0 } ];
+		var offset = tmpOffsets[ 0 ];
 
 		var duplicatedVertices = 0;
 		var newVerticeMaps = 0;
@@ -877,7 +883,7 @@ THREE.BufferGeometry.prototype = {
 			var faceMax = vertexPtr + newVerticeMaps;
 			if ( faceMax > ( offset.index + size ) ) {
 				var new_offset = { start:indexPtr, count:0, index:vertexPtr };
-				offsets.push( new_offset );
+				tmpOffsets.push( new_offset );
 				offset = new_offset;
 
 				//Re-evaluate reused vertices in light of new offset.
@@ -905,19 +911,23 @@ THREE.BufferGeometry.prototype = {
 
 		/* Move all attribute values to map to the new computed indices , also expand the vertex stack to match our new vertexPtr. */
 		this.reorderBuffers( sortedIndices, revVertexMap, vertexPtr );
-		this.offsets = offsets; // TODO: Deprecate
-		this.drawcalls = offsets;
+
+		this.clearDrawCalls();
+		for ( var i = 0; i < tmpOffsets.length; i ++ ) {
+
+			var offset = tmpOffsets[ i ];
+			this.addDrawCall( offset.start, offset.count, offset.index );
+
+		}
 
 		/*
 		var orderTime = Date.now();
 		console.log("Reorder time: "+(orderTime-s)+"ms");
 		console.log("Duplicated "+duplicatedVertices+" vertices.");
 		console.log("Compute Buffers time: "+(Date.now()-s)+"ms");
-		console.log("Draw offsets: "+offsets.length);
+		console.log("Draw tmpOffsets: "+tmpOffsets.length);
 		*/
 
-		return offsets;
-
 	},
 
 	merge: function ( geometry, offset ) {
@@ -1105,13 +1115,7 @@ THREE.BufferGeometry.prototype = {
 
 			var offset = this.offsets[ i ];
 
-			geometry.offsets.push( {
-
-				start: offset.start,
-				index: offset.index,
-				count: offset.count
-
-			} );
+			geometry.addDrawCall( offset.start, offset.count, offset.index );
 
 		}
 

+ 3 - 11
src/core/InstancedBufferGeometry.js

@@ -38,18 +38,10 @@ THREE.InstancedBufferGeometry.prototype.clone = function () {
 
 	}
 
-	for ( var i = 0, il = this.offsets.length; i < il; i++ ) {
+	for ( var i = 0, il = this.drawcalls.length; i < il; i++ ) {
 
-		var offset = this.offsets[i];
-
-		geometry.offsets.push( {
-
-			start: offset.start,
-			index: offset.index,
-			count: offset.count,
-			instances: offset.instances
-
-		} );
+		var offset = this.drawcalls[i];
+		geometry.addDrawCall( offset.start, offset.count, offset.index, offset.instances );
 
 	}
 

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

@@ -70,7 +70,7 @@ THREE.WireframeGeometry = function ( geometry ) {
 
 			var vertices = geometry.attributes.position;
 			var indices = geometry.attributes.index.array;
-			var drawcalls = geometry.drawcalls;
+			var drawcalls = geometry.drawcalls.slice();
 			var numEdges = 0;
 
 			if ( drawcalls.length === 0 ) {

+ 4 - 7
src/objects/PointCloud.js

@@ -80,18 +80,15 @@ THREE.PointCloud.prototype.raycast = ( function () {
 			if ( attributes.index !== undefined ) {
 
 				var indices = attributes.index.array;
-				var offsets = geometry.offsets;
 
-				if ( offsets.length === 0 ) {
+				if ( geometry.offsets.length === 0 ) {
 
-					offsets.push( {
-						start: 0,
-						count: indices.length,
-						index: 0
-					} );
+					offsets.addDrawCall( 0, indices.length, 0 );
 
 				}
 
+				var offsets = geometry.offsets;
+
 				for ( var oi = 0, ol = offsets.length; oi < ol; ++ oi ) {
 
 					var offset = offsets[ oi ];