Skip to content

Commit 8b16594

Browse files
authored
Merge pull request #17501 from donmccurdy/bug-dracoloader-unique-ids
DRACOLoader: Support both default and 'unique ID' attributes.
2 parents f5780f2 + 98375c2 commit 8b16594

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

examples/js/loaders/DRACOLoader.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ THREE.DRACOLoader.prototype = Object.assign( Object.create( THREE.Loader.prototy
9797

9898
var taskConfig = {
9999
attributeIDs: this.defaultAttributeIDs,
100-
attributeTypes: this.defaultAttributeTypes
100+
attributeTypes: this.defaultAttributeTypes,
101+
useUniqueIDs: false
101102
};
102103

103104
this.decodeGeometry( buffer, taskConfig )
@@ -113,7 +114,8 @@ THREE.DRACOLoader.prototype = Object.assign( Object.create( THREE.Loader.prototy
113114

114115
var taskConfig = {
115116
attributeIDs: attributeIDs || this.defaultAttributeIDs,
116-
attributeTypes: attributeTypes || this.defaultAttributeTypes
117+
attributeTypes: attributeTypes || this.defaultAttributeTypes,
118+
useUniqueIDs: !! attributeIDs
117119
};
118120

119121
this.decodeGeometry( buffer, taskConfig ).then( callback );
@@ -454,12 +456,32 @@ THREE.DRACOLoader.DRACOWorker = function () {
454456

455457
var geometry = { index: null, attributes: [] };
456458

457-
// Add attributes of user specified unique id.
459+
// Gather all vertex attributes.
458460
for ( var attributeName in attributeIDs ) {
459461

460462
var attributeType = self[ attributeTypes[ attributeName ] ];
461-
var attributeId = attributeIDs[ attributeName ];
462-
var attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeId );
463+
464+
var attribute;
465+
var attributeID;
466+
467+
// A Draco file may be created with default vertex attributes, whose attribute IDs
468+
// are mapped 1:1 from their semantic name (POSITION, NORMAL, ...). Alternatively,
469+
// a Draco file may contain a custom set of attributes, identified by known unique
470+
// IDs. glTF files always do the latter, and `.drc` files typically do the former.
471+
if ( taskConfig.useUniqueIDs ) {
472+
473+
attributeID = attributeIDs[ attributeName ];
474+
attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeID );
475+
476+
} else {
477+
478+
attributeID = decoder.GetAttributeId( dracoGeometry, draco[ attributeIDs[ attributeName ] ] );
479+
480+
if ( attributeID === -1 ) continue;
481+
482+
attribute = decoder.GetAttribute( dracoGeometry, attributeID );
483+
484+
}
463485

464486
geometry.attributes.push( decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) );
465487

examples/jsm/loaders/DRACOLoader.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ DRACOLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
104104

105105
var taskConfig = {
106106
attributeIDs: this.defaultAttributeIDs,
107-
attributeTypes: this.defaultAttributeTypes
107+
attributeTypes: this.defaultAttributeTypes,
108+
useUniqueIDs: false
108109
};
109110

110111
this.decodeGeometry( buffer, taskConfig )
@@ -120,7 +121,8 @@ DRACOLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
120121

121122
var taskConfig = {
122123
attributeIDs: attributeIDs || this.defaultAttributeIDs,
123-
attributeTypes: attributeTypes || this.defaultAttributeTypes
124+
attributeTypes: attributeTypes || this.defaultAttributeTypes,
125+
useUniqueIDs: !! attributeIDs
124126
};
125127

126128
this.decodeGeometry( buffer, taskConfig ).then( callback );
@@ -461,12 +463,32 @@ DRACOLoader.DRACOWorker = function () {
461463

462464
var geometry = { index: null, attributes: [] };
463465

464-
// Add attributes of user specified unique id.
466+
// Gather all vertex attributes.
465467
for ( var attributeName in attributeIDs ) {
466468

467469
var attributeType = self[ attributeTypes[ attributeName ] ];
468-
var attributeId = attributeIDs[ attributeName ];
469-
var attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeId );
470+
471+
var attribute;
472+
var attributeID;
473+
474+
// A Draco file may be created with default vertex attributes, whose attribute IDs
475+
// are mapped 1:1 from their semantic name (POSITION, NORMAL, ...). Alternatively,
476+
// a Draco file may contain a custom set of attributes, identified by known unique
477+
// IDs. glTF files always do the latter, and `.drc` files typically do the former.
478+
if ( taskConfig.useUniqueIDs ) {
479+
480+
attributeID = attributeIDs[ attributeName ];
481+
attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeID );
482+
483+
} else {
484+
485+
attributeID = decoder.GetAttributeId( dracoGeometry, draco[ attributeIDs[ attributeName ] ] );
486+
487+
if ( attributeID === -1 ) continue;
488+
489+
attribute = decoder.GetAttribute( dracoGeometry, attributeID );
490+
491+
}
470492

471493
geometry.attributes.push( decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) );
472494

0 commit comments

Comments
 (0)