For some reason this was not in the release notes, but I’ve managed to get per-instance material params working in the 0.2 release. This is similar in function to the old material blocks.
I’ve only tested this with HDRP so far.
Create a new HDRP material (I used lit).
Create a blackboard param, for example a Vector4 Color.
Set the Reference field to something you can remember, I used “_Color”
Check the “Hybrid Instanced (experimental)” checkbox on the blackboard variable!
Save your shader
Create a material, and use the shader you created.
Check “Enable GPU instancing” if you want to use that (doesnt seem to be necessary to make this work though).
Set up a prefab with the material and spawn an entity with it in ECS.
Create your own material param component, or you can use the included MaterialColor for example.
[Serializable]
[MaterialProperty("_Color", MaterialPropertyFormat.Float4)]
public struct MaterialColor : IComponentData
{
public float4 Value;
}
[Serializable]
[MaterialProperty("_Multi", MaterialPropertyFormat.Float)]
public struct ColorMultiply : IComponentData
{
public float Value;
}
Voila, ECS rendered meshes with materials that support per-instance params!
Here as an example I set the color and a multiplier value (applied to the color of the upper cube).
It would seem that performance starts degrades after 8000 entities or so with 4 different color variants.
UpdateDynamicRenderBatches takes about 25ms in build with 14,000 entities. 9ms were spent sorting (realistically waiting on a jobID from the looks of it) Comparing to the 200k cubes I had in an earlier instance without color variants, this would be a regression, but there is a chance I am using this system wrong.
The scenario below: A spawner spawning in a cube with a EmissionComponent (Merely a MaterialColor component targeting a different shader component) and a velocity. The cube always heads forward and is spawned with a different rotation than the previous cube. @Joachim_Ante_1 any thoughts on what to do to improve the system either on our side or Unity’s?
I believe everything for the moment is HDRP compliant, and URP will be at a later date. Trying to find the source of that as I remember reading that in the past couple posts.
Right now the only codepath we spent seriously optimising is static entities
There is a bunch more work coming for making dynamic entities a lot faster.
URP is not supported at the moment. Focus has been on HDRP. But it will come.
I’m assuming static as in non-moving/changing entities. If so no worries
If it is just referring to the MaterialProperty block concept, I am currently only able to get ~17k entities (non-dynamically colored) moving at a rough 60fps.
For now I have other things that I have to look at with this update Namely burstable Command buffer and a few other additives. Just need to figure out how to go from a JSON string to a struct with a group of NativeStrings efficiently.
Just to confirm, is this also meant to work by modifying the MaterialColor component during runtime? @siggigg I think I’ve followed all your steps setting up the material. I create Entities at runtime, adding the MaterialColor component.
Yes you can modify color or any other shader parameter at runtime. I have this driving health bar UIs in my game, sending a few parameters including fill percentage.
As long as you follow my instructions then yes. You need to be using HDRP and you need to enable that flag in your shadergraph on the color parameter (and any other parameters you want to pass to the shader).
I then create Entities during runtime with a RenderMesh component using this material as well as a MaterialColor component, each one with random float4 values. All entities show a grey color. If I check the Entity Debugger, I can see the MaterialColor components with random float values.
On Shader Graph did you connect the “_Color” property to the “_BaseColor” of Lit Master?
Only that way you can make it work.
Following that you can also create more properties to to other attributes of the Lit Master node giving you more per instance properties.
Likewise. I am completely ignoring ECS until I don’t have to write my own system to render entities with different material settings efficiently. I also don’t want to use HDRP.