Blitting a texture to the centre of another texture?

Hi,

I have a rectangle mesh as a gameObject and I want to show a texture right in the middle of it, scaled as necessary with respect to its aspect ratio, but not cropped.

The way I started doing it is to create a new RenderTexture with an aspect ratio of my rectangle, and then I got tangled up in how to use the scale and offset arguments to Graphics.Blit() so the source texture (which can be of literally any dimensions and aspect ratio as it’s supplied by the app user) is blitted in its centre.

I can’t seem to wrap my mind around how scale and offset work.

Have you looked into approaches like decals?

Not really… would decals auto-scale in all cases (I’m interested in having the texture shown completely, even if it has an aspect ratio completely different from that of the rectangle mesh)?

It really depends on the decal implementation.

There is a unity decal shader: https://docs.unity3d.com/Manual/shader-NormalDecal.html but you might have to play with the tiling/offset values in a script to get it to fit properly to your scaled rectangle.

There’s also the HDRP Decal projector which works completely differently: https://docs.unity3d.com/Packages/com.unity.render-pipelines.high-definition@7.2/manual/Decal-Projector.html

Ok, I think I found a missing piece of the puzzle: according to some other answers on the forums, Graphics.Blit() always covers the entire destination texture… while I need something which will take a source texture, and scale it and blit it to only a rectangular area within the destination texture.

Is that correct? Is there such an API?