Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Knowledge required to write shaders?

Discussion in 'Shaders' started by elmar1028, Feb 29, 2016.

  1. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,319
    Hey guys,

    I am kinda new to the shader territory. The code looks alien to me o_O

    I wonder what initial knowledge do I need to get started writing shaders? Do I need to know any programming language?

    Thanks in advance!
     
  2. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    190
    Hi,

    if you want to write shaders, you should learn how to program. Do you know what loops, sequences and if statements are?
     
  3. elmar1028

    elmar1028

    Joined:
    Nov 21, 2013
    Posts:
    2,319
    I mainly program in C#, I have quite an experience with it, so it's not a problem.

    I want to know what knowledge is good to have for writing shaders.
     
  4. BrightBit

    BrightBit

    Joined:
    Jan 22, 2013
    Posts:
    190
    Okay, I see. You definitely need some linear algebra knowledge with a focus on computer graphics because sooner or later you will have to deal with vectors, dot products, matrix multiplications (for example for perspective projections) and similar things a lot. Basic knowledge about the gpu pipeline might also be helpful.

    Maybe you should start with Unity's Surface Shaders. They hide a lot of math from you, so I would say that they are a bit easier to deal with for a starter.
     
    ericbegue and elmar1028 like this.
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    9,286
    I highly recommend this tutorial for anyone getting started with shaders:
    http://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/

    Unity actually mirrored the first section of it on the official site, but it's worth while going through all 5 parts. It's great for both beginners and people who already programmers but haven't done shaders before. It details the pipeline, and the syntax for ShaderLab and then goes into Surface shaders (both of those are unique to Unity) before getting into vertex & fragment shaders which is what Surface shaders actually generate.
     
    elmar1028 and BrightBit like this.
  6. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    As BrightBit said, vector math is the main thing (dot products, matrices).

    You do also need to have some understanding of what's costly and what's efficient. The specifics can vary by platform, hardware, etc. but by and large expensive stuff is expensive across the board.

    Some understanding of colourspace tends to be required these days, too.

    Learning proper mathematical notation is also handy if you want to read whitepapers, as they're very rarely given in shader code.

    It's also handy to figure out what the different functions are in the different languages. CG (what most Unity shaders are written in) is mostly pretty close to HLSL (language used by DirectX) but there's a fair few differences when compared to GLSL (language used by OpenGL). It's not crucial, as Unity compiles CG to GLSL/HLSL as required, but it can help you port shader code found elsewhere to CG for use in Unity :)
     
unityunity