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 ArtOfWarfare · Nov 02, 2014 at 09:15 PM · colorcolors

Multiple Colors in Mesh Programmatically?

I can programmatically assign my mesh any one color with code like this:

 renderer.material.color = Color(1.0, 0.5, 1.0)  # Light purple

But I'd like to programmatically assign my mesh multiple colors (per-face/triangles preferred, but per-vertex acceptable).

I saw that Mesh has an attribute called colors, so I tried using it like this (this is nearly identical to the example in the scripting documentation here: http://docs.unity3d.com/ScriptReference/Mesh-colors.html):

 mesh = GetComponent(MeshFilter).mesh
 colors = array(Color, mesh.vertices.Length)
 for i in range(mesh.vertices.Length):
     colors[i] = Color.Lerp(Color.red, Color.green, mesh.vertices[i].z)
 mesh.colors = colors

But my mesh came out a solid white instead of being red, green, or a mix. What am I doing wrong?

Comment
Add comment · Show 3
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 ArtOfWarfare · Nov 02, 2014 at 09:27 PM 0
Share

This looks like it'll be super useful for me learning how to do this: http://docs.unity3d.com/$$anonymous$$anual/ShadersOverview.html

avatar image robertbu · Nov 02, 2014 at 09:33 PM 1
Share

You likely are not using a shader that supports vertex colors. Note sure where you are going with the functionality, but with 3D modeling programs, you can assign different materials to different parts of the mesh. Then inside Unity you can use the materials array to change the color for one set of triangles.

avatar image ArtOfWarfare · Nov 02, 2014 at 10:12 PM 0
Share

$$anonymous$$y mesh is programmatically generated at runtime, so I won't be doing any of this outside Unity (thus there won't be an import step.)

2 Replies

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

Answer by ArtOfWarfare · Nov 03, 2014 at 03:16 AM

After reading up on Shaders for a few hours and seeing a few different examples, I figured out how to modify the diffuse shader to use vertex colors instead of texture colors. Here it is:

 Shader "MyShader" {
 SubShader {
     Tags { "RenderType"="Opaque" }
     LOD 200

 CGPROGRAM
 #pragma surface surf Lambert

 struct Input {
     fixed4 color : COLOR;
 };

 void surf (Input IN, inout SurfaceOutput o) {
     o.Albedo = IN.color;
 }
 ENDCG
 }

 Fallback "VertexLit"
 }

I'm not quite sure what the Tags and LOD do, but the important parts are:

 #pragma surface surf Lambert

Says that I'm going to define a surface shader called surf which I want to have use the Lambert lighting system (that's how my mesh ends up with shadows and stuff rather than looking flat).

Since it's a surface shader, Unity automagically figures out how to pipe data around for me. I just let it know that I need a COLOR as Input to surf - it ends up grabbing the color of the vertex, which is correct. At that point, all I have to do is write the simple one line function surf which just takes the vertex color and applies it to the SurfaceOutput.

If someone else had told me all this hours ago, it would have been pretty helpful for me, I think, so hopefully now that I've written this here, it'll help others in the future.

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
avatar image
1

Answer by Bunny83 · Nov 02, 2014 at 09:41 PM

Most normal shaders don't use the vertex color at all. Only a few specialized shaders like all the particle shaders. Just try different shaders and you will see the vertex colors.

You can easily write your own shader that suits your needs. See this page for more details on how to use vertex colors. It's the 4th example.

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 ArtOfWarfare · Nov 02, 2014 at 10:19 PM 0
Share

Thanks for that, Bunny83. I just began reading up on shaders less than an hour ago so this is still over my head. I copy and pasted that shader code into a new shader, created a new material and applied the shader to it, and then applied the material to my mesh. But now I have the problem that it all renders flat - whereas the diffuse shader interacted with the lightning and shadows, this shader just shows the vertex colors with no respect for lights and shadows. How can I modify it to both utilize the vertex colors as its doing now and to factor in lighting and shadows as it was when it was just using the diffuse shader?

avatar image ArtOfWarfare · Nov 03, 2014 at 03:19 AM 0
Share

Thanks again for your help. I've upvoted your answer since it was helpful in giving me more examples to look at, but it wasn't quite everything I was looking for. I posted another answer (which I've marked as accepted) which more comprehensively covers what I wanted (hopefully it'll help people in the future. Not sure. UnityAnswers seems so broken that I'm a bit skeptical that its search engine would actually ever turn this up, even if it was exactly what someone needed.)

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a Game Accessible for the Blind and Visually Impaired 4 Answers

Random Color 1 Answer

Best way to mix color values? 4 Answers

Set Gameobject a certain color based off of other game objects 1 Answer

RGB Colors as Float Values Converter 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