Skip to content

Add test for GltfLoaderSettings::override_sampler and mipmap rendering #19095

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,14 @@ doc-scrape-examples = true
[package.metadata.example.no_prepass]
hidden = true

[[example]]
name = "gltf_override_sampler"
path = "tests/gltf/gltf_override_sampler.rs"
doc-scrape-examples = true

[package.metadata.example.gltf_override_sampler]
hidden = true

# Animation
[[example]]
name = "animation_events"
Expand Down
Binary file added assets/models/checkerboard/checkerboard.bin
Binary file not shown.
143 changes: 143 additions & 0 deletions assets/models/checkerboard/checkerboard.gltf
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{
"asset":{
"generator":"Khronos glTF Blender I/O v4.3.47",
"version":"2.0"
},
"extensionsUsed":[
"KHR_materials_unlit"
],
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
0
]
}
],
"nodes":[
{
"mesh":0,
"name":"Plane"
}
],
"materials":[
{
"doubleSided":true,
"extensions":{
"KHR_materials_unlit":{}
},
"name":"Material.001",
"pbrMetallicRoughness":{
"baseColorTexture":{
"index":0
},
"metallicFactor":0,
"roughnessFactor":0.9
}
}
],
"meshes":[
{
"name":"Plane",
"primitives":[
{
"attributes":{
"POSITION":0,
"NORMAL":1,
"TEXCOORD_0":2
},
"indices":3,
"material":0
}
]
}
],
"textures":[
{
"sampler":0,
"source":0
}
],
"images":[
{
"mimeType":"image/png",
"name":"checkerboard.ktx2",
"uri":"checkerboard.ktx2"
}
],
"accessors":[
{
"bufferView":0,
"componentType":5126,
"count":4,
"max":[
1,
0,
1
],
"min":[
-1,
0,
-1
],
"type":"VEC3"
},
{
"bufferView":1,
"componentType":5126,
"count":4,
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":4,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5123,
"count":6,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":48,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":48,
"byteOffset":48,
"target":34962
},
{
"buffer":0,
"byteLength":32,
"byteOffset":96,
"target":34962
},
{
"buffer":0,
"byteLength":12,
"byteOffset":128,
"target":34963
}
],
"samplers":[
{
"magFilter":9729,
"minFilter":9987
}
],
"buffers":[
{
"byteLength":140,
"uri":"checkerboard.bin"
}
]
}
Binary file added assets/models/checkerboard/checkerboard.ktx2
Binary file not shown.
178 changes: 178 additions & 0 deletions tests/gltf/gltf_override_sampler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
//! Tests loading the same glTF multiple times but with different values for
//! `GltfLoaderSettings::override_sampler` and `default_sampler`.
//!
//! CAUTION: This test currently fails due to <https://github.com/bevyengine/bevy/issues/18267> -
//! subsequent loads of the same gltf do not respect the loader settings.

use bevy::{
gltf::GltfLoaderSettings,
image::{ImageAddressMode, ImageFilterMode, ImageSamplerDescriptor},
prelude::*,
};

fn main() {
App::new()
.insert_resource(VisibleCombo(KeyCode::Digit1))
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (update_controls, update_camera))
.run();
}

#[derive(Component, Clone)]
struct Combo {
key: KeyCode,
label: &'static str,
sampler: ImageSamplerDescriptor,
}

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
let default_sampler = ImageSamplerDescriptor {
address_mode_u: ImageAddressMode::Repeat,
address_mode_v: ImageAddressMode::Repeat,
mag_filter: ImageFilterMode::Linear,
min_filter: ImageFilterMode::Linear,
mipmap_filter: ImageFilterMode::Nearest,
..Default::default()
};

// Declare combinations of sampler settings linked to a key code.

let combos: &[Combo] = &[
Combo {
key: KeyCode::Digit1,
label: "1: None",
sampler: ImageSamplerDescriptor {
lod_max_clamp: 0.0,
..default_sampler.clone()
},
},
Combo {
key: KeyCode::Digit2,
label: "2: Nearest",
sampler: default_sampler.clone(),
},
Combo {
key: KeyCode::Digit3,
label: "3: Linear",
sampler: ImageSamplerDescriptor {
mipmap_filter: ImageFilterMode::Linear,
..default_sampler.clone()
},
},
Combo {
key: KeyCode::Digit4,
label: "4: Linear, Anisotropic",
sampler: ImageSamplerDescriptor {
mipmap_filter: ImageFilterMode::Linear,
anisotropy_clamp: 16,
..default_sampler.clone()
},
},
];

// Spawn each combination.

for combo in combos.iter() {
let asset = GltfAssetLabel::Scene(0).from_asset("models/checkerboard/checkerboard.gltf");

let sampler = combo.sampler.clone();

let settings = move |settings: &mut GltfLoaderSettings| {
settings.default_sampler = Some(sampler.clone());
settings.override_sampler = true;
};

commands.spawn((
SceneRoot(asset_server.load_with_settings(asset, settings)),
combo.clone(),
));
}

// Spawn camera and text.

commands.spawn((
Camera3d::default(),
Projection::from(PerspectiveProjection {
near: 0.001,
..Default::default()
}),
));

commands.spawn((
Text::default(),
Node {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..Default::default()
},
));
}

#[derive(Resource, PartialEq)]
struct VisibleCombo(KeyCode);

fn update_controls(
mut text: Single<&mut Text>,
keyboard_input: Res<ButtonInput<KeyCode>>,
mut time: ResMut<Time<Virtual>>,
mut combos: Query<(&Combo, &mut Visibility)>,
mut visible_combo: ResMut<VisibleCombo>,
) {
// Update combo visibility.

for (combo, _) in &combos {
if keyboard_input.just_pressed(combo.key) {
*visible_combo = VisibleCombo(combo.key);
}
}

for (combo, mut visibility) in &mut combos {
*visibility = if *visible_combo == VisibleCombo(combo.key) {
Visibility::Visible
} else {
Visibility::Hidden
};
}

// Update pause.

if keyboard_input.just_pressed(KeyCode::Space) {
if time.is_paused() {
time.unpause();
} else {
time.pause();
}
}

// Update help text.

text.clear();

text.push_str(&format!(
"Space: {}\n\n",
if time.is_paused() { "Unpause" } else { "Pause" }
));

text.push_str("Mipmap filter:\n");

for (combo, _) in &combos {
let visible = *visible_combo == VisibleCombo(combo.key);

text.push_str(&format!(
"{}{}\n",
if visible { "> " } else { " " },
combo.label,
));
}
}

fn update_camera(time: Res<Time>, mut query: Query<&mut Transform, With<Camera3d>>) {
for mut transform in &mut query {
let height = (ops::sin(time.elapsed_secs()) * 0.07) + 0.08;

*transform =
Transform::from_xyz(0.2, height, 0.95).looking_at(Vec3::new(0.0, -0.1, 0.0), Vec3::Y);
}
}