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