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 robertmathew · Mar 15, 2011 at 11:31 AM · texturepixelget

get pixel from a texture

How would i can get color from a texture using get.pixel function and apply to a cube

var window1 : Rect = Rect(10,20,300,300); var color1 : Texture2D; var activeColor : Color; var size = Vector3 (100, 10, 100);

  function OnGUI()
  {
  window1 = GUI.Window(0,window1,domy,"picker"); 
  }


  function domy()
  {
  GUI.Label(Rect(30,30,145,150),color1);
  }


  function Update()
  {
  var mouse1 = Input.mousePosition.x;
  var mouse2 = Input.mousePosition.y;
  mouse2 = Screen.height - mouse2+20;
  var rect = Rect(30,30,200,150);        
  if (rect.Contains(Vector3(mouse1,mouse2,Input.mousePosition.z)))       
  {
  var x : int = transform.position.x/size.x *color1.width;
  var z : int = transform.position.z/size.z *color1.height;
  var y : int;
  activeColor = color1.GetPixelBilinear(x,y);    
  Debug.Log(activeColor);
  }

}

inside the rect contains i want to use get.pixel to get the color from the texture named color1 when mouse is over the texture and to apply the got color from texture color1 to a cube how is it possible inside this code. i want to create a color picker

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

1 Reply

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

Answer by Bampf · Mar 15, 2011 at 02:43 PM

First you must convert from the X,Y coordinates of the click to the texture coordinates (typically referred to as U and V.) Then you can fetch the pixel color at that spot.

I don't see a problem with your code to actually get the pixel (using GetPixelBilinear) so that just leaves the texture coordinates.

Texture coordinates always start at 0,0 and go to 1,1. In your picker you are displaying the entire texture in the rectangle (30,30,200,150) so just a little math will convert the coordinates to the 0..1 range.

The following code is not tested, and you may need to flip the Y coordinate, but you'd do something like:

var u = (mouse1 - 30) / 200;  // x --> u
var v = (mouse2 - 30) / 150;  // y --> v
// v = 1.0 - v;  //Uncomment this to flip vertically, if necessary 
activeColor = color1.GetPixelBilinear(u, v);

(Note: if this was a texture on a 3D object with a collider, you'd instead do a Raycast to figure out where the user clicked, then just read the UV coordinates directly from the RaycastHit record.)

Comment
Add comment · Show 4 · 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 robertmathew · Mar 15, 2011 at 03:11 PM 0
Share

it works fine for me can you explain why you used var u = (mouse1 -30)/200; var v = (mouse2 - 30) / 150;

avatar image Bampf · Mar 15, 2011 at 04:33 PM 0
Share

If you like to think graphically, imagine first moving the rectangle over to the origin. That's what the subtraction is for. Then scale the rectangle's width from 200 down to 1. That's what the division is for.

avatar image Bampf · Mar 15, 2011 at 04:37 PM 0
Share

If you like to think mathematically, we are taking an X value that's between 30 and 230, and making it 0 to 1. We want 30 to become 0, 230 to become one. The halfway value (130) should become 0.5. The formula to do a linear scaling like this is: U = (X - rect.left) / rect.width. So if you move or resize your picker, you just need to use the new rectangle values in this formula.

avatar image robertmathew · Mar 16, 2011 at 05:08 AM 0
Share

thanks for the valuable answer and explantion

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

No one has followed this question yet.

Related Questions

Cheapest/Easiest way to just draw a bunch of pixels right to the screen 0 Answers

Change pixel color of Material from URL 1 Answer

Sprite Renderer is wrapping bottom pixel when it shouldn't be 1 Answer

Texture2D.GetTextureRawData doesnt work outside the editor ? 1 Answer

Rotated Pixel Textures 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