@@ -109,9 +109,8 @@ class MirabufInstance {
109
109
}
110
110
111
111
public constructor ( parser : MirabufParser , materialStyle ?: MaterialStyle , progressHandle ?: ProgressHandle ) {
112
- if ( parser . errors . some ( x => x [ 0 ] >= ParseErrorSeverity . Unimportable ) ) {
112
+ if ( parser . errors . some ( x => x [ 0 ] >= ParseErrorSeverity . Unimportable ) )
113
113
throw new Error ( "Parser has significant errors..." )
114
- }
115
114
116
115
this . _mirabufParser = parser
117
116
this . _materials = new Map ( )
@@ -126,39 +125,32 @@ class MirabufInstance {
126
125
}
127
126
128
127
/**
129
- * Parses all mirabuf appearances into ThreeJs materials.
128
+ * Parses all mirabuf appearances into ThreeJS and Jolt materials.
130
129
*/
131
130
private LoadMaterials ( materialStyle : MaterialStyle ) {
132
131
Object . entries ( this . _mirabufParser . assembly . data ! . materials ! . appearances ! ) . forEach (
133
132
( [ appearanceId , appearance ] ) => {
134
- let hex = 0xe32b50
135
- let opacity = 1.0
136
- if ( appearance . albedo ) {
137
- const { A, B, G, R } = appearance . albedo
138
- if ( A && B && G && R ) {
139
- hex = ( A << 24 ) | ( R << 16 ) | ( G << 8 ) | B
140
- opacity = A / 255.0
141
- }
142
- }
143
-
144
- let material : THREE . Material
145
- if ( materialStyle == MaterialStyle . Regular ) {
146
- material = new THREE . MeshPhongMaterial ( {
147
- color : hex ,
148
- shininess : 0.0 ,
149
- shadowSide : THREE . DoubleSide ,
150
- opacity : opacity ,
151
- transparent : opacity < 1.0 ,
152
- } )
153
- } else if ( materialStyle == MaterialStyle . Normals ) {
154
- material = new THREE . MeshNormalMaterial ( )
155
- } else if ( materialStyle == MaterialStyle . Toon ) {
156
- material = World . SceneRenderer . CreateToonMaterial ( hex , 5 )
157
- console . debug ( "Toon Material" )
158
- }
159
-
160
- World . SceneRenderer . SetupMaterial ( material ! )
161
- this . _materials . set ( appearanceId , material ! )
133
+ const { A, B, G, R } = appearance . albedo ?? { }
134
+ const [ hex , opacity ] =
135
+ A && B && G && R ? [ ( A << 24 ) | ( R << 16 ) | ( G << 8 ) | B , A / 255.0 ] : [ 0xe32b50 , 1.0 ]
136
+
137
+ const material =
138
+ materialStyle === MaterialStyle . Regular
139
+ ? new THREE . MeshStandardMaterial ( {
140
+ // No specular?
141
+ color : hex ,
142
+ roughness : appearance . roughness ?? 0.5 ,
143
+ metalness : appearance . metallic ?? 0.0 ,
144
+ shadowSide : THREE . DoubleSide ,
145
+ opacity : opacity ,
146
+ transparent : opacity < 1.0 ,
147
+ } )
148
+ : materialStyle === MaterialStyle . Normals
149
+ ? new THREE . MeshNormalMaterial ( )
150
+ : World . SceneRenderer . CreateToonMaterial ( hex , 5 )
151
+
152
+ World . SceneRenderer . SetupMaterial ( material )
153
+ this . _materials . set ( appearanceId , material )
162
154
}
163
155
)
164
156
}
@@ -178,61 +170,52 @@ class MirabufInstance {
178
170
179
171
const batchMap = new Map < THREE . Material , Map < string , [ mirabuf . IBody , Array < mirabuf . IPartInstance > ] > > ( )
180
172
const countMap = new Map < THREE . Material , BatchCounts > ( )
181
-
182
173
// Filter all instances by first material, then body
183
- for ( const instance of Object . values ( instances ) ) {
184
- const definition = assembly . data ! . parts ! . partDefinitions ! [ instance . partDefinitionReference ! ] !
185
- const bodies = definition . bodies
186
- if ( bodies ) {
187
- for ( const body of bodies ) {
188
- if ( ! body ) continue
189
- const mesh = body . triangleMesh
190
- if (
191
- mesh &&
192
- mesh . mesh &&
193
- mesh . mesh . verts &&
194
- mesh . mesh . normals &&
195
- mesh . mesh . uv &&
196
- mesh . mesh . indices
197
- ) {
198
- const appearanceOverride = body . appearanceOverride
199
- const material : THREE . Material = WIREFRAME
200
- ? new THREE . MeshStandardMaterial ( { wireframe : true , color : 0x000000 } )
201
- : appearanceOverride && this . _materials . has ( appearanceOverride )
202
- ? this . _materials . get ( appearanceOverride ) !
203
- : fillerMaterials [ nextFillerMaterial ++ % fillerMaterials . length ]
204
-
205
- let materialBodyMap = batchMap . get ( material )
206
- if ( ! materialBodyMap ) {
207
- materialBodyMap = new Map < string , [ mirabuf . IBody , Array < mirabuf . IPartInstance > ] > ( )
208
- batchMap . set ( material , materialBodyMap )
209
- }
210
-
211
- const partBodyGuid = this . GetPartBodyGuid ( definition , body )
212
- let bodyInstances = materialBodyMap . get ( partBodyGuid )
213
- if ( ! bodyInstances ) {
214
- bodyInstances = [ body , new Array < mirabuf . IPartInstance > ( ) ]
215
- materialBodyMap . set ( partBodyGuid , bodyInstances )
216
- }
217
- bodyInstances [ 1 ] . push ( instance )
218
-
219
- if ( countMap . has ( material ) ) {
220
- const count = countMap . get ( material ) !
221
- count . maxInstances += 1
222
- count . maxVertices += mesh . mesh . verts . length / 3
223
- count . maxIndices += mesh . mesh . indices . length
224
- } else {
225
- const count : BatchCounts = {
226
- maxInstances : 1 ,
227
- maxVertices : mesh . mesh . verts . length / 3 ,
228
- maxIndices : mesh . mesh . indices . length ,
229
- }
230
- countMap . set ( material , count )
231
- }
232
- }
174
+ Object . values ( instances ) . forEach ( instance => {
175
+ const definition = assembly . data ! . parts ! . partDefinitions ! [ instance . partDefinitionReference ! ]
176
+ const bodies = definition ?. bodies ?? [ ]
177
+ bodies . forEach ( body => {
178
+ const mesh = body ?. triangleMesh ?. mesh
179
+ if ( ! mesh ?. verts || ! mesh . normals || ! mesh . uv || ! mesh . indices ) return
180
+
181
+ const appearanceOverride = body . appearanceOverride
182
+
183
+ const material = WIREFRAME
184
+ ? new THREE . MeshStandardMaterial ( { wireframe : true , color : 0x000000 } )
185
+ : appearanceOverride && this . _materials . has ( appearanceOverride )
186
+ ? this . _materials . get ( appearanceOverride ) !
187
+ : fillerMaterials [ nextFillerMaterial ++ % fillerMaterials . length ]
188
+
189
+ let materialBodyMap = batchMap . get ( material )
190
+ if ( ! materialBodyMap ) {
191
+ materialBodyMap = new Map < string , [ mirabuf . IBody , Array < mirabuf . IPartInstance > ] > ( )
192
+ batchMap . set ( material , materialBodyMap )
233
193
}
234
- }
235
- }
194
+
195
+ const partBodyGuid = this . GetPartBodyGuid ( definition , body )
196
+ let bodyInstances = materialBodyMap . get ( partBodyGuid )
197
+ if ( ! bodyInstances ) {
198
+ bodyInstances = [ body , new Array < mirabuf . IPartInstance > ( ) ]
199
+ materialBodyMap . set ( partBodyGuid , bodyInstances )
200
+ }
201
+ bodyInstances [ 1 ] . push ( instance )
202
+
203
+ if ( countMap . has ( material ) ) {
204
+ const count = countMap . get ( material ) !
205
+ count . maxInstances += 1
206
+ count . maxVertices += mesh . verts . length / 3
207
+ count . maxIndices += mesh . indices . length
208
+ return
209
+ }
210
+
211
+ const count : BatchCounts = {
212
+ maxInstances : 1 ,
213
+ maxVertices : mesh . verts . length / 3 ,
214
+ maxIndices : mesh . indices . length ,
215
+ }
216
+ countMap . set ( material , count )
217
+ } )
218
+ } )
236
219
237
220
// Construct batched meshes
238
221
batchMap . forEach ( ( materialBodyMap , material ) => {
0 commit comments