-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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 normals during mesh scaling #13380
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add a regression test for this? This seems very testable, and important to make sure we don't screw up.
Scale is also handled in transform_by. So it should apply there too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks! A couple comments in the test to explain why the values are correct would be helpful, but I won't block on it.
Shouldn't this also scale tangents? Either way, I'm not sure this is correct, but I don't really understand the covector_scale thing. @Jondolf any opinions since you wrote the initial code for transform_by? |
I'm pretty sure this is wrong. Just multiplying the normal by the scale does not take non-uniform scaling into account properly, and can lead to the normal no longer being perpendicular to the surface. This article illustrates it pretty well. In most resources I've seen, including the one I linked, the "correct" way to transform normals in a way that accounts for non-uniform scaling is to use the transpose of the inverse of the transformation matrix. I imagine the covector scale thing is supposed to achieve the same thing without having to compute the transformation matrix and do a bunch of operations on it, but honestly I'm not that knowledgeable on the math / don't remember it well enough right now. For background on the covector scale thing (originally I also used naive vector scaling), see this thread and the comments by @atlv24. |
Could the covector_scale be multiplied with a sign only vector? That way it would flip things but not rescale anything? |
The article suggests that for a given scale ReasoningSuppose the scaling transform matrix Then the correct transform matrix to apply to the normals would be the transpose of the inverse of the matrix: When applying This is equivalent to componentwise multiplication with the When Is everyone ok with this? |
Hey, thanks for looking in to this and taking the time to write out our reasoning. It was very helpful. I'm sure multiplying by scale is wrong. We do want to multiply by inverse scale. Speaking of which, the following two lines are equivalent. let scaled_normal = (normal.xyz * scale.yzx * scale.zxy).normalized();
let scaled_normal = (normal.xyz / scale.xyz).normalized(); To verify, expand the the latter and then simplify. So it looks like dividing by scale as proposed above would be the same as what we have currently, except perhaps with slightly increased numerical error. Edit: I think theres an absolute value sneaking in there during the simplification. |
Hi, you are absolutely right, a sign is dropped, but that is not the only issue. Please consider the following example: let normal = Vec3::new(1., 1., 0.).normalize();
let scale = Vec3::new(1., 1., 0.);
// When scaling this normal, the result should still be Vec3::new(SQRT_2, SQRT_2, 0.)
// since that is what the result would be for the scale Vec3::new(1., 1., s) as s approaches 0
// This method yealds Vec3::ZERO though since its z component is 0. / 0. == NaN and the Vec3 can't be normalized
let scaled_normal = (normal / scale).normalize_or_zero();
// Likewise, and more importantly / frequent, the following scaled_normal should be Vec3::new(0., 0., 1.) but is Vec3::ZERO instead.
let normal = Vec3::new(1., 1., 0.0001).normalize();
let scaled_normal = (normal / scale).normalize_or_zero(); |
Tangents are covariant, so they should be scaled. I suspect with the normals we should just multiply by a sign vector, and keep the covector form. Here, I'll try to make time for a proper review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is the correct tangent behavior. I suggest PR'ing the normalize_or_zero fix to glam itself.
Co-Authored-By: vero <11307157+atlv24@users.noreply.github.com>
Co-Authored-By: vero <11307157+atlv24@users.noreply.github.com>
Objective
Solution
Vec3::new(1., 1., -1.)
, the normals should be flipped along the Z-axis. For example a normal ofVec3::new(0., 0., 1.)
should becomeVec3::new(0., 0., -1.)
after scaling. This is achieved by multiplying the normal by the reciprocal of the scale, cheking for infinity and normalizing. Before, the normal was multiplied by a covector of the scale, which is incorrect for normals.scale
, not its reciprocal as before