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

Bug 2D Game: Pixel perfect camera + Stretch Fill breaks Camera.WorldToScreenPoint

Discussion in '2D' started by mata2000454, Jul 6, 2023.

  1. mata2000454

    mata2000454

    Joined:
    Jul 1, 2020
    Posts:
    3
    Hello,


    I'm making a 2D game. The important thing is to make camera show only the required area no matter which resolution is selected. I'm using Pixel perfect Camera for that with Stretch Fill, so it fits any resolution.

    Another think I have is a shader that inverts color if vertex position is lower left from inputted screen position (red circle here). This shader works correctly when I don’t used the Stretch Fill, but when it is on, it moves the screen position of inputted object away as you can see from these images (Screen position is passed as vector Camera.main.WorldToScreenPoint(circle.transform.position);)

    16:10 aspect
    upload_2023-7-6_15-45-42.png
    Full HD
    upload_2023-7-6_15-45-21.png
    QHD
    upload_2023-7-6_15-46-16.png

    The screen position offset then changes based on different resolution. If I turn off stretching, output is correct, but i can see around the room.:

    upload_2023-7-6_15-44-47.png

    Any idea how to go around this or possibly other way of doing it? What I'm looking for is a way to show tilemap of specific resolution (496x288) on any camera resolution with black borders around...
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    Until this bug(?) is fixed, you could just create another camera as a child of the main camera(at 0,0,0 local position and rotation) that doesnt have pixel perfect component attached and use it for screen-world space conversions. The child camera component must have the same settings as the main camera

    You can disable the camera component for the child camera and it wont incur much overhead
     
  3. mata2000454

    mata2000454

    Joined:
    Jul 1, 2020
    Posts:
    3
    Unfortunately, that did not fix the issue. Both camera's return same screen position, problem is, that since Pixel perfect Camera transforms the render afterwards, this "correct" position is incorrect in the transformed view during shading. Or at least that's what I think it is.
     
  4. mata2000454

    mata2000454

    Joined:
    Jul 1, 2020
    Posts:
    3
    Alright, thought of an easy workaround which seems to work.
    Instead of passing screen position into a shader, I'm passing world position and in shader calculating world position of each vertex.


    Thanks for help anyway.
     
    venediklee likes this.