• 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 Captain_Dando · Jan 15, 2014 at 11:41 AM · materialtexure

Getting the xy coordinates of a clicked pixel on a 3d model

Hi everyone, is it possible to get the x/y coordinates of a pixel on a model from where you click on it? for example, let's say I have a low-rez texture on a flat plane, generated through code, measuring perhaps 16x16 pixels, and I click on the 3rd pixel from the left and the 10th pixel from the top. is there a way for unity to know those coordinates, so that I can use them, for example, to appear in the console?

Comment
Add comment · Show 10
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 Jessy · Jan 15, 2014 at 12:52 PM 0
Share

The easy answer is yes. What Cartesian space do you need them in?

avatar image Captain_Dando · Jan 15, 2014 at 01:12 PM 0
Share

I don't really need them as world or object coordinates, but rather their positions in the texture itself. I've created a 2d array, which is as wide as the texture and as high. when you click on a pixel, the corresponding entry in the array is selected. so for example, I'd like to be able to click on the fourth pixel to the right, one down, and be shown the entry in the array at [3,0] in the console.

avatar image nesis · Jan 15, 2014 at 01:23 PM 1
Share

Seems like you want to use RaycastHit.textureCoord.

To get the UV coordinate of the part of a model clicked under the cursor, do this in C#:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 Vector2 clickedUvCoords; //will be set to where the user clicked on the model, in UV (texture) coordinates
 bool modelWasClicked = false; //use this to determine whether the model was clicked
 Collider someCollider; //could just be this GameObject's collider, assign whatever you need
 if (someCollider.Raycast(ray,hit,$$anonymous$$athf.Infinity)) {
     clickedUvCoords = hit.textureCoord;
     modelWasClicked = true;
 }
avatar image Captain_Dando · Jan 15, 2014 at 01:38 PM 0
Share

Thanks, sorry if this is a stupid question, but I put this all into update yeah?

avatar image Jessy · Jan 15, 2014 at 02:17 PM 0
Share

RaycastHit.textureCoord is good, but I can't recommend "modelWasClicked".

Show more comments

1 Reply

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

Answer by nesis · Jan 15, 2014 at 03:02 PM

In Update() would work. You'll want to check if the mouse has been clicked with, say, Input.GetMouseButtonDown(0).

modelWasClicked was added under the assumption that Captain_Dando might end up processing the clicked coords after the if statement. Jessy's right in saying it's a bad idea to use that, you're better off putting whatever click-handling code inside that if statement:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 Vector2 clickedUvCoords; //will be set to where the user clicked on the model, in UV (texture) coordinates
 Collider someCollider; //could just be this GameObject's collider, assign whatever you need
 if (someCollider.Raycast(ray,hit,Mathf.Infinity)) {
     clickedUvCoords = hit.textureCoord;
     //your click-handling code here
 }
 //don't do anything after here that assumes the model's been clicked, unless you have a "return;" at the end of the above if statement, or wrap it in an "else {}"
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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

Material not loading 0 Answers

Skinned Mesh Renderer - Adding/Removing Materials via C# 0 Answers

how to change material color for player customization. 2 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