How to create a custom Bevy bundle from a bundle with custom material

152 views Asked by At

My goal is to create a custom Bevy bundle that extends a built-in one so that I can reuse it. I start with MaterialMesh2DBundle which I've modified to have a custom fragment shader by changing the material field to one that is derived from Material2d and spawn it

commands.spawn(MaterialMesh2dBundle {
    mesh: meshes.add(Mesh::from(shape::Quad::default())).into(),
    transform: Transform::default().with_scale(Vec3::splat(640.)),
    material: materials.add(CustomMaterial {}),
    ..default()
});
...

// Custom material
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
struct CustomMaterial {}

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

Now, I would like to put this all into a bundle to reuse in my code. Something like:

#[derive(Bundle)]
struct CustomBundle {
    sprite: MaterialMesh2dBundle<CustomMaterial>,
}

impl Default for CustomBundle {
    fn default() -> Self {
        // default implementation here
    }
}

So that eventually I can just use the custom bundle:

commands.spawn(CustomBundle {
    ..default()
});
0

There are 0 answers