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 GrKl · Jul 07, 2014 at 01:01 PM · shaderoutlinediffusevertex color

Shader: Outlined Diffuse with Vertex Color?

hello all,

Before anything, I must admit I have absolutely no knowledge in shader coding.

I currently use the 'Outlined Diffuse 3" shader: http://wiki.unity3d.com/index.php?title=Outlined_Diffuse_3

But I would like to use what seems to be called 'Vertex Color' with this shader: I build a mesh at runtime, and would like to 'paint' each triangles from this mesh with a different color depending on its Y position. I know my code to do so works as I tested it with other basic shaders. But it does not with the 'Oultined Diffuse 3' shader.

Could anyone explain how I could modify this shader to make that work? Or point out another 'Outline Diffuse Shader' that has this 'Vertex Color' possibility?

If you need any more information to help me, I'll gladly answer. Thank you all

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
4
Best Answer

Answer by CHPedersen · Jul 07, 2014 at 01:32 PM

If you go through that shader's code, you'll notice that it never uses the vertex color, as you correctly guessed. A 'vertex color' is a color associated with each vertex. They are the colors you set on the mesh using the Mesh.colors property. But to be visible, the shader has to actually make use of them, typically by passing them onto the pixel shader in the vertex shader.

The shader you linked to has a vertex function here:

 v2f vert(appdata v) {
     // just make a copy of incoming vertex data but scaled according to normal direction
     v2f o;
     o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  
     float3 norm   = mul ((float3x3)UNITY_MATRIX_IT_MV, v.normal);
     float2 offset = TransformViewToProjection(norm.xy);
  
     o.pos.xy += offset * o.pos.z * _Outline;
     o.color = _OutlineColor;
     return o;
 }

This vertex function appears to be the one that renders the (typically black) outline, so it passes the _OutlineColor on to the pixel shader instead of the vertex color. This, I assume, is fine. So let's not mess with that. Instead, let's have a look at the two surface functions. There is one per subshader, and you should modify both. They're both very simple and have totally identical code bodies, which is these three lines of code

 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
 o.Albedo = c.rgb;
 o.Alpha = c.a;

Notice here, that the color it sets is a multiplication of the _Color property (the main color you set on the Material) and a color read from the material's primary texture. If you want vertex colors here, you have to tell those surface shaders to take the vertex colors into account. First, you must change the surface shaders' Input structs so that they include the interpolated vertex colors, like in the following. This:

 struct Input {
     float2 uv_MainTex;
 };

Must become this:

 struct Input {
     float2 uv_MainTex;
     float4 color : COLOR;
 };

Do this for both subshaders. Next, change the surface shaders to use it. How is up to you. For pure vertex colors, something like this should work:

 o.Albedo = IN.color.rgb;
 o.Alpha = IN.color.a;

But if you want it to mix the vertex colors into what it's already doing, you could just add it to the multiplication, like this:

 fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color * IN.color;
 o.Albedo = c.rgb;
 o.Alpha = c.a;

Doing these things should work, but keep in mind I haven't tested it myself. :) Feel free to report back here with whatever compiler errors your modified shader throws, if any.

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 GrKl · Jul 07, 2014 at 07:16 PM 0
Share

I just have one new question now... Where do you live? So that I can come and give you a massive hug!!!

This is simply perfect. Even though I still have very limited understanding on how shaders work, your clear and detailed explanation worked perfectly. And also kind of gave me a hint on how all that works.

Thanks again!

avatar image CHPedersen · Jul 07, 2014 at 07:58 PM 0
Share

You're absolutely welcome. :-) I'm glad to know it helped you out and made sense. If you're interested in learning more in depth about how shaders work, using Cg, NVidia have a very good tutorial book online here:

http://http.developer.nvidia.com/CgTutorial/cg_tutorial_chapter01.html

That's the one I read. :-)

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

22 People are following this question.

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

Related Questions

Silhouette Toon Shader 0 Answers

Parse Error on OutlinedDiffuse Shader 0 Answers

Outlined Shader Shadows 1 Answer

Outlined Specular shader? 0 Answers

Add vertex colors to Volund/Standard Scatter (Specular, Surface) 0 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