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 example to demonstrate manual generation and UV mapping of 3D mesh (generate_custom_mesh) solve #4922 #8909

Merged
merged 30 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
9c87421
minor improvement to docs
Adamkob12 Jun 18, 2023
12a8e4e
imporved & tested based off input
Adamkob12 Jun 20, 2023
f388979
Added 3d custom texture example
Adamkob12 Jun 21, 2023
51ff304
Revert "imporved & tested based off input"
Adamkob12 Jun 21, 2023
59bd735
Revert "minor improvement to docs"
Adamkob12 Jun 21, 2023
6e0a32e
added to readme
Adamkob12 Jun 21, 2023
9758eba
fix ci error
Adamkob12 Jun 21, 2023
e4b1c9f
Update examples/3d/3d_custom_image.rs
Adamkob12 Jun 22, 2023
8ee4e67
Changed name, improved off input
Adamkob12 Jun 22, 2023
6fa7ed8
added to readme
Adamkob12 Jun 22, 2023
ec37754
fixed ci error
Adamkob12 Jun 22, 2023
6b43643
push to rotate, formatting
Adamkob12 Jun 23, 2023
e958dd2
Tiny typo
alice-i-cecile Jun 23, 2023
c6d16d1
Final touches
Adamkob12 Jun 23, 2023
d67e3cb
typo
Adamkob12 Jun 23, 2023
0da7dc8
Merge remote-tracking branch 'origin/solve_issue_4922' into solve_iss…
Adamkob12 Jun 23, 2023
18aecd4
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
be1ec0f
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
5e5033a
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
8cfa80e
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
79aa593
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
66bf058
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
99cbc10
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
f72a3a9
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
aa175c7
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
8a7df61
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
09ce751
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
5b260c3
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
3201b32
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
1d16b2d
Update examples/3d/generate_custom_mesh.rs
Adamkob12 Jun 23, 2023
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
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,16 @@ description = "A scene showcasing the built-in 3D shapes"
category = "3D Rendering"
wasm = true

[[example]]
name = "3d_custom_image"
path = "examples/3d/3d_custom_image.rs"

[package.metadata.example.3d_custom_image]
name = "3D custom image"
description = "Simple showcase of how to use a custom image as a texture for a 3D mesh"
category = "3D Rendering"
wasm = true

[[example]]
name = "anti_aliasing"
path = "examples/3d/anti_aliasing.rs"
Expand Down
Binary file added assets/textures/custom_image_for_example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
186 changes: 186 additions & 0 deletions examples/3d/3d_custom_image.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
use bevy::prelude::*;
use bevy::render::mesh::Indices;
use bevy::render::render_resource::PrimitiveTopology;

// Notice "ImagePlugin::default_nearest()" in main, because our texture is very low res
// we want it to look pixelated, instead of the default ImagePlugin::default_linear() which will
// smooth our pixelated texture. However, default_linear is usually better for high res textures.
fn main() {
App::new()
.add_plugins(DefaultPlugins.set(ImagePlugin::default_nearest()))
.add_systems(Startup, setup)
.add_systems(Update, rotate)
.run();
}

// Rotate the cube to show off all the sides.
fn rotate(mut query: Query<&mut Transform, With<Handle<Mesh>>>, time: Res<Time>) {
for mut transform in &mut query {
transform.rotate_z(time.delta_seconds() / 1.2);
transform.rotate_x(time.delta_seconds() / 2.0);
}
}

fn setup(
mut commands: Commands,
asset_server: ResMut<AssetServer>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
// Importing the custom texture.
let custom_texture_handle: Handle<Image> =
asset_server.load("textures/custom_image_for_example.png");
// Creating and saving a handle to the mesh.
let cube_mesh_handle: Handle<Mesh> = meshes.add(create_cube_mesh());

// Rendering the mesh with the custom texture using a PbrBundle.
commands.spawn(PbrBundle {
mesh: cube_mesh_handle,
material: materials.add(StandardMaterial {
base_color_texture: Some(custom_texture_handle),
..default()
}),
..default()
});

// Transform for the camera and lighting, looking at (0,0,0) (the position of the mesh).
let camera_and_light_transform =
Transform::from_xyz(3.0, 3.0, 3.0).looking_at(Vec3::ZERO, Vec3::Y);

// Camera in 3D space.
commands.spawn(Camera3dBundle {
transform: camera_and_light_transform,
..default()
});

// Lighting up the scene.
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 9000.0,
range: 100.0,
..default()
},
transform: camera_and_light_transform,
..default()
});
}

fn create_cube_mesh() -> Mesh {
let mut cube_mesh = Mesh::new(PrimitiveTopology::TriangleList);

#[rustfmt::skip]
cube_mesh.insert_attribute(
Mesh::ATTRIBUTE_POSITION,
vec![
// top (facing towards +y)
[0.0, 1.0, 0.0], // vertex with index 0
[1.0, 1.0, 0.0], // vertex wtih index 1
[1.0, 1.0, 1.0], // etc. until 23
[0.0, 1.0, 1.0],
// bottom ` ` (-y)
[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
// forward ` ` (+x)
[1.0, 0.0, 0.0],
[1.0, 0.0, 1.0],
[1.0, 1.0, 1.0],
[1.0, 1.0, 0.0],
// back ` ` (-x)
[0.0, 0.0, 0.0],
[0.0, 0.0, 1.0],
[0.0, 1.0, 1.0],
[0.0, 1.0, 0.0],
// right ` ` (+z)
[0.0, 0.0, 1.0],
[0.0, 1.0, 1.0],
[1.0, 1.0, 1.0],
[1.0, 0.0, 1.0],
// left ` ` (-z)
[0.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[1.0, 1.0, 0.0],
[1.0, 0.0, 0.0],
],
);

// Take a look at the custom image (assets/textures/custom_image_for_example.png)
// so the UV coords will make more sense (note (0.0, 0.0) = Top-Left in UV mapping).
#[rustfmt::skip]
cube_mesh.insert_attribute(
Mesh::ATTRIBUTE_UV_0,
vec![
// Assigning the UV coords for the top side.
[0.5, 0.0], [0.0, 0.0], [0.0, 0.5], [0.5, 0.5],
// Assigning the UV coords for the bottom side.
[0.5, 0.5], [0.5, 0.0], [1.0, 0.0], [1.0, 0.5],
// Assigning the UV coords for the forward side.
[0.5, 1.0], [0.5, 0.5], [1.0, 0.5], [1.0, 1.0],
// Assigning the UV coords for the back side (same as forward because they have the
// same texture)
[0.5, 1.0], [0.5, 0.5], [1.0, 0.5], [1.0, 1.0],
// Assigning the UV coords for the right side.
[0.0, 1.0], [0.0, 0.5], [0.5, 0.5], [0.5, 1.0],
// Assigning the UV coords for the left side (same as right).
[0.0, 1.0], [0.0, 0.5], [0.5, 0.5], [0.5, 1.0],
],
);

// When it comes to smooth, and simple meshes, normals are as simple as the direction of the flat surface
// Assign normals to allow for correct lighting calculations.
#[rustfmt::skip]
cube_mesh.insert_attribute(
Mesh::ATTRIBUTE_NORMAL,
vec![
// Normals for the top side (towards +y)
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 1.0, 0.0],
// Normals for the bottom side (towards -y)
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
[0.0, -1.0, 0.0],
// Normals for the forward side (towards +x)
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
// Normals for the back side (towards -x)
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
[-1.0, 0.0, 0.0],
// Normals for the right side (towards +z)
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
[0.0, 0.0, 1.0],
// Normals for the left side (towards -z)
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
[0.0, 0.0, -1.0],
],
);

// Create the triangles out of the 24 vertices we created.
// To construct a square, we need 2 triangles, therfore 12 traingles in total.
// To construct a triangle, we need the indices of 3 of its defined vertices, adding them one
// by one, in a counter-clockwise order (relative to the position of the viewer, the order
// should appear counter-clockwise from the front of the triangle). Read more about how to correctly build a mesh manually
// in the Bevy documentation, further examples and the implementation of the built-in shapes.
#[rustfmt::skip]
cube_mesh.set_indices(Some(Indices::U32(vec![
0,3,1 , 1,3,2, // top (+y)
4,5,7 , 5,6,7, // bottom (-y)
8,11,9 , 9,11,10, // forward (+x)
12,13,15 , 13,14,15, // backward (-x)
16,19,17 , 17,19,18, // rightward (+z)
20,21,23 , 21,22,23, // leftward (-z)
])));

cube_mesh
}
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Example | Description
[3D Gizmos](../examples/3d/3d_gizmos.rs) | A scene showcasing 3D gizmos
[3D Scene](../examples/3d/3d_scene.rs) | Simple 3D scene with basic shapes and lighting
[3D Shapes](../examples/3d/3d_shapes.rs) | A scene showcasing the built-in 3D shapes
[3D custom image](../examples/3d/3d_custom_image.rs) | Simple showcase of how to use a custom image as a texture for a 3D mesh
[Anti-aliasing](../examples/3d/anti_aliasing.rs) | Compares different anti-aliasing methods
[Atmospheric Fog](../examples/3d/atmospheric_fog.rs) | A scene showcasing the atmospheric fog effect
[Blend Modes](../examples/3d/blend_modes.rs) | Showcases different blend modes
Expand Down