//! This example demonstrates how to create a custom mesh, //! assign a custom UV mapping for a custom texture, //! and how to change the UV mapping at run-time. use bevy::{ prelude::*, render::{ mesh::{Indices, VertexAttributeValues}, render_asset::RenderAssetUsages, render_resource::PrimitiveTopology, }, }; // Define a "marker" component to mark the custom mesh. Marker components are often used in Bevy for // filtering entities in queries with `With`, they're usually not queried directly since they don't // contain information within them. #[derive(Component)] struct CustomUV; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .add_systems(Update, input_handler) .run(); } fn setup( mut commands: Commands, asset_server: Res, mut materials: ResMut>, mut meshes: ResMut>, ) { // Import the custom texture. let custom_texture_handle: Handle = asset_server.load("textures/array_texture.png"); // Create and save a handle to the mesh. let cube_mesh_handle: Handle = meshes.add(create_cube_mesh()); // Render the mesh with the custom texture, and add the marker. commands.spawn(( Mesh3d(cube_mesh_handle), MeshMaterial3d(materials.add(StandardMaterial { base_color_texture: Some(custom_texture_handle), ..default() })), CustomUV, )); // Transform for the camera and lighting, looking at (0,0,0) (the position of the mesh). let camera_and_light_transform = Transform::from_xyz(1.8, 1.8, 1.8).looking_at(Vec3::ZERO, Vec3::Y); // Camera in 3D space. commands.spawn((Camera3d::default(), camera_and_light_transform)); // Light up the scene. commands.spawn((PointLight::default(), camera_and_light_transform)); // Text to describe the controls. commands.spawn(( Text::new("Controls:\nSpace: Change UVs\nX/Y/Z: Rotate\nR: Reset orientation"), Style { position_type: PositionType::Absolute, top: Val::Px(12.0), left: Val::Px(12.0), ..default() }, )); } // System to receive input from the user, // check out examples/input/ for more examples about user input. fn input_handler( keyboard_input: Res>, mesh_query: Query<&Mesh3d, With>, mut meshes: ResMut>, mut query: Query<&mut Transform, With>, time: Res