Specularity in Vertex and Fragment programs

I have a custom shader that I got with the “Lightmap Manager 2”. This shader has no specularity and I need to add it.
I can’t post it here (because it’s a paid plugin) and I’m not used to vertex and fragment programs (I kind of understand the surface shaders).

So, is it possible to add specularity to it? How?

(if you post a vert + frag shader with diffuse + specularity that works with Unity 3.x, it would be great!)

Well, it’s much more complicated as opposed to surface shaders. But of course it’s possible.

I see at least two possible approaches:

  • either try to find other tutorials/examples in the web, there should be plenty available
  • or download Unity’s builtin shaders from before they switched to surface shaders, and try to extract the code you need from there (this might be a bit tricky, since several functions were/are in “UnityCG.cginc”, which got installed with Unity itself, and these functions might have changed since then)

The builtin shaders from Unity 2.6, which still used vert/frag are here, and the relevant shader dealing with specular lighting is called Normal-Glossy.shader.

If you really want to figure it out for yourself, you’ll need to compute and store the light and view directions (in object space) in the vertex shader, and use that info in the fragment shader to compute the resulting light contribution (it’s a power function, using the “shininess” as an exponent, usually between 1-128, and you’ll need the reflected light vector (in respect to the normal), and relate that, depending on your lighting model (Phong/Blinn/Torrance/Sparrow/…), to the view direction). As I said, it’s complicated :wink:

Note that there may be several helpful macros/defines/convenience functions available in said UnityCG.cginc.