Search Unity

Performing game view camera calculations whilst in the scene view.

Discussion in 'Editor & General Support' started by nullstar, May 12, 2012.

  1. nullstar

    nullstar

    Joined:
    Jul 2, 2010
    Posts:
    186
    Hey folks, I'm having a slight issue with one of my current projects which I'm hopeing to get some help on.

    I'm trying to write an editor tool to allow for placement of objects in the 3d scene view but which will snap those objects to calculated world positions so that when they are viewed from the game view through a fixed othographic camera they will be perfectly aligned to a screen spaced grid and exact pixel boundaries.

    Currently the way I'm handling this is I've created an Editor which responds to mouse events in the scene view. When the mouse button is clicked, the editor shoots a ray into the world using 'HandleUtility.GUIPointToWorldRay(Event.current.mousePosition)' to determine the world position that was clicked on. This world position is then converted into a screen space position for my game camera by grabbing the game camera and calling its 'WorldToScreenPoint()' function. The resulting screen point is then snapped to the screen space grid and raycast back into the world to correct the world space position of the object.

    The problem I have is that when in the editor, the game camera pixelHeight, pixelWidth, aspect, and projectionMatrix are all changing in the editor depending upon which view is currently active rather than being fixed relevant to the game view. This means that all my calculations are being thrown off when placing objects from the scene view resulting in my objects not aligning correctly to the screen spaced grid when viewed in game.

    I've thought off precalculating the screen based grid spacing in world space so i can place objects in the 3d scene view without having to perform camera calculations but discounted this since I'm concerned about mathematical drift which could result in non pixel perfect boundary alignment when viewed from the in-game camera which is critical.

    Therefore does anyone know of a way to keep the game camera fixed relative to the game view when in the scene view? Alternatively does anyone know the exact calculations used by the game camera when calling 'WorldToScreenPoint' and 'ScreenToWorldPoint' or have access to that source code? That would let me recreate the exact calculations so I'm not dependant upon using the game camera functions from the editor which would side-step the issue of Unity changing my game camera parameters in the scene view since I could cache the correct game view aspect and projection matrix and use those.

    Any help would be greatly appreciated :).

    Dan.
     
    Last edited: May 12, 2012