Skip to content

Commit

Permalink
Improved mesh builder example
Browse files Browse the repository at this point in the history
  • Loading branch information
ManevilleF committed Feb 1, 2024
1 parent f19435a commit b81b82e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* (**BREAKING**) Fixed `uv` generation for planes and columns (#145):
* UV coords, by default, will be correctly wrapped between (0, 0) and (1, 1)
* `MeshInfo::cheap_hexagonal_column` now returns a mesh with 12 vertices (#145)
* Added gizmos to `mesh_builder` example (#145)

### Deprecations

Expand Down
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ features = [
"default_font",
"png",
"x11",
# The following features are required because of https://github.com/bevyengine/bevy/discussions/9100
"ktx2",
"zstd",
"tonemapping_luts",
"bevy_gizmos",
# Faster compilation
"dynamic_linking",
]
Expand Down
Binary file modified docs/mesh_builder.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 38 additions & 7 deletions examples/mesh_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub fn main() {
.add_plugins(EguiPlugin)
.add_plugins(bevy_inspector_egui::DefaultInspectorConfigPlugin)
.add_systems(Startup, setup)
.add_systems(Update, (show_ui, animate, update_mesh))
.add_systems(Update, (show_ui, animate, update_mesh, gizmos))
.run();
}

Expand Down Expand Up @@ -124,7 +124,7 @@ fn setup(
asset_server: Res<AssetServer>,
) {
let texture = asset_server.load("uv_checker.png");
let transform = Transform::from_xyz(0.0, 0.0, 20.0).looking_at(Vec3::ZERO, Vec3::Y);
let transform = Transform::from_xyz(10.0, 10.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y);
commands.spawn(Camera3dBundle {
transform,
..default()
Expand Down Expand Up @@ -165,22 +165,53 @@ fn animate(
buttons: Res<Input<MouseButton>>,
time: Res<Time>,
) {
if buttons.pressed(MouseButton::Left) {
for event in motion_evr.read() {
for event in motion_evr.read() {
if buttons.pressed(MouseButton::Left) {
let mut transform = transforms.get_mut(info.mesh_entity).unwrap();
transform.rotate_y(event.delta.x * time.delta_seconds());
transform.rotate_x(event.delta.y * time.delta_seconds());
let axis = Vec3::new(event.delta.y, event.delta.x, 0.0).normalize();
let angle = event.delta.length() * time.delta_seconds();
let quaternion = Quat::from_axis_angle(axis, angle);
transform.rotate(quaternion);
}
}
}

fn gizmos(
mut draw: Gizmos,
info: Res<HexInfo>,
transforms: Query<&Transform>,
params: Res<BuilderParams>,
) {
let transform = transforms.get(info.mesh_entity).unwrap();
// Global axis
draw.line(Vec3::NEG_X * 100.0, Vec3::X * 100.0, Color::RED.with_a(0.4));
draw.line(
Vec3::NEG_Y * 100.0,
Vec3::Y * 100.0,
Color::GREEN.with_a(0.4),
);
draw.line(
Vec3::NEG_Z * 100.0,
Vec3::Z * 100.0,
Color::BLUE.with_a(0.4),
);
// Local axis
let radius = info.layout.hex_size.length() * params.scale.length();
draw.circle(Vec3::ZERO, transform.local_x(), radius, Color::RED)
.segments(64);
draw.circle(Vec3::ZERO, transform.local_y(), radius, Color::GREEN)
.segments(64);
draw.circle(Vec3::ZERO, transform.forward(), radius, Color::BLUE)
.segments(64);
}

fn update_mesh(params: Res<BuilderParams>, info: Res<HexInfo>, mut meshes: ResMut<Assets<Mesh>>) {
if !params.is_changed() {
return;
}
let mut new_mesh = ColumnMeshBuilder::new(&info.layout, params.height)
.with_subdivisions(params.subdivisions)
.with_offset(Vec3::NEG_Y * params.height / 2.0)
.with_offset(Vec3::NEG_Y * params.height / 2.0 * params.scale.y)
.with_scale(params.scale)
.with_caps_uv_options(params.caps_uvs)
.with_multi_sides_uv_options(match params.sides_uvs_mode {
Expand Down
2 changes: 2 additions & 0 deletions src/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ impl MeshInfo {
}
}

/// Computes mesh data for an hexagonal plane facing `Vec3::Y` with 6
/// vertices and 4 triangles, ignoring the `layout` origin
#[must_use]
pub(crate) fn center_aligned_hexagonal_plane(layout: &HexLayout) -> Self {
let corners = layout.center_aligned_hex_corners();
Expand Down
4 changes: 2 additions & 2 deletions src/mesh/uv_mapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,14 @@ impl UVOptions {

/// Default values for hexagonal planes or column caps
#[must_use]
#[deprecated(since = "0.13.1", note = "Use `UVOptions::new` instead")]
#[deprecated(since = "0.14.0", note = "Use `UVOptions::new` instead")]
pub const fn cap_default() -> Self {
Self::new()
}

/// Default values for quads
#[must_use]
#[deprecated(since = "0.13.1", note = "Use `UVOptions::new` instead")]
#[deprecated(since = "0.14.0", note = "Use `UVOptions::new` instead")]
pub const fn quad_default() -> Self {
Self::new()
}
Expand Down

0 comments on commit b81b82e

Please sign in to comment.