How to create a microscope viewport

So, I am a bit new to Unity, though I have worked with a myriad of game engines over the last 12 years. My strength is mostly in programming (C, C++, Java, AS3 and C#). Now, having said that I am at a complete loss on how to tackle the following. We have a project at work where we are creating a fully functional 3D Microscope. What I am stuck on is how to create the Ocular Lens view that you see here:

This view is suppose to represent what you would see when you look through the to ocular lens. Once you drop a slide on to the stage area it immediately appears (zoomed-in, of course) in the inset view in the upper-left (Ocular Lens Viewport). This viewport needs to be a masked layer so that it can represent the lens’ view. When you remove the slide the image is immediately removed. This will also need to work for all different slides, so I will need to load those images in dynamically.

What is getting me is how do I load an image into a masked layer (much like in Flash) so that the image only appears within the bounds of the circle. Now, I also have to be able to adjust the 3D model to focus/unfocus the image as well and adjust the ocular lens distance to match that of the users eyes (2 images become 1 in a sense) but that is really secondary to my main issue. Any thoughts on how to do this?

I have thought of two approaches, one is to set up a separate camera in the scene and script out a manager that will be responsible for loading/unloading the images as well as sending/receiving messages to and from the 3D model. The other approach is to integrate it (the ocular lens mask) into the HUD, but I have a feeling that that would get really cluttered and confusing later on when we add more slides.

Any help will result in my bowing to your awesomeness! ty :slight_smile:

Why not use a shader that just masks out the middle part for you, and just have that render out over the picture with the slide on it? Or have the inside of the circle be transparent and display it that way.

I have not worked a lot with shaders. Do you have an example of what you are talking about? Is there a reference that I can look at?

Have a camera rendering to that area (using the viewport rect), and put a GUITexture (where the circle in the middle is transparent) on top.

–Eric

You could use the texture mask shader from the unify wiki.

How would an image be hidden behind that mask if it was a shader. When I try that I am left with just a plane that has a alpha’d circle in the center. Would the images have to be children of that?

The page explains how to do it. But essentially you put a white circle with a black exterior in a texture in a material with the texture mask shader. You put that material on a plane that you put in front of the camera. Through the magic of z buffering and testing, it will only draw things that are behind the white, the black area will be transparent. Set that camera to depth only clear and voila, you’ll have a circular drawn area.

To be clear, the masking plane has to be between the camera and whatever you want to be masked out.

Sorry, that’s not quite the right shader. Try this one:

Shader "minimap/TextureMask" {
   Properties
   {
      _Mask ("Culling Mask", 2D) = "white" {}
      _Cutoff ("Alpha cutoff", Range (0,1)) = 0.5
   }

   SubShader
   {
        Tags {"Queue" = "Background"} 
        Blend SrcAlpha OneMinusSrcAlpha 
        Lighting Off 
        ZWrite On 
        ZTest Always

        Alphatest LEqual [_Cutoff]

        Pass
        {
            SetTexture [_Mask] {combine texture}
        }
   }
}

follow the rest of the steps on the linked page (generate alpha, etc…)