Skip to content
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

Add test for mesh transformations #18194

Open
wants to merge 5 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
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4127,6 +4127,15 @@ description = "Demonstrates specular tints and maps"
category = "3D Rendering"
wasm = true

[[example]]
name = "transform_mesh"
path = "tests/3d/transform_mesh.rs"
doc-scrape-examples = true
required-features = ["pbr_anisotropy_texture"]
Copy link
Contributor Author

@greeble-dev greeble-dev Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that requiring feature pbr_anisotropy_texture is either a bug in the shader or the documentation. The example doesn't actually use anisotropy textures, but if I disable them then anisotropy doesn't work at all.

I'll file that as a bug if this PR goes through.


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

[profile.wasm-release]
inherits = "release"
opt-level = "z"
Expand Down
128 changes: 128 additions & 0 deletions tests/3d/transform_mesh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//! Test that transforming a mesh correctly updates normals and tangents.

use bevy::prelude::*;
use bevy::render::camera::ScalingMode;

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_systems(Startup, (setup_environment, setup_meshes))
.add_systems(Update, animate_light)
.run();
}

fn setup_environment(
mut commands: Commands,
mut mesh_assets: ResMut<Assets<Mesh>>,
mut material_assets: ResMut<Assets<StandardMaterial>>,
) {
let description = "(left to right)\n\
0: Original mesh.\n\
1: Transformed via mesh attributes.\n\
2: Transformed via mesh attributes, normals and tangents recalculated.\n\
3: Transformed via entity.";

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

commands.spawn((
Camera3d::default(),
Transform::from_xyz(0.0, 0.0, 1.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
Projection::Orthographic(OrthographicProjection {
scaling_mode: ScalingMode::AutoMin {
min_width: 13.0,
min_height: 5.0,
},
..OrthographicProjection::default_3d()
}),
));

commands.spawn((
Transform::from_xyz(1.0, 1.0, 0.5).looking_at(Vec3::ZERO, Vec3::Y),
DirectionalLight {
shadows_enabled: true,
..default()
},
));

commands.spawn((
Transform::from_xyz(0.0, 0.0, -11.0),
Mesh3d(mesh_assets.add(Plane3d::default().mesh().size(100.0, 100.0).normal(Dir3::Z))),
MeshMaterial3d(material_assets.add(StandardMaterial {
base_color: Color::srgb(0.05, 0.05, 0.15),
reflectance: 0.2,
..default()
})),
));
}

fn setup_meshes(
mut commands: Commands,
mut mesh_assets: ResMut<Assets<Mesh>>,
mut material_assets: ResMut<Assets<StandardMaterial>>,
) {
let material = MeshMaterial3d(material_assets.add(StandardMaterial {
base_color: Color::srgb(0.2, 0.4, 0.2),
// Add anisotropy so that lighting is dependent on tangents.
anisotropy_rotation: 0.5,
anisotropy_strength: 1.0,
..Default::default()
}));

let transform = Transform::from_scale(Vec3::new(1.5, 0.5, 1.0)).with_rotation(
Quat::from_axis_angle(Vec3::splat(1.0).normalize(), 135.0_f32.to_radians()),
);

let original_mesh = Mesh::from(Sphere::new(1.0))
.with_computed_normals()
.with_generated_tangents()
.unwrap();

let transformed_mesh = original_mesh.clone().transformed_by(transform);

let recalculated_mesh = transformed_mesh
.clone()
.with_computed_normals()
.with_generated_tangents()
.unwrap();

let original_mesh_handle = Mesh3d(mesh_assets.add(original_mesh));

commands.spawn((
Transform::from_xyz(-4.5, 0.0, -10.0),
original_mesh_handle.clone(),
material.clone(),
));

commands.spawn((
Transform::from_xyz(-1.5, 0.0, -10.0),
Mesh3d(mesh_assets.add(transformed_mesh)),
material.clone(),
));

commands.spawn((
Transform::from_xyz(1.5, 0.0, -10.0),
Mesh3d(mesh_assets.add(recalculated_mesh)),
material.clone(),
));

commands.spawn((
Transform::from_xyz(4.5, 0.0, -10.0) * transform,
original_mesh_handle.clone(),
material.clone(),
));
}

fn animate_light(mut lights: Query<&mut Transform, With<DirectionalLight>>, time: Res<Time>) {
for mut transform in lights.iter_mut() {
transform.translation = vec3(ops::cos(time.elapsed_secs()), 1.0, 1.0);
transform.look_at(Vec3::ZERO, Vec3::Y);
}
}
Loading