Entities not rendering with Hybrid Renderer v2

Hi, my . My entities have translation, scale, localtoworld and rendermesh components, I populate the render mesh with this custom mesh:

    public static Mesh GetTextureMesh(int Width, int Height)
    {
        Mesh mesh = new Mesh();

        Vector3[] vertices = new Vector3[4]
        {
            new Vector3(0, 0, 0),
            new Vector3(Width, 0, 0),
            new Vector3(0, Height, 0),
            new Vector3(Width, Height, 0)
        };
        mesh.vertices = vertices;

        int[] triangles = new int[6]
        {
            0, 2, 1,
            2, 3, 1
        };
        mesh.triangles = triangles;

        Vector3[] normals = new Vector3[4]
        {
            -Vector3.forward,
            -Vector3.forward,
            -Vector3.forward,
            -Vector3.forward
        };
        mesh.normals = normals;

        Vector2[] uv = new Vector2[4]
        {
            new Vector2(0, 0),
            new Vector2(1, 0),
            new Vector2(0, 1),
            new Vector2(1, 1)
        };
        mesh.uv = uv;

        return mesh;
    }

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:

    public static Material GetSpriteMaterial(string Material, string SpritePath)
    {
        Material material = new Material(Resources.Load<Material>("Materials/" + Material));
        Texture2D texture = Resources.Load<Texture2D>("Sprites/" + SpritePath);
        material.mainTexture = texture;
        return material;
    }

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.

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
How to set RenderMesh during authoring? for example

I added those, but still nothing, here is my entire archetype for that entity:

var archetype = World.DefaultGameObjectInjectionWorld.EntityManager.CreateArchetype(
            typeof(Translation),
            typeof(Scale),
            typeof(LocalToWorld),
            typeof(RenderMesh),
            typeof(RenderBounds),
            typeof(PerInstanceCullingTag),
            typeof(WorldToLocal_Tag)
            );

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.

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 :frowning: 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.

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.

2 Likes

Is there a possibility to add runtime-changes in the future (I mean that arent dangerous/hard/not-build-in)?

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

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.