@@ -41,8 +41,11 @@ impl Model {
4141 ..Default :: default ( )
4242 } ;
4343
44- let ( models, materials_result) = tobj:: load_obj ( obj_path, & load_options)
45- . map_err ( |e| RendererError :: ModelLoad ( format ! ( "{}: {}" , path, e) ) ) ?;
44+ let ( models, materials_result) =
45+ tobj:: load_obj ( obj_path, & load_options) . map_err ( |e| RendererError :: ModelLoad {
46+ path : path. to_string ( ) ,
47+ source : Box :: new ( e) ,
48+ } ) ?;
4649
4750 // Load materials — warn on failure, fall back to empty vec.
4851 let materials = match materials_result {
@@ -55,7 +58,7 @@ impl Model {
5558
5659 let mut vertices: Vec < Vertex > = Vec :: new ( ) ;
5760 let mut faces: Vec < Face > = Vec :: new ( ) ;
58- let mut texture_cache: HashMap < PathBuf , Texture > = HashMap :: new ( ) ;
61+ let mut texture_cache: HashMap < PathBuf , Arc < Texture > > = HashMap :: new ( ) ;
5962
6063 for model in & models {
6164 let mesh = & model. mesh ;
@@ -134,7 +137,7 @@ impl Model {
134137 material_id : Option < usize > ,
135138 materials : & [ tobj:: Material ] ,
136139 directory : & str ,
137- texture_cache : & mut HashMap < PathBuf , Texture > ,
140+ texture_cache : & mut HashMap < PathBuf , Arc < Texture > > ,
138141 ) -> Material {
139142 let mat_idx = match material_id {
140143 Some ( idx) if idx < materials. len ( ) => idx,
@@ -193,22 +196,23 @@ impl Model {
193196 fn load_texture_cached (
194197 texture_name : Option < & str > ,
195198 directory : & str ,
196- cache : & mut HashMap < PathBuf , Texture > ,
197- ) -> Option < Texture > {
199+ cache : & mut HashMap < PathBuf , Arc < Texture > > ,
200+ ) -> Option < Arc < Texture > > {
198201 let name = texture_name?;
199202 if name. is_empty ( ) {
200203 return None ;
201204 }
202205
203206 let full_path = PathBuf :: from ( directory) . join ( name) ;
204207
205- if let Some ( cached ) = cache. get ( & full_path) {
206- return Some ( cached . clone ( ) ) ;
208+ if cache. contains_key ( & full_path) {
209+ return Some ( Arc :: clone ( cache . get ( & full_path ) . unwrap ( ) ) ) ;
207210 }
208211
209212 match Texture :: load_from_file ( & full_path) {
210213 Ok ( tex) => {
211- cache. insert ( full_path, tex. clone ( ) ) ;
214+ let tex = Arc :: new ( tex) ;
215+ cache. insert ( full_path, Arc :: clone ( & tex) ) ;
212216 Some ( tex)
213217 }
214218 Err ( e) => {
@@ -244,8 +248,8 @@ mod tests {
244248 let result = Model :: load ( "/nonexistent/path/model.obj" ) ;
245249 assert ! ( result. is_err( ) ) ;
246250 match result. unwrap_err ( ) {
247- RendererError :: ModelLoad ( msg ) => {
248- assert ! ( !msg . is_empty( ) ) ;
251+ RendererError :: ModelLoad { path , .. } => {
252+ assert ! ( !path . is_empty( ) ) ;
249253 }
250254 other => panic ! ( "Expected ModelLoad error, got: {:?}" , other) ,
251255 }
0 commit comments