0
\$\begingroup\$

I have a texture shown over the screen using screen space UV's. The Texture is 495x,596x, Screen size is 888,500, but can change. The Texture has a Tile of (3.469775f,1.392644f) to correct for distortion from being stretched to the Screen's size, and has repeat set to Clamped. So basically, I'm showing a Texture of an arbitrary size on screen and using Tile and Offset in the shader to correct for distortion and keep the texture at its original aspect ratio.

Now my problem is I want to Zoom in and out by a set pixel amount in Screen space. So lets say: I need the texture to increase in width by 10 pixels in screen space. What is a formula I can use to recalculate the Tile and Offset so that the texture stays at the same aspect it was but increases by the amount I want it zoomed?

I've been trying things like: 1 Pixel = (1f / Camera.main.pixelWidth). So to zoom by 10 pixels I would go something like Tile.x += (10f * (1f / Camera.main.pixelWidth)); But this doesn't seem quite right, I'm not seeing it increase the Texture by the right number of pixels...

My current function is something like this, but for Zoom = 5f, it's only enlarging by about 2px. AspectRatio = Tiling in the Material and WidthOffset/HeightOffset is just the Offset.

public void ZoomOverlay(float ZoomAmount)
{
    AspectRatio.x -= ZoomAmount * (AspectRatio.x / (float)Camera.main.pixelWidth);
    AspectRatio.y -= (ZoomAmount * (AspectRatio.x / (float)Camera.main.pixelHeight)) / 2f;

    WidthOffset += ((ZoomAmount / 2f) * (AspectRatio.x / (float)Camera.main.pixelWidth));
    HeightOffset += ((ZoomAmount * (AspectRatio.x / (float)Camera.main.pixelWidth)) / 2f) / AspectRatio.x;
}
\$\endgroup\$
  • \$\begingroup\$ Why are you not playing with Camera's Field of View (3D) or Size 2D instead for Zooming? \$\endgroup\$ – Hamza Hasan Dec 24 '15 at 1:53
  • \$\begingroup\$ Basically a user uploads a picture, chooses some points on it that are aligned with a mesh, then the UV coordinates are baked into the Mesh so it can be textured as seen via the projection and moved around. \$\endgroup\$ – Jeremiah Leslie Dec 24 '15 at 3:48
0
\$\begingroup\$

The inverse of the texture size corresponds to 1 texel (texture pixel).

pseudo-code:

offset.x = (float)borderPixels.x / (float)tex.width;
offset.y = (float)borderPixels.y / (float)tex.height;

tile.x = 1.0f - offset.x * 2;
tile.y = 1.0f - offset.y * 2;

where borderPixels is the number of pixels to "remove" around the texture.

This will scale the UV coordinates to cut off the desired number of texels.

After that you can scale the render plane geometry to the proper aspect ratio.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.