2
\$\begingroup\$

Is it possible in Unity to make some sort of bounding box (with a running application) within the current viewpoint of the actual screen?

Let me explain:

I'm working on a project which uses a projection for visual output, the screen I am projecting on is smaller than the actual projectors resolution. This means that the unused spaces around the actual working application, are black and visually hidden.

Made an image for exra explanation:

enter image description here

As i'm new to Unity and still learning, I'm not sure if this is even possible because of the use of camera's and environment. I happen to know that it's possible in three.js. But if anyone could point me in the right direction or explain that it's impossible would be much appreciated because I'm not sure where to look anymore!

\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

You can simply set the camera’s Viewport in the inspector. It defaults to x,y,w,h= 0,0,1,1. You could, for example, set it to 0.2,0.1,0.6,0.8

You can include another camera (full screen) that doesn’t render anything but has a clear color of black and has a depth set so it renders before the main camera to ensure you get black around the edges.

The values are relative to the size of the screen (or rather, the game window, if your game is not running in full screen) as a percentage. Taking .5 as an example, as a coordinate (x or y) it refers to the center of the screen, and as a dimension (w or h) it refers to half the screen size.

So for the example above, the values are .2, .1, .6, .8, meaning it is positioned 20% in from the left edge of the screen, taking up 60% of the screen's width. And then the y coordinate starts at 10% from the bottom of the screen, taking up 80% of the screen's height.

If you add up the coordinate and the dimension horizontally (20% + 60%) you will get 80%, representing the right edge of the image. Note that 100% - 80% = 20%, the same as the left edge inset, meaning it is therefore centered on screen horizontally.

The same math applies vertically. 10% + 80% = 90%, and 100% - 90% = 10%. If you don't want the image centered, adjust the coordinate and dimension so that 100% - (coordinate + dimension) != coordinate

\$\endgroup\$
2
  • \$\begingroup\$ thx didnt know it was this easy \$\endgroup\$
    – FutureCake
    Commented Jul 23, 2018 at 15:54
  • \$\begingroup\$ It might help to explain in a bit more detail that the numbers are relative to the full width/height of the window or screen (so 0.5 is half the width, or half the height) \$\endgroup\$
    – DMGregory
    Commented Jul 24, 2018 at 13:36
1
\$\begingroup\$

Yes, you can do it easily in unity. But first, you need to understand Camera, RenderTexture, Canvas, and RawImage in Unity.

Below is step by step procedure to achieve the result you wanted.

  1. Create a New Scene
  2. Add New Camera using Right Click > Camera
  3. Change Camera's Projection to "Orthographic" (You can skip this step, or set according to your requirement)
  4. Select the "Main Camera" which is by default available in New Scene.
  5. Change Camera's Clear Flags to Solid Color and change background to "Black" and change projection to "Orthographic"
  6. Create RawImage by Right Click > UI > RawImage
  7. Now set RawImage Position to the are you want to see ex."Working View"
  8. Go to project > Create > New RenderTexture
  9. Select the RawImage and Drag the RenderTexture you created to RawImage's Texture property.
  10. Drag that RenderTexture into the New Camera's "TargetTexture". (Camera we created manually is New Camera)

I hope this helps!

What actually happens here is we transfer Whatever Render into the new camera to "RenderTexture" and make it render back to the scene using RawImage.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ why do you use an orthographic camera? whats wrong with a perspective camera? \$\endgroup\$
    – FutureCake
    Commented Jul 23, 2018 at 15:43
  • \$\begingroup\$ I have never tried with perspective before you tell, and it's working fine with a Perspective camera as well. Thanks for the suggestion. \$\endgroup\$ Commented Jul 24, 2018 at 12:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .