Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Compute area on screen

Discussion in 'Editor & General Support' started by Quentin-G, Feb 13, 2013.

  1. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Hey guys,

    I'm having a bit of trouble with my project right now. Basically, I have a few characters and some objects in a scene, and I need to figure out how much space they occupy on the screen. To do so, I used the skeleton of the characters to define bounding boxes. So from the bounding box surfaces, I can compute how much space it represents in the 2D plane of the camera. But it doesn't take into account two elements :
    - Some surfaces only appear partially on the screen.
    - The occlusion : some object/characters are partially (or completely) hidden by some other objects/characters.

    So, does anyone have a suggestiopn of how I could compute the area on the screen.

    Thanks,
    Quentin.
     
  2. PaulR

    PaulR

    Joined:
    Nov 14, 2012
    Posts:
    43
    This is an interesting problem. I guess it depends on whether you need pixel accurate results, or a rough estimation.

    For a rough estimation, you could maybe try shooting a grid of rays, and effectively sample your scene. Assign a certain coverage value for when a ray hits (maybe if you detect an edge - 2 adjacent rays, 1 hits, 1 misses - shoot shoot additionals rays between them to get a better estimate of coverage).. The more rays you shoot the more accurate, but slower it will be.

    For a pixel perfect solution, you would need to do either:
    1) write your own software rasterizer

    2) use something similar to a GS histogram generator http://http.developer.nvidia.com/GPUGems3/gpugems3_ch41.html
    You would need to do something like have the shaders for your object output a specific value to maybe the alpha channel, then you can use a gs shader to effectively add up these values. You'd need unity pro for this, since you need to read from the render target.
     
  3. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Thanks for the answer.

    I already thought about these solutions, and I eliminated the idea of throwing rays on all the screen because of the computation time. I don't have enough time to implement a rasterizer, but I came up with an idea.

    Basically, I can get the coordinates of my BB in camera coordinates. So instead of throwing rays on the whole screen, I could simply send rays on my BB and see what % actually receive the rays (which is something I also need to know for my project). from this, I can get a pretty good estimation of the area occupied by my BB on the screen.

    What do you think of this solution ? Is there anyway to improve it a bit ?

    Thanks again.
     
    Last edited: Feb 15, 2013
  4. lockbox

    lockbox

    Joined:
    Feb 10, 2012
    Posts:
    519
  5. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Yep, I define my BB using 8 Vector3 and applying the proper translation, rotation and scaling using the skeleton of my characters. It's working quite well.

    And for computing the area, instead of using raycasting, I finally thought of another solution. I duplicated my Scene and the cameras (but not the characters) and translated it so that it won't appear in my main scene and I'm displaying my BB using the same offset. This way, for the same camera position, I can have 2 renderings :



    Then, by applying a basic shader on my bounding box (removing all the shading effects), I will be able to render a simple version of my scene to a texture and compute the area occupied on the screen simply by counting the pixel of the color (instead of throwing rays).

    I should be able to make it work, but it made me realize something : If I only display the camera with the basic rendering then, Unity doesn't compute everything in the scene (maybe some sort of optimisation process) and the bounding box computed are then completely wrong :



    Basically, it seems that everything that is not display at least on one camera is not properly animated. This is a big problem for me, is there a way to correct this problem ?

    Thanks guys.
     
  6. Quentin-G

    Quentin-G

    Joined:
    Dec 20, 2012
    Posts:
    29
    Ok, I manage to find a trick to solve this problem : I added a camera that render the whole scene in one pixel. Now, all the animation are computed and my BB are correct. But if anyone knows a better way, I'm interested.

    About the computation of the area I have another question. Is it possible to render a camera in a texture without Unity-Pro ? According to the few posts I read it doesn't seem to be possible but I wanted to make sure of it.

    Thanks again,
    Quentin.