Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by KHex · Dec 12, 2011 at 05:33 PM · shaderscg

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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 :)

  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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KHex · Dec 12, 2011 at 06:09 PM 0
Share

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?

avatar image
0

Answer by Bunny83 · Dec 12, 2011 at 05:45 PM

  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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KHex · Dec 12, 2011 at 06:07 PM 0
Share

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

avatar image Bunny83 · Dec 12, 2011 at 06:56 PM 0
Share

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.

avatar image
0

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);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spherical Hamonics - ShadeSH9 and Surface Shader issue 0 Answers

Using Vertex Color increases Draw Call 0 Answers

How to get camera location in object space? 1 Answer

Shader Help? Need an Alpha Test, then a "set pixel" sort of command 1 Answer

Shader Variable Types 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges