- Home /
Question by
TigrisGames · Nov 24, 2020 at 06:37 PM ·
camerashadershadersimage effectsanti-aliasing
screen flipped upside down [image effect]
i'm using 2 image effects. when i enable both of them , the screen is flipped on y .i know it is about using #if UNITY_UV_STARTS_AT_TOP but i'm not an expret in shader. any help
shader 1 :
Shader "Chromatic Aberration"
{
Properties
{
[HideInInspector]
_MainTex ("Texture", 2D) = "white" {}
_amount ("Amount",Range(0.0,0.01)) = 0.005
}
Subshader
{
Pass
{
CGPROGRAM
#pragma vertex vertex_shader
#pragma fragment pixel_shader
#pragma target 3.0
sampler2D _MainTex;
float _amount;
float4 _RenderedScene_TexelSize;
struct structure
{
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
structure vertex_shader (float4 vertex:POSITION, float2 uv:TEXCOORD0)
{
structure vs;
vs.vertex = UnityObjectToClipPos (vertex);
vs.uv = uv;
return vs;
}
float4 pixel_shader (structure ps) : COLOR
{
float2 uv = ps.uv.xy;
float3 color;
color.r = tex2D( _MainTex, float2(uv.x+_amount,uv.y) ).r;
color.g = tex2D( _MainTex, uv ).g;
color.b = tex2D( _MainTex, float2(uv.x-_amount,uv.y) ).b;
color *= (1.0 - _amount* 0.5);
return float4(color,1.0);
}
ENDCG
}
}
}
shader 2 :
Shader "The Developer/SS Texture Based Outline"
{
Properties
{
_OutlineColor("Outline Color", Color) = (1,1,1,1)
_OutlineThreshold("Outline Threshold", Float) = 3
_OutlineWidth("Outline Width", Float) = 2
_EdgeStrengthen ("Edge Strengthen", Float) = 2
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
// No culling or depth
// Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct v2f
{
float4 position : POSITION;
float4 screenPos : TEXCOORD0;
};
fixed4 _OutlineColor;
half _OutlineThreshold;
half _OutlineWidth;
half _EdgeStrengthen;
sampler2D _CameraDepthTexture;
sampler2D _MainTex;
float4 _RenderedScene_TexelSize;
v2f vert(appdata input)
{
v2f output;
output.position = UnityObjectToClipPos(input.vertex);
output.screenPos = output.position;
return output;
}
#define PSL01UVC(tex, uv1, uv2) pow(\
(tex2D(tex, uv1) - tex2D(tex, uv2)) * _EdgeStrengthen, \
2)
float4 outline;
half4 pixel;
half2 uv;
half onePixelW, onePixelH;
fixed i;
void OutlinePixel(sampler2D tex){
outline = 0;
for(i = 1 ; i <= _OutlineWidth ; ++i)
outline +=
PSL01UVC(tex, half2(uv.x - i * onePixelW, uv.y),
half2(uv.x + i * onePixelW, uv.y)) +
PSL01UVC(tex, half2(uv.x, uv.y + i * onePixelH),
half2(uv.x, uv.y - i * onePixelH)) +
PSL01UVC(tex, half2(uv.x - i * onePixelW, uv.y - i * onePixelH),
half2(uv.x + i * onePixelW, uv.y + i * onePixelH)) +
PSL01UVC(tex, half2(uv.x - i * onePixelW, uv.y + i * onePixelH),
half2(uv.x + i * onePixelW, uv.y - i * onePixelH));
}
half4 frag(v2f input) : SV_Target
{
uv = input.screenPos.xy / input.screenPos.w;
uv.x = (uv.x + 1) * .5;
uv.y = (uv.y + 1) * .5;
pixel = tex2D(_MainTex, uv);
onePixelW = 1.0 / _ScreenParams.x;
onePixelH = 1.0 / _ScreenParams.y;
OutlinePixel(_MainTex);
#if UNITY_UV_STARTS_AT_TOP
if (_RenderedScene_TexelSize.y < 0) //this is returning false even when the image is upside down!
{
uv.y = 1 - uv.y;
}
#endif
return lerp(pixel, _OutlineColor, (outline.r + outline.g + outline.b) >= _OutlineThreshold ? 1 : 0);
}
ENDCG
}
}
}
Comment