Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Morph Attributes to be serialized/deserialized in Buffer Geometry #15792

Closed
Closed
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
119 changes: 119 additions & 0 deletions examples/models/json/boxmorph_buffergeometry.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
{
"metadata": {
"version": 4.5,
"type": "BufferGeometry",
"generator": "BufferGeometry.toJSON"
},
"uuid": "0034AD0F-BDC9-4884-A9ED-C9750293AC10",
"type": "BufferGeometry",
"morphAttributes": {
"position": [
{
"itemSize": 3,
"type": "Float32Array",
"array": [100,100,100,50,-50,50,50,50,-50,50,-50,50,50,-50,-50,50,50,-50,-50,50,-50,-50,-50,-50,-50,50,50,-50,-50,-50,-50,-50,50,-50,50,50,-50,50,-50,-50,50,50,50,50,-50,-50,50,50,100,100,100,50,50,-50,-50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,50,-50,-50,50,-50,50,-50,50,50,-50,-50,50,100,100,100,-50,-50,50,50,-50,50,100,100,100,50,50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,-50,-50,-50,50,-50],
"normalized": false
},
{
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,50,-50,50,100,100,-100,50,-50,50,50,-50,-50,100,100,-100,-50,50,-50,-50,-50,-50,-50,50,50,-50,-50,-50,-50,-50,50,-50,50,50,-50,50,-50,-50,50,50,100,100,-100,-50,50,50,50,50,50,100,100,-100,-50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,50,-50,-50,50,-50,50,-50,50,50,-50,-50,50,50,50,50,-50,-50,50,50,-50,50,50,50,50,100,100,-100,50,-50,-50,-50,50,-50,50,-50,-50,-50,-50,-50,-50,50,-50],
"normalized": false
},
{
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,100,-100,100,50,50,-50,100,-100,100,50,-50,-50,50,50,-50,-50,50,-50,-50,-50,-50,-50,50,50,-50,-50,-50,-50,-50,50,-50,50,50,-50,50,-50,-50,50,50,50,50,-50,-50,50,50,50,50,50,50,50,-50,-50,-50,50,-50,-50,-50,100,-100,100,-50,-50,-50,50,-50,-50,100,-100,100,-50,50,50,-50,-50,50,50,50,50,-50,-50,50,100,-100,100,50,50,50,50,50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,-50,-50,-50,50,-50],
"normalized": false
},
{
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,50,-50,50,50,50,-50,50,-50,50,100,-100,-100,50,50,-50,-50,50,-50,-50,-50,-50,-50,50,50,-50,-50,-50,-50,-50,50,-50,50,50,-50,50,-50,-50,50,50,50,50,-50,-50,50,50,50,50,50,50,50,-50,-50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,100,-100,-100,50,-50,50,-50,50,50,-50,-50,50,50,50,50,-50,-50,50,50,-50,50,50,50,50,50,50,-50,100,-100,-100,-50,50,-50,100,-100,-100,-50,-50,-50,-50,50,-50],
"normalized": false
},
{
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,50,-50,50,50,50,-50,50,-50,50,50,-50,-50,50,50,-50,-100,100,-100,-50,-50,-50,-50,50,50,-50,-50,-50,-50,-50,50,-50,50,50,-100,100,-100,-50,50,50,50,50,-50,-50,50,50,50,50,50,50,50,-50,-50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,50,-50,-50,50,-50,50,-50,50,50,-50,-50,50,50,50,50,-50,-50,50,50,-50,50,50,50,50,50,50,-50,50,-50,-50,-100,100,-100,50,-50,-50,-50,-50,-50,-100,100,-100],
"normalized": false
},
{
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,50,-50,50,50,50,-50,50,-50,50,50,-50,-50,50,50,-50,-50,50,-50,-50,-50,-50,-100,100,100,-50,-50,-50,-50,-50,50,-100,100,100,-50,50,-50,-100,100,100,50,50,-50,-100,100,100,50,50,50,50,50,-50,-50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,50,-50,-50,50,-50,50,-100,100,100,-50,-50,50,50,50,50,-50,-50,50,50,-50,50,50,50,50,50,50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,-50,-50,-50,50,-50],
"normalized": false
},
{
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,50,-50,50,50,50,-50,50,-50,50,50,-50,-50,50,50,-50,-50,50,-50,-100,-100,-100,-50,50,50,-100,-100,-100,-50,-50,50,-50,50,50,-50,50,-50,-50,50,50,50,50,-50,-50,50,50,50,50,50,50,50,-50,-50,-50,50,-100,-100,-100,50,-50,50,-100,-100,-100,50,-50,-50,50,-50,50,-50,50,50,-50,-50,50,50,50,50,-50,-50,50,50,-50,50,50,50,50,50,50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-100,-100,-100,-50,50,-50],
"normalized": false
},
{
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,50,-50,50,50,50,-50,50,-50,50,50,-50,-50,50,50,-50,-50,50,-50,-50,-50,-50,-50,50,50,-50,-50,-50,-100,-100,100,-50,50,50,-50,50,-50,-50,50,50,50,50,-50,-50,50,50,50,50,50,50,50,-50,-100,-100,100,-50,-50,-50,50,-50,50,-50,-50,-50,50,-50,-50,50,-50,50,-50,50,50,-100,-100,100,50,50,50,-100,-100,100,50,-50,50,50,50,50,50,50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,-50,-50,-50,50,-50],
"normalized": false
}]
},
"data": {
"attributes": {
"position": {
"itemSize": 3,
"type": "Float32Array",
"array": [50,50,50,50,-50,50,50,50,-50,50,-50,50,50,-50,-50,50,50,-50,-50,50,-50,-50,-50,-50,-50,50,50,-50,-50,-50,-50,-50,50,-50,50,50,-50,50,-50,-50,50,50,50,50,-50,-50,50,50,50,50,50,50,50,-50,-50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,50,-50,-50,50,-50,50,-50,50,50,-50,-50,50,50,50,50,-50,-50,50,50,-50,50,50,50,50,50,50,-50,50,-50,-50,-50,50,-50,50,-50,-50,-50,-50,-50,-50,50,-50],
"normalized": false
},
"normal": {
"itemSize": 3,
"type": "Float32Array",
"array": [1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1,0,0,-1],
"normalized": false
},
"color": {
"itemSize": 3,
"type": "Float32Array",
"array": [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1],
"normalized": false
},
"uv": {
"itemSize": 2,
"type": "Float32Array",
"array": [0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0,1,0,0,1,1,0,0,1,0,1,1],
"normalized": false
}
},
"groups": [
{
"start": 0,
"materialIndex": 0,
"count": 6
},
{
"start": 6,
"materialIndex": 1,
"count": 6
},
{
"start": 12,
"materialIndex": 2,
"count": 6
},
{
"start": 18,
"materialIndex": 3,
"count": 6
},
{
"start": 24,
"materialIndex": 4,
"count": 6
},
{
"start": 30,
"materialIndex": 5,
"count": 6
}]
}
}
42 changes: 42 additions & 0 deletions src/core/BufferGeometry.js
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,48 @@ BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototy
if ( this.name !== '' ) data.name = this.name;
if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;

data.morphAttributes = {};

// serialize position morphs if they exist
if ( Array.isArray( this.morphAttributes.position ) ) {

data.morphAttributes.position = [];
for ( var i = 0; i < this.morphAttributes.position.length; ++ i ) {

var position = this.morphAttributes.position[ i ];
var array = Array.prototype.slice.call( position.array );

data.morphAttributes.position.push( {
itemSize: position.itemSize,
type: position.array.constructor.name,
array: array,
normalized: position.normalized
} );

}

}

// serialize normal morphs if they exist
if ( Array.isArray( this.morphAttributes.normal ) ) {

data.morphAttributes.normal = [];
for ( var i = 0; i < this.morphAttributes.normal.length; ++ i ) {

var normal = this.morphAttributes.normal[ i ];
var array = Array.prototype.slice.call( normal.array );

data.morphAttributes.normal.push( {
itemSize: normal.itemSize,
type: normal.array.constructor.name,
array: array,
normalized: normal.normalized
} );

}

}

if ( this.parameters !== undefined ) {

var parameters = this.parameters;
Expand Down
42 changes: 42 additions & 0 deletions src/loaders/BufferGeometryLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,51 @@ Object.assign( BufferGeometryLoader.prototype, {

if ( json.name ) geometry.name = json.name;
if ( json.userData ) geometry.userData = json.userData;
geometry.morphAttributes = this.parseMorphAttributes( json );

return geometry;

},

parseMorphAttributes: function ( json ) {

var newMorphAttributes = {};

var morphAttributes = json.morphAttributes;
if ( morphAttributes !== undefined ) {

if ( morphAttributes.position !== undefined ) {

newMorphAttributes.position = [];
for ( var i = 0; i < morphAttributes.position.length; ++ i ) {

var position = morphAttributes.position[ i ];
var typedArray = new TYPED_ARRAYS[ position.type ]( position.array );
newMorphAttributes.position.push( new BufferAttribute( typedArray, position.itemSize, position.normalized ) );

}

}

if ( morphAttributes.normal !== undefined ) {

newMorphAttributes.normal = [];
for ( var i = 0; i < morphAttributes.normal.length; ++ i ) {

var normal = morphAttributes.normal[ i ];
var typedArray = new TYPED_ARRAYS[ normal.type ]( normal.array );
newMorphAttributes.normal.push( new BufferAttribute( typedArray, normal.itemSize, normal.normalized ) );

}

}

}

return newMorphAttributes;

},

setPath: function ( value ) {

this.path = value;
Expand All @@ -101,6 +141,8 @@ Object.assign( BufferGeometryLoader.prototype, {

} );



var TYPED_ARRAYS = {
Int8Array: Int8Array,
Uint8Array: Uint8Array,
Expand Down
3 changes: 3 additions & 0 deletions src/loaders/ObjectLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,9 @@ Object.assign( ObjectLoader.prototype, {
if ( data.name !== undefined ) geometry.name = data.name;
if ( geometry.isBufferGeometry === true && data.userData !== undefined ) geometry.userData = data.userData;

// Morph Attributes are already loaded for BufferGeometry at this point
if ( geometry.isBufferGeometry === true && data.type != 'BufferGeometry' ) geometry.morphAttributes = bufferGeometryLoader.parseMorphAttributes( json );

geometries[ data.uuid ] = geometry;

}
Expand Down