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:


(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);


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.


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.

more ▼

asked Dec 12 '11 at 05:33 PM

KHex gravatar image

31 14 15 15

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

  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.

more ▼

answered Dec 12 '11 at 05:43 PM

Statement gravatar image

25.3k 41 78 197

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?

Dec 12 '11 at 06:09 PM KHex
(comments are locked)
10|3000 characters needed characters left
  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.
more ▼

answered Dec 12 '11 at 05:45 PM

Bunny83 gravatar image

67.2k 22 69 269

I edited above to expand on #1. Could you explain why that isn't allowed or won't work?

Dec 12 '11 at 06:07 PM KHex

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.

Dec 12 '11 at 06:56 PM Bunny83
(comments are locked)
10|3000 characters needed characters left

Have you forgotten adding the fragment code?

half4 frag (v2f i) : COLOR
    return half4 (i.color, 1);
more ▼

answered Mar 09 '13 at 08:55 AM

Yanger_xy gravatar image

310 23 27 38

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Dec 12 '11 at 05:33 PM

Seen: 4483 times

Last Updated: Mar 09 '13 at 08:55 AM