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.

First off: Congrats on learning shaders! Once you wrap your head around them you’ll start having a ton of fun with various techniques :slight_smile:

  1. 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.

  2. 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”.

  3. 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.

  1. 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.
  2. 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).
  3. sv_position vs position. nothing Unity specific.

Have you forgotten adding the fragment code?

half4 frag (v2f i) : COLOR
{
    return half4 (i.color, 1);
}