Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to pick color in the screen point(x,y)

Discussion in 'Immediate Mode GUI (IMGUI)' started by JohnSonLi, Apr 21, 2012.

  1. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    If I now have the mouse position (x,y) int the screen,anyone tells me how to get the pixel rgb at the position?
     
  2. dodo

    dodo

    Joined:
    Jul 13, 2011
    Posts:
    49
    Use a rendertexture on your main camera, then read the pixel color with Texture2D.GetPixel
     
  3. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    use a rendertexture on the camera ???? what does it mean????
     
  4. _Petroz

    _Petroz

    Joined:
    May 13, 2010
    Posts:
    730
  5. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Other than using a RenderTexture, you could capture a screenshot of the camera view, and then find the right pixel. Though consider that it's quite expensive, so you won't be able to do it every frame while maintaining a smooth framerate.
     
  6. JohnSonLi

    JohnSonLi

    Joined:
    Apr 15, 2012
    Posts:
    586
    screenshot???how to capture it ?
     
  7. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Code (csharp):
    1.  
    2. Texture2D GetScreenshot( bool argb32 )
    3. {
    4.     Rect viewRect = Camera.main.pixelRect;
    5.     Texture2D tex = new Texture2D( (int)viewRect.width, (int)viewRect.height, ( argb32 ? TextureFormat.ARGB32 : TextureFormat.RGB24 ), false );
    6.     tex.ReadPixels( viewRect, 0, 0, false );
    7.     tex.Apply( false );
    8.     return tex;
    9. }
    10.  
     
    Mashimaro7 and ifurkend like this.
  8. frogsbo

    frogsbo

    Joined:
    Jan 16, 2014
    Posts:
    79
    Thanks for that, here's an update to this thread, because the unity get screenshot example is abit outdated, here is a version:

    if (GUI.Button(Rect(160,120,150,20),"screentest"))
    {
    // Make a new texture of the right size and
    // read the camera image into it.
    var tex = new Texture2D(Screen.width, Screen.height);
    tex.ReadPixels(new Rect(0, 0, 128, 128), 0, 0);
    tex.Apply();

    // Set the display texture to the newly captured image.
    //display.material.mainTexture = tex;

    // Reset the grab variable to avoid making multiple
    // captures.
    }

    it's unfinished cos i just realised in need the texture color not the ambient lighting+texturecolor,but it compiles.
     
  9. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    this is a complete version:

    code doesnt work on this forum for me at the moment....

    press middle mouse button it will print the color at the screenpoint.


    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. var tex : Texture2D;
    5. var mpos : Vector3;
    6.  
    7.  
    8. function Update () {
    9. if (Input.GetMouseButtonDown(2)) { moonrake();}
    10.  
    11. // print (Input.mousePosition);
    12. }
    13.  
    14. function moonrake (){
    15.     mpos = Input.mousePosition;
    16.     var ray = Camera.main.ScreenPointToRay (mpos);
    17.     var cube : GameObject  = GameObject.CreatePrimitive(PrimitiveType.Cube);
    18.     cube.transform.localScale = Vector3(20, 20, 1);
    19.     cube.transform.position = Vector3(0, 0.5, 0);
    20.     cube.transform.LookAt(camera.main.transform,Vector3.up);
    21.     Destroy (cube.renderer.material);
    22.  
    23.     // Application.CaptureScreenshot("Assets/savedmeshes/assets/ " + "Screenshot2.png");
    24.     sshot();
    25.  
    26.  
    27.  
    28. }
    29.  
    30. function sshot()//:Texture2D
    31. {
    32.     // Make a new texture of the right size and
    33.     // read the camera image into it.
    34.     var tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    35.     yield  WaitForEndOfFrame();
    36.     tex.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    37.     tex.Apply();
    38.  
    39.     var bla = tex.GetPixel  ( mpos.x, mpos.y );
    40.     print(bla);
    41.  
    42.  
    43.     //Destroy (tex);
    44.  
    45.     //return tex;
    46. }
    47.  
    48.  
    49.