Skip to content

Instantly share code, notes, and snippets.

@nir1082
Created March 31, 2019 13:06
Show Gist options
  • Save nir1082/e3558ae65c6a51599099d69a37e7ad97 to your computer and use it in GitHub Desktop.
Save nir1082/e3558ae65c6a51599099d69a37e7ad97 to your computer and use it in GitHub Desktop.
[Unity] 画像を移動・拡縮・回転するシェーダー
Shader "Unlit/AffineTransform"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_PositionX ("Position X", float) = 0.0
_PositionY ("Position Y", float) = 0.0
_ScaleX ("Scale X", float) = 1.0
_ScaleY ("Scale Y", float) = 1.0
_Rotate ("Rotate", float) = 0.0
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
LOD 100
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
#pragma multi_compile_fog
#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;
float _PositionX;
float _PositionY;
float _ScaleX;
float _ScaleY;
float _Rotate;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
float2 pivot = float2 (0.5, 0.5);
float3 uv = float3 (o.uv.x, o.uv.y, 1);
_Rotate = _Rotate * 0.0174533;
float3x3 posisionMatrix = {
1, 0, 0,
0, 1, 0,
-_PositionX, -_PositionY, 1
};
float3x3 scaleMatrix = {
1/_ScaleX, 0, 0,
0, 1/_ScaleY, 0,
0, 0, 1
};
float3x3 rotateMatrix = {
cos (_Rotate), sin (_Rotate), 0,
-sin (_Rotate), cos (_Rotate), 0,
0, 0, 1
};
float3 mulUV = mul (mul (mul (uv, posisionMatrix) - pivot, rotateMatrix), scaleMatrix);
o.uv = float2 (mulUV.x, mulUV.y) + pivot;
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
// apply fog
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment