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
0
Question by 7toni7 · Oct 15, 2014 at 07:57 PM · meshvertex colormesh color

How to get the colors of the vertices in a mesh? [or average color of a triangle]

Hi all,

I'm trying to obtain the color of the vertices in a mesh once a directional light makes its action on the mesh. However all the values for all the vertices that I'm obtaining are exactly the same: RGBA(255, 255, 255, 255), and this is not correct since my mesh is grey and it has parts of shadows.

The code is:

 Mesh _mesh_runtime = _67P_3Dobject.GetComponent<MeshFilter>().mesh;
 Color32[] vertex_colors = _mesh_runtime.colors32;
     
 Debug.Log(vertex_colors[index_of_vertex].ToString());


My mesh has a default diffuse shader. Do you know what is happening? Cheers and thank you!

Comment
Add comment · Show 5
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 robertbu · Oct 15, 2014 at 08:00 PM 1
Share

The vertex colors do not change based on light. The are used by a shader as one input in deter$$anonymous$$ing the color of the pixels it renders.

avatar image 7toni7 · Oct 15, 2014 at 08:13 PM 0
Share

Thanks robertbu. $$anonymous$$y aim is to obtain the color of each of the triangles in the following image, so I thought that a good way was to obtain the color of the vertices in each triangle and make the average.

If not, How could I do this? Should I use another approach?

alt text

2.png (490.7 kB)
avatar image robertbu · Oct 15, 2014 at 09:03 PM 0
Share

I don't have an in-depth knowledge of the area of Unity that you need to solve this problem. I'd be exa$$anonymous$$ing lightmapping. If you can get access to the baked textures, you can use the uv coordinates associates with the vertices to get the color. But that is only a untested idea on my part.

avatar image 7toni7 · Oct 15, 2014 at 09:48 PM 0
Share

Thank you all but I don't think this is a good solution, since I'm not using camera at all

I mean, my scene has a GameObject with a mesh, and a directional light. Then, I rotate the gameobject to see how the light acts on each of the triangles of the mesh.

$$anonymous$$y aim is to know in each moment which is the average color of each of the triangles, and thus set a threshold (in terms of darkness) to know if a particular triangle is occluded or not. If the average color of the triangle is darker than the threshold, the triangle is occluded, if not it is illu$$anonymous$$ated.

Do you know any other way to know the color of a triangle? Thanks!

avatar image 7toni7 · Oct 16, 2014 at 06:17 AM 0
Share

Hi JohnyHilly, thank you very much for your great answer! It can be very useful, anyway, what should I do if I need to compute the brightness of the triangle, rather than just facing?

I mean, you've talked about brightness dropoff, but, how Can I do this?

Thank you very much!

2 Replies

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

Answer by JonnyHilly · Oct 15, 2014 at 08:55 PM

if you have unity pro... render your camera to a render texture, then you can sample any of the pixel's rgb color that you want from the render texture. You'll just need to convert your screen space co-ordinate, into a texture space co-ordinate.

alternatively... use vectors. compute facing angle or brightness yourself. vec1 = polygon . normal

vec2 = light.position - poly.position //for a spot or point light

or

vec2 = -light.direction //for a directional

float dp = Vector3.dot(vec1, vec2);

if(dp>0) //threshold check

 visible = true;

else

visible = false;

//1=facing (bright) 0 = 90 degrees(dark) -1 = facing opposite direction (dark)

//if you need to go further and use brightness rather than just facing... you can dp to approximate lighting brightness, if you want a threshold.

Comment
Add comment · Show 6 · 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 Eric5h5 · Oct 15, 2014 at 09:04 PM 1
Share

You don't need rendertextures, just read the screen pixels, however either way that only works if the vertex in question is actually visible on-camera so it's not really a feasible solution in most cases.

avatar image JonnyHilly · Oct 16, 2014 at 12:07 AM 0
Share

you point the render texture camera along the same vector as whatever you need... the raycast, or the regular screen camera. screen pixels don't always work (might be off camera or behind)

avatar image JonnyHilly · Oct 16, 2014 at 12:10 AM 0
Share

another thing you could do is do a raycast to the triange... get the surface normal. Then do your own dot product lighting calculation, vs the light source direction, to compute brightness on your own. Depending on how accurate you want it... This can be a simple front face/backface check against the light vector (from light to the polygon) or apply a dot product to a cos() brightness dropoff to deter$$anonymous$$e surface brightness. (see answer above)

avatar image 7toni7 · Oct 16, 2014 at 10:12 AM 0
Share

Hi JohnyHilly, thank you very much for your great answer! It can be very useful, anyway, what should I do if I need to compute the brightness of the triangle, rather than just facing?

I mean, you've talked about brightness dropoff, but, how Can I do this?

Thank you very much!

avatar image 7toni7 · Oct 16, 2014 at 10:12 AM 0
Share

How could I compute the brightness/color of a particular triangle due to the influence of a directional light? I need like a kind of occlusion value which indicates how illu$$anonymous$$ated or occluded is a triangle. Is this possible?

I've done the cross product recommended by JohnyHilly but it doesn't work for triangles which are occluded by other triangles...I mean, it only works for convex shapes...

Show more comments
avatar image
0

Answer by 7toni7 · Oct 17, 2014 at 06:43 PM

Easy, I've solved the problem placing the main camera on the normal vector of each facet of the mesh, and making the camera point to that facet and rendering the color of that facet (using a very low value of the field of view of the camera to constraint the camera pointing). Consequently, I can obtain a rendered image of each facet in the mesh, thus allowing me to compute the final brightness of each one ;).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AddAdditionalVertexStreams and static meshes 0 Answers

How to use Vertex Colors 0 Answers

Changes to Mesh Vertex Colors Reset on Build 1 Answer

Vertex colors are being stripped after build 1 Answer

Best practice to change vertex color on a large amount of quads? 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