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

Mesh is missing requested attribute: Vertex_Normal error #13054

Open
EngoDev opened this issue Apr 21, 2024 · 1 comment · May be fixed by #13569
Open

Mesh is missing requested attribute: Vertex_Normal error #13054

EngoDev opened this issue Apr 21, 2024 · 1 comment · May be fixed by #13569
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@EngoDev
Copy link

EngoDev commented Apr 21, 2024

Bevy version

0.13.1

[Optional] Relevant system information

If your bug is rendering-related, copy the adapter info that appears when you run Bevy.

AdapterInfo { name: "NVIDIA GeForce RTX 4060", vendor: 4318, device: 10370, device_type: DiscreteGpu, driver: "NVIDIA", driver_info: "550.54.14", backend: Vulkan }

What you did

I created a custom shader that uses compressed normals and position data into a u32 . Meaning I only needed my custom attribute in my shader layout so I removed the rest of the standard layout items like POSITION and NORMAL

This is my code:

pub const ATTRIBUTE_VOXEL_DATA: MeshVertexAttribute =
    MeshVertexAttribute::new("VoxelTypeId", 988540917, VertexFormat::Uint32x2);


#[derive(Asset, TypePath, AsBindGroup, Debug, Clone, Default)]
pub struct ChunkMaterial {
    #[texture(1, dimension = "2d_array")]
    #[sampler(2)]
    pub texture: Handle<Image>,
}

impl Material for ChunkMaterial {
    fn vertex_shader() -> ShaderRef {
        "shaders/chunk_shader_new.wgsl".into()
    }

    fn fragment_shader() -> ShaderRef {
        "shaders/chunk_shader_new.wgsl".into()
    }

    fn alpha_mode(&self) -> AlphaMode {
        AlphaMode::Opaque
    }

    fn specialize(
            _pipeline: &bevy::pbr::MaterialPipeline<Self>,
            descriptor: &mut bevy::render::render_resource::RenderPipelineDescriptor,
            layout: &bevy::render::mesh::MeshVertexBufferLayout,
            _key: bevy::pbr::MaterialPipelineKey<Self>,
        ) -> Result<(), bevy::render::render_resource::SpecializedMeshPipelineError> {

        // The shader location needs to match the one in the prepass_io
        // https://github.com/bevyengine/bevy/blob/main/crates/bevy_pbr/src/prepass/prepass_io.wgsl
        let vertex_layout = layout.get_layout(&[
            ATTRIBUTE_VOXEL_DATA.at_shader_location(30),
        ])?;

        descriptor.vertex.buffers = vec![vertex_layout];
        // descriptor.primitive.cull_mode = None;

        Ok(())
    }

    fn prepass_vertex_shader() -> ShaderRef {
        "shaders/chunk_prepass.wgsl".into()
    }

    fn prepass_fragment_shader() -> ShaderRef {
        "shaders/chunk_prepass.wgsl".into()
    }
}

What went wrong

I get the following error from bevy:
Mesh is missing requested attribute: Vertex_Normal (MeshVertexAttributeId(1), pipeline type: Some("bevy_pbr::prepass::PrepassPipeline<bevy_voxel::voxel_engine::rendering::ChunkMaterial>"))

Additional information

More info can be checked by reading this message and the conversation that lead to it and after:
https://discord.com/channels/691052431525675048/866787577687310356/1231657933490753666

It contains a possible explanation from Jasmine on what is the problem.

I used a workaround to add the Normal attribute in my layout and then supply dummy data to make it happy.

let vertex_layout = layout.get_layout(&[
    Mesh::ATTRIBUTE_NORMAL.at_shader_location(3),
    ATTRIBUTE_VOXEL_DATA.at_shader_location(30),
])?;
let mut normals: Vec<[f32; 3]> = Vec::new();
normals.resize(mesh.vertices.len(), [0.0, 0.0, 0.0]);
mesh.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normals);
@EngoDev EngoDev added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Apr 21, 2024
@JMS55
Copy link
Contributor

JMS55 commented Apr 21, 2024

The issue is here:

vertex_attributes.push(Mesh::ATTRIBUTE_NORMAL.at_shader_location(3));

@NthTensor NthTensor added A-Rendering Drawing game state to the screen and removed S-Needs-Triage This issue needs to be labelled labels Apr 23, 2024
@Ansraer Ansraer linked a pull request May 29, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants