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

StandardMaterial's double_sided field does not prevent backface culling #3729

Closed
rparrett opened this issue Jan 20, 2022 · 2 comments
Closed
Labels
A-Rendering Drawing game state to the screen C-Bug An unexpected or incorrect behavior

Comments

@rparrett
Copy link
Contributor

rparrett commented Jan 20, 2022

Bevy version

main (cb2ba19)

Operating system & version

Macos 12.0.1. (M1)

What you did

Modify the 3d_scene example in the following way

use bevy::prelude::*;

fn main() {
    App::new()
        .insert_resource(Msaa { samples: 4 })
        .add_plugins(DefaultPlugins)
        .add_startup_system(setup)
        .run();
}

/// set up a simple 3D scene
fn setup(
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<StandardMaterial>>,
) {
    // plane
    commands.spawn_bundle(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
        material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
        ..Default::default()
    });
    // square
    let square_material = materials.add(StandardMaterial {
        base_color: Color::rgb(0.8, 0.7, 0.6),
        double_sided: true,
        ..Default::default()
    });
    commands.spawn_bundle(PbrBundle {
        mesh: meshes.add(Mesh::from(shape::Quad::default())),
        material: square_material,
        transform: Transform::from_xyz(0.0, 0.5, 0.0)
            .with_rotation(Quat::from_rotation_y(std::f32::consts::PI)),
        ..Default::default()
    });
    // light
    commands.spawn_bundle(PointLightBundle {
        point_light: PointLight {
            intensity: 1500.0,
            shadows_enabled: true,
            ..Default::default()
        },
        transform: Transform::from_xyz(4.0, 8.0, 4.0),
        ..Default::default()
    });
    // camera
    commands.spawn_bundle(PerspectiveCameraBundle {
        transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
        ..Default::default()
    });
}

What you expected to happen

Square and shadow

What actually happened

Just shadow

image

Additional information

@superdump mentioned on discord that this is a bug and double_sided should be affecting culling. It looks like there's some code in the shaders that's flipping normals, but as far as I can tell, there's no machinery that might affect culling.

I was able to fix this by adding specialization for cull_mode, and hooking double_sized up to it, but something is slightly off with the lighting so I may not have done that correctly.

I'm also not sure whether it's more appropriate to use 2 bits to represent every possible cull mode, or just one to enable/disable culling.

If I have time later though, I'll disentangle the code from another idea I was working on and throw a PR up, but this should be a pretty quick one for someone who actually knows what they're doing in bevy's renderer.

@rparrett rparrett added C-Bug An unexpected or incorrect behavior S-Needs-Triage This issue needs to be labelled labels Jan 20, 2022
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen and removed S-Needs-Triage This issue needs to be labelled labels Jan 20, 2022
@aevyrie
Copy link
Member

aevyrie commented Jan 20, 2022

Piggybacking to note that we were using double_sided and noticed this regression when upgrading to 0.6.

This statement was incorrect, the issue I was seeing in that case was caused by something other than the 0.6 upgrade.

However, I was able to replicate the issue using the replication steps above.

@rparrett
Copy link
Contributor Author

rparrett commented Jan 22, 2022

It turns out that the "quad flipping" that I was using to demonstrate this was perhaps not actually meant to flip the normals (see #3741).

I replaced the example above with one that just rotates the default quad 180 degrees. (result is the same)

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.

4 participants