-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
greeble-dev
wants to merge
5
commits into
bevyengine:main
Choose a base branch
from
greeble-dev:transform-mesh-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4fe71ea
Added `transform_mesh` test.
greeble-dev 62f8300
Added mesh that's transformed via entity. Simplified mesh building.
greeble-dev 955c1b5
Tidy up.
greeble-dev 79319d0
Merge branch 'bevyengine:main' into transform-mesh-test
greeble-dev 440cfb5
Errant newline.
greeble-dev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.