# Rotate a vertex about "u" Axis

I made an axis and I want to rotate my vertex about it.

  float3 axis = point2 - point1;
float magnitude = sqrt(pow(axis.x,2)+pow(axis.y,2)+pow(axis.z,2));
float3 u = axis / magnitude;


from wikipedia :

![Rotation matrix from axis and angle][1]

so I did this:

float coss = _CosTime[1] * 0.1;
float sinn = _SinTime[1] * 0.1;
float3x3 tm = float3x3(float3(coss+pow(u.x,2) * (1 - coss),u.x*u.y*(1-coss)-u.z*sinn, u.x*u.z*(1-coss)+u.y*(1-sinn)),float3(u.y*u.x*(1-coss)-u.z*sinn,coss+pow(u.y,2) * (1 - coss),u.y*u.z*(1-coss)-u.x*sinn),float3(u.z*u.x*(1-coss)-u.y*sinn,u.z*u.y*(1-coss)-u.x*sinn,coss+pow(u.z,2) * (1 - coss)));
o.pos = float4(mul(v.vertex.xyz, tm),1);


this makes the object disapear…What am I doing wrong?

void geom(triangle v2f input[3], inout TriangleStream<v2f> OutputStream)
{
v2f test = (v2f)0;
float coss = cos(_Time[1] * _Amount);
float sinn = sin(_Time[1] * _Amount);
float3 axis = input[1].worldPosition.xyz - input[0].worldPosition.xyz;
float magnitude = sqrt(pow(axis.x,2)+pow(axis.y,2)+pow(axis.z,2));
float3 u = axis / magnitude;
float3x3 tm = float3x3(normalize(float3(coss+pow(u.x,2) * (1 - coss),u.x*u.y*(1-coss)-u.z*sinn, u.x*u.z*(1-coss)+u.y*(1-sinn))),normalize(float3(u.y*u.x*(1-coss)-u.z*sinn,coss+pow(u.y,2) * (1 - coss),u.y*u.z*(1-coss)-u.x*sinn)),normalize(float3(u.z*u.x*(1-coss)-u.y*sinn,u.z*u.y*(1-coss)-u.x*sinn,coss+pow(u.z,2) * (1 - coss))));
float3 normal = normalize(cross(input[1].worldPosition.xyz - input[0].worldPosition.xyz, input[2].worldPosition.xyz - input[0].worldPosition.xyz));
for(int i = 0; i < 3; i++)
{
test.normal = normal;

test.pos = input*.pos;*

•   		if(i == 2){*


_ test.pos = float4(mul(input*.worldPosition,tm),1);_
_
}_
_ test.uv = input.uv;
OutputStream.Append(test);
}
}_
[1]: {\displaystyle R={\begin{bmatrix}\cos \theta +u_{x}^{2}\left(1-\cos \theta \right)&u_{x}u_{y}\left(1-\cos \theta \right)-u_{z}\sin \theta &u_{x}u_{z}\left(1-\cos \theta \right)+u_{y}\sin \theta \\u_{y}u_{x}\left(1-\cos \theta \right)+u_{z}\sin \theta &\cos \theta +u_{y}^{2}\left(1-\cos \theta \right)&u_{y}u_{z}\left(1-\cos \theta \right)-u_{x}\sin \theta \\u_{z}u_{x}\left(1-\cos \theta \right)-u_{y}\sin \theta &u_{z}u_{y}\left(1-\cos \theta \right)+u_{x}\sin \theta &\cos \theta +u_{z}^{2}\left(1-\cos \theta \right)\end{bmatrix}}.}*

What you created there is just a rotation matrix. The rotation happens always around the origin. From your code it’s not clear in what space your “v.vertex.xyz” is. If it’s a position in worldspace you will rotate around the world origin. If it’s a position in local space you will rotate around the local origin.

If you want to rotate around a particular point you have to offset your position, do the rotation and translate it back.

Ps: do not use “pow” for simple powers. Instead of pow(u.x,2) you should use u.x*u.x.

edit

There are several things wrong in your shader. First of all you adapted the rotation matrix wrong. You had several +/- errors as well as a “(1-sin)” instead of just “sin”.

Next problem is _CosTime and _SinTime return the cosine / sine of the time. You can’t “scale” those. They need to be in the range -1 to 1, otherwise the whole rotation makes no sense. If you want to be able to scale the time you can’t use the precalculated values but have to calculate the sin and cos yourself. i.e. (sin(_Time[1] * _Amount)).

Next problem is that the geometry shader takes the output of the vertex shader. In the example “vertex” is already transformed into clipspace (2d screen space in range -1 to 1 on each axis). You may want to keep the output of the vertex shader in local space and perform the local to clipspace transform in the geometry shader.

Here i modifed the example to rotate each triangle around the triangle center point around the triangle normal vector. The rotation is carried out in local space.

        v2f vert (appdata v)
{
v2f o;
o.vertex = v.vertex;
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.normal = v.normal;
o.worldPosition = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}

[maxvertexcount(3)]
void geom(triangle v2f input[3], inout TriangleStream<v2f> OutputStream)
{
v2f test = (v2f)0;
float3 normal = normalize(cross(input[1].vertex.xyz - input[0].vertex.xyz, input[2].vertex.xyz - input[0].vertex.xyz));
float3 center = (input[0].vertex + input[1].vertex + input[2].vertex)/3;
float3 u = normal;
float c = _CosTime[2];
float s = _SinTime[2];
float cr = 1 - c;
float sr = 1 - s;
float3x3 tm = float3x3(
float3(c+u.x*u.x * cr,     u.x*u.y*cr-u.z*s,  u.x*u.z*cr+u.y*s),
float3(u.y*u.x*cr+u.z*s,   c+u.y*u.y * cr,    u.y*u.z*cr-u.x*s),
float3(u.z*u.x*cr-u.y*s,   u.z*u.y*cr+u.x*s,  c+u.z * u.z * cr)
);
for(int i = 0; i < 3; i++)
{
test.normal = mul(unity_ObjectToWorld,normal);
float3 pos = input*.vertex - center;*


test.vertex = UnityObjectToClipPos(float4(mul(tm, pos) + center,1));
test.uv = input*.uv;*
OutputStream.Append(test);
}
}