Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Johan 4 · May 29, 2011 at 10:53 AM · colorpixelgetpixel

Getting pixel color on ray hit

What I need is to get the texture's pixel color where the ray hit. I need it so I can check if color = blabla, then do something. Seeing as rays hit colliders and not meshes, I don't know to get pixel color from a mesh texture. I know there is Texture2D.GetPixel, but I don't know how to get it from 3d into 2d and to the right pixel.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Eric5h5 · May 29, 2011 at 03:44 PM

Use RaycastHit.textureCoord. Note that it only works with mesh colliders.

Comment
Add comment · Show 3 · 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 Johan 4 · May 29, 2011 at 05:10 PM 0
Share

But I need it to work on meshes that don't use mesh colliders.

avatar image Eric5h5 · May 29, 2011 at 05:33 PM 0
Share

Well, it doesn't. ;) There's some way to do it by writing your own routines no doubt, but I have no idea what the math would be.

avatar image Eric5h5 · May 29, 2011 at 05:36 PM 0
Share

Although, what might work is to convert the RaycastHit.point to screen space using WorldToScreenPoint, and then use ReadPixels on that point. It wouldn't work if the hit.point is behind some object, but depending on what you're doing, it might be O$$anonymous$$.

avatar image
0

Answer by Statement · May 29, 2011 at 11:30 AM

Manually reverted to 1st revision:


I am unaware of any such built in functionality, though I wouldn't be surprised if anyone made and shared a similar tool already. I'd check google if you haven't. But basically you'd need to perform a test vs every triangle to get the barycentric coordinates of intersection between the triangle and the ray. Then, you can interpolate the triangle UVs using the barycentric coordinates to get the final UV coordinate for your hit test.

When you have the UVs, you can rescale the UVs to texture pixel space. That is, the UVs are usually in the 0 to 1 range where textures are like 0 to 255 (256 pixels). Note that some models may repeat the texture so it's possible to get an UV outside of the range 0 to 1, like -1 or 2.34 or -502.2.

That said, I wouldn't be surprised if there is any built in, premade or easier solution. This is just what came to my mind.

Comment
Add comment · Show 7 · 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 Statement · May 29, 2011 at 11:43 AM 0
Share

This requires the collider to be a $$anonymous$$eshCollider. For another explanation about getting the UV from an arbitrary mesh, see my revision history as I first explained the "long way" around and then realized you can probably just do with the RaycastHit.

avatar image Johan 4 · May 29, 2011 at 12:02 PM 0
Share

Revision history? Where do I see that?

avatar image Statement · May 29, 2011 at 12:25 PM 0
Share

You should see "Edited xx $$anonymous$$utes ago". Press that and you should see the other revisions.

avatar image Johan 4 · May 29, 2011 at 01:15 PM 0
Share

Uh, I don't see anything like that.

avatar image aldonaletto · May 29, 2011 at 01:22 PM 0
Share

$$anonymous$$e neither. This new Unity Answers is still very buggy.

Show more comments

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

How to pixelate an image texture for faster pixel color recognition? 2 Answers

Get Average Colour From Texture? 1 Answer

Add "burn marks" on hit directly to a texture using pixel coordinates 0 Answers

Using GetPixel to instantiate blocks of different pixel colors 1 Answer

Material doesn't have a color property '_Color' 4 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