- Home /
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 :
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[i].pos;
if(i == 2){
test.pos = float4(mul(input[i].worldPosition,tm),1);
}
test.uv = input[i].uv;
OutputStream.Append(test);
}
}
Answer by Bunny83 · Jul 16, 2017 at 10:56 AM
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[i].vertex - center;
test.vertex = UnityObjectToClipPos(float4(mul(tm, pos) + center,1));
test.uv = input[i].uv;
OutputStream.Append(test);
}
}
when I do this o.worldPosition = float4(mul(v.worldPosition.xyz,tm),1);
nothing happens and o.pos= float4(mul(v.worldPosition.xyz,tm),1);
makes vertex rotate in a very weired way...
You have only provided tiny snippets of shader code. Also you haven't actually said what you want to achieve. ("rotating around an axis" is way too unspecific):
What kind of shader do you program? Is this in a surface shader or a traditional vertex shader?
Where does point1 and point2 comes from? In which space are they defined? (local or world space?).
$$anonymous$$atrix multiplication in Unity is usually left-hand i.e. (
mul(tm, v.worldPosition.xyz)
) though it depends on your matrix layout. I haven't checked yours.
its a geometry shader. two points are two vertex of a triangle and I want to rotate the other vertex around the axis they make.see this
I've edited my answer and included an example. If you want to rotate each triangle around one of it's edges you can do:
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;
float3 u = normalize(input[1].vertex - input[0].vertex);
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)
);
normal = mul(tm, normal);
for(int i = 0; i < 3; i++)
{
test.normal = mul(unity_ObjectToWorld,normal);
float3 pos = input[i].vertex - center;
test.vertex = UnityObjectToClipPos(float4(mul(tm, pos) + center,1));
test.uv = input[i].uv;
OutputStream.Append(test);
}
}
This will rotate around the first vertex and uses the axis from the first to the second vertex. It also rotates the local normal vector in order to get proper lighting. However the example didn't have any lighting besides a hardcoded directional light.
thanks,i just learned about shaders yesterday. lighting gonna be there soon.
Your answer
Follow this Question
Related Questions
Getting the forward position of an object in a vertex shader 0 Answers
Stop Billboard Texture Rotation in Triplanar Shader 0 Answers
How to rotate a triangle created in a geometry shader around its center 1 Answer
Efficient way to keep a fixed rotation for sprite? 0 Answers
Locking rotation makes object spin 0 Answers