| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use bevy::{ |
| 6 |
asset::RenderAssetUsages, |
| 7 |
mesh::{Indices, VertexAttributeValues}, |
| 8 |
prelude::*, |
| 9 |
render::render_resource::PrimitiveTopology, |
| 10 |
}; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#[derive(Component)] |
| 16 |
struct CustomUV; |
| 17 |
|
| 18 |
fn main() { |
| 19 |
App::new() |
| 20 |
.add_plugins(DefaultPlugins) |
| 21 |
.add_systems(Startup, setup) |
| 22 |
.add_systems(Update, input_handler) |
| 23 |
.run(); |
| 24 |
} |
| 25 |
|
| 26 |
fn setup( |
| 27 |
mut commands: Commands, |
| 28 |
asset_server: Res<AssetServer>, |
| 29 |
mut materials: ResMut<Assets<StandardMaterial>>, |
| 30 |
mut meshes: ResMut<Assets<Mesh>>, |
| 31 |
) { |
| 32 |
|
| 33 |
let custom_texture_handle: Handle<Image> = asset_server.load("textures/array_texture.png"); |
| 34 |
|
| 35 |
let cube_mesh_handle: Handle<Mesh> = meshes.add(create_cube_mesh()); |
| 36 |
|
| 37 |
|
| 38 |
commands.spawn(( |
| 39 |
Mesh3d(cube_mesh_handle), |
| 40 |
MeshMaterial3d(materials.add(StandardMaterial { |
| 41 |
base_color_texture: Some(custom_texture_handle), |
| 42 |
..default() |
| 43 |
})), |
| 44 |
CustomUV, |
| 45 |
)); |
| 46 |
|
| 47 |
|
| 48 |
let camera_and_light_transform = |
| 49 |
Transform::from_xyz(1.8, 1.8, 1.8).looking_at(Vec3::ZERO, Vec3::Y); |
| 50 |
|
| 51 |
|
| 52 |
commands.spawn((Camera3d::default(), camera_and_light_transform)); |
| 53 |
|
| 54 |
|
| 55 |
commands.spawn((PointLight::default(), camera_and_light_transform)); |
| 56 |
|
| 57 |
|
| 58 |
commands.spawn(( |
| 59 |
Text::new("Controls:\nSpace: Change UVs\nX/Y/Z: Rotate\nR: Reset orientation"), |
| 60 |
Node { |
| 61 |
position_type: PositionType::Absolute, |
| 62 |
top: px(12), |
| 63 |
left: px(12), |
| 64 |
..default() |
| 65 |
}, |
| 66 |
)); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
fn input_handler( |
| 72 |
keyboard_input: Res<ButtonInput<KeyCode>>, |
| 73 |
mesh_query: Query<&Mesh3d, With<CustomUV>>, |
| 74 |
mut meshes: ResMut<Assets<Mesh>>, |
| 75 |
mut query: Query<&mut Transform, With<CustomUV>>, |
| 76 |
time: Res<Time>, |
| 77 |
) { |
| 78 |
if keyboard_input.just_pressed(KeyCode::Space) { |
| 79 |
let mesh_handle = mesh_query.single().expect("Query not successful"); |
| 80 |
let mesh = meshes.get_mut(mesh_handle).unwrap(); |
| 81 |
toggle_texture(mesh.into_inner()); |
| 82 |
} |
| 83 |
if keyboard_input.pressed(KeyCode::KeyX) { |
| 84 |
for mut transform in &mut query { |
| 85 |
transform.rotate_x(time.delta_secs() / 1.2); |
| 86 |
} |
| 87 |
} |
| 88 |
if keyboard_input.pressed(KeyCode::KeyY) { |
| 89 |
for mut transform in &mut query { |
| 90 |
transform.rotate_y(time.delta_secs() / 1.2); |
| 91 |
} |
| 92 |
} |
| 93 |
if keyboard_input.pressed(KeyCode::KeyZ) { |
| 94 |
for mut transform in &mut query { |
| 95 |
transform.rotate_z(time.delta_secs() / 1.2); |
| 96 |
} |
| 97 |
} |
| 98 |
if keyboard_input.pressed(KeyCode::KeyR) { |
| 99 |
for mut transform in &mut query { |
| 100 |
transform.look_to(Vec3::NEG_Z, Vec3::Y); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
#[rustfmt::skip] |
| 106 |
fn create_cube_mesh() -> Mesh { |
| 107 |
|
| 108 |
Mesh::new(PrimitiveTopology::TriangleList, RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD) |
| 109 |
.with_inserted_attribute( |
| 110 |
Mesh::ATTRIBUTE_POSITION, |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
vec![ |
| 116 |
|
| 117 |
[-0.5, 0.5, -0.5], |
| 118 |
[0.5, 0.5, -0.5], |
| 119 |
[0.5, 0.5, 0.5], |
| 120 |
[-0.5, 0.5, 0.5], |
| 121 |
|
| 122 |
[-0.5, -0.5, -0.5], |
| 123 |
[0.5, -0.5, -0.5], |
| 124 |
[0.5, -0.5, 0.5], |
| 125 |
[-0.5, -0.5, 0.5], |
| 126 |
|
| 127 |
[0.5, -0.5, -0.5], |
| 128 |
[0.5, -0.5, 0.5], |
| 129 |
[0.5, 0.5, 0.5], |
| 130 |
[0.5, 0.5, -0.5], |
| 131 |
|
| 132 |
[-0.5, -0.5, -0.5], |
| 133 |
[-0.5, -0.5, 0.5], |
| 134 |
[-0.5, 0.5, 0.5], |
| 135 |
[-0.5, 0.5, -0.5], |
| 136 |
|
| 137 |
[-0.5, -0.5, 0.5], |
| 138 |
[-0.5, 0.5, 0.5], |
| 139 |
[0.5, 0.5, 0.5], |
| 140 |
[0.5, -0.5, 0.5], |
| 141 |
|
| 142 |
[-0.5, -0.5, -0.5], |
| 143 |
[-0.5, 0.5, -0.5], |
| 144 |
[0.5, 0.5, -0.5], |
| 145 |
[0.5, -0.5, -0.5], |
| 146 |
], |
| 147 |
) |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
.with_inserted_attribute( |
| 153 |
Mesh::ATTRIBUTE_UV_0, |
| 154 |
vec![ |
| 155 |
|
| 156 |
[0.0, 0.2], [0.0, 0.0], [1.0, 0.0], [1.0, 0.2], |
| 157 |
|
| 158 |
[0.0, 0.45], [0.0, 0.25], [1.0, 0.25], [1.0, 0.45], |
| 159 |
|
| 160 |
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2], |
| 161 |
|
| 162 |
[1.0, 0.45], [0.0, 0.45], [0.0, 0.2], [1.0, 0.2], |
| 163 |
|
| 164 |
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45], |
| 165 |
|
| 166 |
[0.0, 0.45], [0.0, 0.2], [1.0, 0.2], [1.0, 0.45], |
| 167 |
], |
| 168 |
) |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
.with_inserted_attribute( |
| 174 |
Mesh::ATTRIBUTE_NORMAL, |
| 175 |
vec![ |
| 176 |
|
| 177 |
[0.0, 1.0, 0.0], |
| 178 |
[0.0, 1.0, 0.0], |
| 179 |
[0.0, 1.0, 0.0], |
| 180 |
[0.0, 1.0, 0.0], |
| 181 |
|
| 182 |
[0.0, -1.0, 0.0], |
| 183 |
[0.0, -1.0, 0.0], |
| 184 |
[0.0, -1.0, 0.0], |
| 185 |
[0.0, -1.0, 0.0], |
| 186 |
|
| 187 |
[1.0, 0.0, 0.0], |
| 188 |
[1.0, 0.0, 0.0], |
| 189 |
[1.0, 0.0, 0.0], |
| 190 |
[1.0, 0.0, 0.0], |
| 191 |
|
| 192 |
[-1.0, 0.0, 0.0], |
| 193 |
[-1.0, 0.0, 0.0], |
| 194 |
[-1.0, 0.0, 0.0], |
| 195 |
[-1.0, 0.0, 0.0], |
| 196 |
|
| 197 |
[0.0, 0.0, 1.0], |
| 198 |
[0.0, 0.0, 1.0], |
| 199 |
[0.0, 0.0, 1.0], |
| 200 |
[0.0, 0.0, 1.0], |
| 201 |
|
| 202 |
[0.0, 0.0, -1.0], |
| 203 |
[0.0, 0.0, -1.0], |
| 204 |
[0.0, 0.0, -1.0], |
| 205 |
[0.0, 0.0, -1.0], |
| 206 |
], |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
.with_inserted_indices(Indices::U32(vec![ |
| 244 |
0,3,1 , 1,3,2, |
| 245 |
4,5,7 , 5,6,7, |
| 246 |
8,11,9 , 9,11,10, |
| 247 |
12,13,15 , 13,14,15, |
| 248 |
16,19,17 , 17,19,18, |
| 249 |
20,21,23 , 21,22,23, |
| 250 |
])) |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
fn toggle_texture(mesh_to_change: &mut Mesh) { |
| 255 |
|
| 256 |
let uv_attribute = mesh_to_change.attribute_mut(Mesh::ATTRIBUTE_UV_0).unwrap(); |
| 257 |
|
| 258 |
let VertexAttributeValues::Float32x2(uv_attribute) = uv_attribute else { |
| 259 |
panic!("Unexpected vertex format, expected Float32x2."); |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
for uv_coord in uv_attribute.iter_mut() { |
| 264 |
|
| 265 |
if (uv_coord[1] + 0.5) < 1.0 { |
| 266 |
|
| 267 |
uv_coord[1] += 0.5; |
| 268 |
} else { |
| 269 |
|
| 270 |
uv_coord[1] -= 0.5; |
| 271 |
} |
| 272 |
} |
| 273 |
} |