Search Unity

Frustum to Matrix

Discussion in 'Scripting' started by metervara, Sep 26, 2008.

  1. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Fighting with the projectionMatrix again...

    In short I'm working on some billboard impostors. I've managed to create the desired frustum I want for my billboard rendering camera but translating it to a matrix is not working out so well.

    See attached image for reference -> The cyan-colored lines show the frustum, the numbers 0-7 are the positions for each of those corners. They're stored in billboardVerts (Vector3[8]). My main camera and billboard rendering camera have the same position and rotation. A is the result of my projectionMatrix attempt (code below).

    I've looked at the function PerspectiveOffCenter from the examples in the docs, and also glFrustum which looks like the exact same function.

    First attempts were way off but I got close with this: (check image for position indexes)

    Code (csharp):
    1. PerspectiveOffCenter(billboardVerts[0].x,billboardVerts[1].x,billboardVerts[0].y,billboardVerts[3].y,(BBCenter[0] - mainCam.transform.position).magnitude,(BBCenter[1] - mainCam.transform.position).magnitude);
    BBcenter[0] is the center of the near frustum plane (white in image), and BBcenter[0] the center of the far one.

    Can't seem to get my head around it so any help is greatly appreciated.

    thanks,
    Patrik
     

    Attached Files:

  2. Jonathan Czeck

    Jonathan Czeck

    Joined:
    Mar 17, 2005
    Posts:
    1,713
    Perhaps this helps? I made it a while ago trying out impostors. It has its problems but it kind of works. I never cleaned it up either. But you can watch the ~1 million vertices go down to something like 350 in the stats window from pressing play.

    If I understand right, I think the thing you may be looking for is CropMatrix() in Matrix4x4x.cs and its usage in ImpostorRenderCamera.js.

    Cheers,
    -Jon
     

    Attached Files:

  3. metervara

    metervara

    Joined:
    Jun 15, 2006
    Posts:
    203
    Hej Jon.

    Thanks for sharing -> Your CropMatrix() function did the trick! I was trying to solve the problem by directly setting the projectionMatrix, but your approach is a lot easier to understand :)

    One thing I'm wondering is if it's possible to set the near&far clip planes directly in the matrix or if I always have to do that through the camera variables?

    I also had a few issues with view frustum culling of my billboard but solved it by keeping the transform.position of the billboard right in front of the camera. Is that the way to go?

    /Patrik