- Home /
unity shaders: what is surf?
hello, to my understanding a CG shader must always contain a vertex program and a fragment program.
but when looking at unity shaders i sometimes see
#pragma surface surf Lambert
and then the implementation of this surf function
void surf (Input IN, inout SurfaceOutput o) { ... }
what exactly is this surface function as opposed to normal shaders?
thanks!
Answer by xCRKx TyPHooN · Apr 05, 2011 at 05:56 PM
To my understanding, surface shaders are the top most layer of a shader. The vertex and fragment programs still exist in the lower levels, its just handled for you when you use the syntax. Ultimately, this allows you to design and write your own shaders quickly without the need to write the necessary sub-code. On the flip side, you lose control and flexibility. For a complex shader its probably best to write the shader from scratch using the vertex/fragment functionality.
Here is a good link for surface shaders
Answer by Owen-Reynolds · Apr 05, 2011 at 05:21 PM
Not exactly an answer, but you are allowed to write just a vert or fragment program by itself.
Without any shader code, the graphics card runs the "built-in" vertex and fragment shaders. They do about what you think -- the default VS puts the vert where the camera should see it, passes along one set of UV coords, and the normal. The default FragS applies one texture with diffuse lighting.
There are piles of switches you can set, like turning on specular, setting specular color, setting an extra texture (with a required setting for how they combine.) ShaderLab code is an easyish way to set those switches. Of course, the settings don't affect your shaders. They are for the default shaders only, which your own Vert or Frag shaders replace.
DISCLAIMER: I've used OpenGL to do the stuff below, but not ShaderLab
Suppose you just want to wiggle the verts around. You write your vert shader, and then request "the built-in shader with settings for Lambert lighting" (I believe Lambert was the first to write a diffuse only, no specular yet.)
Suppose you just want to wavy-blend 6 textures on a sin wave. You write only the fragment shader and say "set up the default VS that sends me 6 UVs."
In theory, using a built-in might run faster. Using a built-in with 20 settings to accomplish something you could do in two lines of shader code might run faster or slower. An Andriod or Iphone might prefer built-in shaders more or less than a PC would.
Your answer
Follow this Question
Related Questions
Extruded Surface Shader + Inner Fill Color 0 Answers
Can vertex colours be SET in a surface shader? Or must it be done through code? 1 Answer
Shader add vertex displacement 0 Answers
How can I get shadows in a transparent Surface Shader? 0 Answers
Why does the 3D Text shader show specular at certain angles? 2 Answers