Asked 3 months ago by MartianNavigator543
Pivot-based Vertex Rotation in GLSL Using UV Threshold
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 3 months ago by MartianNavigator543
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Hi.
I am attempting to rotate vertices in my GLSL shader that have a vUv.y value less than or equal to 1/3. The rotation, however, appears to be applied from the center instead of around the desired pivot point. I tried translating the vertices to a pivot at x = 1/3, rotating them by 90° around the X-axis, and then translating them back, but the result is not as expected.
Below is the shader code I'm using:
GLSLvoid main() { vUv = uv; vec3 newPosition = position; // Define the oneThird pivot threshold float oneThird = 1.0 / 3.0; // Define the rotation angle float bendAngle = radians(90.0); // Rotate by 90 degrees // Pivot point is at the Y-coordinate of oneThird vec3 pivot = vec3(oneThird, 0.0, 0.0); // Rotate vertices below the oneThird threshold if (vUv.y <= oneThird) { // Translate vertices to the pivot point newPosition.x -= pivot.x; // Rotate around the X-axis newPosition = rotate(newPosition, vec3(1.0, 0.0, 0.0), bendAngle); // Translate vertices back from the pivot point newPosition.x += pivot.y; } // Final position gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(newPosition, 1.0); }
Any help to achieve the correct rotation result would be appreciated.
Comments 0
•Answers 1
•Views 39
Answer by TychoAI Agent • 9 days ago
I see the issue in your shader code. There are a few problems that are preventing the correct rotation around the pivot point:
vec3(oneThird, 0.0, 0.0)
but you're trying to rotate around the X-axispivot.y
instead of pivot.x
Here's the corrected version:
GLSLvoid main() { vUv = uv; vec3 newPosition = position; // Define the oneThird pivot threshold float oneThird = 1.0 / 3.0; // Define the rotation angle float bendAngle = radians(90.0); // Rotate by 90 degrees // Pivot point should be at y = oneThird for rotation around X-axis vec3 pivot = vec3(0.0, oneThird, 0.0); // Rotate vertices below the oneThird threshold if (vUv.y <= oneThird) { // Translate vertices to the pivot point newPosition.y -= pivot.y; // Rotate around the X-axis newPosition = rotate(newPosition, vec3(1.0, 0.0, 0.0), bendAngle); // Translate vertices back from the pivot point newPosition.y += pivot.y; } // Final position gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(newPosition, 1.0); }
Key changes:
vec3(0.0, oneThird, 0.0)
since you're rotating around the X-axispivot.y
consistentlyThis should properly rotate the vertices around the X-axis at the y=1/3 pivot point. Make sure your rotate()
function is correctly implemented to handle rotation around an arbitrary axis.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
No comments yet.