Maybe this is how they’re meant to work, but the Unity docs don’t explicitly state so, so I’m wondering maybe I’m missing something.
I’ve been using a Camera Pixelate script I found online for a while on my Weapon Camera for my FPS game. It’s been working great, but now that I’ve started to add UI elements I’ve noticed that camera renders the weapon over everything - including the HUD elements. I’ve played with the depth and sort order to no avail, so I’m wondering if it’s just a genuine limitation, and if so what a better route would be.
The script originally used GUI.DrawTexture, however Googling suggested to use Graphics.DrawTexture instead. This has not made a difference.
CameraPixelate script:
public class WeaponCameraTest : MonoBehaviour
{
public RenderTexture renderTexture;
void Start()
{
int realRatio = Mathf.RoundToInt(Screen.width / Screen.height);
renderTexture.width = NearestSuperiorPowerOf2(Mathf.RoundToInt(renderTexture.width * realRatio));
Debug.Log("(Pixelation)(Start)renderTexture.width: " + renderTexture.width);
}
void OnGUI()
{
GUI.depth = 0;
Graphics.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), renderTexture);
}
int NearestSuperiorPowerOf2(int n)
{
return (int)Mathf.Pow(2, Mathf.Ceil(Mathf.Log(n) / Mathf.Log(2)));
}
}
I thought the GUI.depth might’ve been the problem, but I’ve changed that to no avail. I also tried setting renderTexture.depth through code which doesn’t appear to work.
And this is the Render Texture itself, although I’ve played with all the settings and observed no difference:
Maybe it’s just my misunderstanding of how Render Textures work - if so is there a better solution I should be looking at? The purpose of this script is just to pixelate the weapon camera (e.g. the weapon being held in an FPS), but not the main camera.