Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Entities not rendering with Hybrid Renderer v2

Discussion in 'Graphics for ECS' started by Klusimo, Jan 19, 2021.

  1. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Hi, my <title>. My entities have translation, scale, localtoworld and rendermesh components, I populate the render mesh with this custom mesh:
    Code (CSharp):
    1.  
    2.     public static Mesh GetTextureMesh(int Width, int Height)
    3.     {
    4.         Mesh mesh = new Mesh();
    5.  
    6.         Vector3[] vertices = new Vector3[4]
    7.         {
    8.             new Vector3(0, 0, 0),
    9.             new Vector3(Width, 0, 0),
    10.             new Vector3(0, Height, 0),
    11.             new Vector3(Width, Height, 0)
    12.         };
    13.         mesh.vertices = vertices;
    14.  
    15.         int[] triangles = new int[6]
    16.         {
    17.             0, 2, 1,
    18.             2, 3, 1
    19.         };
    20.         mesh.triangles = triangles;
    21.  
    22.         Vector3[] normals = new Vector3[4]
    23.         {
    24.             -Vector3.forward,
    25.             -Vector3.forward,
    26.             -Vector3.forward,
    27.             -Vector3.forward
    28.         };
    29.         mesh.normals = normals;
    30.  
    31.         Vector2[] uv = new Vector2[4]
    32.         {
    33.             new Vector2(0, 0),
    34.             new Vector2(1, 0),
    35.             new Vector2(0, 1),
    36.             new Vector2(1, 1)
    37.         };
    38.         mesh.uv = uv;
    39.  
    40.         return mesh;
    41.     }
    And I have material (basic one with Unlit/Transparent) that I load trough Resources.Load, assign texture to it and add it to the entity:

    Code (CSharp):
    1.     public static Material GetSpriteMaterial(string Material, string SpritePath)
    2.     {
    3.         Material material = new Material(Resources.Load<Material>("Materials/" + Material));
    4.         Texture2D texture = Resources.Load<Texture2D>("Sprites/" + SpritePath);
    5.         material.mainTexture = texture;
    6.         return material;
    7.     }
    I update the rendermesh trough entities.foreach.withoutburst().run()
    Trough entity debugger, my mesh and material are added to the entity, but they are not rendered.

    I use URP and the newest version of everything.

    And nothing.
     
  2. joelv

    joelv

    Unity Technologies

    Joined:
    Mar 20, 2015
    Posts:
    203
    You need a few more components that that. Currently I propose you look at the code in MeshRendererConversion.cs in the hybrid renderer package for what components are needed.

    You seem to be missing:
    PerInstanceCullingTag
    RenderBounds
    WorldToLocal_Tag

    In the next package drop we will be providing a utility function that adds all requires components to an entity and makes it renderable.

    EDIT: this seems to be discussed in a couple of other threads as well
    https://forum.unity.com/threads/how-to-set-rendermesh-during-authoring.1041385/#post-6740434 for example
     
  3. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    I added those, but still nothing, here is my entire archetype for that entity:
    Code (CSharp):
    1. var archetype = World.DefaultGameObjectInjectionWorld.EntityManager.CreateArchetype(
    2.             typeof(Translation),
    3.             typeof(Scale),
    4.             typeof(LocalToWorld),
    5.             typeof(RenderMesh),
    6.             typeof(RenderBounds),
    7.             typeof(PerInstanceCullingTag),
    8.             typeof(WorldToLocal_Tag)
    9.             );
     
  4. scarface117

    scarface117

    Joined:
    Nov 9, 2019
    Posts:
    16
    I'd recommend following a different approach.
    Currently it seems everything is convoluted and it's not following Unity's own path of conversion.
    Also I don't get why you have to load the same material every time (maybe it's for this example purposes) but you could just create a prefab with the material you need, and then instantiate that prefab.

    It depends a lot on what you're trying to achieve.

    If you don't have a constraint on whether certain material can be applied to a lot of different meshes, and on the other hand, have specific meshes asigned for particular materials (In other words, there will be not to many instances of Material/Mesh combination) you could create prefabs, and instantiate them.

    You could add those prefab an authoring component if you want to extend functionality.

    Hybrid Renderer V2 doesn't get along with creating RenderMesh components from scratch, I've noticed it works a lot better when using the default Unity's Conversion System.

    Let me know if you need me to elaborate on this advice, but it should be pretty straightforward:

    - Create a Prefab that has MeshRenderer and MeshFilter
    - If possible assign the needed Mesh and Material manually to that prefab
    - Create one prefab for each Material/Mesh combination you need.
    - Create a Library that takes all these prefabs, and you can use an Entity Command Buffer to instantiate them, everytime you need to.
    - The library could be an entity that contains an EntityBuffer filled with these Prefabs (Entity type)
    - In order to do this library, you could create an AuthoringComponent (MonoBehaviour that implements IConvertGameObjectToEntity) that takes a List of GameObjects, then also Implement IDeclareReferencedPrefabs so you can add the prefabs you need.
    - Then add these prefabs to a DynamicBuffer filled with entities.
    - You could create an enum that allows you to link these entities witha more readable name.
    - You could also create a ComponentSystem (or SystemBase) that allows you to access this specific and singleton entity which contains the references to the prefabs you need for instantiation.


    I could provide more details on implementation if needed, hopefully this will serve as a better approach to what you're doing.

    Also, HybridRenderer V2 and RenderMesh components are very delicate and in my experience they don't fully support runtime modifications, so that's another point on why having everything pre-defined will provide you with better performance and more stability.
     
  5. xshadowmintx

    xshadowmintx

    Joined:
    Nov 4, 2016
    Posts:
    47
    I've got to say, this is currently the worst of all worlds; it's using the old game object system for prefabs, the conversion to an ECS object is 'copy and paste unity implementation' and even if you get the obscure required data attributes correct, the renderer to fragile and can't cope with runtime changes.

    I also tried to do this (ie. instantiate my own custom renderable objects purely in code with no entity conversion) and struggled with it and gave up; what makes me :( is that there's no meaningful reason to expect that's going to change.

    I think it's entirely fair to raise to complain that this isn't even close to ideal.

    Hey @joelv, this sucks. That there are multiple threads about it is an indication that it sucks, not that it's a problem the community is just ok with.
     
  6. joelv

    joelv

    Unity Technologies

    Joined:
    Mar 20, 2015
    Posts:
    203
    We are aware of that and we are working towards making it easier. Hybrid renderer 0.11 will have a runtime available utility function that sets up a renderable entity for you, it will be the exact code we run in conversion time but available as a util at runtime.
     
    Lurking-Ninja and Timboc like this.
  7. Klusimo

    Klusimo

    Joined:
    May 7, 2019
    Posts:
    76
    Is there a possibility to add runtime-changes in the future (I mean that arent dangerous/hard/not-build-in)?
     
  8. Arycama

    Arycama

    Joined:
    May 25, 2014
    Posts:
    182
    I'm pretty sure there's a bug in 2020.2 related to SRP Batcher and BatchRendererGroup. (Which is what Hybrid Renderer uses under the hood)

    Hope it gets fixed soon, it's broken my custom ocean system unless I disable SRP Batcher for the entire project. (Which isn't exactly ideal...)

    https://fogbugz.unity3d.com/default.asp?1307714_5778k36lo52uh1sp
     
  9. joelv

    joelv

    Unity Technologies

    Joined:
    Mar 20, 2015
    Posts:
    203
    Changing/creating mesh should be possible. Probably creating a new material as well. If it is not working perhaps you could create a minimal repro for what you want supported, submit a bug and we can see if it really is a bug or some new support we need to implement.