Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Center Zoom

Discussion in 'Shaders' started by MatheusMarkies, Aug 11, 2020.

  1. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    Good night.
    I want to zoom in on a Render Image, but when I multiply its UV by a constant value, the zone is applied in one direction (left diagonal).
    Capturar.PNG asgtsafg.PNG

    Code:
    Code (CSharp):
    1. float lightIntensity(float2 fxUV, float thr) {
    2.     float4 colorChannel = GetSource(float2(fxUV.x * 1 / thr,fxUV.y * 1/thr));
    3.     float greyscale = (colorChannel.r + colorChannel.g + colorChannel.b) / 3.0f;
    4.     return greyscale;
    5. }
    GetSource:
    Code (CSharp):
    1. float4 GetSource(float2 fxUV) {
    2.     return SAMPLE_TEXTURE2D(_PostFXSource, sampler_PostFXSource, fxUV);
    3. }
    PostFXSource = image from camera backBuffer

    I want the zoom to be centered. I believe I have to add an offSet to fix this.
    I have no idea how to do this offset.
     
  2. MatheusMarkies

    MatheusMarkies

    Joined:
    Apr 16, 2017
    Posts:
    67
    asgtsafg.PNG
    I know that and the same thing, I left to add a multiplier in the future.
    As I reduced the image by 2x, I have to move 1/2. But it didn't work, bro.
    For some reason it keeps going to the left
    ezgif-6-614571fa4b5d.gif

    What I wanted to do is this cut here:
    Sem Título-1.png

    So I can get only the object that the player is looking at.

    I tried this too:
    Code (CSharp):
    1.     return SAMPLE_TEXTURE2D(_PostFXSource, sampler_PostFXSource, float2(fxUV.x * 1 / thr, fxUV.y * 1 / thr) + float2(fxUV.x / thr + ((fxUV.x / thr) * 0.5) , fxUV.y / thr + ((fxUV.y / thr) * 0.5)));
    Did not work
     
    Last edited: Aug 11, 2020
  3. Deloix

    Deloix

    Joined:
    Feb 26, 2015
    Posts:
    3
    you have old resolution 1500,900 and you need read centered pixels 3 times zoomed so you read between 500 - 1000 on X and 300 - 600 on Y... Center is 750,450 so just make 750 - 1500 = -750 what is start offset from center and divide it by "thr" so -750 / 3 = -250. Now you know where start on X when you zoomed 3x and you starting on 750 + (-250) = 500 as on your picture
    Code (CSharp):
    1.  
    2. // my pseudocode
    3. float2 center = float2(screen.width / 2, screen.height / 2 );
    4. float zoom = 3;
    5. float2 startingPointOffest = center - float2(screen.width, screen.height);
    6. float2 zoomedStartPosition = center + startingPointOffset / zoom ;
    7. float4[,] zoomedColors;    // 500,300
    8. for(y = 0; y < screen.height/ zoom; y++){
    9.     for(x = 0; x < screen.width / zoom; x++){
    10.      zoomedColor[x,y] = GetColor(zoomedStartPosition.x + x, zoomedStartPosition.y + y);
    11.     }
    12. }
    13.  
    14. Texture2D = zoomedColors;
    15.  
    16.  
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,238
    Texture UVs in a shader are normalized to a 0.0 to 1.0 range on the x and y. (0.0, 0.0) is the bottom left, (1.0, 1.0) is the top right. Multiplying 0.0 by any value is 0.0, so it doesn't change, hence the zoom into the corner. If you want to zoom into the center, the easiest way to do that by offsetting the UVs by 0.5 so that (0.0, 0.0) is now the center, multiplying, and then counter offsetting by 0.5 afterwards.

    Code (csharp):
    1. float2 zoomedUV = (uv - 0.5) / zoom + 0.5;
     
    MatheusMarkies likes this.