Search Unity

How do you learn to write shaders with Unity ?

Discussion in 'Shaders' started by anthonov, Nov 7, 2021.

  1. anthonov

    anthonov

    Joined:
    Sep 24, 2015
    Posts:
    160
    I have some good experience with C# and now Im trying to learn how to write shaders with Unity but each step is just incredibly difficult for me and I feel like I do something wrong but I don't know why.

    The obstacles are :

    -The editor that come with Unity (Visual Studio community) is just NOT READY to write shaders for Unity. HLSL files are interpreted as miscellaneous files, without any kind of help for code edition. No predictions, no auto completions, no auto indent, no formating, no errors tracking, no debug tools, -just nothing-.
    You blindnessly write things and you hope it works. Of cours since you are learning it never works and you don't know why. Am I alone in this situation ?

    - Ressources from Unity themself are messy to my taste.
    I know Unity is not a school but they really lack of pedagogy. The best explanations/tutorials are not from them. They are from youtubers that takes time to share knowledge (if you take time to search for them). They are also not schools. But the Unity's ressources are just too much already specialised, like they are for people that already know these stuff. It's just hard to read them and kinda difficult to find a start point or having a global view of what is going on.

    So is there some of you that already went into this ? How do you managed to get paste this wall ?
    Is this Unity side really "new user ready" ?
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,716
    I don't know about alone, but after writing shaders for a while, I find IDE "niceties" to actually be hindrances. Auto completion is only nice for API exploration and... that's about it.

    About formatting, there are plugins for VSCode and Sublime Text to have some syntax highlighting and I feel like that's enough for me:

    upload_2021-11-7_15-52-28.png

    I found this to be pretty useful : https://developer.download.nvidia.com/cg/index_stdlib.html

    Other than that, the best thing to do is find some very simple unlit shaders (so they have no "unity magic" and it's clearer what you're doing) and start tweaking things. After getting some basics down I feel like you can make the jump to surface shaders much more easily.

    (I'm only suggesting starting with Unlit shaders, since when I first started learning writing shaders in Unity, the auto magical parts of surface shaders made things more confusing for me).

    For the rest, random googling and asking on the forums helps as well.

    NOTE: The above advice is intended for writing either vert / frag or surface shaders for the built-in pipeline. IMO URP and HDRP are very difficult to write shaders for, and totally unsuitable for beginners to learn to write shaders, at least currently.
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    One but if warning about using the Cg documentation for writing shaders in Unity.

    Unity does not use Cg, it uses HLSL. Nvidia stopped developing Cg over a decade ago, and Unity removed any use of it from the engine a few years after that. 99% of the time this fact doesn’t matter since Cg and HLSL use identical syntax and have nearly identical functionality. But the problem is they are nearly identical, but not actually identical. So you can come across situations where code that you write that follows Cg’s documentation will not compile.

    It’s better to use the actual HLSL documentation.
    https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-intrinsic-functions

    However the Cg documentation is still valuable because it often talks a bit more about how or what certain functions do, event showing example code that closely matches how those functions are internally implemented even today.
     
    AcidArrow likes this.