• 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
0
Question by shrutiturner · Apr 05, 2018 at 11:02 AM · colorverticesvertexcolor32

How can I assign one color to one vertex?

I have written a method that allows me to click on the object and print out the vertex number nearest to the mouse click. I now want to assign that vertex a color so that I can then do multiple clicks and establish where those vertices are.

I'm not sure how to go about assigning the color to the individual vertex. Having looked at many examples, the color is always being applied to a range of values, which means that you're mainly dealing with array types, but I can't seem to translate this over to just one individual vertex.

So far I have written the following primitive code, which I know doesn't work but I'm not sure how to go about it.

  int a = mesh.vertices[mesh.triangles[3 * triangleIndex + 0]]; //vertex number
     
  Color32 black = new Color32(0, 0, 0, 1); //type color32
  //here is where I want to assign black to the vertex number held in 'a'
                
   mesh.colors32 = black;

Do I need to create a shader to do this? I've never made a shader before so not sure if this is how I should go about it? Ultimately, I won't be using these colors, I just need to use them so that visually I can see which vertices I have selected as I need them going forward in my project.

Comment
Add comment · Show 2
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 Cherno · Apr 05, 2018 at 11:20 AM 0
Share

The $$anonymous$$esh class has an array of type Color. each color element in this collection corresponds to the vertex at the same index in the vertices array. The color array is optional, however, so if no colors are assigned when the mesh is created then it's likely to be empty. So, to assign a color to a certain vertex, you would do something like

 Color32[] colors = mesh.colors32;
 colors[0] = new Color32(0,0,0,255);
 mesh.colors32 = colors;

Of course, you also need a shader that supports vertex colors. I believe there is a version of the Standard Shader available that included vertex colors.

avatar image Remy_Unity Cherno · Apr 05, 2018 at 11:37 AM 1
Share

Pay attention that is your example :

 int a = mesh.vertices[mesh.triangles[3 * triangleIndex + 0]]; //vertex number

mesh.vertices is a Vector3 array. This can't be an int.

A user on the forum posted a Standard Shader that uses vertex colors, so you don't have to do it by yourself : https://forum.unity.com/threads/standard-shader-with-vertex-colors.316529/

Also, ins$$anonymous$$d of fully modifying the mesh, you can use $$anonymous$$eshRenderer.additionalVertexStream to only change the vertex color values : https://docs.unity3d.com/ScriptReference/$$anonymous$$eshRenderer-additionalVertexStreams.html

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by bschla · Apr 05, 2018 at 11:55 AM

mesh.colors32 (and mesh.colors) is an array with same entries as mesh.vertices array, so mesh.color[32] would describe the color of mesh.vertices[32]

and yes, Unity' standard shader does not support vertex colors, but you can tweak the standard shader a bit, to see the vertex colors or use particle shader (just to see colors)

maybe this will help: https://docs.unity3d.com/ScriptReference/Mesh-colors.html

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

78 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 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 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

Shader to change Vertex Colors 0 Answers

how to change color in unity scripting overtime?,how to change color gradually with C# 2 Answers

How do you make a submesh properly? 0 Answers

How do I get the color of a certain mesh vertex? 0 Answers

Vertex colour transparency and Beast lightmapping? 3 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