Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/core/BufferAttribute.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,13 +394,19 @@ Object.assign( BufferAttribute.prototype, {

toJSON: function () {

return {
const data = {
itemSize: this.itemSize,
type: this.array.constructor.name,
array: Array.prototype.slice.call( this.array ),
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;

}

} );
Expand Down
12 changes: 2 additions & 10 deletions src/core/BufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -929,11 +929,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy

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 );

}

Expand All @@ -950,11 +946,7 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy

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 ) );

}

Expand Down
9 changes: 9 additions & 0 deletions src/loaders/BufferGeometryLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ class BufferGeometryLoader extends Loader {
}

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 );

}
Expand Down
19 changes: 17 additions & 2 deletions test/unit/src/core/BufferAttribute.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,29 @@ export default QUnit.module( 'Core', () => {

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(), {
itemSize: 3,
type: 'Float32Array',
array: [ 1, 2, 3, 4, 5, 6 ],
normalized: true
normalized: false
}, '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
Expand Down
28 changes: 28 additions & 0 deletions test/unit/src/loaders/BufferGeometryLoader.tests.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/* global QUnit */

import { BufferAttribute } from '../../../../src/core/BufferAttribute';
import { BufferGeometry } from '../../../../src/core/BufferGeometry';
import { BufferGeometryLoader } from '../../../../src/loaders/BufferGeometryLoader';
import { DynamicDrawUsage } from '../../../../src/constants';

export default QUnit.module( 'Loaders', () => {

Expand All @@ -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.'
);

} );

} );

} );