- Home /
Converting Fixed Function Shader to Surface Shader
Hi everyone,
I am trying to convert a custom fixed function shader to a surface shader to get realtime shadows working but I am stuck on one command, which is
SetTexture [_MainTex]
{
Combine previous * primary DOUBLE
}
The previous part is
fixed4 c = tex * _Color;
but I am not sure what primary and DOUBLE equate to. I hope someone can help.
Cheers, Oliver
Answer by Kryptos · Jan 18, 2012 at 08:37 AM
RTFM.
ShaderLab syntax references: http://unity3d.com/support/documentation/Components/SL-Shader.html
Primary and DOUBLE are related to texturing: http://unity3d.com/support/documentation/Components/SL-SetTexture.html
Oh come on. Give me some credit.
Of course I read the manual, but
Primary is the color from the lighting calculation or the vertex color if it is bound.
Does not help me much finding equivalent functionality for surface shaders. And yes, I have searched the forums, the documents and the answers section.
Also
The formulas specified above can optionally be followed by the keywords Double or Quad to make the resulting color 2x or 4x as bright.
I tried
c = c * 2; // too bright
c = c * c; // too dark
c = c * c * 2; // too red
what does 2x as bright mean. Its not very easy to put that into code for me.
I even opened the compiled shader to search for the statement, but even in compiled form it is still
Combine previous * primary DOUBLE
Sorry I did not mean to insult you.
Getting realtime shadows to work have nothing to do with the use of surface shader ins$$anonymous$$d of fixed function. Realtime shadows work perfectly with fixed function.
$$anonymous$$ake sure that your shader is rendering in the opaque queue.
Tags { "RenderType"="Opaque" }
See: http://unity3d.com/support/documentation/$$anonymous$$anual/Shadows.html and http://unity3d.com/support/documentation/$$anonymous$$anual/Shadow%20Troubleshooting.html
No worries :)
You are right, I forgot to specify, casting shadows does work, but receiving shadows was the problem. They would just not be drawn.
Anyways, I now use the surface shader below and receiving shadows works fine now. Thank you anyways for your help.
Oh, and for anyone wondering. DOUBLE does mean c = c * 2; $$anonymous$$y 'c' was just too bright to begin with.
Answer by Oliver Eberlei · Jan 18, 2012 at 02:07 PM
I have been playing around with the surface shader to make the lightning calculations look the similar to the ones produced by the fixed function shader. This is the surface shader I ended up with, it looks hacked, but at least in the game it looks the same and receiving shadows works properly now.
I used the Self-Illumination/Diffuse Shader as a starting point.
void surf (Input IN, inout SurfaceOutput o) { fixed4 tex = tex2D(_MainTex, IN.uv_MainTex); fixed4 c = tex * _Color * 0.5; //Why 0.5? I tried to match the lighting //on a white sphere first and this worked //but it was just a guessed number o.Albedo = c.rgb; o.Emission = c.rgb * tex2D(_Illum, IN.uv_Illum).a;
o.Albedo = o.Albedo * 0.5865; //These two values are magic numbers I just
o.Emission = o.Emission * 1.173; //found through trial and error
o.Albedo = o.Albedo * 2; //This is the DOUBLE part
o.Emission = o.Emission * 2;
o.Alpha = c.a;
}