@@ -15,10 +15,6 @@ export type GLTFElementToThreeObjectMap = Map<GLTFElement, ThreeObjectSet>;
15
15
export type ThreeObjectToGLTFElementHandleMap =
16
16
Map < ThreeSceneObject , GLTFReference > ;
17
17
18
- interface UserDataMapping {
19
- userData : { associations : any } ;
20
- }
21
-
22
18
const $threeGLTF = Symbol ( 'threeGLTF' ) ;
23
19
const $gltf = Symbol ( 'gltf' ) ;
24
20
const $gltfElementMap = Symbol ( 'gltfElementMap' ) ;
@@ -27,7 +23,6 @@ const $parallelTraverseThreeScene = Symbol('parallelTraverseThreeScene');
27
23
28
24
const $correlateOriginalThreeGLTF = Symbol ( 'correlateOriginalThreeGLTF' ) ;
29
25
const $correlateCloneThreeGLTF = Symbol ( 'correlateCloneThreeGLTF' ) ;
30
- const $getAssociationsData = Symbol ( 'getAssociationsData' ) ;
31
26
32
27
/**
33
28
* The Three.js GLTFLoader provides us with an in-memory representation
@@ -61,56 +56,51 @@ export class CorrelatedSceneGraph {
61
56
}
62
57
}
63
58
64
- static [ $getAssociationsData ] ( object : ThreeSceneObject ) {
65
- // Ensures userData exists on the threeObject.
66
- const userDataMapping = object as UserDataMapping ;
67
- userDataMapping . userData = userDataMapping . userData || { } ;
68
- userDataMapping . userData . associations =
69
- userDataMapping . userData . associations || { } ;
70
- return userDataMapping . userData . associations ;
71
- }
72
-
73
-
74
59
private static [ $correlateOriginalThreeGLTF ] ( threeGLTF : ThreeGLTF ) :
75
60
CorrelatedSceneGraph {
76
61
const gltf = threeGLTF . parser . json as GLTF ;
77
62
78
63
const associations =
79
- threeGLTF . parser . associations as Map < Object3D , GLTFReference > ;
64
+ threeGLTF . parser . associations as Map < ThreeSceneObject , GLTFReference > ;
80
65
const gltfElementMap : GLTFElementToThreeObjectMap = new Map ( ) ;
81
66
82
67
const defaultMaterial = { name : 'Default' } as Material ;
83
68
const defaultReference = { type : 'materials' , index : - 1 } as
84
69
GLTFReferencePair ;
85
70
86
- // NOTE: IE11 does not have Map iterator methods
87
- associations . forEach ( ( gltfMappings , threeObject ) => {
88
- // Note: GLTFLoader creates a "default" material that has no corresponding
89
- // glTF element in the case that no materials are specified in the source
90
- // glTF. In this case we append a default material to allow this to be
91
- // operated upon.
92
- if ( gltfMappings == null ) {
71
+ for ( const threeMaterial of associations . keys ( ) ) {
72
+ // Note: GLTFLoader creates a "default" material that has no
73
+ // corresponding glTF element in the case that no materials are
74
+ // specified in the source glTF. In this case we append a default
75
+ // material to allow this to be operated upon.
76
+ if ( threeMaterial instanceof Material &&
77
+ associations . get ( threeMaterial ) == null ) {
93
78
if ( defaultReference . index < 0 ) {
94
79
if ( gltf . materials == null ) {
95
80
gltf . materials = [ ] ;
96
81
}
97
82
defaultReference . index = gltf . materials . length ;
98
83
gltf . materials . push ( defaultMaterial ) ;
99
-
100
- // Updates the self-lookup user data.
101
- CorrelatedSceneGraph [ $getAssociationsData ] ( threeObject ) . materials =
102
- gltf . materials . length ;
103
84
}
104
85
105
- gltfMappings = { materials : defaultReference . index } ;
86
+ threeMaterial . name = defaultMaterial . name ;
87
+ associations . set ( threeMaterial , { materials : defaultReference . index } ) ;
88
+ }
89
+ }
90
+
91
+ // Creates a reverse look up map (gltf-object to Three-object)
92
+ for ( const [ threeObject , gltfMappings ] of associations ) {
93
+ if ( gltfMappings ) {
94
+ const objWithUserData = threeObject as { userData : { associations : { } } } ;
95
+ objWithUserData . userData = objWithUserData . userData || { } ;
96
+ objWithUserData . userData . associations = gltfMappings ;
106
97
}
107
98
108
99
for ( const mapping in gltfMappings ) {
109
100
if ( mapping != null && mapping !== 'primitives' ) {
110
101
const type = mapping as GLTFReferenceType ;
111
102
const elementArray = gltf [ type ] || [ ] ;
112
103
const gltfElement = elementArray [ gltfMappings [ type ] ! ] ;
113
-
114
104
if ( gltfElement == null ) {
115
105
// TODO: Maybe throw here...
116
106
continue ;
@@ -126,7 +116,7 @@ export class CorrelatedSceneGraph {
126
116
threeObjects . add ( threeObject ) ;
127
117
}
128
118
}
129
- } ) ;
119
+ }
130
120
131
121
return new CorrelatedSceneGraph (
132
122
threeGLTF , gltf , associations , gltfElementMap ) ;
@@ -147,49 +137,14 @@ export class CorrelatedSceneGraph {
147
137
const cloneThreeObjectMap : ThreeObjectToGLTFElementHandleMap = new Map ( ) ;
148
138
const cloneGLTFElementMap : GLTFElementToThreeObjectMap = new Map ( ) ;
149
139
150
- const defaultMaterial = { name : 'Default' } as Material ;
151
- const defaultReference = { materials : - 1 } as GLTFReference ;
152
-
153
140
for ( let i = 0 ; i < originalThreeGLTF . scenes . length ; i ++ ) {
154
141
this [ $parallelTraverseThreeScene ] (
155
142
originalThreeGLTF . scenes [ i ] ,
156
143
cloneThreeGLTF . scenes [ i ] ,
157
144
( object : ThreeSceneObject , cloneObject : ThreeSceneObject ) => {
158
- let elementReference =
145
+ const elementReference =
159
146
upstreamCorrelatedSceneGraph . threeObjectMap . get ( object ) ;
160
147
161
- if ( ( ( object as Mesh ) . isMesh || ( object as Material ) . isMaterial ) &&
162
- elementReference == null ) {
163
- // Checks if default material was already added to the gltf.
164
- if ( cloneGLTF . materials && cloneGLTF . materials . length ) {
165
- const material =
166
- cloneGLTF . materials [ cloneGLTF . materials . length - 1 ] ;
167
- if ( material . name === 'Default' ) {
168
- defaultReference . materials = cloneGLTF . materials . length - 1 ;
169
- CorrelatedSceneGraph [ $getAssociationsData ] ( object ) . materials =
170
- defaultReference . materials ;
171
- }
172
- }
173
-
174
- // Adds the default material if the default material was not
175
- // added.
176
- if ( defaultReference . materials ! < 0 ) {
177
- if ( cloneGLTF . materials == null ) {
178
- cloneGLTF . materials = [ ] ;
179
- }
180
- defaultReference . materials = cloneGLTF . materials . length ;
181
- cloneGLTF . materials . push ( defaultMaterial ) ;
182
-
183
- CorrelatedSceneGraph [ $getAssociationsData ] ( object ) . materials =
184
- defaultReference . materials ;
185
- // Applies the user-data to the cloneObject.
186
- ( cloneObject as UserDataMapping ) . userData =
187
- ( object as UserDataMapping ) . userData ;
188
- }
189
-
190
- elementReference = defaultReference ;
191
- }
192
-
193
148
if ( elementReference == null ) {
194
149
return ;
195
150
}
0 commit comments