Created
April 3, 2018 07:22
-
-
Save kaiware007/d1ecb39abfdd2e3a70c0514d12e7d838 to your computer and use it in GitHub Desktop.
UVを回転&上下左右反転できるシェーダーのサンプル UV top / bottom / left / right Flipping & 90 Degree Rotation Shader sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
【注意】スクリプトから変更する場合、material.EnableKeyword/DisableKeywordでオンオフする必要がある
ex) material.EnableKeyword("_ROTATEFLAG_ANGLE0");