Inverting rotation in 3D, to make an object always face the camera?

前端 未结 4 917
暗喜
暗喜 2020-12-01 03:49

i have lots of sprites arranged in 3D space, and their parent container has rotations applied. How do i reverse the sprites 3D rotation, that they always face the camera (Ac

4条回答
  •  误落风尘
    2020-12-01 04:35

    The easiest way to do this is "clearing" the rotational part of the transform matrix. Your typical homogenous transformation looks like this

    | xx xy xz xw |
    | yx yy yz yw |
    | zx zy zz zw |
    | wx wy wz ww | 
    

    with wx = wy = wz = 0, ww = 1. If you take a closer look you'll see that in fact this matrix is composed of a 3x3 submatrix defining the rotation, a 3 subvector for the translation and a homogenous row 0 0 0 1

    | R       T |
    | (0,0,0) 1 |
    

    For a billboard/sprite you want to keep the translation, but get rid of the rotation, i.e. R = I. In case some scaleing was applied the identity needs to be scaled as well.

    This gives the following recipie:

    d = sqrt( xx² + yx² + zx² )

    | d 0 0 T.x |
    | 0 d 0 T.y |
    | 0 0 d T.z |
    | 0 0 0   1 |
    

    Loading this matrix allows you to draw camera aligned sprites.

提交回复
热议问题