旋转变换稍微有点复杂,略过推导过程还是先看基本公式
与缩放平移不同,对于三维空间下的物体,总要围绕其中一个轴来旋转,所以变换的其实是二维坐标,下面是点围绕z轴进行旋转
P1x是点P在旋转前的x坐标?
使用矩阵
围绕z轴旋转的二维旋转矩阵
可以在shader中使用这个矩阵实现旋转,但还不够,我们还是要用一个4x4矩阵
?围绕x轴旋转
?围绕y轴旋转
围绕z轴旋转
?
?最后,在shader中实现
Varyings vert(Attributes v)
{
Varyings o = (Varyings)0;
// 平移矩阵
float4x4 T = float4x4
(
1,0,0,_Translate.x,
0,1,0,_Translate.y,
0,0,1,_Translate.z,
0,0,0,1
);
v.positionOS = mul (T,v.positionOS);
// 缩放变换_X轴
float4x4 M_scale = float4x4
(
_Scale.x,0,0,0,
0,_Scale.y,0,0,
0,0,_Scale.z,0,
0,0,0,1
);
v.positionOS = mul(M_scale,v.positionOS);
// 旋转变换_X轴
float4x4 M_rotationX = float4x4
(
1,0,0,0,
0,cos(_Rotation.x),sin(_Rotation.x),0,
0,-sin(_Rotation.x),cos(_Rotation.x),0,
0,0,0,1
);
v.positionOS = mul(M_rotationX,v.positionOS);
// 旋转变换_Y轴
float4x4 M_rotationY = float4x4
(
cos(_Rotation.y),0,sin(_Rotation.y),0,
0,1,0,0,
-sin(_Rotation.y),0,cos(_Rotation.y),0,
0,0,0,1
);
v.positionOS = mul(M_rotationY,v.positionOS);
// 旋转变换_Z轴
float4x4 M_rotationZ = float4x4
(
cos(_Rotation.z),sin(_Rotation.z),0,0,
-sin(_Rotation.z),cos(_Rotation.z),0,0,
0,0,1,0,
0,0,0,1
);
v.positionOS = mul(M_rotationZ,v.positionOS);
o.positionCS = TransformObjectToHClip(v.positionOS);
o.uv = TRANSFORM_TEX(v.uv,_MainTex);
return o;
}
再把参数暴露出来
?资料来自taecg
|