fyrox_impl/resource/model/loader.rs
1// Copyright (c) 2019-present Dmitry Stepanov and Fyrox Engine contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in all
11// copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19// SOFTWARE.
20
21//! Model loader.
22
23use crate::{
24 asset::{
25 io::ResourceIo,
26 loader::{
27 BoxedImportOptionsLoaderFuture, BoxedLoaderFuture, LoaderPayload, ResourceLoader,
28 },
29 manager::ResourceManager,
30 options::{try_get_import_settings, try_get_import_settings_opaque, BaseImportOptions},
31 },
32 core::{uuid::Uuid, TypeUuidProvider},
33 engine::SerializationContext,
34 resource::model::{Model, ModelImportOptions},
35};
36use fyrox_resource::state::LoadError;
37use std::{path::PathBuf, sync::Arc};
38
39/// Default implementation for model loading.
40pub struct ModelLoader {
41 /// Resource manager to allow complex model loading.
42 pub resource_manager: ResourceManager,
43 /// Node constructors contains a set of constructors that allows to build a node using its
44 /// type UUID.
45 pub serialization_context: Arc<SerializationContext>,
46 /// Default import options for model resources.
47 pub default_import_options: ModelImportOptions,
48}
49
50impl ResourceLoader for ModelLoader {
51 fn extensions(&self) -> &[&str] {
52 &["rgs", "fbx"]
53 }
54
55 fn data_type_uuid(&self) -> Uuid {
56 Model::type_uuid()
57 }
58
59 fn load(&self, path: PathBuf, io: Arc<dyn ResourceIo>) -> BoxedLoaderFuture {
60 let resource_manager = self.resource_manager.clone();
61 let node_constructors = self.serialization_context.clone();
62 let default_import_options = self.default_import_options.clone();
63
64 Box::pin(async move {
65 let io = io.as_ref();
66
67 let import_options = try_get_import_settings(&path, io)
68 .await
69 .unwrap_or(default_import_options);
70
71 let model = Model::load(
72 path,
73 io,
74 node_constructors,
75 resource_manager,
76 import_options,
77 )
78 .await
79 .map_err(LoadError::new)?;
80
81 Ok(LoaderPayload::new(model))
82 })
83 }
84
85 fn try_load_import_settings(
86 &self,
87 resource_path: PathBuf,
88 io: Arc<dyn ResourceIo>,
89 ) -> BoxedImportOptionsLoaderFuture {
90 Box::pin(async move {
91 try_get_import_settings_opaque::<ModelImportOptions>(&resource_path, &*io).await
92 })
93 }
94
95 fn default_import_options(&self) -> Option<Box<dyn BaseImportOptions>> {
96 Some(Box::<ModelImportOptions>::default())
97 }
98}