- Home /
About Cg shaders and parameters
Learning shaders at the moment and mainly only one thing at the beginning that confuses me. I am basing everything I ask now just on what's described at:
http://unity3d.com/support/documentation/Manual/ShaderTut2.html
(1.) If I set o.pos just to v.vertex, the model disappears (can see the wireframe is I highlight in properties). Why is this if everything is at default anyways?
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
vs
o.pos = v.vertex;
(2.) Probably related to the first but if I change the vertex signature to just take in a COLOR parameter with no mention of the position, the model won't disappear but will instead revert back to a pink color until I add in a position. I thought whatever you ignore is just sent out as it is, default.
(3.) In the context of Cg and Unity, can anyone just tell me the difference between SV_POSITION and POSITION? Changing it around but not seeing any change.
Edit:
In terms of #1, here's a better example of my confusion.
appdata_full vert (appdata_full v)
{
v.vertex = mul (UNITY_MATRIX_MVP, v.vertex);
v.color = v.normal * 0.5 + 0.5;return v;
}
Why wouldn't the above work? It doesn't The POSITION semantic is being sent in and being returned as output along with the color. I judge that it's not working as the color isn't changing.
Answer by Statement · Dec 12, 2011 at 05:43 PM
First off: Congrats on learning shaders! Once you wrap your head around them you'll start having a ton of fun with various techniques :)
Because the vertex shader allow you to do other tricks, like animations or transforms of various kinds. It's built for flexibility, but also require you to write (or copypaste) the usual boilerplate code for all shaders that don't do anything out of the ordinary.
I don't know what you're up to here, but a pink model usually means "there is something wrong with your shader, we made it pink so you notice it".
According to this thread at gamedev.net:
Semantics with the SV prefix are "system value" semantics. This means that they have a specific meaning to the pipeline. In the case of SV_Position, if it's attached to a vertex shader output that means that the output will contain he final transformed vertex position used for rasterization.
Ok so in terms of #1 there are things which are always expected as output. However, I edited to expand on #1 above. The appdata_full is being sent in, wired to the right semantics and then output again. Still see the magenta color unless I am making a new struct to send as output. Why wouldn't this work?
Answer by Bunny83 · Dec 12, 2011 at 05:45 PM
Your vertex shader have to transform the incoming local-space vertex position to a screen-space position. This is done by multiplying the local position with the objects world matrix (also called model-matrix [M]) to convert it into world space. After that you multiply with the inverse-Camera matrix [V] to have the position in view-space. And finally with the most important matrix the projection matrix [P] to actually project the 3D coordinate onto your 2D screen. Those 3 matrices are pre combined by Unity into the MVP matrix which do all those transformations at once.
A vertex definition without a position isn't possible. That means your shader can't be compiled properly and Unity uses it's default replacement shader (the nice solid pink).
sv_position vs position. nothing Unity specific.
I edited above to expand on #1. Could you explain why that isn't allowed or won't work?
Well, i don't have time to test it at the moment, but i guess this line won't work:
v.color = v.normal * 0.5 + 0.5;
color is a 4 component vector (r,g,b,a). multiplying with 0.5 should work according vector math, but adding a single float value (0.5) to an vector usually won't work but i'm not sure. I'm not that familiar with cg shaders, i don't use them that frequently ;).
Btw. If your shader contains errors they should be pointed out in the console inside Unity... Shader error messages aren't very useful in most cases, but at least you know there's something wrong.
Answer by Yanger_xy · Mar 09, 2013 at 08:55 AM
Have you forgotten adding the fragment code?
half4 frag (v2f i) : COLOR
{
return half4 (i.color, 1);
}