Skip to content

Commit 5c1ee32

Browse files
authored
Merge pull request #15809 from takahirox/FixBufferGeometrySerialization
Fix and clean up BufferGeometry/Attribute serialization
2 parents 21224cd + 61a4919 commit 5c1ee32

File tree

3 files changed

+63
-28
lines changed

3 files changed

+63
-28
lines changed

src/core/BufferGeometry.js

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -980,17 +980,15 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
980980

981981
}
982982

983-
data.data = { attributes: {}, morphAttributes: {} };
983+
data.data = { attributes: {} };
984984

985985
var index = this.index;
986986

987987
if ( index !== null ) {
988988

989-
var array = Array.prototype.slice.call( index.array );
990-
991989
data.data.index = {
992990
type: index.array.constructor.name,
993-
array: array
991+
array: Array.prototype.slice.call( index.array )
994992
};
995993

996994
}
@@ -1001,20 +999,23 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
1001999

10021000
var attribute = attributes[ key ];
10031001

1004-
var array = Array.prototype.slice.call( attribute.array );
1005-
1006-
data.data.attributes[ key ] = {
1002+
var attributeData = {
10071003
itemSize: attribute.itemSize,
10081004
type: attribute.array.constructor.name,
1009-
array: array,
1005+
array: Array.prototype.slice.call( attribute.array ),
10101006
normalized: attribute.normalized
10111007
};
10121008

1009+
if ( attribute.name !== '' ) attributeData.name = attribute.name;
1010+
1011+
data.data.attributes[ key ] = attributeData;
1012+
10131013
}
10141014

1015-
var morphAttributes = this.morphAttributes;
1015+
var morphAttributes = {};
1016+
var hasMorphAttributes = false;
10161017

1017-
for ( var key in morphAttributes ) {
1018+
for ( var key in this.morphAttributes ) {
10181019

10191020
var attributeArray = this.morphAttributes[ key ];
10201021

@@ -1024,20 +1025,31 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
10241025

10251026
var attribute = attributeArray[ i ];
10261027

1027-
array.push( {
1028-
name: attribute.name,
1028+
var attributeData = {
10291029
itemSize: attribute.itemSize,
10301030
type: attribute.array.constructor.name,
10311031
array: Array.prototype.slice.call( attribute.array ),
10321032
normalized: attribute.normalized
1033-
} );
1033+
};
1034+
1035+
if ( attribute.name !== '' ) attributeData.name = attribute.name;
1036+
1037+
array.push( attributeData );
10341038

10351039
}
10361040

1037-
data.data.morphAttributes[ key ] = array;
1041+
if ( array.length > 0 ) {
1042+
1043+
morphAttributes[ key ] = array;
1044+
1045+
hasMorphAttributes = true;
1046+
1047+
}
10381048

10391049
}
10401050

1051+
if ( hasMorphAttributes ) data.data.morphAttributes = morphAttributes;
1052+
10411053
var groups = this.groups;
10421054

10431055
if ( groups.length > 0 ) {

src/loaders/BufferGeometryLoader.js

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,30 +51,36 @@ Object.assign( BufferGeometryLoader.prototype, {
5151
var attribute = attributes[ key ];
5252
var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
5353

54-
geometry.addAttribute( key, new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ) );
54+
var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
55+
if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
56+
geometry.addAttribute( key, bufferAttribute );
5557

5658
}
5759

5860
var morphAttributes = json.data.morphAttributes;
5961

60-
for ( var key in morphAttributes ) {
62+
if ( morphAttributes ) {
6163

62-
var attributeArray = morphAttributes[ key ];
64+
for ( var key in morphAttributes ) {
6365

64-
var array = [];
66+
var attributeArray = morphAttributes[ key ];
6567

66-
for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
68+
var array = [];
6769

68-
var attribute = attributeArray[ i ];
69-
var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
70+
for ( var i = 0, il = attributeArray.length; i < il; i ++ ) {
7071

71-
var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
72-
if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
73-
array.push( bufferAttribute );
72+
var attribute = attributeArray[ i ];
73+
var typedArray = new TYPED_ARRAYS[ attribute.type ]( attribute.array );
7474

75-
}
75+
var bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
76+
if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
77+
array.push( bufferAttribute );
78+
79+
}
7680

77-
geometry.morphAttributes[ key ] = array;
81+
geometry.morphAttributes[ key ] = array;
82+
83+
}
7884

7985
}
8086

test/unit/src/core/BufferGeometry.tests.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ export default QUnit.module( 'Core', () => {
809809

810810
var index = new BufferAttribute( new Uint16Array( [ 0, 1, 2, 3 ] ), 1 );
811811
var attribute1 = new BufferAttribute( new Uint16Array( [ 1, 3, 5, 7 ] ), 1 );
812+
attribute1.name = "attribute1";
812813
var a = new BufferGeometry();
813814
a.name = "JSONQUnit.test";
814815
// a.parameters = { "placeholder": 0 };
@@ -817,7 +818,6 @@ export default QUnit.module( 'Core', () => {
817818
a.addGroup( 0, 1, 2 );
818819
a.boundingSphere = new Sphere( new Vector3( x, y, z ), 0.5 );
819820
var j = a.toJSON();
820-
821821
var gold = {
822822
"metadata": {
823823
"version": 4.5,
@@ -833,7 +833,8 @@ export default QUnit.module( 'Core', () => {
833833
"itemSize": 1,
834834
"type": "Uint16Array",
835835
"array": [ 1, 3, 5, 7 ],
836-
"normalized": false
836+
"normalized": false,
837+
"name": "attribute1"
837838
}
838839
},
839840
"index": {
@@ -856,6 +857,22 @@ export default QUnit.module( 'Core', () => {
856857

857858
assert.deepEqual( j, gold, "Generated JSON is as expected" );
858859

860+
// add morphAttributes
861+
a.morphAttributes.attribute1 = [];
862+
a.morphAttributes.attribute1.push( attribute1.clone() );
863+
j = a.toJSON();
864+
gold.data.morphAttributes = {
865+
"attribute1": [ {
866+
"itemSize": 1,
867+
"type": "Uint16Array",
868+
"array": [ 1, 3, 5, 7 ],
869+
"normalized": false,
870+
"name": "attribute1"
871+
} ]
872+
};
873+
874+
assert.deepEqual( j, gold, "Generated JSON with morphAttributes is as expected" );
875+
859876
} );
860877

861878
QUnit.test( "clone", ( assert ) => {

0 commit comments

Comments
 (0)