I tried everything from unity manual and from google, this is what I get (or vice versa) no matter what I do…
I created a little custom RP, first it renders opaque geometry, then a skybox, and finally post fx shader takes color + depth textures and inverts the green channel on the background.
The problem is in this shader. When I pass uv’s to fragment program and use SAMPLE_TEXTURE2D, counter-flip works in both views. However, when I switch to position approach and LOAD_TEXTURE2D instead, unity always flips the game view, regardless of what I do with positionCS in vertex program.
All tutorials say:
#if UNITY_UV_STARTS_AT_TOP
pos.y = -pos.y;
#endif
But in my case it doesn’t do anything, although the condition is always true. _ProjectionParams.x and _TexelSize.y are always positive as well. What am I doing wrong?
Shader code:
Shader "Hidden/FxComposition"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
HLSLPROGRAM
#pragma target 3.5
#pragma vertex Vert
#pragma fragment Frag
#include "Assets/Custom RP/Shader Library/Common.hlsl"
TEXTURE2D(_MainTex);
TEXTURE2D(_DepthTex);
SAMPLER(sampler_point_clamp);
struct Attributes
{
float3 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
Varyings Vert(Attributes input)
{
Varyings output;
output.positionCS = TransformObjectToHClip(input.positionOS);
output.uv = input.uv;
#if UNITY_UV_STARTS_AT_TOP
output.positionCS.y *= -1.0;
output.uv.y = 1.0 - output.uv.y;
#endif
return output;
}
float4 Frag(Varyings input) : SV_Target
{
//Works as intended
//float4 color = SAMPLE_TEXTURE2D(_MainTex, sampler_point_clamp, input.uv);
//float depth = SAMPLE_DEPTH_TEXTURE(_DepthTex, sampler_point_clamp, input.uv);
//Upside down in game view
int2 screenPos = (int2)input.positionCS.xy;
float4 color = LOAD_TEXTURE2D(_MainTex, screenPos);
float depth = LOAD_TEXTURE2D(_DepthTex, screenPos).r;
return depth > 0 ? color : float4(color.r, 1 - color.g, color.ba);
}
ENDHLSL
}
}
}