1
- use std:: sync:: Arc ;
2
-
3
1
use ahash:: { HashMap , HashMapExt } ;
4
2
use gltf:: texture:: WrappingMode ;
5
3
use itertools:: Itertools ;
6
4
use smallvec:: SmallVec ;
7
5
8
6
use crate :: {
9
- mesh:: { GpuMesh , Material , Mesh , MeshError } ,
10
- renderer:: MeshInstance ,
7
+ mesh:: { CpuMesh , Material , MeshError } ,
11
8
resource_managers:: { GpuTexture2D , ImageDataDesc , TextureManager2D } ,
12
- RenderContext , Rgba32Unmul ,
9
+ CpuMeshInstance , CpuModel , CpuModelMeshKey , RenderContext , Rgba32Unmul ,
13
10
} ;
14
11
15
12
#[ derive( thiserror:: Error , Debug ) ]
@@ -41,7 +38,7 @@ pub fn load_gltf_from_buffer(
41
38
mesh_name : & str ,
42
39
buffer : & [ u8 ] ,
43
40
ctx : & RenderContext ,
44
- ) -> Result < Vec < MeshInstance > , GltfImportError > {
41
+ ) -> Result < CpuModel , GltfImportError > {
45
42
re_tracing:: profile_function!( ) ;
46
43
47
44
let ( doc, buffers, images) = {
@@ -105,25 +102,28 @@ pub fn load_gltf_from_buffer(
105
102
} ) ;
106
103
}
107
104
108
- let mut meshes = HashMap :: with_capacity ( doc. meshes ( ) . len ( ) ) ;
105
+ let mut re_model = CpuModel :: default ( ) ;
106
+ let mut mesh_keys = HashMap :: with_capacity ( doc. meshes ( ) . len ( ) ) ;
109
107
for ref mesh in doc. meshes ( ) {
110
108
re_tracing:: profile_scope!( "mesh" ) ;
111
109
112
110
let re_mesh = import_mesh ( mesh, & buffers, & images_as_textures, & ctx. texture_manager_2d ) ?;
113
- meshes. insert (
114
- mesh. index ( ) ,
115
- ( Arc :: new ( GpuMesh :: new ( ctx, & re_mesh) ?) , Arc :: new ( re_mesh) ) ,
116
- ) ;
111
+ let re_mesh_key = re_model. meshes . insert ( re_mesh) ;
112
+ mesh_keys. insert ( mesh. index ( ) , re_mesh_key) ;
117
113
}
118
114
119
- let mut instances = Vec :: new ( ) ;
120
115
for scene in doc. scenes ( ) {
121
116
for node in scene. nodes ( ) {
122
- gather_instances_recursive ( & mut instances, & node, & glam:: Affine3A :: IDENTITY , & meshes) ;
117
+ gather_instances_recursive (
118
+ & mut re_model. instances ,
119
+ & node,
120
+ & glam:: Affine3A :: IDENTITY ,
121
+ & mesh_keys,
122
+ ) ;
123
123
}
124
124
}
125
125
126
- Ok ( instances )
126
+ Ok ( re_model )
127
127
}
128
128
129
129
fn map_format ( format : gltf:: image:: Format ) -> Option < wgpu:: TextureFormat > {
@@ -154,7 +154,7 @@ fn import_mesh(
154
154
buffers : & [ gltf:: buffer:: Data ] ,
155
155
gpu_image_handles : & [ GpuTexture2D ] ,
156
156
texture_manager : & TextureManager2D , //imported_materials: HashMap<usize, Material>,
157
- ) -> Result < Mesh , GltfImportError > {
157
+ ) -> Result < CpuMesh , GltfImportError > {
158
158
re_tracing:: profile_function!( ) ;
159
159
160
160
let mesh_name = mesh. name ( ) . map_or ( "<unknown" , |f| f) . to_owned ( ) ;
@@ -277,7 +277,7 @@ fn import_mesh(
277
277
return Err ( GltfImportError :: NoTrianglePrimitives { mesh_name } ) ;
278
278
}
279
279
280
- let mesh = Mesh {
280
+ let mesh = CpuMesh {
281
281
label : mesh. name ( ) . into ( ) ,
282
282
triangle_indices,
283
283
vertex_positions,
@@ -293,10 +293,10 @@ fn import_mesh(
293
293
}
294
294
295
295
fn gather_instances_recursive (
296
- instances : & mut Vec < MeshInstance > ,
296
+ instances : & mut Vec < CpuMeshInstance > ,
297
297
node : & gltf:: Node < ' _ > ,
298
298
transform : & glam:: Affine3A ,
299
- meshes : & HashMap < usize , ( Arc < GpuMesh > , Arc < Mesh > ) > ,
299
+ meshes : & HashMap < usize , CpuModelMeshKey > ,
300
300
) {
301
301
let ( scale, rotation, translation) = match node. transform ( ) {
302
302
gltf:: scene:: Transform :: Matrix { matrix } => {
@@ -324,11 +324,11 @@ fn gather_instances_recursive(
324
324
}
325
325
326
326
if let Some ( mesh) = node. mesh ( ) {
327
- if let Some ( ( gpu_mesh , mesh ) ) = meshes. get ( & mesh. index ( ) ) {
328
- let mut gpu_mesh =
329
- MeshInstance :: new_with_cpu_mesh ( gpu_mesh . clone ( ) , Some ( mesh . clone ( ) ) ) ;
330
- gpu_mesh . world_from_mesh = transform;
331
- instances . push ( gpu_mesh ) ;
327
+ if let Some ( mesh_key ) = meshes. get ( & mesh. index ( ) ) {
328
+ instances . push ( CpuMeshInstance {
329
+ mesh : * mesh_key ,
330
+ world_from_mesh : transform,
331
+ } ) ;
332
332
}
333
333
}
334
334
}
0 commit comments