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

Area of icosphere mesh triangles is inconsistent #12480

Closed
EmiOnGit opened this issue Mar 14, 2024 · 4 comments · Fixed by #12482
Closed

Area of icosphere mesh triangles is inconsistent #12480

EmiOnGit opened this issue Mar 14, 2024 · 4 comments · Fixed by #12482
Labels
A-Math Fundamental domain-agnostic mathematical operations A-Rendering Drawing game state to the screen C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy

Comments

@EmiOnGit
Copy link
Contributor

EmiOnGit commented Mar 14, 2024

Bevy version

0.13

What you did

I've noticed that the mesh generated by Sphere is not containing equally sized triangles.
See below for the minimal required code.

Sphere::default().mesh() creates a SphereMeshBuilder with SphereMeshBuilder::kind == SphereKind::Ico.
As normally expected, the documentation for SphereKind::Ico states:

An icosphere, a spherical mesh that consists of equally sized triangles.

However, I couldn't validate this with following code:

use bevy::{prelude::*, render::mesh::PrimitiveTopology};

fn main() {
    let sphere_mesh_builder = Sphere::default().mesh();
    // Make sure we use a `Icosphere`
    match sphere_mesh_builder.kind {
        SphereKind::Ico { .. } => {}
        _ => panic!(),
    }

    let mesh = sphere_mesh_builder.build();

    // indices of vertices
    let indices: Vec<usize> = mesh.indices().unwrap().iter().collect();
    // Extract position of vertices
    let positions = mesh
        .attribute(Mesh::ATTRIBUTE_POSITION)
        .unwrap()
        .as_float3()
        .unwrap();
    // Vertices 0,1,2,3,4,5 create triangles 0,1,2 and 3,4,5 since we have a `TriangleList`.
    assert_eq!(mesh.primitive_topology(), PrimitiveTopology::TriangleList);
    let areas: Vec<f32> = indices
        .chunks_exact(3)
        // triangle is &[usize;3]
        .map(|triangle| {
            let a = Vec3::from_array(positions[triangle[0]]);
            let b = Vec3::from_array(positions[triangle[1]]);
            let c = Vec3::from_array(positions[triangle[2]]);
            // a,b,c are the three corner points of the triangle
            (a, b, c)
        })
        .map(|(a, b, c)| area_of_triangle(a, b, c))
        .collect();
    println!("max area {:?}", areas.clone().into_iter().reduce(f32::max));
    println!("min area {:?}", areas.clone().into_iter().reduce(f32::min));
    println!("first 10 {:?}", &areas[..10]);
}
fn area_of_triangle(a: Vec3, b: Vec3, c: Vec3) -> f32 {
    let ab = b - a;
    let ac = c - a;
    ab.cross(ac).length() / 2.
}
max area Some(0.0743312)
min area Some(0.06445146)
first 10 [0.06445148, 0.064451486, 0.064451486, 0.06595752, 0.06595752, 0.06595753, 0.06482972, 0.0724672, 0.06482972, 0.07246719]

The formula for calculating the area of the triangle can be found here for example: 1 2

@EmiOnGit EmiOnGit added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Mar 14, 2024
@alice-i-cecile alice-i-cecile added A-Math Fundamental domain-agnostic mathematical operations and removed S-Needs-Triage This issue needs to be labelled labels Mar 14, 2024
@alice-i-cecile
Copy link
Member

Can you attach a screenshot of the wireframe of one of these generated icospheres? That may give us a better sense of what's wrong.

@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen S-Needs-Investigation This issue requires detective work to figure out what's going wrong labels Mar 14, 2024
@alice-i-cecile alice-i-cecile changed the title Sphere mesh is not as documented Area of icosphere mesh triangles is inconsistent Mar 14, 2024
@Adamkob12
Copy link
Contributor

Vertices 0,1,2,3,4,5 create triangles 0,1,2 and 3,4,5

Couldn't there be defined indices?
So the triangles wouldn't necessarily be defined in order like that.

@EmiOnGit
Copy link
Contributor Author

Couldn't there be defined indices?

Yeah you're right, I didn't know about that so my fault.
When using the right indices, the area does still fluctuate but only by around 10%.
Not sure but this might be in the expected deviation, right?
Wireframe also looks as expected. I've also updated the code above.

image

@EmiOnGit
Copy link
Contributor Author

Update:
It seems the triangles of icospheres are not designed to be exactely the same size to begin with. Therefore a change of following documentation for the Icosphere would be helpful to prevent further mistakes

An icosphere, a spherical mesh that consists of equally sized triangles.

@alice-i-cecile alice-i-cecile added C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy and removed S-Needs-Investigation This issue requires detective work to figure out what's going wrong C-Bug An unexpected or incorrect behavior labels Mar 14, 2024
github-merge-queue bot pushed a commit that referenced this issue Mar 15, 2024
#12482)

# Objective
Fixes #12480 
by removing the explicit mention of equally sized triangles from the doc
for icospheres

Co-authored-by: Emi <emanuel.boehm@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Math Fundamental domain-agnostic mathematical operations A-Rendering Drawing game state to the screen C-Docs An addition or correction to our documentation D-Trivial Nice and easy! A great choice to get started with Bevy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants