Search Unity

Question What is the proper way to change appereance of an entity

Discussion in 'Graphics for ECS' started by Santonian, Aug 23, 2022.

  1. Santonian

    Santonian

    Joined:
    Sep 23, 2020
    Posts:
    20
    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:
    Code (CSharp):
    1.         Entities.WithStructuralChanges().
    2.             ForEach((Entity ent, MaterialChangerData changer, in RenderMesh mesh) => {
    3.                 RenderMesh modifiedMesh = mesh;
    4.                 modifiedMesh.material = changer.changeToMaterial;
    5.                 entityManager.RemoveComponent<MaterialChangerData>(ent);
    6.                 entityManager.SetSharedComponentData<RenderMesh>(ent, modifiedMesh);
    7.             }).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
     
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,269
    Usually it is better to just destroy and instantiate new entities than it is to try and change the RenderMesh on them.
     
    charleshendry likes this.
  3. Santonian

    Santonian

    Joined:
    Sep 23, 2020
    Posts:
    20
    But then I would have to have different prefabs (different Materials/Shader) for the same object... sound like a huge overhead ...
     
  4. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    351
    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.
     
  5. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,269
    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.
     
  6. Santonian

    Santonian

    Joined:
    Sep 23, 2020
    Posts:
    20
    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.