#Re-use custom 2d Material

7 messages · Page 1 of 1 (latest)

clever talon
#

Hi, i'm a complete rendering noob and am getting started with shaders.

Let's say I have a custom material like:

#[derive(AsBindGroup, TypeUuid, Clone)]
#[uuid = "1a70d46f-b455-437d-a50b-ce3a21a803f5"]
pub struct SegmentMaterial {
    // main color of the segment
    #[uniform(0)]
    color: Color,
}

Which I add to my Res<'w, Assets<SegmentMaterial>>, resource.

  • Can i share this material across multiple entities (via a MaterialMesh2dBundle) but still have a different color for each entity?
    (i.e. I only have a bunch of strong handles to the same underlying asset)
    Or do i have to create different 'SegmentMaterial' structs because my entities will have different colors?

Or phrased differently, it's not possible to update the color of a given MaterialMesh2DBundle, similar to what Sprites allow? Which would mean that this is less performant than using sprites

remote axle
#

You will have to create multiple instances of the SegmentMaterial.

#

Like this works:

let mat1_handle = segment_materials.add(SegmentMaterial {
    color: Color::BLUE,
});
let mat2_handle = segment_materials.add(SegmentMaterial {
    color: Color::GREEN,
});
commands.spawn(MaterialMesh2dBundle {
    material: mat1_handle.clone(),
    ..default()
});
commands.spawn(MaterialMesh2dBundle {
    material: mat1_handle,
    ..default()
});
commands.spawn(MaterialMesh2dBundle {
    material: mat2_handle,
    ..default()
});
#

Not sure if this clarifies what you are trying to figure out.

clever talon
#

Yeah that's what I figured. I was just thinking that it could be wasteful if the only changing across the bundles is the color.

Are MaterialMesh more compute-intensive than sprites?

remote axle
#

I'm not really sure as I haven't used sprites or 2d meshes before. But I know sprites have a lot of optimizations (maybe batching?) so I would assume they're faster

#

You could use something else to vary the property rather than a material uniform. Like maybe a vertex attribute or something? Idk what this looks like in bevy 2d