Search Unity

HDRP Custom Shaders Examples

Discussion in 'Shaders' started by EvaldasZmitra, May 15, 2019.

  1. EvaldasZmitra

    EvaldasZmitra

    Joined:
    Jan 28, 2017
    Posts:
    2
    If you're anything like me you will have started writing GLSL shaders where everything is clear, then gone into Unity's ShaderLab and struggled with semantics and all the other confusing, but probably necessary stuff. Now the struggle returns with SRP (HDRP in particular). I found myself not being able to create a simple triangle or a simple lithing shader once again! I bet there are at least several hundred people like me who want to write some interesting shaders but are unable to because of lack of documentation and examples.

    Please send leave some links to simple shaders that work on HDRP here.

    Preferably help us get started by sending an example for generating 1 triangle with a geometry shader in HDRP.
    Reading different vertex buffers.
     
    fct509, EthanFischer and AverageCG like this.
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,343
    We're all waiting for that. The HDRP is still in flux, so no one outside of Unity really knows how to write shaders for the HDRP unless you dig through the source code.

    Unity's official stance is "use Shader Graph". Writing shaders that work with the HDRP is enormously complex, like an order of magnitude more complex than the built in rendering paths.
     
    EthanFischer and shegway like this.
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Is this still the case (no official examples)?

    I'm trying to find some. And this thread came up first.
    Best I could find is keijiro's repo, might be of use for someone as well.

    https://github.com/keijiro/TestbedHDRP

    Nodes are fine as long its not something as complex as porting terrain, or speedtree shaders, or any other custom vfx done prior to the HDRP update.

    So any extra examples would be nice.

    Edit:
    Also, this is probably the gold
    https://catlikecoding.com/unity/tutorials/scriptable-render-pipeline/
     
    Last edited: Feb 14, 2020
    Olmi and EricWilliams like this.
  4. EthanFischer

    EthanFischer

    Joined:
    Feb 10, 2016
    Posts:
    46
    As someone who is new to learning shaders, the lack of documentation and overwhelming amount of options is frustrating. I'm halfway through an online course where I'm learning how to write shader code for unity, and I just came to find out that I won't be able to apply what I'm learning to my SRP project. At least not directly.

    Thanks to @bgolus, at least I know now that unity wants me to lean into shader graph. But it would have been nicer to not have to seek a thread like this out to find this out. Ugh
     
  5. Ruuds

    Ruuds

    Joined:
    Dec 3, 2017
    Posts:
    17
    Did anything change? how did you guys figure out how to work with HDRP?
     
    Foxaphantsum likes this.
  6. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    In HDRP 7.X, if you right-click on the master-node in a shader graph, you get an option in the context menu that lets you generate a shader based on the shader you created with shader graph. It will be a real monster of a shader, but you can study it along with other variations of the shader, in order to figure out how to write a shader for HDRP. I've seen the occasional post from people that claim to have learned how to hand write shaders for HDRP this way, but they also said there was a lot of trial and error before they figured out which parts make up the minimum requirements for an HDRP.

    It's no replacement for proper documentation, which is something that HDRP has always lacked.
     
  7. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Can be a little bit easier to just look in the HDRP package's shader folder and see how the existing shaders are written. Then you're privy to all the handy macros they've written to make things simpler. And you can always find out what the macro is doing by doing a text search of the hlsl and include files in the HDRP and Core packages.
     
    Egad_McDad likes this.
  8. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    Oh, I've done that when it comes to custom passes. And, if they do the thing that I suggested, then they're going to have to do that also, because they're going to have to look up what the macros get translated to. A person can spend days digging through all the shader files, drawing diagrams, and writing their own documentation.
     
  9. Draykoon

    Draykoon

    Joined:
    May 23, 2020
    Posts:
    3
    I write some tutorials for a simple pixel and geometry shader.
    If you don't want to use their lighting models it looks actually very similar to the standard surface approach.

    pixel shader:


    source code: https://github.com/Paltoquet/HDRPCustomShader

    example:


    Code (CSharp):
    1. HLSLPROGRAM
    2.             // List all the attributes needed in your shader (will be passed to the vertex shader)
    3.             // you can see the complete list of these attributes in VaryingMesh.hlsl
    4.             #define ATTRIBUTES_NEED_TEXCOORD0
    5.             #define ATTRIBUTES_NEED_NORMAL
    6.             #define ATTRIBUTES_NEED_TANGENT
    7.  
    8.             // List all the varyings needed in your fragment shader
    9.             #define VARYINGS_NEED_TEXCOORD0
    10.             #define VARYINGS_NEED_TANGENT_TO_WORLD
    11.          
    12.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/RenderPass/CustomPass/CustomPassRenderers.hlsl"
    13.             #include "Packages/com.unity.render-pipelines.high-definition/Runtime/RenderPipeline/ShaderPass/VertMesh.hlsl"
    14.  
    15.             float4 _ColorMap_ST;
    16.             float4 _Color;
    17.  
    18.             PackedVaryingsType Vert(AttributesMesh inputMesh)
    19.             {
    20.                 VaryingsType varyingsType;
    21.                 varyingsType.vmesh = VertMesh(inputMesh);
    22.                 return PackVaryingsType(varyingsType);
    23.             }
    24.  
    25.             float4 Frag(PackedVaryingsToPS packedInput) : SV_Target
    26.             {
    27.  
    28.                 float4 outColor = float4(0.0, 0.2, 0.8, 1.0);
    29.                 return outColor;
    30.             }
    31.  
    32.             #pragma vertex Vert
    33.             #pragma fragment Frag
    34.  
    35.             ENDHLSL
     
    Last edited: Jun 5, 2021
    ReformSim, bb8_1, jjbish and 3 others like this.
  10. fct509

    fct509

    Joined:
    Aug 15, 2018
    Posts:
    108
    jajajajajajajajajajajajaja

    If you had posted this 3 or 4 days sooner, I would have saved quite a few hours of work. This isn't too far off from how I pulled the VertexID data.
     
  11. gattusoarthur

    gattusoarthur

    Joined:
    Apr 29, 2020
    Posts:
    5
    Any hint on how to include HDRP files in visual studio?
     
  12. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Never tried, you could add a folder reference, but that change might get overwritten when code recompiled in editor. Just use search text in files. Keep open the files that have the content you're interested in using.

    If you look at HDRP shaders you can see how their #includr lines written the path to reference those files.