【Unity3d】【Camera】Perspective camera UI conversion (3DUI + maintain screen ratio)

0. Basic knowledge points

  1. Orthographic camera :
    The orthographicSize of the orthographic camera is half the screen height, that is, orthographicSize = H/2.

  2. Perspective camera :
    The aspect of the camera is the aspect ratio of the camera's viewport: aspect = W/H,
    that is: W = H * aspect
    The screen height H of the perspective camera changes with the center distance and fov of the camera. The formula is:
    H = distance * tan(fov*0.5) * 2

  3. Camera viewport size and screen resolution :

  4. The aspect ratio of the camera is equal to the aspect ratio of the screen

  5. When the viewport size height is H and the screen height is ScreenH, the ratio PixelsPerSize = ScreenH / H can be obtained

  6. In Unity3d, the Sprite setting option Pixels Per Unit = 100 means that one unit of the sprite represents 100 pixels.

  7. Therefore, when setting the orthographic camera to render Sprite, if SpriteSize = ScreenSize, just cover the screen, then:
    SpriteSize.H * PixelsPerUnit = ScreenSize.H = orthoSize*2 * PixelsPerUnit

Therefore, we get: orthoSize = ScreenSize.H / PixelsPerUnit / 2
At this time, when Sprite scale = 1, it is consistent with the result of UGUI camera rendering.

1. Integrated conversion:

When the size of the object in the orthographic camera is required to be the same as in the perspective camera, the display size in the orthographic camera is used as the basis (eg. display in the UI camera)

  • Method 1.0 : Move the position of the object in the perspective camera to ensure the same display size. (Principle of near large and far small)
    Principle: Ensure that the size of the perspective camera is the same as the size of the orthographic camera:
    that is: H (perspective) = H (orthogonal)
    then: H (orthogonal) = H (perspective) = distance * tan(fov * 0.5) * 2,
    we get, in a perspective camera, this object should be distance from the camera = H (orthogonal) / (tan(fov * 0.5) * 2).
    Where H (orthogonal) = orthoSize* 2.
  • Method 1.1 : From method 1.0, we can know that H is related to both distance and fov. Therefore, you can also modify fov alone , or modify distance and fov at the same time to get the same result:
    • tan(fov*0.5) = H/(distance * 2) = orthSize * 2 /(distance * 2) = orthSize / distance
    • fov = Atan(orthSize / distance) / 0.5 = Atan(orthSize / distance) *2。
  • Method 2 : In the perspective camera, zoom to ensure the same size is displayed.
    1. First, you need to obtain the distance to the perspective camera and the fov of the perspective camera at the same scale as in the UI camera, as the standard: Standard: When
      scale = 1, distance(0) and fov(0) are obtained.
      • If the size of UGUI installed in standard size is calculated , it can be obtained according to the principle in [0.3. Camera viewport size and screen resolution] :
        • Actual orthographic camera orthoSize = Screen.H /100 *0.5
        • H (orthogonal) = distance (tan(fov 0.5)*2) = orthoSize *2
        • So, we get distance * tan(fov*0.5) = Screen.H /100
        • If fov is fixed, distance can be obtained
        • If distance is fixed, fov can be obtained
        • You can also get the value of distance * tan(fov*0.5) for subsequent calculations.
        • (Let DXF = distance * tan(fov 0.5) ) (DXF: Distance x tan( F ov 0.5f)
          is obtained from the camera viewport size H = distance * tan(fov*0.5) * 2, H
          (x) = distance (x) * tan(fov(x)*0.5) *2So
          , when distance and fov change, the proportion of change is:
          S(x) = H(x)/ H(0)
          = distance(x) * tan (fov(x)*0.5) *2 /(distance(0)*tan(fov(0)*0.5)*2)
          = distance(x)*tan(fov(x)*0.5) / ( distance(0) * tan(fov(0)*0.5) )
          = DXF(x) /
          Scale(x) = scale(0) * S(x) at x where DXF(0) is, which is the enlarged ratio.
    • When fov remains unchanged, it is simplified to:
      S(x) = distance(x) / distance(0)

2. Note: the distance between the object and the perspective camera

  1. Under normal circumstances, the distance vector between the object and the perspective camera is deltaVec = camTr.position - tr.postion
  2. However, when calculating the scale of an object in a perspective camera, the plane distance of the object in the perspective flat joint should be used:
    1. projDistance =Abs( Vector3.Dot(deltaVec,camTr.forward) )

3. Others: In a perspective camera, objects always face the camera:

Set tr.transform.forward = camTr.forward.

Guess you like

Origin blog.csdn.net/yanchezuo/article/details/129420904