[Solved] Offsetting a texture position and overlaying it on camera with Graphics.Blit?

I am attempting to use OnRenderImage to overlay a texture over my camera.

I am able to overlay the image but I really need to be able to also offset the center of the image (move it left/right) by a specific amount without stretching.

This image shows what I have currently, and the image is stretched to fill the entire canvas (thus dimensions of image must match camera exactly otherwise it will look deformed).

The code example I am using is very simple. But I need to be able to slide that image around based on an offset?

public class PreRenderScript : MonoBehaviour {

    [SerializeField]
    public Sprite spriteOverlay = null;

    [SerializeField]
    Material mat;

    void OnRenderImage (RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit (source, destination);
        Graphics.Blit (spriteOverlay.texture, destination, mat); //How can I offset the position of the spriteOverlay?
    }

}

Could someone please help me figure out how to do this? Thanks!

Original Overlay Image:

I reviewed the documentation for Graphics.Blit again and I just came across Graphics.BlitMultiTap which has an offset parameter that you specify in pixels!

So now I have updated my script to this (which appears to be exactly what I needed):

public class PreRenderScript : MonoBehaviour {

    [SerializeField]
    Vector2 offset;

    [SerializeField]
    public Sprite spriteOverlay = null;

    [SerializeField]
    Material mat;

    void OnRenderImage (RenderTexture source, RenderTexture destination)
    {
        Graphics.Blit (source, destination);
        Graphics.BlitMultiTap (spriteOverlay.texture, destination, mat, offset);
    }

}
1 Like