Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Use MaterialPropertyBlock for custom UI Graphic shader?

Discussion in 'General Graphics' started by glennpow, May 14, 2015.

  1. glennpow

    glennpow

    Joined:
    Jan 30, 2012
    Posts:
    56
    I'm trying to figure out the best practice for dealing with custom UI shaders/materials. I have a set of Graphics (RawImages) and they are all supposed to use a custom material/shader that I wrote. I copied the Default UI shader and made my changes, and the shader and material seem to work fine, but I'd rather not have to create unique materials for each Graphic that uses this shader (since they will all use unique material properties, such as for Brightness and Contrast).

    I see in the shader the use of [PerRendererData] before the main texture property, which I assume is because the texture is set by the CanvasRenderer during rendering (presumably using MaterialPropertyBlocks). This seems optimal, since then all the Graphics can share the same material (and probably also the same draw call, if the textures are sprites on the same sheet). I would like to also utilize this optimal workflow.

    Is there some way that I can set the MaterialPropertyBlock in a custom RawImage class? Or is there a better way for me to have access to the custom material properties for each unique Graphic?
     
  2. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    957
    You can use a material modifier component to adjust materials per graphic. I used this approach to make shader keywords work with masked graphics. Using different settings will of course result in different batches.

    Alternatively, use a vertex modifier and encode your material properties in the vertex uv1, color, or tangent channels. It's more convoluted, but won't break batches as everything still uses the same material settings.
     
    rsodre likes this.
  3. glennpow

    glennpow

    Joined:
    Jan 30, 2012
    Posts:
    56
    Yes! Material modifiers seem like the way to go. I've used vertex mods quite regularly, but I hadn't seen the material variations.
    Thanks so much, and your plugin looks very cool! With the new UI version, can you create real outlines around text (not Unity's fake outline vert mod)? Also, can the SDF texture be created at runtime, and not saved to disk or is that not performant?
     
  4. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    957
    You can generate SDF textures at run time, although the generator is currently not really optimized for it. It's about as fast as it can get, it's just messy with memory. Once you have the SDF texture, you can add outlines and such. Then you can use that for a bitmap font.

    You could grab Unity's font atlas and generate an SDF from it, but it's not ideal. The characters aren't spaced with SDF in mind, leaving no room for effects. And when using dynamic fonts, you'd need to regenerate the SDF each time Unity adjusts the atlas.
     
  5. glennpow

    glennpow

    Joined:
    Jan 30, 2012
    Posts:
    56
    Excellent points. Runtime usage probably wouldn't be feasible in most cases.
    Still a cool plugin, and open-source ftw! Thanks again.
     
  6. Jasper-Flick

    Jasper-Flick

    Joined:
    Jan 17, 2011
    Posts:
    957
    Sure! But although it's free right now, it's not open source.