- Home /
Move the viewport, but maintain its size.
Is there a way to use the Normalized View Port Rect but maintain its size? When I adjust the x and y at the, the viewport shrinks. I need to be able to keep it at Screen.width and Screen.height, but move it.
Is this possible?
Thanks
Answer by Kujo87 · Nov 07, 2013 at 03:36 PM
Sorted this now. For anyone wanting to do similar in the future, I did it using this:
CamScript.cs:
void OnPreCull()
{
var r = new Rect(0f, 0f, 1f, 1f);
var alignFactor = Vector2.one;
if (viewportOffset.y >= 0f)
{
// Sliding down
r.height = 1f - viewportOffset.y;
alignFactor.y = 1f;
}
else
{
// Sliding up
r.y = -viewportOffset.y;
r.height = 1f + viewportOffset.y;
alignFactor.y = -1f;
}
if (viewportOffset.x >= 0f)
{
// Sliding right
r.width = 1f - viewportOffset.x;
alignFactor.x = 1f;
}
else
{
// Sliding left
r.x = -viewportOffset.x;
r.width = 1f + viewportOffset.x;
alignFactor.x = -1f;
}
// Avoid division by zero
if (r.width == 0f)
{
r.width = 0.001f;
}
if (r.height == 0f)
{
r.height = 0.001f;
}
camera.rect = new Rect(0, 0, 1, 1);
camera.ResetProjectionMatrix();
Matrix4x4 m = camera.projectionMatrix;
camera.rect = r;
Matrix4x4 m2 = Matrix4x4.TRS(
new Vector3(alignFactor.x * (-1 / r.width + 1), alignFactor.y * (-1 / r.height + 1), 0),
Quaternion.identity,
new Vector3(1 / r.width, 1 / r.height, 1));
camera.projectionMatrix = m2 * m;
}
Usage:
Vector2 screenPos = gameCamera.WorldToScreenPoint (new Vector3 (transform.position.x, transform.position.y, _dartDistance));
screenPos = gameCamera.ScreenToViewportPoint (screenPos);
screenPos = new Vector2 (-Utils.NewRangeCalc (0.0f, 1.0f, -0.5f, 0.5f, screenPos.x), -Utils.NewRangeCalc (0.0f, 1.0f, -0.5f, 0.5f, screenPos.y));
CamScript.viewportOffset = screenPos;
The OnPreCull method has its origin in the centre so I had to scale the screenPos from 0-1 to -0.5-0.5 to get it to work right.
Answer by Cherno · Oct 14, 2013 at 01:22 PM
It's possible, there is a user script of a minimap camera where you can specify the dimensions as well as the position on screen. I can'T remember it's name, you just have to search a bit for it.
Can this be done during update? I'm after moving the viewport around on user input.
I've had a look around at some of the $$anonymous$$imap scripts after your suggestion, but most of them are making the viewport smaller (for a picture in picture look)... I require my viewport to stay the same size at all times and be full screen.
Thanks
wouldn't it be easyer and same effect just to transform.position of camera?
No as this doesn't give the desired effect I was looking for. It would either cause the object I'm moving round to distort with the perspective of the camera, or if I set the transform of the camera equal to the object I'm moving, it would appear not to move (as I was culling everything except for the object)
Its quite a specific effect I was looking for and achieved with the methods below
Your answer
Follow this Question
Related Questions
Build for Windows : Camera rect always stretched 0 Answers
camera.pixelrect not behaving properly 3 Answers
drawings secondary cameras in specifics places 1 Answer
set Edge Collider 2D to the borders of the camera's viewport 0 Answers
Changing the Camera viewport rect results in resizing the objects rendered by the specific camera ? 1 Answer