Shader "Coustom/XYZShader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_MoveOffset("MoveOffset",vector)=(1,1,1,1)
_Scale("Scale",vector)=(1,1,1,1)
_Rotate("RotateAngle",vector) = (0,0,0,1)
}
SubShader
{
Blend SrcAlpha OneMinusSrcAlpha
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;
float4 _MainTex_ST;
float4 _MoveOffset;
float4 _Scale;
float4 _Rotate;
//顺序:先缩放,后旋转(z->x->y),后平移
v2f vert (appdata v)
{
v2f o;
float z_theta = UNITY_PI*_Rotate.z/180;
float x_theta = UNITY_PI*_Rotate.x/180;
float y_theta = UNITY_PI*_Rotate.y/180;
//平移矩阵
float4x4 mov_mat = {
1,0,0,_MoveOffset.x,
0,1,0,_MoveOffset.y,
0,0,1,_MoveOffset.z,
0,0,0,1
};
//缩放矩阵
float4x4 scal_mat={
_Scale.x,0,0,0,
0,_Scale.y,0,0,
0,0,_Scale.z,0,
0,0,0,1
};
//旋转z轴
float4x4 z_rot_mat = {
cos(z_theta),-sin(z_theta),0,0,
sin(z_theta),cos(z_theta),0,0,
0,0,1,0,
0,0,0,1
};
//旋转x轴
float4x4 x_rot_mat={
1,0,0,0,
0,cos(x_theta),-sin(x_theta),0,
0,sin(x_theta),cos(x_theta),0,
0,0,0,1
};
//旋转y轴
float4x4 y_rot_mat={
cos(y_theta),0,sin(y_theta),0,
0,1,0,0,
-sin(y_theta),0,cos(y_theta),0,
0,0,0,1
};
//进行z轴旋转
float4 world_pos = mul(z_rot_mat,v.vertex);
//进行x轴旋转
world_pos = mul(x_rot_mat,world_pos);
//进行y轴旋转
world_pos = mul(y_rot_mat,world_pos);
//进行缩放
world_pos = mul(scal_mat,world_pos);
//进行平移
world_pos = mul(mov_mat,world_pos);
//最后输出
o.vertex = mul(UNITY_MATRIX_VP,world_pos);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
// sample the texture
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}