Override RenderMesh mesh uvs

Is it possible to override RenderMesh mesh uvs? I would like to do it in order to make dots animation system and other systems which operate on uvs.

You can set the vertex data on the mesh, if that’s what you mean. The RenderMesh is just a handle to an underlying Unity Mesh object, which has several ways to set UVs (slow way: Unity - Scripting API: Mesh.uv fast way but requires you set whole vertex stream: Unity - Scripting API: Mesh.SetVertexBufferData)

1 Like

I know how to set mesh’s uvs, the thing is how can I have per instance uv changes every few frames without big structural changes because of shared component changes.

The mesh is just a reference. There are no structural changes on ECS side.

Yes, that’s the thing. I would like every entity to have component with just the uvs which override RenderMesh’s mesh uvs for this particular entity. This way I can implement easy animation system which just changes uvs in the IComponentData

What exactly are you trying to accomplish with this? It’s almost certainly something that should be handled in a vertex shader not on the CPU. If it can’t be done in a vertex shader, what about a compute shader?

If it absolutely must be done on the CPU, one way would be 2 vertex streams, one of {position, normal} and another of {uv, tangent}. The meshes share the same first stream but have separate versions of the second stream. I’m not sure if this is even possible using the Mesh APIs, you might need to hack the render pipeline. It certainly supports multiple vertex streams, but I don’t think the streams can be shared between meshes.

Or, if there are only a few of these, copy the whole vertex data to each, with slight UV changes. However, you’re still uploading data from the CPU to the GPU every frame, which can get slow.

1 Like

Do you need to change the UVs directly, or would changing an offset suffice? With an offset, you can submit a float2 per entity to the GPU rather than a float2 per vertex per entity. You can easily implement the texture offset in shader graph if you don’t want to write shader code. And then you can specify a MaterialProperty IComponentData to have the HybridRenderer upload it per instance.

1 Like

I am using it in 2D game so I guess offset would suffice. You are sure it would work for changing uv in spritesheet texture?

Depends on your implementation of your sprite sheet. But most likely, yes.