Shared Materials with different Properties

Hey,

I wrote a shader for uGUI which has some properties.
When I assign the material with the shader to two images and change the properties, both Images are affected of course.
The easiest solution: clone the material.

However, I guess this is not the optimal solution because it probably increases draw calls. I wonder if it is possible to change the properties all the time just before rendering for each image. Not sure if this would increase performance or not…

But any way I tried to achieve this didn’t work. My closest guess was to assign the variables in the UpdateMaterial() Method of my class which extends the UI Image class – No luck.

My Question is: How to handle Materials with different properties the most performant way.

push
Did nobody ever work with materials and uGUI?

One material has one setting.
If you want different settings, like images, basecolor, emission, then that’s another material.
There is no way around this.

Unless…

You could bundle the images into one texture, then set up two meshes with UVs that cover the image areas in the one texture. Then the meshes need to be manually Combined or dynamically Batched if the vert count is under 300 ish. But still the, basic settings cannot be different, the one image holds the two sub images all other setting must be the same.

Or…

If you are just making uGUI things when you import an image you can set it to:
Sprite(2D and UI)
Then make a name for the PackingTag and use that for each image.
Read up on SpriteMode, packingTag, pixels per unit etc…
The savings here is hard to understand, because if the images are on different Panels I think they are separate draw calls still. The basic settings must still be the same.

So just make two materials and dont worry about it, unless you are displaying several hundred of them.

I have never made a shader for uGUI.

Hi Tzan,
Thanks for your reply.

I am not sure if we are talking about the same problem.
I don’t want to do things with UVs or Textures… these things can handle the uGUI-Image Component by itself.
What I want is using my own shader with an image. The Properties are have are basically float values.
I want to change them in the shader before the image is drawn like it does internally with the texture, uv coordinates and vertex colors.
I wonder if this is possible and if it would be faster than having different materials (or if there is a better way in the uGUI universe)

I was editing more while you posted :slight_smile:

So basically use multiple materials.

There might be some direct drawing to the screen hardware thing, but I’ve never done that.

Take a look at this.

In general things sit in the Hierarchy and wait to be rendered.
If you change a Material setting 100 times, only the last setting will be used.

If you need to render two images, having different values in the shader, then you need two materials.

However, if you just need to modify a few of the shader parameters, then you can pass them in the vertices (that’s how uGUI handles colors, uv, etc without breaking the batching).

To do it, just create a VertexModifier, and for every vertex, set the parameters you want to modify. You have 3 floats that you can easily pass using uv1.x, uv1.y, tangent.w. Although with a few of packing you could pass a few more at lower bit resolution.

On SmartEdge I use a similar approach to modify parameters while still sharing the materials.

1 Like

Hi @Inter-Illusion ,

thanks for your tip with the VertexModifier. I will investigate this a bit more. Does it improve performance as I guessed?
The downside of vertex modifier is that it is very restricted and the shaders have to use unused default parameters… is that correct?

It will depend on how many of these objects you will be changing at the same time.

AFAIK, the way that the Unity UI does the batches, is that at rebuild time, it gets the vertices for all UI elements and merges them into a buffer, so that when rendering, its just 1 drawcall for everything using the same material.
If you change any of the elements it just batched together, then the buffer is rebuilt again.

If you have several elements in screen at the same time that you want to modify, then, yes, it should help with performance, because the drawcall cost will be higher than the rebuild-batching cost.
But if you just one to modify 1 element, then having it as a separated material is faster, because the cost of merging all vertices will be higher than the extra drawcall.

e.g. if you have several coin images in screen using that shader and you want to modify the parameters per coin, then yes, creating the shader and passing with the vertex modifier is faster. If you just want a single button to flash, then having the extra material is better.

VertexModifiers give you access to all parameters used for UI. But because Unity UI tries to pass as few per-vertex parameters as possible (to reduce bandwidth), then you don’t have as many parameters as you normally do when manually building a Mesh object (e.g. no UV3, UV4, Normals.xyz and Tangents.xyz can be used either, etc).

Hope that helps,
Frank

1 Like

Thanks for your detailed answer. This makes all sense :slight_smile:
So I will go for the vertex-modifier way because if I only have one object the performance is probably good enough even if it is a bit slower than with an extra material. But if I have lots of objects the performance is better. This case is more important.

just one thing where I am not sure if we are talking about the same thing: you are talking about a VertexModifier and UV3, UV4 and normal.xyz and tangent.xyz.
I cannot pass a VertexModifier object to the vertexHelper class to add a vertex. Instead I can pass a UIVertex class. There I have color (reserved), position (reserved), uv0 (reserved) and: uv1.xy, normal.xyz, tangent.xyzw. Can I use the latter three for my variables? or do I miss something?

Exactly that.
Well, I’m used to say VertexModifier, but that’s obsolete now, you should use a MeshModifier instead.
As you say, that’s a script that you add to your UI object and allows you to modify the UIVertices inside the VertexHelper list.
https://docs.unity3d.com/ScriptReference/UI.BaseMeshEffect.html

As of 5.5 only uv1.xy and tangent.w are free. (this is changing in 5.6 where you can add more channels to the canvas)

But now, even if your shader doesn’t use tangent.xyz and normal.xyz, you can’t sent anything in those 6 floats, given that unity multiplies them by the normal matrix when doing the batching to avoid issues with non-uniform scales. So, those 6 floats are all screwed and I haven’t found a way to revert them.

Nonetheless, you can pack several values in those uv1.xy and tangent.w. For instance in SmartEdge, I need to pass 3 values (0…1 range with only 8 bits of accuracy), so, I packed those 3 into the uv1.x and still have uv1.y and tangent.w for other things!

2 Likes