difference between surface shaders and vertex shaders

Hi all.
I had read the documentation of unity’s shader lab , but I haven’t understand the difference between the surface and vertex shaders.

Why Angrybot ( sample project ) has used all it’s main shaders as vertex shader ?

Why unity3d has used all it’s main built-in shaders as surface shader ?

For example , The Phong shader needs the to get light position and calculate the reflection vector and then it needs to calculate the Value of specular and diffuse contributions. So should I Implement that by vertex shaders or surface shaders ?

For example to get a light position , I think in a vertex shader , I should use _WorldSpaceLightPos0 to get the position of first light.

But if surface shaders are light interdependent, So ,by according to that , there is no need to get light positon within a surface Shader ?

Am I correct ?

Thanks in advance…

Vertex shaders calculate things at a per-vertex level.
Fragment shaders calculate things at a per-pixel level, often using information that’s been calculated and passed on to it by the vertex shader.

All shaders are written using a vertex shader, most of them have fragment shaders, too.

Surface shaders end up as the same thing, eventually. It’s just that Unity automates a lot of the repetitive and lengthy code and functions to handle lighting, shadows, lightmaps, etc… all of this stuff would be a colossal pain to write out every time you wanted to make a simple shader, so the folks at Unity wrote a system to do it for us. It still gets the light position exactly as you would have to in a shader you wrote yourself.

It does end up containing a lot of “overhead”; stuff you might not actually ever need. On desktops, that’s not such a big deal - they can handle it, but on mobile it can make a huge difference in framerate. It’s often more efficient to write a vert frag shader that only does exactly what you need it to and no more.

Unity also exposes a lot of helper functions for when you’re writing your own shaders, so to get the light direction, you’d simply do float3 lightDir = ObjectSpaceLightDir(v.vertex); in the vertex shader. View direction would be float3 viewDir = ObjectSpaceViewDir(v.vertex); and so on…

3 Likes

thanks