Last active
June 6, 2024 21:43
-
-
Save rparrett/53a9f21f3259ef282f110fc952058fdd to your computer and use it in GitHub Desktop.
Bevy 0.12 hollow circle mesh
This file contains hidden or 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 sort of thing will be built into Bevy in version 0.14 by meshing the new `Annulus` primitive. | |
pub fn hollow_circle(inner_radius: f32, thickness: f32, sides: usize) -> Mesh { | |
let outer_radius = inner_radius + thickness; | |
debug_assert!(sides > 2, "hollow_circle requires at least 3 sides."); | |
let vertices = sides * 2; | |
let mut positions = Vec::with_capacity(vertices); | |
let mut normals = Vec::with_capacity(vertices); | |
let mut uvs = Vec::with_capacity(vertices); | |
let step = std::f32::consts::TAU / sides as f32; | |
for i in 0..sides { | |
let theta = std::f32::consts::FRAC_PI_2 - i as f32 * step; | |
let (sin, cos) = theta.sin_cos(); | |
positions.push([cos * outer_radius, sin * outer_radius, 0.0]); | |
normals.push([0.0, 0.0, 1.0]); | |
uvs.push([0.5 * (cos + 1.0), 1.0 - 0.5 * (sin + 1.0)]); | |
positions.push([cos * inner_radius, sin * inner_radius, 0.0]); | |
normals.push([0.0, 0.0, 1.0]); | |
uvs.push([0.5 * (cos + 1.0), 1.0 - 0.5 * (sin + 1.0)]); | |
} | |
let mut indices = Vec::with_capacity((vertices + 2) * 3); | |
let vertices = vertices as u32; | |
for i in (0..vertices).step_by(2) { | |
indices.extend_from_slice(&[i, i + 2, i + 1, i + 1, i + 2, i + 3]); | |
} | |
indices.extend_from_slice(&[0, vertices - 2, vertices - 1, 0, vertices - 1, 1]); | |
Mesh::new(PrimitiveTopology::TriangleList) | |
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, positions) | |
.with_inserted_attribute(Mesh::ATTRIBUTE_NORMAL, normals) | |
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs) | |
.with_indices(Some(Indices::U32(indices))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment