Search Unity

Question Wireframe shader compatible with SRP batching

Discussion in 'Shaders' started by beevik_, Jul 7, 2021.

  1. beevik_

    beevik_

    Joined:
    Sep 27, 2020
    Posts:
    101
    Has anyone written a wireframe shader compatible with SRP batching? Surprisingly, Unity does not include such a shader (as far as I can tell).

    I have a Universal RP project using DOTS hybrid rendering (v2). This requires that I use only materials that are compatible with SRP batching. Unfortunately, I also have need of a wireframe view mode, but I cannot find any info on obtaining or building a wireframe shader that is compatible with SRP batching.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    This page from their documentation has a simple example of an SRP compatible unlit vertex fragment shader, and unlit shader with a color property.
    https://docs.unity3d.com/Packages/c...riting-shaders-urp-basic-unlit-structure.html
    https://docs.unity3d.com/Packages/c...2/manual/writing-shaders-urp-unlit-color.html

    From that you can try and apply the information from this Catlike Coding tutorial on geometry shaders and wireframe rendering.
    https://catlikecoding.com/unity/tutorials/advanced-rendering/flat-and-wireframe-shading/

    The reason why I’m pointing you towards that instead of something using Shader Graph is it is not possible to add wireframe rendering using Shader Graph. At least not by itself. The Catlike Coding tutorial requires using geometry shaders, and Unity does not support geometry shaders in Shader Graph. They will never support geometry shaders with Shader Graph, because some newer rendering APIs are removing support for it, specifically Apple’s Metal, but others may follow suit in the future. Also plenty of older Android devices don’t support geometry shaders either, nor do stand alone VR headsets like the Quest and Quest 2.

    The alternative to geometry shaders is to bake the necessary barycentric data into the mesh itself. That is make sure each triangle has UVs or vertex colors that are different values for each vertex. That’s all the geometry shader is doing. Sadly this is data the GPU already has, and uses for interpolating values across triangle surfaces, but isn’t exposed in a lot of graphics APIs, so you have to provide it manually.

    If you want to avoid having to figure all this out how to get this working yourself, this asset supports the URP, and has been the standard for wireframe shaders for years now. If for some reason the included shaders don’t work with the SRP Batcher, which I have no reason to think they don’t, the author should be able to fix them for you. Otherwise you could use the custom Shader Graph node the asset comes with to make an SRP Batcher compatible shader.
    https://assetstore.unity.com/packages/vfx/shaders/wireframe-shader-181386#description