RenderTexture upside down

I mapped a RenderTexture onto a quad and it appears flipped vertically.
Should be kind of expected since I’m using DX: Unity - Manual: Writing shaders for different graphics APIs

There the example shader code that should fix it.

#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
        uv.y = 1-uv.y;
#endif

First off, can I read the UNITY_UV_STARTS_AT_TOP or a similar value from script?

Seconds, when I put the code into my vertex shader, I get “Shader error in ‘CornerGeometry’: undeclared identifier ‘_MainTex_TexelSize’ at line 70 (on d3d9)”.

The entire shader:

Shader "CornerGeometry" {

    Properties {
    _Color ("Main Color", Color) = (1,1,1,1)
    _MainTex ("Base (RGB) Trans (A)", 2D) = "white" { }
    }

    SubShader { 

    Tags { "QUEUE"="Transparent" "IGNOREPROJECTOR"="true" "RenderType"="Transparent" }

    Pass {
     
        ZWrite On
        ZTest Less
        ColorMask 0
   
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag

        // vertex shader
        float4 vert (float4 vpos : POSITION) : SV_POSITION {
       
         return mul(UNITY_MATRIX_MVP, vpos);
        }

        // fragment shader
        fixed4 frag (void) : SV_Target {
         
         return fixed4(0.0, 1.0, 0.0, 1.0);
        }
       
        ENDCG
    }
     
    Pass {
   
        ZWrite Off
        ZTest Equal
        ColorMask RGBA
        Blend SrcAlpha OneMinusSrcAlpha
   
        CGPROGRAM
        #pragma vertex vert
        #pragma fragment frag
//        #include "UnityShaderVariables.cginc"

        // uniforms
        float4 _MainTex_ST;

        // vertex shader input data
        struct appdata {
         float3 pos : POSITION;
         half4 color : COLOR;
         float3 uv0 : TEXCOORD0;
        };

        // vertex-to-fragment interpolators
        struct v2f {
         fixed4 color : COLOR0;
         float2 uv0 : TEXCOORD0;
         float4 pos : SV_POSITION;
        };

        // vertex shader
        v2f vert (appdata IN) {
         
         #if UNITY_UV_STARTS_AT_TOP
         if (_MainTex_TexelSize.y < 0)
               In.uv0.y = 1 - IN.uv0.y;
         #endif
         
         v2f o;
         o.color = saturate(IN.color);
         // compute texture coordinates
         o.uv0 = IN.uv0.xy * _MainTex_ST.xy + _MainTex_ST.zw;
         // transform position
         o.pos = mul(UNITY_MATRIX_MVP, float4(IN.pos,1));
         
         return o;
        }

        // textures
        sampler2D _MainTex;
        fixed4 _Color;

        // fragment shader
        fixed4 frag (v2f IN) : SV_Target {
       
         fixed4 col;
         fixed4 tex;
         
         // SetTexture #0
         tex = tex2D (_MainTex, IN.uv0.xy);
         col.rgb = tex * IN.color * _Color;
         col.a = tex.a * IN.color.a * _Color.a;
         col *= 2;
         
         // alpha test
         if (col.a <= 0) clip(-1);
         
         return col;
        }

        ENDCG
    }
    }
}

What to do? Thanks

Not as far as I know. This is a preprocessor define, so unlike a regular if, the code actually gets complete removed if the platform in question does not have UNITY_UV_STARTS_AT_TOP defined.

Well, it’s not declared, so the compiler is right. Add this before the vertex shader:

float2 _MainTex_TexelSize;

Thanks. Have declared float2 _MainTex_TexelSize; and I compile.

But it renders the same, probably because the UNITY_UV_STARTS_AT_TOP is either not defined or gives a wrong value. I guess it’s not defined, and I tested whether that condition says true or false-or-undefined empirically, in other part of the code.
I included

 #include "UnityShaderVariables.cginc"
        #include "UnityCG.cginc"

since saw it in some other shader code, but that made no difference.

What if you skip this line?

if (_MainTex_TexelSize.y < 0)

I’ve never used that line myself and just directly flipped based on the definition of UNITY_UV_STARTS_AT_TOP.

1 Like

Yup, I was myself wondering why the “if (_MainTex_TexelSize.y < 0)” condition there, but uncertain. I removed it and now that works. Thanks a lot. Not that it would make me entirely calm as to whether or not to use it;)
UNITY_UV_STARTS_AT_TOP says true here on d3d9, I made a mistake in my test yesterday though, sorry.
As to reading a similar value from script, I think I can read SystemInfo.graphicsDeviceType, for enough.

1 Like

Hey guys. So I was able to fix my issue using your suggestions.
However, I have a second problem - when I take a render texture screenshot of the camera, I get the inverted texture again.

Any ideas?

All of the above stuff is to correct for sampling from a texture that’s upside down in a shader. If you take a texture that’s upside down and copy it to the CPU using ReadPixels, you’re getting the upside down version. You can either flip it in C# code, or you can try blitting it to another render texture before using ReadPixels on that.