What is the proper way to change appereance of an entity

Hi all,

I am implementing a basebuilding game (URP). Naturally, you can place stuff like walls, objects, and so on. Then a character moves to the position and builds the object. After the build process, the object persists in the world.

To achive this visually I have my entity show a mesh of the object with a shader that shows some kind of hologram instead of the real object. During the build process, I want to show the final mesh that appears out of thin air with a nice effect. And finally, the entity just shows the object.

Right now I have a PreFab of every final object. I also have one material with the holographic shader and one material with the constructing shader.

To change the materials of the entity I do something like this:

        Entities.WithStructuralChanges().
            ForEach((Entity ent, MaterialChangerData changer, in RenderMesh mesh) => {
                RenderMesh modifiedMesh = mesh;
                modifiedMesh.material = changer.changeToMaterial;
                entityManager.RemoveComponent<MaterialChangerData>(ent);
                entityManager.SetSharedComponentData<RenderMesh>(ent, modifiedMesh);
            }).Run();

This has some drawbacks, for example, the BaseMap and NormalMap of the original material do not carry over to the new one and I did not (yet) figure out to copy it over.

Is this the right way to do it at all?

If there is another way, if so, what is it?

Any help is greatly appreciated.

Cheers
Reinhard

Usually it is better to just destroy and instantiate new entities than it is to try and change the RenderMesh on them.

1 Like

But then I would have to have different prefabs (different Materials/Shader) for the same object… sound like a huge overhead …

Changing the RenderMesh shared component as you describe is one way to do this in 0.50 and 0.51. There will be a structural change with some associated performance cost.

But is it really the same object? Do you want your holographic turret to shoot at enemies?

Changing a shared component has the same performance impact as destroying an entity and creating a new one in its place. But you can create and destroy entities in Burst, while manipulating shared components has to be done without Burst.

Well, of course not. It’s more like an unfinished version. That’s why it’s tagged with an UnderConstruction Tag which is filtered in most EntityQueries.

This is a valid point. Thank you for elaborating on this.