Search Unity

How to do "overlays"?

Discussion in 'Scripting' started by CarlL, Aug 28, 2009.

  1. CarlL

    CarlL

    Joined:
    Nov 11, 2008
    Posts:
    83
    I'm trying to duplicate and improve upon a medical simulation I originally started in Adobe Director.

    In the simulation, the observer rotates around a 3D patient, and can look into the eye using a hand lens, and see the retina magnified.

    To do this, I had 2 cameras in Director, one that showed the entire scene with the patient and hand lens, and another camera with a higher zoom, linked to the first. I took the image from that camera, continuously converted it to a bitmap and used Director's "overlay" function to continuously place that bitmap on top of the hand lens in the view from the first camera.

    So , an overlay in Director allows you to place an image from one camera onto the screen as something like a sprite.

    Is there anything like this in Unity?

    Otherwise, is there any way to achieve the same effect?

    Thanks so much for the help. This is the last major problem I need to solve in order to finish this project!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Rendertextures (requires Unity Pro). I couldn't quite tell from your description, but if the second camera view is 2D and always faces the screen, then you don't need a rendertexture and can just have a smaller inset camera that you can move around as desired.

    --Eric
     
  3. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    Yup, Eric has it spot-on:

    1. Create a render texture and use that as part of some GUI layer (very much like you were doing in Director). Pro-only.

    2. Use multiple cameras where one renders picture-in-picture style. Indie or Pro.
     
  4. CarlL

    CarlL

    Joined:
    Nov 11, 2008
    Posts:
    83
    Thanks guys.

    I only have Indie right now. I might be able to get my client to spring for pro for me if it's absolutely necessary to get the effect.

    With the render texture solution (#1), how do you convert the camera view into a texture? I see:

    Camera.targetTexture

    ??



    Or preferably solution #2 (since I have Indie), I looked up "picture in Picture" in the manual and got:

    EditorGUI.DrawTextureAlpha
    GUI.DrawTexture

    Doesn't seem clear how to set a camera to be a "picture in picture".

    I did see also:

    Handles.DrawCamera
    Description
    Draws a camera inside a rectangle.

    is that it?

    Ultimately I need to mask the result into a circle because the hand lens is a circle, but I can settle for a square for now.
     
  5. CarlL

    CarlL

    Joined:
    Nov 11, 2008
    Posts:
    83
    Also, if I get pro, and convert my camera view into a texture, could I just keep wrapping that texture onto my circular lens, so it's masked as a circle? I tried that method in Director and it worked, but had a slower framerate than the overlay method.
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    With a rendertexture, it's literally a camera rendered as a texture. So you can use it just like any other texture, and do whatever you like with it. GUI.DrawTexture isn't useful unless you have a texture to draw, which you get with a rendertexture.

    As far as Indie methods, it would have to be a square. Just use another camera, change the normalized viewport rect and make it have a higher depth than the regular camera.

    --Eric
     
  7. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    As to doing multiple cameras read the Camera page in the docs, pay attention to the depth and normalized viewport rect values. And Eric, why would it have to be square? You can do non-square picture-in-picture camera views.
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's not called a Viewport Rect for nothing. ;) If you're referring to masking techniques like this, then yes, that can provide a non-square looking camera. I was actually referring to rendertextures being able to be applied to any arbitrary shape, though indeed the above technique might be useful in this case too. If there's another way, I'd be interested to know. :)

    --Eric
     
  9. CarlL

    CarlL

    Joined:
    Nov 11, 2008
    Posts:
    83
    Thanks guys. The viewport rect works great, and that link for doing masking looks like it will work fine. Thanks!!!!!
     
  10. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    FWIW, "square" implies the same height and width and that is not a requirement/limitation with cameras inside of Unity. Now if you would have said "your overlays must be rectangular" I'd have said nothing... ;)