Per-instance material params support in Entities 0.2!

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.

  1. Create a new HDRP material (I used lit).
  2. Create a blackboard param, for example a Vector4 Color.
  3. Set the Reference field to something you can remember, I used “_Color”
  4. Check the “Hybrid Instanced (experimental)” checkbox on the blackboard variable!

5205743--517985--upload_2019-11-23_15-24-45.png

  1. Save your shader
  2. Create a material, and use the shader you created.
  3. Check “Enable GPU instancing” if you want to use that (doesnt seem to be necessary to make this work though).
  4. Set up a prefab with the material and spawn an entity with it in ECS.
  5. 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).

5205743--518009--upload_2019-11-23_15-34-2.png

35 Likes

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?

7 Likes

Just tested if it works with URP, and it isn’t :frowning:

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.

11 Likes

I’m assuming static as in non-moving/changing entities. If so no worries :slight_smile:

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 :smile: 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. :slight_smile:

Only Lit Master node of HDRP works right now.

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.

1 Like

Another question:

so i just use this material and attach the component to an entity (and the rendermesh as shared component) and then it just works?

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).

1 Like

Sorry - still can’t get it working :face_with_spiral_eyes:

I created an HDRP shader based on Lit graph.
5256467--525365--upload_2019-12-7_20-18-37.png

I create an HDRP material using this shader.
5256467--525371--upload_2019-12-7_20-20-4.png

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.

I’m using Unity 2020.1.0.a with Entities 0.3.0

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.

Thanks @GilCat . That’s it working now - hadn’t used Shader Graph before

1 Like

Yes this works perferctly :slight_smile:

Really looking forward to when this works with URP, I love the simplicity of it. Better/more rendering solutions are badly needed in dots right now.

4 Likes

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.

4 Likes

I’m using entities 0.4.0 and in URP I see the hybrid instanced check mark but it doesn’t seem to be working. Is this a bug?

at the moment it works only for HDRP, see the reply from Joachim above

Is it possible to have two or more properties inside a single component? Or do i need to create a component for each value that i want to submit?