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 ronronmx · Nov 27, 2011 at 09:25 AM · shadervertexcolor

How to get Vertex Color in a cg shader?

I can't figure this one out...I need to read the vertex colors of the mesh and multiply it with the diffuse color (or use them as masks).

In ShaderLab this is done by adding the following lines: ColorMaterial AmbientAndDiffuse Lighting Off

I tried adding these to my cg shader but it doesn't return the vertex colors, so I'm sure I have to do it in the vertex shader but I don't know how.

Do i need to use a second pass for that? Thanks!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by CHPedersen · Nov 28, 2011 at 02:40 PM

Vertex Colors behave a little weird in Unity's CG code. If you were using Cg from OpenGL, for example, it would be enough to just mark the input variable with the : COLOR semantic. That's supposed to tell the Cg compiler that you want it to store the vertex color in that variable. But in Unity, you don't have the same control over what data is passed to the shader, because that kind of stuff is wrapped inside the Mesh class. I was tearing out my hair over this for half a day a couple of months ago, until I read this site closely:

http://unity3d.com/support/documentation/Components/SL-VertexProgramInputs.html

Look at the text right above where it describes the 6 vertex attributes that you can pass. Notice that it says "If you want to access different vertex data, you have to declare vertex structure yourself. The structure members must be from the following list:"

As I said, it took me half a day to realize that when they write "must be", they mean business, and it really does mean MUST BE. So, to get the vertex colors, you HAVE to declare a struct for the input value, and the variable MUST be called "color". Here's an example Cg vertex program that correctly grabs the vertex colors in a way Unity accepts (it's just a vertex/pixel shader pass-through):

Shader "Custom/ExampleVertexColorShader" { Properties { } SubShader { Tags { "RenderType"="Opaque"} pass { CGPROGRAM #pragma vertex wfiVertCol #pragma fragment passThrough #include "UnityCG.cginc"

         struct VertOut
         {
             float4 position : POSITION;
             float4 color : COLOR;
         };

         struct VertIn
         {
             float4 vertex : POSITION;
             float4 color : COLOR;
         };

         VertOut wfiVertCol(VertIn input, float3 normal : NORMAL)
         {
             VertOut output;
             output.position = mul(UNITY_MATRIX_MVP,input.vertex);
             output.color = input.color;
             return output;
         }

         struct FragOut
         {
             float4 color : COLOR;
         };

         FragOut passThrough(float4 color : COLOR)
         {
             FragOut output;
             output.color = color;
             return output;
         }
         ENDCG

     }
 }
 FallBack "Diffuse"

}

Again, Unity is really totally anal about this: The variable MUST be called "color", and it MUST be defined in a struct, or it won't store the vertex color in it.

Comment
Add comment · Show 4 · 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 ronronmx · Nov 28, 2011 at 08:03 PM 0
Share

Thanks for your answer Christian! It was very helpful. I actually figured it out last night and was able to get vertex colors to appear, but I didn't know that the "color" variable HAD to be defined in a struct...you saved me hours of scratching my head for future shaders lol

$$anonymous$$aybe you can help me out with another question I have about shaders, take a look here: http://answers.unity3d.com/questions/189574/shade4pointlights-and-shadevertexlights-custom-sha.html

Thanks again!

avatar image CHPedersen · Nov 28, 2011 at 08:55 PM 0
Share

You're very welcome. :) Perhaps I might persuade you to mark the answer as accepted, then? I'll take a look at the other question in the meantime.

avatar image ronronmx · Nov 30, 2011 at 10:14 PM 0
Share

Crap sorry I completely forgot to mark it as accepted, it's done now :) Thanks again!

avatar image Nidick · Sep 18, 2013 at 02:12 PM 0
Share

Hi Folks, how can i put a difuse texture on this shader? its possible ?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Material doesn't have a color property '_Color' 4 Answers

Creating models with color only, from script 1 Answer

[Unity Free]Add textures to existing Unity DOF Shader 0 Answers

Best Practice For Shader Complexity + IOS 1 Answer


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