There is some good news (that I’m sure some of you already know)… It is possible to use the new per-instance-material-params for some common parameters without editing shaders and doing a lot of extra work. I can’t comment on how performant it is, but it’s quite easy to use.
Everything I read said it only worked with HDRP and you had to somehow make your own shader with shader graph, and set up some experimental things and create a material, and make a component, and do a bunch of other voodoo… and so much stuff that I just decided it wasn’t worth the hassle. In the past 3 months I probably spent an entire day trying to figure out how to change the base color of a single instance.
Today I went to DOTS Hybrid Renderer | Hybrid Renderer | 0.8.0-preview.19 and noticed the “Built-in material property overrides” section has some common parameters listed, such as “base color”. It took a lot of mucking around before I finally figured out how to change the overrides at runtime.
So for anyone else having trouble figuring out what to do… here’s an example for changing the base color:
I know this works with the built-in URP Lit shader, but I’m not sure if it works with others. Disappointingly, it does NOT work with SimpleLit.
After adding using Unity.Rendering; there are really only two easy steps.
Step 1, Option A) If you’re using a hybrid approach and have a mesh on a prefab (that’s being converted to an entity), you will add a URPMaterialPropertyBaseColor “URP Material Property Base Color Authoring” to your prefab.
Step 1, Option B) Or if you’re doing it in pure ECS, whenever you instantiate your call something like EntityManager.AddComponent(entity).
Step 2, Option A) If you can fetch the objects via query, you’ll have something like:
Entities.ForEach((ref URPMaterialPropertyBaseColor color) => {
color.Value.x = UnityEngine.Color.yellow.r;
color.Value.y = UnityEngine.Color.yellow.g;
color.Value.z = UnityEngine.Color.yellow.b;
color.Value.w = UnityEngine.Color.yellow.a;
});
Step 2, Option B) Or if you are fetching the entity within another query you can do something like:
if (EntityManager.HasComponent<URPMaterialPropertyBaseColor>(entity)) {
URPMaterialPropertyBaseColor color = EntityManager.GetComponentData<URPMaterialPropertyBaseColor>(entity);
color.Value.x = UnityEngine.Color.yellow.r;
color.Value.y = UnityEngine.Color.yellow.g;
color.Value.z = UnityEngine.Color.yellow.b;
color.Value.w = UnityEngine.Color.yellow.a;
}
There are several other “built-in” parameters listed in the documentation link I provided above. The way I figured out what the components were called was by going to this folder: C:\Users\Me\AppData\Local\Unity\cache\packages\packages.unity.com\com.unity.rendering.hybrid@0.8.0-preview.18\Unity.Rendering.Hybrid\URPMaterialProperties (where Me is the username). I didn’t find anything in the documentation that listed the IComponent derived struct names, so that folder was really helpful in figuring out what components were available.
One of the ones I tried was URPMaterialPropertyEmissionColor, but it never seemed to work. I don’t know if it’s a bug or if it doesn’t work with certain shaders, or if it needs to be handled differently than the base color one, or if there’s some global setting for emission stuff that needs to be tweaked.
My test setup is 2020.1.2 with Hybrid Renderer V2 0.8.0-preview.18 and URP.