Sfoglia il codice sorgente

BufferAttribute .name, .usage, .updateRange serialization (#21279)

* Serialize BufferAttribute .name, .usage, and .updateRange

* Tests: Update BufferAttribute unit test to check .name, .usage, and .updateRange serialization

* Tests: Add BufferLoader unit test to check if serialized BufferAttribute can be deserialized

* Remove BufferAttribute.name serialization from BufferGeometry.toJSON() because it should be done in BufferAttribute.toJSON()
Takahiro 4 anni fa
parent
commit
7821479720

+ 7 - 1
src/core/BufferAttribute.js

@@ -394,13 +394,19 @@ Object.assign( BufferAttribute.prototype, {
 
 
 	toJSON: function () {
 	toJSON: function () {
 
 
-		return {
+		const data = {
 			itemSize: this.itemSize,
 			itemSize: this.itemSize,
 			type: this.array.constructor.name,
 			type: this.array.constructor.name,
 			array: Array.prototype.slice.call( this.array ),
 			array: Array.prototype.slice.call( this.array ),
 			normalized: this.normalized
 			normalized: this.normalized
 		};
 		};
 
 
+		if ( this.name !== '' ) data.name = this.name;
+		if ( this.usage !== StaticDrawUsage ) data.usage = this.usage;
+		if ( this.updateRange.offset !== 0 || this.updateRange.count !== - 1 ) data.updateRange = this.updateRange;
+
+		return data;
+
 	}
 	}
 
 
 } );
 } );

+ 2 - 10
src/core/BufferGeometry.js

@@ -931,11 +931,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 
 			const attribute = attributes[ key ];
 			const attribute = attributes[ key ];
 
 
-			const attributeData = attribute.toJSON( data.data );
-
-			if ( attribute.name !== '' ) attributeData.name = attribute.name;
-
-			data.data.attributes[ key ] = attributeData;
+			data.data.attributes[ key ] = attribute.toJSON( data.data );
 
 
 		}
 		}
 
 
@@ -952,11 +948,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
 
 
 				const attribute = attributeArray[ i ];
 				const attribute = attributeArray[ i ];
 
 
-				const attributeData = attribute.toJSON( data.data );
-
-				if ( attribute.name !== '' ) attributeData.name = attribute.name;
-
-				array.push( attributeData );
+				array.push( attribute.toJSON( data.data ) );
 
 
 			}
 			}
 
 

+ 9 - 0
src/loaders/BufferGeometryLoader.js

@@ -123,6 +123,15 @@ class BufferGeometryLoader extends Loader {
 			}
 			}
 
 
 			if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
 			if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
+			if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage );
+
+			if ( attribute.updateRange !== undefined ) {
+
+				bufferAttribute.updateRange.offset = attribute.updateRange.offset;
+				bufferAttribute.updateRange.count = attribute.updateRange.count;
+
+			}
+
 			geometry.setAttribute( key, bufferAttribute );
 			geometry.setAttribute( key, bufferAttribute );
 
 
 		}
 		}

+ 17 - 2
test/unit/src/core/BufferAttribute.tests.js

@@ -245,14 +245,29 @@ export default QUnit.module( 'Core', () => {
 
 
 		QUnit.test( "toJSON", ( assert ) => {
 		QUnit.test( "toJSON", ( assert ) => {
 
 
-			const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3, true );
+			const attr = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 );
 			assert.deepEqual( attr.toJSON(), {
 			assert.deepEqual( attr.toJSON(), {
 				itemSize: 3,
 				itemSize: 3,
 				type: 'Float32Array',
 				type: 'Float32Array',
 				array: [ 1, 2, 3, 4, 5, 6 ],
 				array: [ 1, 2, 3, 4, 5, 6 ],
-				normalized: true
+				normalized: false
 			}, 'Serialized to JSON as expected' );
 			}, 'Serialized to JSON as expected' );
 
 
+			const attr2 = new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3, true );
+			attr2.name = 'attributeName';
+			attr2.setUsage( DynamicDrawUsage );
+			attr2.updateRange.offset = 1;
+			attr2.updateRange.count = 2;
+			assert.deepEqual( attr2.toJSON(), {
+				itemSize: 3,
+				type: 'Float32Array',
+				array: [ 1, 2, 3, 4, 5, 6 ],
+				normalized: true,
+				name: 'attributeName',
+				usage: DynamicDrawUsage,
+				updateRange: { offset: 1, count: 2 }
+			}, 'Serialized to JSON as expected with non-default values' );
+
 		} );
 		} );
 
 
 		// OTHERS
 		// OTHERS

+ 28 - 0
test/unit/src/loaders/BufferGeometryLoader.tests.js

@@ -1,6 +1,9 @@
 /* global QUnit */
 /* global QUnit */
 
 
+import { BufferAttribute } from '../../../../src/core/BufferAttribute';
+import { BufferGeometry } from '../../../../src/core/BufferGeometry';
 import { BufferGeometryLoader } from '../../../../src/loaders/BufferGeometryLoader';
 import { BufferGeometryLoader } from '../../../../src/loaders/BufferGeometryLoader';
+import { DynamicDrawUsage } from '../../../../src/constants';
 
 
 export default QUnit.module( 'Loaders', () => {
 export default QUnit.module( 'Loaders', () => {
 
 
@@ -26,6 +29,31 @@ export default QUnit.module( 'Loaders', () => {
 
 
 		} );
 		} );
 
 
+		QUnit.test( "parser - attributes - circlable", ( assert ) => {
+
+			const loader = new BufferGeometryLoader();
+			const geometry = new BufferGeometry();
+			const attr = new BufferAttribute( new Float32Array( [ 7, 8, 9, 10, 11, 12 ] ), 2, true );
+			attr.name = 'attribute';
+			attr.setUsage( DynamicDrawUsage );
+			attr.updateRange.offset = 1;
+			attr.updateRange.count = 2;
+
+			geometry.setAttribute( 'attr', attr );
+
+			const geometry2 = loader.parse( geometry.toJSON() );
+
+			assert.ok( geometry2.getAttribute( 'attr' ),
+				'Serialized attribute can be deserialized under the same attribute key.' );
+
+			assert.deepEqual(
+				geometry.getAttribute( 'attr' ),
+				geometry2.getAttribute( 'attr' ),
+				'Serialized attribute can be deserialized correctly.'
+			);
+
+		} );
+
 	} );
 	} );
 
 
 } );
 } );