Skip to content

Instantly share code, notes, and snippets.

@kaiware007
Created April 3, 2018 07:22
Show Gist options
  • Save kaiware007/d1ecb39abfdd2e3a70c0514d12e7d838 to your computer and use it in GitHub Desktop.
Save kaiware007/d1ecb39abfdd2e3a70c0514d12e7d838 to your computer and use it in GitHub Desktop.
UVを回転&上下左右反転できるシェーダーのサンプル UV top / bottom / left / right Flipping & 90 Degree Rotation Shader sample
Shader "Unlit/Flip Rotation"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
[KeywordEnum(ANGLE0, ANGLE90, ANGLE180, ANGLE270)] _ROTATEFLAG("Rotation", Float) = 0
[Toggle] _FLIP_X("Flip X", Float) = 0
[Toggle] _FLIP_Y("Flip Y", Float) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _ROTATEFLAG_ANGLE0 _ROTATEFLAG_ANGLE90 _ROTATEFLAG_ANGLE180 _ROTATEFLAG_ANGLE270
#pragma shader_feature _ _FLIP_X_ON
#pragma shader_feature _ _FLIP_Y_ON
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
float2 uv = i.uv;
// Flip
#ifdef _FLIP_X_ON
uv.x = 1.0 - uv.x;
#endif
#ifdef _FLIP_Y_ON
uv.y = 1.0 - uv.y;
#endif
// Rotation
float2 uv2 = uv;
#ifdef _ROTATEFLAG_ANGLE90
uv2.x = uv.y;
uv2.y = 1.0 - uv.x;
#elif _ROTATEFLAG_ANGLE180
uv2 = 1.0 - uv;
#elif _ROTATEFLAG_ANGLE270
uv2.x = 1.0 - uv.y;
uv2.y = uv.x;
#endif
fixed4 col = tex2D(_MainTex, uv2);
return col;
}
ENDCG
}
}
}
@kaiware007
Copy link
Author

【注意】スクリプトから変更する場合、material.EnableKeyword/DisableKeywordでオンオフする必要がある
ex) material.EnableKeyword("_ROTATEFLAG_ANGLE0");

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment