#some 2d entities become invisible in 0.11

23 messages · Page 1 of 1 (latest)

sullen sun
#

so i upgraded to bevy 0.11 and all my custom shaders in my 2d game stopped working

#

src/main.rs

fn main () {
    App::new()
    .add_plugins(DefaultPlugins
        .set(AssetPlugin {
            #[cfg(debug_assertions)]
            watch_for_changes: ChangeWatcher::with_delay(Duration::from_millis(200)),
            ..default()
        })
        .set(WindowPlugin {
            primary_window: Some(Window {
                title: format!("title"),
                resolution: WindowResolution::new(600.0, 600.0),
                transparent: true,
                decorations: false,
                resizable: true,
                #[cfg(target_os = "linux")]
                composite_alpha_mode: CompositeAlphaMode::PreMultiplied,
                ..default()
            }),
            ..default()
        })
    )
    .insert_resource(ClearColor(Color::rgba(0.0, 0.0, 0.0, 0.75)))
    .add_plugins(Material2dPlugin::<CustomShader>::default())
    .add_systems(Startup, spawn_camera)
    .add_systems(Startup, test_system)
    .run();
}

#[derive(AsBindGroup, TypeUuid, TypePath, Debug, Clone)]
#[uuid = "f690fdae-d598-45ab-8225-97e2a3f056e0"]
pub struct CustomShader {
    #[uniform(0)] color: Color,
}

impl Material2d for CustomShader {
    fn fragment_shader() -> ShaderRef {
        "shaders/test_shader.wgsl".into()
    }
}

fn test_system (
    mut commands: Commands,
    mut meshes: ResMut<Assets<Mesh>>,
    mut materials: ResMut<Assets<CustomShader>>
) {
    commands.spawn(MaterialMesh2dBundle {
        mesh: meshes.add(Mesh::from(shape::Quad {
            size: Vec2::splat(200.0),
            flip: false
        })).into(),
        material: materials.add(CustomShader {
            color: Color::BLUE,
        }),
        ..default()
    });
}
#

assets/shaders/test_shader.wgsl

#import bevy_pbr::mesh_vertex_output MeshVertexOutput

struct CustomMaterial {
    color: vec4<f32>,
};

@group(1) @binding(0)
var<uniform> material: CustomMaterial;

@fragment
fn fragment(
    mesh: MeshVertexOutput,
) -> @location(0) vec4<f32> {
    return material.color;
}
#

i expect a blue square on a translucent black window, but there's no square

half ferry
#

Can I see spawn_camera?

sullen sun
#
pub fn spawn_camera (mut commands: Commands) {
    commands.spawn(Camera2dBundle {
        transform: Transform::from_xyz(0.0, 0.0, 0.0),
        ..Default::default()
    });
}
half ferry
#

Try to keep the default transform instead?

#

or z=500. let's say

sullen sun
#

it works now

half ferry
#

@sullen sun Okay, 2D frustum culling was added in 0.11

#

Your camera is at z=0 and looks to negative Z

#

So it doesn't see stuff that is at z=0, only z<0

sullen sun
#

what's 2d frustum culling ? i know about 3d frustums

half ferry
#

It's the same thing, except instead of being a truncated pyramid it's a cuboid

#

Everything that is not inside it is not considered for rendering, that's the culling part

#

Because your square is at the border of the frustum, on the near plane, it's not picked up I guess? (I feel like it should but idk rounding errors)

#

The default for Camera2DBundle is to place the camera at z=999.9

#

with a far of 1000.

sullen sun
#

yep, i didn't read the whole guide

half ferry
#

You're not the first one that has a problem after setting the camera transform to z=0.

#

It should probably be mentioned in the migration guide too

sullen sun
#

some entities become invisible in 0.11

#

some 2d entities become invisible in 0.11