Adding a background to a camera view regardless the change of angle of view

Hi all,

may be the wrong section to ask, but i’m trying to add a background image to a viewer i’m trying to do.
basically i need to change the field of view of the camera and also moving it back and forth.
i’ve checked the old post and they use 2 basic distinct way, first is to create a sprite and parent it to the camera
work nice but when i dynamically change the camera field of view, the background image shrink or go bigger.

what i need is a typical flat render regardless the camera position or field of view

the best would be to be able to put an image in the background tab instead of just picking a color

any idea anyone ?

thanks by advance

One option could be to use 2 cameras.
The first one that renders only the background (could be orthographic) and a second one for all the rest.
You will have to specify the Depth property of the second camera to a bigger value of the first one to ensure that is rendered over the first one. Also the Clear flag should be set to Depth only to don’t clear the rendering of the first camera.

Skybox.

Does the Skybox support a simple texture that is not a “box”?

It can. If you use a shader that just draws the image based on screen position. A very simple version would be:

Shader "Skybox/Fixed Image Background" {
    Properties {
        _Color("  Color", color) = (1, 1, 1, 1)
        [NoScaleOffset] _MainTex ("  Map", 2D) = "white" {}
    }

    SubShader {
        Tags { "QUEUE"="Background"
               "RenderType"="Background"
             }
            
        LOD 200
        Cull Off ZWrite Off

        Pass {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"

            fixed4 _Color;
            sampler2D _MainTex;

            struct vertOut {
                float4 pos : SV_POSITION;
                float4 scrPos : TEXCOORD0;
            };

            vertOut vert(appdata_base v) {
                vertOut o;
                o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                o.scrPos = ComputeScreenPos(o.pos);
                return o;
            }

            fixed4 frag(vertOut i) : SV_Target {   
                float2 uv = (i.scrPos.xy / i.scrPos.w);
                return tex2D(_MainTex, uv) * _Color;
            }

            ENDCG
        }
    }
}

This will just pin it the screen. It will stretch to fill.

Interesting solution.

What I don’t like is that the skybox renders 1.7k triangles with 5k verts. And to use it just for a static background image is a waste of resources.

Verts/tris are very cheap. Though pretty small, the overall performance is a bit better, due to how a skybox renders, and no extra overhead from gameobjects make up the difference pretty quickly. Doing it via UI is a closer in overall performance, but multiple cameras is the most expensive over either. Ultimately, the hit in either way is still very small to trival, but a skybox has the advantage of being dead simple implement. :wink: All things being equal, simple wins.

1 Like

Yes is true. I was wandering if there could be a third way as a post render effect. But as you say skybox is a simple solution to implement and maintain.

How would you control the proportion of the image to avoid stretching of the texture when the screen ratio changes?

thanks you for both your answer and yes i need to maintain the image ratio.

Edit : image ration seem’s to be respected works really fin, thanks you for the tips !!