【HLSL】回転行列【串刺し】
2020年2月22日
すぐ忘れるのでメモ
串を刺して回すイメージだ
2×2行列
float2x2 MakeRotation( float angle)
{
float cs = cos( angle);
float sn = sin( angle);
return float2x2(
cs, -sn,
sn, cs
);
}
3×3行列
float3x3 MakeRotation( float3 axis, float angle)
{
float cs = cos( angle);
float sn = sin( angle);
float oneMinusCos = 1.0 - cs;
float3 squared = axis*axis;
float xym = axis.x*axis.y*oneMinusCos;
float xzm = axis.x*axis.z*oneMinusCos;
float yzm = axis.y*axis.z*oneMinusCos;
float3 xyzSin = axis * sn;
return float3x3(
squared.x * oneMinusCos + cs, xym - xyzSin.z, xzm + xyzSin.y,
xym + xyzSin.z, squared.y * oneMinusCos + cs, yzm - xyzSin.x,
xzm - xyzSin.y, yzm + xyzSin.x, squared.z * oneMinusCos + cs
);
}
4×4行列
float4x4 MakeRotation( float4 axis, float angle)
{
float cs = cos( angle);
float sn = sin( angle);
float oneMinusCos = 1.0 - cs;
float3 squared = axis*axis;
float xym = axis.x*axis.y*oneMinusCos;
float xzm = axis.x*axis.z*oneMinusCos;
float yzm = axis.y*axis.z*oneMinusCos;
float3 xyzSin = axis * sn;
return float4x4(
squared.x * oneMinusCos + cs, xym - xyzSin.x, xzm + xyzSin.y, 0,
xym + xyzSin.z, squared.y * oneMinusCos + cs, yzm - xyzSin.x, 0,
xzm - xyzSin.y, yzm + xyzSin.x, squared.z * oneMinusCos + cs, 0,
0, 0, 0, 1
);
}