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

Fix brush transforms #283

Merged
merged 7 commits into from
Feb 23, 2023
Merged

Fix brush transforms #283

merged 7 commits into from
Feb 23, 2023

Conversation

dfrg
Copy link
Collaborator

@dfrg dfrg commented Feb 23, 2023

This fixes an incorrect application of the inverse transform for radial gradients in fine.

Also fixes an edge case in SceneBuilder where supplying a brush transform that is identical to the path transform generates a corrupt encoding.

Discovery of both done by @DJMcNab on this zulip thread.

Should fix #281

This fixes an incorrect application of the inverse transform for radial gradients in fine.

Also fixes an edge case in `SceneBuilder` where a brush transform is identical to the path transform leading to a corrupt encoding.
@@ -120,10 +120,16 @@ impl Encoding {
}

/// Encodes a transform.
pub fn encode_transform(&mut self, transform: Transform) {
///
/// If the given transform is different from the current one, encodes it an
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

encodes it an -> encodes it and

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. Fixed, ty.

shader/fine.wgsl Outdated
@@ -251,7 +251,7 @@ fn main(
for (var i = 0u; i < PIXELS_PER_THREAD; i += 1u) {
let my_xy = vec2(xy.x + f32(i), xy.y);
// TODO: can hoist y, but for now stick to the GLSL version
let xy_xformed = rad.matrx.xz * my_xy.x + rad.matrx.yw * my_xy.y - rad.xlat;
let xy_xformed = rad.matrx.xy * my_xy.x + rad.matrx.zw * my_xy.y - rad.xlat;
Copy link
Collaborator

@armansito armansito Feb 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I tested this change locally as is it didn't fix the broken rotation for me on the blend_grid test (you can reproduce it with the new Q/E controls that @DJMcNab added).

However I was able to fix it by reverting your change to fine.wgsl back to the original and applying @DJMcNab's suggestion on zulip to change shaders/draw_leaf.wgsl#153 to:

let inv_mat = inv_det * vec4(matrx.w, matrx.y, matrx.z, matrx.x);

If I combined your change to fine.wgsl with the change to draw_leaf.wgsl I was getting an even weirder rotation behavior.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix in this PR comes from experimenting with radial gradient rotation in general.

I used this codepen as reference: https://codepen.io/dfrgy/pen/KKxMpWj and added a corresponding example to the brush transform test scene. Without these changes, the transform is very wonky.

The original inverse calculation is algebraically correct so I'm assuming the problem is elsewhere but I haven't been able to track down the source of the reversed rotation direction in the blend grid. I'll keep investigating.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the inverse of the translation components was incorrect. This seems to be working for all cases now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or not...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, good to go :)

Copy link
Collaborator

@armansito armansito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested the changes locally and it all looks good! I left one question/suggestion for readability in shader/draw_leaf.wgsl. Feel free to push back if you don't agree.

Otherwise lgtm.

@@ -151,8 +151,11 @@ fn main(
let r1 = bitcast<f32>(scene[dd + 6u]);
let inv_det = 1.0 / (matrx.x * matrx.w - matrx.y * matrx.z);
let inv_mat = inv_det * vec4(matrx.w, -matrx.y, -matrx.z, matrx.x);
var inv_tr = inv_mat.xz * translate.x + inv_mat.yw * translate.y;
inv_tr += p0;
var inv_tr = inv_det * vec2(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to confirm my understanding, the 2x2 matrix is stored in column-major order in the vector, correct?

A slightly shorter formulation that reuses inv_mat and built-in matrix multiplication could look like:

let inv_tr = mat2x2<f32>(inv_mat.xy, inv_mat.zw) * (-translate) - p0;

which has the added benefit of declaring inv_tr as immutable.

Feel free to push back if you disagree (or if there's a performance assumption that I'm missing here). Otherwise the PR overall lgtm.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the lack of matrix types in the codebase is simply a tradition at this point. I like this formulation and we should probably do more of it moving forward. Updated!

@armansito
Copy link
Collaborator

Thank you for the back and forth!

@armansito armansito merged commit 61ae3cd into main Feb 23, 2023
@dfrg dfrg deleted the brush-fix branch February 23, 2023 06:40
@DJMcNab DJMcNab mentioned this pull request Oct 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird behaviour with rotating gradients
2 participants