Use the same mesh with different entities

Is there a way to use the same mesh for more entities without copying it?

I am generating the mesh like this:

let mesh = world.exec(|loader: AssetLoaderSystemData<'_, Mesh>| {
    loader.load_from_data(
        Shape::Cube
            .generate::<(Vec<Position>, Vec<Normal>, Vec<Tangent>, Vec<TexCoord>)>(None)
            .into(),
        (),
    )
});

end in a loop with different materials and transforms, I am creating the entities like this:

            world
                .create_entity()
                .with(mesh.clone())
                .with(material)
                .with(transform)
                .build();

But I think the clone could be avoided somehow…

Rust prevents you from spreading your references around and lose overview over which variables are alive and what they contain. You cannot prevent the clone afaik, however this clone is cheap in Amethyst, since you do not clone the actual mesh, but only a handle to the mesh data. Think of it as cloning a pointer.

2 Likes