实现图片像素翻转只要实现图片UI翻转即可,
主要代码如下:
float4 frag(v2f vert) : COLOR //COLOR是输出数据
{
float2 OffsetUV = vert.uv;
OffsetUV.x = OffsetUV.x * _RotateX;//x轴翻转
OffsetUV.y = OffsetUV.y * _RotateY;//y轴翻转
float4 texCol = tex2D(_MainTex, OffsetUV);
return texCol;
}
具体代码如下:
Shader "ImageRepeat"
{
Properties
{
_mainTexture("mainTexture", 2D) = "white" {}
_X("_X", Range( -1 , 1)) = -1
_Y("_Y", Range( -1 , 1)) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
CGINCLUDE
#pragma target 3.0
ENDCG
Blend Off
Cull Back
ColorMask RGBA
ZWrite On
ZTest LEqual
Offset 0 , 0
Pass
{
Name "Unlit"
CGPROGRAM
#ifndef UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX
//only defining to not throw compilation error over Unity 5.5
#define UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input)
#endif
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float4 color : COLOR;
UNITY_VERTEX_INPUT_INSTANCE_ID
float4 ase_texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
float4 ase_texcoord : TEXCOORD0;
};
uniform sampler2D _mainTexture;
uniform float4 _mainTexture_ST;
uniform float _X;
uniform float _Y;
v2f vert ( appdata v )
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
UNITY_TRANSFER_INSTANCE_ID(v, o);
o.ase_texcoord.xy = v.ase_texcoord.xy;
//setting value to unused interpolator channels and avoid initialization warnings
o.ase_texcoord.zw = 0;
float3 vertexValue = float3(0,0,0) ;
#if ASE_ABSOLUTE_VERTEX_POS
v.vertex.xyz = vertexValue;
#else
v.vertex.xyz += vertexValue;
#endif
o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}
fixed4 frag (v2f i ) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
fixed4 finalColor;
float2 uv0_mainTexture = i.ase_texcoord.xy * _mainTexture_ST.xy + _mainTexture_ST.zw;
float2 temp_cast_0 = (( ( uv0_mainTexture.x * _X ) + ( uv0_mainTexture.y * _Y ) )).xx;
finalColor = tex2D( _mainTexture, temp_cast_0 );
return finalColor;
}
ENDCG
}
}
}
实现效果如下: