Pretty new to shaders so bear with me, messing with a fragment shader Image Effect that stretches the left and right side of the screen. Shader displaces fragments positively in the x axis based on an input textures red value and negatively by green.
However I seem to have a weird issue wherein the effect is flipped horizontally in editor, that is, it displaces positively in the green and negatively in the red. For example if I comment out one sides effect (for example positive displacement) then the editor will show the effect on the right side of the screen, whereas any build (WebGL, standalone, android etc) will show the effect on the left of screen. Naturally I can work around it by simply taking this into account, but I’d really like to understand what is happening. Does Unity Editor for some reason read textures right to left? Or does it read colour values GRBA? I can’t seem to find any information on horizontal flipping, only tons of vertical from variations between DX and OpenGL, which isn’t the case here because building to both behaves the same.
For what it’s worth the effect seems much cruder in any build than it does in editor, so any better ways of accomplishing this or information on how Unity handles shaders differently would be ideal.
Shader "Hidden/EdgeStretch"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_DisplacementTex("Displacement Texture", 2D) = "white" {}
_DisplacementValue("Displacement Coefficient", Range(0.0, 1.0)) = 1.0
}
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;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
sampler2D _DisplacementTex;
float _DisplacementValue;
v2f vert (appdata v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.uv;
if (_ProjectionParams.x < 0)
o.uv[1] = 1 - o.uv[1];
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//Grab Red Green values from displacement texture
fixed2 value = tex2D(_DisplacementTex, i.uv);
fixed2 modified = value / 3;
//Modify the co-ordinate and clamp it between 0 and 1
//Displace positively by red and negatively by green on X axis
i.uv[0] -= modified[0] * _DisplacementValue;
i.uv[0] += modified[1] * _DisplacementValue / 2;
i.uv = saturate(i.uv);
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}