Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Entities Graphics and custom shader properties issue

Discussion in 'Graphics for ECS' started by grogzgr, Mar 7, 2023.

  1. grogzgr

    grogzgr

    Joined:
    Jan 10, 2014
    Posts:
    3
    I have a custom URP Unlit Shader created in Shader Graph with boolean property that I want to set on a per instance basis in a System. From the documentation it seems like it should be a straight forward thing to do, but try as I might, it's not working for me.

    If needed I can post snippets of code, but hopefully the following description will trigger someone to say "oh yeah, you need to do .... "

    I have created a managed prefab, used the Baker to convert it to an entity, then used this entity to instantiate multiple entities using EntityManager.Instantiate() . The material on the prefab uses a custom URP Unlit shader with a custom property that is "Exposed" and has the "Shader Declaration" set to "Hybrid Per Instance". I created the IComponentData struct with the MaterialProperty set to the "Reference" name of my exposed property.
    I have done this as per the manual page:
    https://docs.unity3d.com/Packages/c...phics@1.0/manual/material-overrides-code.html

    I am setting the property in a ISystem using my custom aspect. The component reference in the aspect is declared as a RefRW, and I am setting the value using:

    Code (CSharp):
    1. myAspect.myComponent.ValueRW.Activated = 1f;
    In the inspector I can see that the property is being set on the Entity instance, but it does not seem to be having an effect on the shader.

    What am I missing?

    Unity version: 2022.2.0f1
    Entities: 1.0.0-pre.15
    Entities Graphics: 1.0.0-pre.15
    Universal RP: 14.0.4
     
  2. suity447

    suity447

    Joined:
    Oct 18, 2022
    Posts:
    33
    I am not 100 % sure but I seem to remember that boolean properties can have some unexpected behaviours. It seems that your IComponentData is a float? If it is a bool in the shader graph maybe change it to an float/int/uint and add a comparison block in the graph to output a bool.
     
    grogzgr likes this.
  3. grogzgr

    grogzgr

    Joined:
    Jan 10, 2014
    Posts:
    3
    Well you got it. According to what I read a boolean in a shader should be set by a float in a IComponentData, but this turns out not to be the case currently.

    So I did as you suggested and replaced the boolean property in the Shader with a float property. Then I changed the Predicate in the Branch node to be fed the "Out" of a new Comparison Node, with the Comparison node checking if the new float property was 1 or 0. This just feels wrong, and a hack, but it works so ...

     
  4. JussiKnuuttila

    JussiKnuuttila

    Unity Technologies

    Joined:
    Jun 7, 2019
    Posts:
    283
    Unity's shader system does not support booleans, and shaders in general don't really support booleans as input/output data, only as results of computations done inside the shader. Using an int or a float as you have done is the proper solution.