- Home /
Rendering a 3D object within a specific area/bound?
Hello, I'm trying to recreate the "Examine" feature in Resident Evil Remake. Currently, I have the rotation right, what's left is how to setup a background, and handle the camera(s) responsible for that. (By background I mean the image behind the object)
FYI I'm using NGUI. I have no problem setting up a background, with a separate camera showing only that background and the object to examine. But the problem lies when I try to Zoom in/out. What I wanna do, is I want to zoom in/out to the object, without it getting out of the background!
1- If I parent both the background and the 3d object to some empty gameobject, and try to zoom in that way (tried both orthogonal and perspective cams), both the 3d object and the background will get affected and enlarged (zoomed in/out to/from), I don't want that, I just want the weapon/3d object to get affected, I want the background to stay still, so...
2- If I try to separate the background from the weapon, so that when I zoom in/out I only affect the weapon, what happens is that if I zoom in too much, the weapon/object will get out of the bounds/area of the background, like so: (In this image, the background is being rendered by another camera, and the weapon by another, both cams are ortho, and I'm zooming in by changing the size of the viewport)
I hope my problem is clear. So my question is, how can I zoom in/out to the weapon, in a way that it will always be (rendered?) inside the background?
Thanks for the help.
EDIT: This is exactly what I'm trying to achieve, notice in the left image how a small portion of the right side of the shotgun isn't showing up, same thing in the right image, the top side of the shotgun... they don't show up, as if they're cut.
by "background", you mean the shaded box in the image, correct?
Are you using Unity Pro? $$anonymous$$y guess would be with a screenspace shader and a mask. Or just a screenspace shader that would render the weapon onto the backgrond object.
Unfortunately 'currently' no, I'm not using Unity Pro. But does it have to be pro for me to do this? - Any other way around?
After reading your question, I'm not sure what you consider an acceptable solution. You can calculate the relative size of objects as viewed by the camera by converting to Viewport coordinates. Viewport coordinates start in the lower left of the screen at (0,0) and go to (1,1) in the upper right. Convert the lower left corner and upper right of your 'background' to viewport coordinates. Do the same for the gun. Then you can compare their relative screen sizes.
The conversion is done using Camera.WorldtoViewportPoint().
Answer by vexe · Aug 12, 2013 at 09:06 AM
I GOT IT!! :))
The trick was to mess with the Camera's Normalized viewport rect, don't let the camera render the whole screen, but only a part of it! The zooming in/out is done by 'uniformly' scaling the weapon (bigger/smaller)
From the docs:
Normalized View Port Rect: Four values > that indicate where on the screen this camera view will be drawn, in Screen Coordinates (values 0-1). X: The beginning horizontal position that the camera view will be drawn. Y: The beginning vertical position that the camera view will be drawn. W: (Width) Width of the camera output on the screen. H: (Height) Height of the camera output on the screen.
Here are my camera settings in the inspector:
I just set the beginning of the viewport to where I like within the screen, and set its width/height to fit my 'background' XD
Thanks for anybody who offered their help.
$$anonymous$$eep in $$anonymous$$d that the Camera component also has the pixelRect property which allows you to set the cameras rest in screen coordinates ins$$anonymous$$d of normalized coordinates. Note: screen coordinates have 0,0 at the bottom left corner.
Thanks, this is very useful, I will keep that in $$anonymous$$d. I've always wondered what those 'things' do in the camera :D
But strange why the property is only accessible via code, couldn't find it in the inspector... :/
Your answer
Follow this Question
Related Questions
Orthographic position to Perspective position 0 Answers
Is it possible to view two camera at the same time? 2 Answers
flickering lines when zooming fov (both GUI and ortho cameras). 2 Answers
orthographic camera view looks different from perspective view 2 Answers
Adjust Camera PixelRect Based on Screen Resolution In Unity 0 Answers