Search Unity

Question Creating SubGraph for Barycentric Coordinates using SV_Barycentric

Discussion in 'Shader Graph' started by GoshawkMachinist, Apr 19, 2023.

  1. GoshawkMachinist

    GoshawkMachinist

    Joined:
    Oct 28, 2020
    Posts:
    13
    Hello, I'm trying to create SubGraph that would make accessible Barycentric Coordinates for shader graph use. Aleksandrk on Shaders forum recommended to use SV_Barycentric to get coordinates.
    https://forum.unity.com/threads/bar...ragment_shader_barycentric-extension.1426257/

    I'm not sure, if it is possible to crate SubGraph with SV_Barycentric. And how to do it. I've tried without success:


    I've also tried change function body to:

    #pragma require barycentrics
    void BarycentricCoordinate_float3(float3 BaryLinear : SV_Barycentrics, out float3 Out)
    {
    Out = BaryLinear;
    }
     
  2. GoshawkMachinist

    GoshawkMachinist

    Joined:
    Oct 28, 2020
    Posts:
    13
    I've made some digging. It seems, it isn't possible to use #pragma keyword in shader graph. :-(

    It seems, it is not possible to use SV_Barycentric in shader graph. I'm thinking about pushing Barycentric Coordinates into UV in the Tessalation Domain shader (from SV_TessFactor / SV_InsideTessFactor). I'm going to use Tessalation anyway.
     
  3. wwWwwwW1

    wwWwwwW1

    Joined:
    Oct 31, 2021
    Posts:
    769
    Not an expert, but have you tried using
    #include_with_pragmas "*.hlsl"
    to include #pragma keywords?

    What I mean is to set the custom function node type to "File" and use
    #include_with_pragmas
    to include another hlsl file that requires barycentrics.
     
  4. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    I don't believe the shader pipeline can pass barycentric or any other built-in semantic to a random function. These semantics have their values generated at specific points in the pipeline if you're requesting them during that stage.

    Barycentric would be computed after the vertex pass, so it's a value you can have passed into the fragment pass by defining it as a parameter on your fragment function.

    For example:
    void frag(v2f in, float3 bary : SV_Barycentrics) : SV_Target

    So this is a case where you'll have to write a custom shader instead. Or at the very least, make what you can in shadergraph, then "view generated shader" and modify it to have the barycentrics and finish it off as shader code.
     
  5. GoshawkMachinist

    GoshawkMachinist

    Joined:
    Oct 28, 2020
    Posts:
    13
    You are right. I'm thinking exactly about this. Hopfully shader graph can generate this in the future. Like adding checkboxes to generate things like SV_Barycentrics. It is very restrictive right now.