Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Get color of texture under mouse position

Discussion in 'Scripting' started by Sabo, Sep 6, 2011.

  1. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    How would I go about getting the color of the texture under the mouse position?
     
  2. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Unless theres a built in way to do this, which i don't think there is, your best bet is to use Application.CaptureScreenshot and check the pixel from the screenshot made.
     
  3. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    I was thinking something along the lines of getting the mouse coordinates and divide the screen size with those. That would get us two values between 0 and 1, which I was hoping we somehow could use as texture coordinates.
     
  4. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
    Unless your rendering to texture / target. I think it would still require you using screenshots.

    you can use Camera.ScreenToViewportPoint to get the 0..1 coordinate.
     
  5. Garrus

    Garrus

    Joined:
    Sep 4, 2011
    Posts:
    7
    Code (csharp):
    1. Vector2 pos = Input.mousePosition; // Mouse position
    2. RaycastHit hit;
    3. Camera _cam = Camera.mainCamera; // Camera to use for raycasting
    4. Ray ray = _cam.ScreenPointToRay(pos);
    5. Physics.Raycast(_cam.transform.position, ray.direction, out hit, 10000.0f);
    6. Color c;
    7. if(hit.collider) {
    8.     Texture2D tex = (Texture2D)hit.collider.gameObject.renderer.material.mainTexture; // Get texture of object under mouse pointer
    9.     c = tex.GetPixelBilinear(hit.textureCoord2.x, hit.textureCoord2.y); // Get color from texture
    10. }
     
  6. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    Thanks a bunch!
     
  7. bdev

    bdev

    Joined:
    Jan 4, 2011
    Posts:
    656
  8. San999

    San999

    Joined:
    May 7, 2013
    Posts:
    1
    everytime i clicked the color is same
     
    superjhc123 likes this.
  9. manishbhatt

    manishbhatt

    Joined:
    Oct 31, 2015
    Posts:
    1
    Here is a script to get pixel from texture and prints its RGB

    using UnityEngine;
    using System.Collections;

    public class TextureColor : MonoBehaviour
    {

    public Camera cam;
    void Start()
    {
    cam = Camera.main;
    }
    void Update()
    {
    if (!Input.GetMouseButton(0))
    return;

    RaycastHit hit;
    if (!Physics.Raycast(cam.ScreenPointToRay(Input.mousePosition), out hit))
    return;

    Renderer rend = hit.transform.GetComponent<Renderer>();
    MeshCollider meshCollider = hit.collider as MeshCollider;
    if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null || meshCollider == null)
    return;

    Texture2D tex = rend.material.mainTexture as Texture2D;
    Vector2 pixelUV = hit.textureCoord;
    Debug.Log("XYY:::"+pixelUV);
    pixelUV.x *= tex.width;
    pixelUV.y *= tex.height;

    Debug.Log("X:::"+pixelUV.x +" Y::"+pixelUV.y);
    Color32 c;

    c = tex.GetPixel ((int)pixelUV.x, (int)pixelUV.y);

    Debug.Log("color::"+ c);
    Debug.Log( c.ToString());
    }



    }


    Don't forget to make Texture Read/writable and apply mesh collider to the object
     
    FardinHaque likes this.
unityunity