TLDR: Does anyone know of any pitfalls of setting up render textures with pixel art? And how to do it correctly? And what might cause pixel distortion / warping on the render texture, even though the rest of the pixel art is fine? More details below.
I had some reflections set up nicely like below, at 240 x 135 resolution:
But when I tried to scroll the background, the pixels kept warping shape and size and flickering, as seen below. I actually had the ripple effect turned off below, so it should be a still reflection, but it’s clearly not.
Now this reflection is done with a second camera aimed at the top section of the screen, but I did it without a pixel perfect camera component. I thought the lack of a pixel perfect camera was what was causing problems, so due to this conversation here, I decided to change my resolution to 320 / 180 (which is half of 360p). : Pixel Perfect Camera keeps moving and changing size? Bug? - #7 by spiney199
I have a second camera with pixel perfect camera set up at a resolution of 320 x 150, as that is how much of the top half of the screen I want to capture, but that does this weird flickering business, where it looks like the pixels on the render texture are moving back and forth.
At first I thought it could be because the second camera was not in the exact right spot, but even if I set it to 320 x 180, and at 0,0, exactly how I have my main camera setup, it still distorts. Because the main camera is fine, I do not think it’s an issue with how my camera or sprites or resolution is set up, but specifically seems to be how I’m rendering things to the render texture.
I think the remaining issue is your render texture size doesn’t match your screen output, so you’re still going to get non-whole number pixels across the x axis.
It should match your target output pixel-perfect reference resolution, and as you also determined, both the main camera and render texture camera should have matching output resolutions/size.
Pretty sure you dont need a pixel perfect camera on your secondary camera. Your secondary camera is just used for creating a texture that’ll be rendered by your main, pixel perfect camera
Make sure your output texture’s resolution and gameobject scale is scaled based on the current resolution. For example, if your target resolution is 2x reference resolution, you’ll need a texture that is scaled 2x from the base resolution. Otherwise, you’ll lose data. To explain it better imagine you don’t need a secondary camera; your main camera would render the reflections at 2x the base resolution. When using a secondary camera + render texture with 1x base resolution, you’d lose the pixels. Not sure if I could explain it well
It could also be your secondary camera’s pixel perfect settings. Try making the secondary camera’s reference resolution to 320x180, and then disable the pixel perfect component(or dont, up to you)
Oh, also make sure the reflection camera is rendered before the main cam
Cheers
PS: if you need to read up on some pixel perfect setup with multiple cameras, you can check this github repo’s “setup and some notes” section(point 5,13,15) for an unrelated technique
Unfortunately I’ve tried having the resolution of the 2nd camera, and the render texture, the same as the main camera, but it doesn’t fix it.
One other thought I did have was, the water sprite itself may need to be a more exact size to work. So I tried making the water 320 x 150, as well as the 2nd camera and render texture, but that didn’t work.
I then tried making them all 320 x 180, but that didn’t work. So like here:
The green square is the second camera, which is 320 x 180, same as the main camera. The render texture still goes weird. The other problem with doing it like this, is the 2nd camera is also rendering the water that I do not want to reflect, so I assume there must be a better solution, but I tried to work around it by sliding the water up so that the extra “water” part is behind the sky via layer ordering, like this:
But again, that didn’t work either. And I have to move the water up or it would look like this:
The only other option is to move the 2nd camera up, so that the bottom edge stops where the water line starts, and that gives me this:
But that doesn’t really fix it either. All of these I believe are as close to perfectly pixel aligned as I can get.
Sadly, taking the pixel perfect camera off doesn’t fix it. I’m not sure what you mean by “target resolution”. My reference resolution is 320 x 180 on both cameras.
My aspect ratio that I’ve got for the game in the “Game Window” is 1920 x 1080. Not sure if you are referring to another area? Afaik it should just stretch the game to fit the screen.
But currently I have all of the graphics and cameras designed around the idea of working at 320 x 180, then letting Unity scale it up at runtime.
The current game view resolution, 1080p, 1440p etc.
It used to be 320x150 before you tried this though
Not really sure where the problem lies at this point but let me try to make a step by step guide for pixel perfect secondary cameras:
Create a new camera(C2) as a child of the main camera(C1)
You can offset the C2’s position but you need to make sure the offset is pixel perfect(the offset may even need to be pixel perfect based on reference resolution)
3 C2 will only render the layer of reflections(L2); C1 will render every other layer than L2.
C2 will render to a render texture(RT) that has width/height same as the target resolution
You can increase or decrease the orthographic size of C2, just make sure you end up having an integer resolution, and multiply the size of the RT
Create a new quad as a child of C2, with it’s gameobject Y scale as the orthographic size of C2 * 2. Set the quad’s x scale as Yscale * aspect ratio(for 1080p, it’d be Yscale * 16 / 9). This quad will have a sprite unlit material with the “sprite texture” set as RT
Set C2’s priority to be less than C1’s(so it updates the RT before C1 renders it, otherwise RT will be 1 frame behind)
C2 doesn’t need pixel perfect component, C1 needs it(to add bars etc.) C2 only needs to have a correct orthographic size
Make sure the render texture’s format is R8G8B8A8_UNORM(or something similar) and filter mode is
“Point”
Also, just try to make a simpler secondary camera pixel perfect setup before trying to invert for the reflections. That’d just reduce the chance of errors
Thanks for your suggestions, I will try these solutions shortly, however as a quick experiment, I tried the following:
Turning “point filter” off on the render texture
Turning “pixel perfect camera” off on both cameras
I figured, this should make it clear if it’s a rendering issue or to do with pixels distorting. Neither fixed the issue, so I’m suspecting it’s either how I set up the 2nd camera, or how I’m outputting to the render texture. It could even be how I have set up shader graph.