-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Add image sampler configuration in GLTF loader #17875
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 16 commits
2dc9122
49ba75c
7849035
c08e14b
3400dd1
cefffe9
8c9da48
bc4efb1
22af97c
239fa89
7766896
adac31e
06852c3
f0eac14
2fdfb2d
2b4cbc9
8d98eb8
c488723
d0e73e7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -97,11 +97,15 @@ mod vertex_attributes; | |
|
||
extern crate alloc; | ||
|
||
use alloc::sync::Arc; | ||
use std::sync::Mutex; | ||
|
||
use bevy_platform_support::collections::HashMap; | ||
|
||
use bevy_app::prelude::*; | ||
use bevy_asset::AssetApp; | ||
use bevy_image::CompressedImageFormats; | ||
use bevy_ecs::prelude::Resource; | ||
use bevy_image::{CompressedImageFormats, ImageSamplerDescriptor}; | ||
use bevy_render::{mesh::MeshVertexAttribute, renderer::RenderDevice}; | ||
|
||
/// The glTF prelude. | ||
|
@@ -114,10 +118,57 @@ pub mod prelude { | |
|
||
pub use {assets::*, label::GltfAssetLabel, loader::*}; | ||
|
||
// Has to store an Arc as there is no other way to mutate fields of asset loaders. | ||
/// Stores default [`ImageSamplerDescriptor`] in main world. | ||
#[derive(Resource)] | ||
pub struct DefaultGltfImageSampler(Arc<Mutex<ImageSamplerDescriptor>>); | ||
|
||
impl DefaultGltfImageSampler { | ||
/// Creates a new [`DefaultGltfImageSampler`]. | ||
pub fn new(descriptor: &ImageSamplerDescriptor) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about making this constructor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I have to @alice-i-cecile There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer the simpler and more flexible fully public design here. |
||
Self(Arc::new(Mutex::new(descriptor.clone()))) | ||
} | ||
|
||
/// Returns the current default [`ImageSamplerDescriptor`]. | ||
pub fn get(&self) -> ImageSamplerDescriptor { | ||
self.0.lock().unwrap().clone() | ||
} | ||
|
||
/// Makes a clone of internal [`Arc`] pointer. | ||
/// | ||
/// Intended only to be used by code with no access to ECS. | ||
pub fn get_mutex(&self) -> Arc<Mutex<ImageSamplerDescriptor>> { | ||
axlitEels marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.0.clone() | ||
} | ||
|
||
/// Replaces default [`ImageSamplerDescriptor`]. | ||
/// | ||
/// Doesn't apply to samplers already built on top of it, i.e. `GltfLoader`'s output. | ||
/// Assets need to manually be reloaded. | ||
pub fn set(&self, descriptor: &ImageSamplerDescriptor) { | ||
*self.0.lock().unwrap() = descriptor.clone(); | ||
} | ||
} | ||
|
||
/// Adds support for glTF file loading to the app. | ||
#[derive(Default)] | ||
pub struct GltfPlugin { | ||
custom_vertex_attributes: HashMap<Box<str>, MeshVertexAttribute>, | ||
/// The default image sampler to lay glTF sampler data on top of. | ||
/// | ||
/// Can be modified with [`DefaultGltfImageSampler`] resource. | ||
pub default_sampler: ImageSamplerDescriptor, | ||
/// Registry for custom vertex attributes. | ||
/// | ||
/// To specify, use [`GltfPlugin::add_custom_vertex_attribute`]. | ||
pub custom_vertex_attributes: HashMap<Box<str>, MeshVertexAttribute>, | ||
} | ||
|
||
impl Default for GltfPlugin { | ||
fn default() -> Self { | ||
GltfPlugin { | ||
default_sampler: ImageSamplerDescriptor::linear(), | ||
custom_vertex_attributes: HashMap::default(), | ||
} | ||
} | ||
} | ||
|
||
impl GltfPlugin { | ||
|
@@ -156,9 +207,13 @@ impl Plugin for GltfPlugin { | |
Some(render_device) => CompressedImageFormats::from_features(render_device.features()), | ||
None => CompressedImageFormats::NONE, | ||
}; | ||
let default_sampler_resource = DefaultGltfImageSampler::new(&self.default_sampler); | ||
let default_sampler = default_sampler_resource.get_mutex(); | ||
app.insert_resource(default_sampler_resource); | ||
app.register_asset_loader(GltfLoader { | ||
supported_compressed_formats, | ||
custom_vertex_attributes: self.custom_vertex_attributes.clone(), | ||
default_sampler, | ||
}); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.