Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mirror anchor offset when flipping is active #1600

Merged
merged 1 commit into from
Nov 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions assets/shaders/world2d.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,14 @@ void main() {
// this is the position where we want to draw the subtex in 2D
vec4 obj_clip_pos = proj * view * model * vec4(obj_world_position, 1.0);

// if the subtex is flipped, we also need to flip the anchor offset
// essentially, we invert the coordinates for the flipped axis
float anchor_x = float(flip_x) * -1.0 * anchor_offset.x + float(!flip_x) * anchor_offset.x;
float anchor_y = float(flip_y) * -1.0 * anchor_offset.y + float(!flip_y) * anchor_offset.y;

// offset the clip position by the offset of the subtex anchor
// this basically aligns the object position and the subtex anchor
obj_clip_pos += vec4(anchor_offset.xy, 0.0, 0.0);
// imagine this as pinning the subtex to the object position at the subtex anchor point
obj_clip_pos += vec4(anchor_x, anchor_y, 0.0, 0.0);

// create a move matrix for positioning the vertices
// uses the scale and the transformed object position in clip space
Expand All @@ -51,9 +56,15 @@ void main() {
0.0, 0.0, 1.0, 0.0,
obj_clip_pos.x, obj_clip_pos.y, obj_clip_pos.z, 1.0);

// finally calculate the vertex position
// calculate the final vertex position
gl_Position = move * vec4(v_position, 0.0, 1.0);

// if the subtex is flipped, we also need to flip the uv tex coordinates
// essentially, we invert the coordinates for the flipped axis

// !flip_x is default because OpenGL uses bottom-left as its origin
float uv_x = float(!flip_x) * uv.x + float(flip_x) * (1.0 - uv.x);
float uv_y = float(flip_y) * uv.y + float(!flip_y) * (1.0 - uv.y);

vert_uv = vec2(uv_x, uv_y);
}