Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

HDRP Custom shaders

Discussion in 'Graphics Experimental Previews' started by Chaiker, Mar 9, 2018.

  1. Chaiker

    Chaiker

    Joined:
    Apr 14, 2014
    Posts:
    63
    Hi there. Do HDRP has something like surface shaders in standard pipeline? How can I write custom shaders with vertex animation or UV animation etc. for HDRP?

    Thanks.
     
  2. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    not yet, and possibly not ever (although unity have said they would like an abstraction like this for shadergraph in the future)
     
    EthanFischer likes this.
  3. Chaiker

    Chaiker

    Joined:
    Apr 14, 2014
    Posts:
    63
    But shader graph generate file that has only nodes information and nothing about shader. And what I see in shader graph it is master node, and it seems like something surface output structure like in all visual shader editors.
     
  4. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    Yeah but its not a surface shader.
     
  5. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    IMO your best bet will be copying the code for the standard HD PBR master node in the shadergraph package when it becomes compatible with HDRP, and making your own custom master node starting from this.

    I used this approach to make a toon shader in the LWRP, and it was very simple
    https://i.gyazo.com/eefabafce1f1a388991f4c44d521675f.mp4
    Now I can use my "Toon" node in shadergraph just like with any other node. I basically just use ShaderGraph as a fancy custom inspector for my shader. All the logic that really matters is in code

    EDIT: Here's something to keep us busy until release:
    https://github.com/Unity-Technologies/ShaderGraph/blob/HDMasterMerge/HDPipeline/HDPBRSubShader.cs
    https://github.com/Unity-Technologi...raph/Editor/Data/MasterNodes/PBRMasterNode.cs
     
    Last edited: Mar 13, 2018
  6. Reanimate_L

    Reanimate_L

    Joined:
    Oct 10, 2009
    Posts:
    2,788
    huh, never realize that branch before. Is it stable to be tested with HDRP?
     
  7. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Not sure, I haven't tried shadergraph with HD yet
     
  8. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    I think we must drop our all surface shader experiences and start learning HLSL language for shader programming
     
    antoripa likes this.
  9. Dolzen

    Dolzen

    Joined:
    Mar 26, 2014
    Posts:
    90
    Hello! can you share what you did for creating that toon shader?

    What I was doing to achieve toon shading is modding the dot product between the light direction and the normal in this function LightingPhysicallyBased() in Lighting.hlsl

    Thanks!
     
  10. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    How did you get the light direction?

    I know how to using inbuilt pipeline but I cant find any concrete info on how to in new one?

    Really want to be able to do NdotL etc
     
  11. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    If we take the LWRP for example, you'll find this in the "Lighting.hlsl" file in the LWRP package:


    This should be a good place to start studying the whole system. You might need to go look at PBRMasterNode.cs in ShaderGraph to find out how all this is connected too. I actually forgot how I did all this
     
    Sab_Rango and MadeFromPolygons like this.
  12. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    This is EXACTLY what I have been looking for, for a couple of days now! Thankyou for this!!!
     
  13. PhilSA

    PhilSA

    Joined:
    Jul 11, 2013
    Posts:
    1,926
    Some additional info I just pieced together:
    • "LightWeightPBRSubShader.cs" is what defines the master PBR node in LWRP
    • it uses "lightweightPBRForwardPass.template" to define the main contents of the generated shader
    • In "lightweightPBRForwardPass.template", the main lighting calculations are done by calling "LightweightFragmentPBR()" in the frag shader
    • "LightweightFragmentPBR()" is contained in "Lighting.hlsl"
    So you have two options here:
    • Alter "lightweightPBRForwardPass.template" to make it calculate lighting using your own custom approach
    • Create your own custom equivalent of "LightWeightPBRSubShader.cs" and make it use another template entirely
    The only thing I can't remember is how "LightWeightPBRSubShader.cs" actually gets registered in ShaderGraph as a node that you can use
     
    jason_yak and MadeFromPolygons like this.
  14. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    Thanks this is seriously useful information!

    I am about to dive into the world of custom shaders using the SRP and will post back any findings I have on the forum and reddit so we can collate all this (diving in and out of different threads and forums to get all this info wasnt the best route me thinks!)
     
  15. andbc

    andbc

    Joined:
    Jan 25, 2014
    Posts:
    18
    Any news on this subject ? I would like to write a custom lightning and I would be very interested in what you have to share
     
  16. RohitWasTaken

    RohitWasTaken

    Joined:
    Jun 3, 2018
    Posts:
    7
    can anyone help me with this error?
     

    Attached Files:

  17. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507

    This is a PBR surface shader I wrote in 8th grade with Unity 5. Hopefully Unity 2018 HDRP will eventually make it easier to customize BDRF because it really gives a chance to give your project it's own feel.
     
  18. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Screen Shot 2016-11-09 at 9.09.18 PM.png Sorry image upload failure
     
    Last edited: Jul 20, 2018
  19. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Is there any chance Shader Graph will support custom lighting configurations?
     
  20. MythrilMan51

    MythrilMan51

    Joined:
    Apr 24, 2017
    Posts:
    146
    If I could get a dime every time someone asks that... Even myself... Who was YELLING about it.
     
  21. Grimreaper358

    Grimreaper358

    Joined:
    Apr 8, 2013
    Posts:
    789
    Mr-Mechanical likes this.
  22. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    https://github.com/Unity-Technologi...mmit/136cf51b2b07ceb9dda73cfd4bbe144021863507
    Thanks, this looks very promising.

    For now, I'll try Shader Forge (with legacy pipeline) to get used to visual shader scripting. I've only used HLSL/GLSL, but from what I've seen visual shader scripting seems like the future and is here to stay.
     
    Last edited: Jul 30, 2018
  23. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Yeah having a clean split between HLSL style code and higher level graphs is the way forward, particularly as optimisation for graphs continues to get better.

    Although to be honest, I doubt you'll remotely be able to match the quality of HDRP lighting, considering the people who did that, invented the formulas you used.

    For stylised stuff I think they're making a node or some such for it.
     
  24. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,967
    I also agree we will never reach that quality, but then again I am always amazed by what this community does with the right tools in their hands!
     
    Mr-Mechanical likes this.
  25. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Sure but it's actually quite obnoxious to assume you'll beat something that actually perceptually matches ground truth raytracing with the correct media applied.

    Stylised art is a different matter :D
     
    NeatWolf likes this.
  26. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    That wasn't the goal, no one said that was. I am experimenting with a shader that supplies an approximation intermediate between HDRP and LWRP shaders but implemented using HDRP (targeted for mobile GPUs).

    Correction: The unity team did not "invent" the formulas I used. The formulas I used for my 2016 shader are Schlick's approximation for fresnel and Blinn-Phong for specular. As for as I know these folks don't work at Unity (though it would be super cool if they did).

    https://en.wikipedia.org/wiki/Schlick's_approximation
    https://en.wikipedia.org/wiki/Blinn–Phong_shading_model
     
    Last edited: Aug 3, 2018
  27. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
    Plenty of amazing looking Unity games use custom lighting models (CSR racing 2). It's not an "obnoxious" request. No render pipeline is perfect. The Scriptable Render pipeline was specifically designed with extensibility in mind for this reason. No reason to berate younger (16 year old) Unity developers for attempting to contribute to the CG community (that's pretty low).
     
    Last edited: Aug 6, 2018
  28. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    No berating intended. Had no idea of your age, but in the other posts you made you did talk a big game about improving things and I was surprised given the staff working on HDRP have an AAA list longer than my arm.

    I apologise for any offence and lets keep the thread on topic ;)
     
  29. PitHeimes

    PitHeimes

    Joined:
    Oct 18, 2015
    Posts:
    322
    Have to say, that it of course is possible to enhance the current set of shaders. See below for some paintable wetness with raindrops. Just an extended Lit Shader. Extension was done by mainly injecting methods in about 3-4 places, so that we can ensure an easy update process while the HDRP Lit Shader evolves. Of course this is far from perfect, but indeed, hell yeah, it can be done once you stick your head under the bonnet and figure out how things are structured with the new HDRP shaders.

     
    Last edited: Aug 13, 2018
  30. TheWanderingBen

    TheWanderingBen

    Joined:
    Nov 3, 2015
    Posts:
    98
    Looking good, PitHeimes! When you say "an extended Lit Shader", did you copy the entire code into a new shader file (replacing all the unfortunate relative path ("../..") includes) or is there a nicer way of extending a shader?
     
  31. PitHeimes

    PitHeimes

    Joined:
    Oct 18, 2015
    Posts:
    322
    You can reference the HDRP files with "/HDRP/" references. But a quick find a replace get's that done in no minute.
    Basically I have a couple of fixed injection points, e.g. after all the surfaceData was done, or right before calculating surfaceData is done, etc.. etc..
     
  32. TheWanderingBen

    TheWanderingBen

    Joined:
    Nov 3, 2015
    Posts:
    98
    Cool! Definitely smart to keep your changes contained in case of updates. So is there a way of writing "use HDRP/Lit pass 1" or is all that code that you'd have to copy over?
     
  33. PitHeimes

    PitHeimes

    Joined:
    Oct 18, 2015
    Posts:
    322
    Yeah that would be all the code to copy over. How the update process works at the moment:

    1. An update is done. Yeah, update HDRP.
    2. Copy Lit.shader and LitData.hlsl [If there have been any updates to those files] [If there only have been updates regarding shadows or lighting or anything I do not need to update anything. Just if specific Lit Shader things changed]
    3. add #include Custom_Lit_PreSurface.hlsl and #include Custom_Lit_PostSurface.hlsl at the correct places in LitData.hlsl
    4. add #include Custom_Lit_Properties.hlsl in Lit.shader to make sure to have my custom properties in place.

    Time to update: About 5 minutes with some copy&paste errors. So we can live with that as long as ShaderGraph keeps its current state.

     
    Last edited: Aug 14, 2018
  34. Mr-Mechanical

    Mr-Mechanical

    Joined:
    May 31, 2015
    Posts:
    507
  35. PitHeimes

    PitHeimes

    Joined:
    Oct 18, 2015
    Posts:
    322
    You currently have to write your own node (as usual) or as I highly recommend dig your head into how the shader works and rip off some basic Lit Shader where you can do anything you want. Geometry Shaders work well with HDRP too.

     
  36. rz_0lento

    rz_0lento

    Joined:
    Oct 8, 2013
    Posts:
    2,361
    Amplify Shader Editor just got HDRP support as well, so there's one additional (paid) option now besides custom code and Shader Graph.
     
  37. HD-MI

    HD-MI

    Joined:
    Mar 9, 2017
    Posts:
    13
    While the above is true, Amplify still can't go around the limitations that the normal Unity Shader Graph has regarding HDRP (such as transparent materials breaking up or camera depth not working). The devs are very active in its development though and are trying I'm sure.

    What PitHeimes has posted in this thread seems to be the way to go if you want anything more advanced. For somebody who doesn't know much HLSL, do you guys reckon it would be doable to do custom stuff with Shader Graph then copy the code it generates and tweak the Lit shader with that?
     
  38. Grimreaper358

    Grimreaper358

    Joined:
    Apr 8, 2013
    Posts:
    789

    It should be the same code, matter of fact you wont even need to tweak the lit shader since you can just directly edit the version generated by Shader Graph. Also keep in mind that Shader Graph dosen't support the Lit Shader yet it just supports a basic PBR and Unlit master nodes, a full Lit Master node with all the options of the default Lit Shader is coming. https://github.com/Unity-Technologies/ScriptableRenderPipeline/tree/sg/lit-master-node
     
  39. Partha_online

    Partha_online

    Joined:
    Aug 24, 2013
    Posts:
    8
    how to sample a shadow map texture in HDRP using a custom shader
     
  40. EthanFischer

    EthanFischer

    Joined:
    Feb 10, 2016
    Posts:
    46
    Is there any support for custom shaders in HDRP now?