Search Unity

Surface or vertex/fragment shader?

Discussion in 'Shaders' started by eneroth3, Nov 12, 2018.

  1. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    I'm trying to learn shaders and don't really get how to work with surface shaders vs vertex/fragment shaders.

    Typically I don't want to calculate lighting and such myself, so a higher level surface shader should be better. However it seems like a lot of what I want to do (or more like everything I'm trying to do) cannot be done in a surface shader.

    One thing I wanted to try is to animate a flag moving in the wind. This is quite easily done by offsetting vertices in the vertex shader, but I don't want to do all the lighting stuff myself. From what I can see offsetting individual vertices it cannot be done in a surface shader though.

    Another thing that I need for my game is a shader that fades between textures depending on the distance to the camera. Again blending textures is super simple in a surface shader, but I need to do it on a vertex level to get the varying camera distance (and interpolate the blending for each fragment). And again I don't want to manually handle lighting.

    Am I missing something here? Can a surface shader have custom code run for each vertex, but fall back to some built in fragment shader doing the lighting? Or do I need to find code handling lighting and manually implement it in a custom vertex/fragment shader? Are there helper functions for adjusting the fragment color for lighting?
     
  2. skilfuldriver

    skilfuldriver

    Joined:
    May 20, 2015
    Posts:
    19
  3. eneroth3

    eneroth3

    Joined:
    Oct 22, 2018
    Posts:
    63
    Hi Matt

    I got it to work late yesterday night but was too tired to update my own thread (I tried to remove it though but couldn't find how to).

    I'm probably just having problems wrapping my head around the surface shader as it is an extra level of abstraction, and not very intuitive what it actually does. I first had the impression it run once on each surface (mesh) based on its name but now understand it's more like a high level fragment shader.

    It took me a long time to realize I could reference _WorldSpaceCameraPos in a surface shader as it wasn't mentioned in the surface shader documentation, while e.g. viewDir is. I also expected information about what functions Unity call, like vert, to be covered in the main documentation and not mentioned in the examples section. In my experience code examples typically gives context to what has already been specified, not introducing new concepts.

    Anyhow, it seems like I've overcome this obstacle now and am moving in the right direction!

    Cheers
     
    skilfuldriver likes this.