Search Unity

Help - function GetPixel in VR

Discussion in 'Getting Started' started by mafikes, Oct 15, 2019.

  1. mafikes

    mafikes

    Joined:
    Mar 24, 2014
    Posts:
    2
    Hello,

    I'am testing VR (3D) and new to unity.

    I want to get color from image (like color picker).

    Function GetPixel working well for me, but in another direction or rotation returns bad COLOR.
    Only when camera is forward to image, solution works well.

    Why? Can you help me please?

    Thank you.

    My code is
    Code (csharp):
    1.  
    2. Color color = image.sprite.texture.GetPixel(
    3. (int)((pickerObj.transform.position.x - transform.position.x) * (1 / GetComponent<RectTransform>().localScale.x) * (1 / GetComponentInParent<Canvas>().scaleFactor)),
    4. (int)((pickerObj.transform.position.y - transform.position.y) * (1 / GetComponent<RectTransform>().localScale.y) * (1 / GetComponentInParent<Canvas>().scaleFactor)) + GetComponent<Image>().sprite.texture.height)
    5. );
    6.  
     
    Last edited: Oct 15, 2019
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    What is the bad color?
     
    mafikes likes this.
  3. mafikes

    mafikes

    Joined:
    Mar 24, 2014
    Posts:
    2
    Hi, I resolved this issue today.

    Bad color was -> I pick red color -> return was black.

    I fix this with set canvas to width and height 1 and set scale.

    Calculate position
    Code (CSharp):
    1.        
    2. var posx = pickerObj.transform.localPosition.x * 256; // image width
    3. var posy = pickerObj.transform.localPosition.y * 256 + 256; // image heght
    4.  
    Thanks.