Search Unity

Pixel Color on a Sphere

Discussion in 'Physics' started by Deleted User, Aug 23, 2015.

  1. Deleted User

    Deleted User

    Guest

    Hey,

    This is my first post so I hope it's in the correct location.

    I've searched all over the internet and I can't find exactly what I'm looking for or I'm not implementing it correctly.

    I want to be able to click on objects in the scene and get the color of the pixel in the texture assigned to the object I click in 3D. I don't need to take into account shadows or anything, just the texture info. I can get it to work fine with a cube but I'm having issues with cylinders and spheres. I'm looking for a solution to eventually work on any shape, even prefabs.

    My Code (essentially copied from other solutions):

    Code (CSharp):
    1. void SomeFunction()
    2. {
    3.         RaycastHit hit;
    4.         Ray theRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    5.  
    6.         if(!Input.GetMouseButtonDown(0))
    7.             return;
    8.            
    9.         Debug.Log ("Testing");
    10.  
    11.         if(!Physics.Raycast (theRay,out hit))
    12.             return;
    13.  
    14.         Debug.Log ("A Hit " + hit.transform.position);
    15.  
    16.         Renderer rend = hit.transform.GetComponent<Renderer>();
    17.         Texture2D tex = rend.material.mainTexture as Texture2D;
    18.         Vector2 pixelUV = hit.textureCoord;
    19.  
    20.         Debug.Log (pixelUV);
    21.  
    22.         pixelUV.x *= rend.material.mainTexture.width;
    23.         pixelUV.y *= rend.material.mainTexture.height;
    24.  
    25.         Debug.Log (tex.GetPixel((int)pixelUV.x,(int)pixelUV.y));
    26. }
    Line 20 - Debug.Log (pixelUV)

    Always returns (0.0, 0.0) for the Sphere and only works for the top and bottom of the cylinder.
    My thought is that it has to do with how textures get "wrapped" around the sphere based on each polygon it consists of. I hope it's something simple I'm just missing.

    Thank you very much
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    Are you using mesh colliders for those?
     
  3. Deleted User

    Deleted User

    Guest

    Thanks for the quick response. Yes I have the mesh colliders, forgot to mention that, oops. I didn't change any of the settings in any of them though. The cylinder and sphere's mesh colliders look like the cube which does work.
     
  4. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    What do you mean they look like the cube?

    Sorry that I'm insisting but by default, a Unity Sphere has a sphere collider and a Unity cylinder has a capsule collider. (both of those are not mesh colliders)

    Also according to the manual:
    Which is what you're getting, which makes me think you're not using Mesh Colliders.
     
  5. Deleted User

    Deleted User

    Guest

    I've added a mesh collider to all three of them. The cube worked so I set the "mesh colliders" of the cylinder and sphere to look the same. They have nothing added or checked. The only thing the "mesh colliders" have are the "Mesh" category preset to have either "Cube" , "Sphere" or "Cylinder".

    I guess I should've said I did the same thing to the sphere and cylinder after I got the cube to work properly.
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    Could you try a mesh you created yourself and see if that works?

    If everything is using mesh colliders there is no reason at all why a cube should work and a sphere wouldn't.
     
  7. Deleted User

    Deleted User

    Guest

    I've never "created" a mesh before. I've started reading about doing them at run time right now, is this what you are referring to? I'll work on the math while I wait for your response.

    Thanks for the help.
     
  8. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    I meant an object from a 3d modelling package such as blender or whatever.
     
    Deleted User likes this.
  9. Deleted User

    Deleted User

    Guest

    You are amazing. That totally worked!!!!

    I'm new to forums in general. Do I need to close a thread and is there a way to give you props officially?
     
  10. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,791
    You don't need to do anything, cheers :)