Răsfoiți Sursa

Merge pull request #18537 from Mugen87/dev32

InterleavedBufferAttribute: Added toJSON() and clone().
Mr.doob 5 ani în urmă
părinte
comite
b133e53dc0

+ 5 - 0
docs/api/en/core/InterleavedBufferAttribute.html

@@ -45,6 +45,11 @@
 			How many values make up each item.
 		</p>
 
+		<h3>[property:String name]</h3>
+		<p>
+		Optional name for this attribute instance. Default is an empty string.
+		</p>
+
 		<h3>[property:Integer offset]</h3>
 		<p>
 			The offset in the underlying array buffer where an item starts.

+ 5 - 0
docs/api/zh/core/InterleavedBufferAttribute.html

@@ -44,6 +44,11 @@
 			队列中每个矢量有多少个元素构成。
 		</p>
 
+		<h3>[property:String name]</h3>
+		<p>
+		Optional name for this attribute instance. Default is an empty string.
+		</p>
+
 		<h3>[property:Integer offset]</h3>
 		<p>
 			缓存队列中每个元素的起始位置的偏移量。

+ 16 - 3
examples/js/exporters/GLTFExporter.js

@@ -506,9 +506,22 @@ THREE.GLTFExporter.prototype = {
 
 				for ( var a = 0; a < attribute.itemSize; a ++ ) {
 
-					// @TODO Fails on InterleavedBufferAttribute, and could probably be
-					// optimized for normal BufferAttribute.
-					var value = attribute.array[ i * attribute.itemSize + a ];
+					var value;
+
+					if ( attribute.itemSize > 3 ) {
+
+						 // no support for interleaved data for itemSize > 3
+
+						value = attribute.array[ i * attribute.itemSize + a ];
+
+					} else {
+
+						if ( a === 0 ) value = attribute.getX( i );
+						if ( a === 1 ) value = attribute.getY( i );
+						if ( a === 2 ) value = attribute.getZ( i );
+						if ( a === 3 ) value = attribute.getW( i );
+
+					}
 
 					if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
 

+ 16 - 3
examples/jsm/exporters/GLTFExporter.js

@@ -528,9 +528,22 @@ GLTFExporter.prototype = {
 
 				for ( var a = 0; a < attribute.itemSize; a ++ ) {
 
-					// @TODO Fails on InterleavedBufferAttribute, and could probably be
-					// optimized for normal BufferAttribute.
-					var value = attribute.array[ i * attribute.itemSize + a ];
+					var value;
+
+					if ( attribute.itemSize > 3 ) {
+
+						 // no support for interleaved data for itemSize > 3
+
+						value = attribute.array[ i * attribute.itemSize + a ];
+
+					} else {
+
+						if ( a === 0 ) value = attribute.getX( i );
+						if ( a === 1 ) value = attribute.getY( i );
+						if ( a === 2 ) value = attribute.getZ( i );
+						if ( a === 3 ) value = attribute.getW( i );
+
+					}
 
 					if ( componentType === WEBGL_CONSTANTS.FLOAT ) {
 

+ 9 - 0
src/core/InterleavedBufferAttribute.d.ts

@@ -1,3 +1,4 @@
+import { BufferAttribute } from './BufferAttribute';
 import { InterleavedBuffer } from './InterleavedBuffer';
 import { Matrix4 } from './../math/Matrix4';
 /**
@@ -12,6 +13,7 @@ export class InterleavedBufferAttribute {
 		normalized?: boolean
 	);
 
+	name: string;
 	data: InterleavedBuffer;
 	itemSize: number;
 	offset: number;
@@ -23,6 +25,7 @@ export class InterleavedBufferAttribute {
 	readonly isInterleavedBufferAttribute: true;
 
 	applyMatrix4( m: Matrix4 ): this;
+	clone(): BufferAttribute;
 	getX( index: number ): number;
 	setX( index: number, x: number ): InterleavedBufferAttribute;
 	getY( index: number ): number;
@@ -45,5 +48,11 @@ export class InterleavedBufferAttribute {
 		z: number,
 		w: number
 	): InterleavedBufferAttribute;
+	toJSON(): {
+		itemSize: number,
+		type: string,
+		array: number[],
+		normalized: boolean
+	};
 
 }

+ 54 - 0
src/core/InterleavedBufferAttribute.js

@@ -1,4 +1,5 @@
 import { Vector3 } from '../math/Vector3.js';
+import { BufferAttribute } from './BufferAttribute.js';
 
 /**
  * @author benaadams / https://twitter.com/ben_a_adams
@@ -8,6 +9,8 @@ var _vector = new Vector3();
 
 function InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, normalized ) {
 
+	this.name = '';
+
 	this.data = interleavedBuffer;
 	this.itemSize = itemSize;
 	this.offset = offset;
@@ -152,6 +155,57 @@ Object.assign( InterleavedBufferAttribute.prototype, {
 
 		return this;
 
+	},
+
+	clone: function () {
+
+		console.log( 'THREE.InterleavedBufferAttribute.clone(): Cloning an interlaved buffer attribute will deinterleave buffer data.' );
+
+		var array = [];
+
+		for ( var i = 0; i < this.count; i ++ ) {
+
+			var index = i * this.data.stride + this.offset;
+
+			for ( var j = 0; j < this.itemSize; j ++ ) {
+
+				array.push( this.data.array[ index + j ] );
+
+			}
+
+		}
+
+		return new BufferAttribute( new this.array.constructor( array ), this.itemSize, this.normalized );
+
+	},
+
+	toJSON: function () {
+
+		console.log( 'THREE.InterleavedBufferAttribute.toJSON(): Serializing an interlaved buffer attribute will deinterleave buffer data.' );
+
+		var array = [];
+
+		for ( var i = 0; i < this.count; i ++ ) {
+
+			var index = i * this.data.stride + this.offset;
+
+			for ( var j = 0; j < this.itemSize; j ++ ) {
+
+				array.push( this.data.array[ index + j ] );
+
+			}
+
+		}
+
+		// deinterleave data and save it as an ordinary buffer attribute for now
+
+		return {
+			itemSize: this.itemSize,
+			type: this.array.constructor.name,
+			array: array,
+			normalized: this.normalized
+		};
+
 	}
 
 } );