Search Unity

Pixels Moved in scene Transitions

Discussion in 'Scripting' started by maryamk, Sep 25, 2017.

  1. maryamk

    maryamk

    Joined:
    Apr 4, 2017
    Posts:
    18
    Hello
    I have a script in which at some point the camera translates a specified amount, hence the whole scene translates. I would like to know the number of pixels that the camera goes over in this translation.

    I have been trying it with WorldToScreenPoint. However, after the translate it gives me zero values.
    Code (CSharp):
    1.     Vector3 screenPos1 = cam.WorldToScreenPoint(target.position);
    2.         transform.Translate(1, 0, 0);
    3.         Vector3 screenPos2 = cam.WorldToScreenPoint(target.position);    
    4.         float px = Mathf.Abs(screenPos2.x - screenPos1.x);
    5.         float py = Mathf.Abs(screenPos2.y - screenPos1.y);
    6.         float pz = Mathf.Abs(screenPos2.z - screenPos1.z);
    I have selected the target as the camera itself! which I know is not right. (both the cam and the target are the Main Camera in the scene)

    Are there any other ways I can measure this?

    Thank you
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    "Pixels of camera movement" isn't even a well-defined concept unless your camera is Orthographic, in which case, you can calculate the answer based just on the camera properties (e.g. orthographicSize). What kind of camera do you have?
     
    maryamk likes this.
  3. maryamk

    maryamk

    Joined:
    Apr 4, 2017
    Posts:
    18
    Thank you JoeStrout. I didn't know about this property of the camera. My camera actually has perspective projection.
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    OK, so in perspective, the issue is not well-defined. What is a "pixel"? At what distance from the camera do you want to measure these pixels?

    What is it you're actually trying to accomplish here?
     
    maryamk likes this.
  5. maryamk

    maryamk

    Joined:
    Apr 4, 2017
    Posts:
    18
    I see.
    I am actually doing a cognitive study and I my experiment involves Unity and an EyeTracker. I was trying to find the visual angle (in degrees) of the scene the user is looking at. The formula is this:
    theta = 2 * arctang ( pix-width / (2*viewDistance) )

    so for one pixel width I do this:
    Code (CSharp):
    1. pixWidth = 1/(Screen.dpi);
    2.  
    and when there is a scene translation, I was thinking of calculating the number of pixels on the screen that the camera is translated for and multiply it by the size of one pixel on the display.

    Code (CSharp):
    1.         float viewDist = 55; // in cm
    2.         float pixMovex = px * (pixWidth * 2.54f); // num of pixels moved * size of pixel in cm
    3.         float thetax = 2 * ((Mathf.Atan(pixMovex / (2 * viewDist))) * Mathf.Rad2Deg);
     
  6. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I see. I think I would recommend keeping your camera position the same in all scenes (which you could ensure, for example, by making it a prefab, as long as you never move it in code). That eliminates that variable entirely.

    Also, depending on what you're doing, consider using an Orthographic camera. That will allow you to establish a fixed relationship between units in world space, and pixels on the screen (which you can then convert to degrees of visual angle). With a perspective camera, of course, this relationship is much more complex — things closer to the camera appear larger on screen than things further away.