瀏覽代碼

BufferGeometry: Added .toNonIndexed().

Mr.doob 9 年之前
父節點
當前提交
484beae7a2
共有 1 個文件被更改,包括 50 次插入0 次删除
  1. 50 0
      src/core/BufferGeometry.js

+ 50 - 0
src/core/BufferGeometry.js

@@ -65,6 +65,8 @@ THREE.BufferGeometry.prototype = {
 
 		this.attributes[ name ] = attribute;
 
+		return this;
+
 	},
 
 	getAttribute: function ( name ) {
@@ -77,6 +79,8 @@ THREE.BufferGeometry.prototype = {
 
 		delete this.attributes[ name ];
 
+		return this;
+
 	},
 
 	addGroup: function ( start, count, materialIndex ) {
@@ -820,6 +824,52 @@ THREE.BufferGeometry.prototype = {
 
 	},
 
+	toNonIndexed: function () {
+
+		if ( this.index === null ) {
+
+			console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' );
+			return this;
+
+		}
+
+		var geometry2 = new THREE.BufferGeometry();
+
+		var indices = this.index.array;
+		var attributes = this.attributes;
+
+		for ( var name in attributes ) {
+
+			var attribute = attributes[ name ];
+
+			var array = attribute.array;
+			var itemSize = attribute.itemSize;
+
+			var array2 = new array.constructor( indices.length * itemSize );
+
+			var index = 0;
+			var index2 = 0;
+
+			for ( var i = 0; i < indices.length; i ++ ) {
+
+				index = indices[ i ] * itemSize;
+
+				for ( var j = 0; j < itemSize; j ++ ) {
+
+					array2[ index2 ++ ] = array[ index ++ ];
+
+				}
+
+			}
+
+			geometry2.addAttribute( name, new THREE.BufferAttribute( array2, itemSize ) );
+
+		}
+
+		return geometry2;
+
+	},
+
 	toJSON: function () {
 
 		var data = {