I am splitting a quad made of two triangles into 2^n triangles using a tessellation shader where n is how many triangles are generated from each of the two triangles(Currently set to 3). In the geometry shader, I want to take the new triangles and rotate them in place. Even when I offset them based on the center, they still seem to be rotating around a point that isn’t their center. The triangles closer to the origin, however, seem to be messed up a lot less.
This is just a few seconds into the rotation. As you can see, the triangles near the center are a lot less offset than the ones farther away. Here is the geometry shader code. The uv1 stores the vertex position in object space. t is a uniform that represents the timestep of the rotation.
float4 worldOne = mul(unity_ObjectToWorld, g0.data.uv1);
float4 worldTwo = mul(unity_ObjectToWorld, g1.data.uv1);
float4 worldThree = mul(unity_ObjectToWorld, g2.data.uv1);
float3 u = normalize(cross(worldTwo - worldOne, worldThree - worldOne));
float center = (worldOne + worldTwo + worldThree) / 3;
float3 pos1 = worldOne - center;
worldOne = float4(rotatePosition(u, t * .25, pos1) + center,1);
float3 pos2 = worldTwo - center;
worldTwo = float4(rotatePosition(u, t * .25, pos2) + center,1);
float3 pos3 = worldThree - center;
worldThree = float4(rotatePosition(u, t * .25, pos3) + center,1);
float3 objectOne = mul(unity_WorldToObject, worldOne);
float3 objectTwo = mul(unity_WorldToObject, worldTwo);
float3 objectThree = mul(unity_WorldToObject, worldThree);
g0.data.vertex = UnityObjectToClipPos(objectOne);
g1.data.vertex = UnityObjectToClipPos(objectTwo);
g2.data.vertex = UnityObjectToClipPos(objectThree);
and the helper function:
float3 rotatePosition(float3 axis, float angle, float3 p)
{
float3 n = axis; // the axis to rotate about
// Specify the rotation transformation matrix:
float3x3 m = float3x3(
n.x*n.x * (1.0f - cos(angle)) + cos(angle), // column 1 of row 1
n.x*n.y * (1.0f - cos(angle)) + n.z * sin(angle), // column 2 of row 1
n.x*n.z * (1.0f - cos(angle)) - n.y * sin(angle), // column 3 of row 1
n.y*n.x * (1.0f - cos(angle)) - n.z * sin(angle), // column 1 of row 2
n.y*n.y * (1.0f - cos(angle)) + cos(angle), // ...
n.y*n.z * (1.0f - cos(angle)) + n.x * sin(angle), // ...
n.z*n.x * (1.0f - cos(angle)) + n.y * sin(angle), // column 1 of row 3
n.z*n.y * (1.0f - cos(angle)) - n.x * sin(angle), // ...
n.z*n.z * (1.0f - cos(angle)) + cos(angle) // ...
);
// Apply the rotation to our 3D position:
float3 q = mul(m,p);
return q;
}
What am I doing wrong/missing?