#Query and modify mesh

15 messages · Page 1 of 1 (latest)

open raven
#

Hi here !
I created a mesh, like this:

fn example(
    mut commands: Commands,
    meshes: &mut ResMut<Assets<Mesh>>,
    materials: &mut ResMut<Assets<ColorMaterial>>,
) {
    commands.spawn(
        (
            ProgressDone,
            MaterialMesh2dBundle {
                mesh: meshes.add(Mesh::from(shape::Quad::new(Vec2::ZERO))).into(),
                transform: Transform {
                    translation: Vec3::ZERO,
                    ..Default::default()
                },
                material: materials.add(ColorMaterial::from(Color::GREEN)),
                ..default()
            },
        )
    );
}

I don't know how I should Query that to be able to modify the shape. For example, from shape::Quad::new(Vec2::ZERO)) to shape::Quad::new(Vec2::new(100., 10.)))

How should I write my query to be able to do that ?

winter bough
#

@open raven If you just want to do scaling then you should modify the scale field of the Transform component.
If you want to edit or replace a mesh on an entity you can query the Handle<Mesh> component, and then get either replace it with a different handle, or get the actual mesh through Res<Assets<Mesh>> and modify it, but it's more tedious and has some drawbacks

open raven
red mountain
#

@winter bough How would you associate the mesh with an entity? I am assuming the Handle<Mesh> is a resource.

winter bough
#

@red mountain Handle<Mesh> is a component, it is associated with an entity when you spawn it with a bundle containing it (which is the case for MaterialMesh2dBundle, or PbrBundle, a bundle is just a way to add multiple components to an entity, it would add all the components of the fields of the bundle), you can also add it to an entity directly.
Assets<Mesh> is a resource. With meshes: ResMut<Assets<Mesh>> and a mesh: Mesh you can get a mesh handle with let mesh_handle = meshes.add(mesh), and get a mesh back with let mesh = meshes.get(mesh_handle).unwrap()

red mountain
winter bough
#

No it's a component?

#

You can also store it in a resource if you want/need

#

But that would be a resource you make yourself

#

Assets<Mesh> is a resource that may be where the confusion is? @red mountain

south fjord
winter bough
radiant steeple
#

In most cases if you want to change the mesh you can just create a new mesh and replace the handle such that it points to the new mesh, althought it is possible to edit the data in the existing mesh