Issue with icon rotation in IconLayer
It seems that rotating an icon generates distortion. It's mainly an issue with the aspect ratio. See the animation below in which a tall and thin rectangle is rotated around its center.
The correct result should be something like this:

It looks like in the vertex shader, the vertex is rotated before scaling, which stretches the texture wrongly.
void main(void) {
...
vertex = rotate_by_angle(vertex, instanceAngles) * iconSize_clipspace *
sizeScale * instanceScale;
...
}
Here is a solution that correctly handles the rotation. Basically we want to scale before rotation. Then in the rotation function, we need to factor in the aspect ratio since the vertex contains fraction relative to the viewport size rather than the absolute size in pixels. The rotation looks like an ellipse rather than a circle.
vec2 rotate_by_angle(vec2 vertex, float angle, float aspect_ratio) {
...
float aspect_ratio_reverse = aspect_ratio == 0.0 ? aspect_ratio : 1.0 / aspect_ratio;
mat2 rotationMatrix = mat2(cos_angle, - aspect_ratio_reverse * sin_angle, aspect_ratio * sin_angle, cos_angle);
...
}
void main(void) {
...
float aspect_ratio = viewportSize.x == 0.0 ? viewportSize.x : viewportSize.y / viewportSize.x;
vertex *= iconSize_clipspace;
vertex = rotate_by_angle(vertex, instanceAngles, aspect_ratio) * sizeScale * instanceScale;
...
}
I did not create a PR for this for now since there might be better ways to handle this. Let me know if you think this is a reasonable solution and would want me to create a PR.
@rivulet-zhang - looks like you have found (and solved) an important issue.
I am trying to understand why this has not been an issue in other contexts. Is the extreme aspect ratio issue in your example the result of an very non-square screen? And the aspect ratio issue disappears if the screen is resized to be square?
Anyway, the general idea is that the shaders coordinates (positions after projection, distances after scaling) should be in "pixel" values, so that the shader can specify things in pixel widths.
Your fix adds a bit of complexity, so I wonder if we could build scaling into the project functions so that the shaders do not need to care about it.
Also, a small nit: Call it aspectRatioInverse instead of aspectRatioReverse?
Hi @ibgreen
I am trying to understand why this has not been an issue in other contexts. Is the extreme aspect ratio issue in your example the result of an very non-square screen? And the aspect ratio issue disappears if the screen is resized to be square?
I think it is the aspect ratio of the texture that matters more. I guess you mostly use textures that are nearly squared (e.g., examples/icon/data/location-icon-atlas.png). In those case, even the aspect ratio of the screenshot is extreme, the texture itself won't distort too much. In contrast, when we use an extremely non-squared texture such as the tall and thin rectangle in the example, it would generate significant distortion, even in a squared viewport.
Specifically, in the below code, once the rotated vertex is multiplied by the iconSize_clipspace, it would be stretched based on the scaling factor calculated from the original texture (before rotation), which I think is the major reason that generates the distortion. You can imagine that however the texture is rotated, it is always constrained in the axis-aligned bounding box of the original texture without rotation.
vertex = rotate_by_angle(vertex, instanceAngles) * iconSize_clipspace *
sizeScale * instanceScale;
Anyway, the general idea is that the shaders coordinates (positions after projection, distances after scaling) should be in "pixel" values, so that the shader can specify things in pixel widths.
Good point. It may make the code cleaner if we calculate the texture size in pixel rather than the fraction at the beginning and stick to it for scaling and rotation operation. In this case, at least we do not need to factor in the aspect ratio in the rotation function.
Your fix adds a bit of complexity, so I wonder if we could build scaling into the project functions so that the shaders do not need to care about it.
Not sure if it is cost-effective since it is a pretty special case and not sure if other layers may potentially have the same problem...
Also, a small nit: Call it aspectRatioInverse instead of aspectRatioReverse? Good point.
@rivulet-zhang are you working on this? Based on discussions with @tangential , with no rotation, icon quality issue reported in #895 is improved. We should fix this before we dig into #895, they could be duplicates.
@1chandu I will create a PR to fix this bug.
@rivulet-zhang , thanks for the PR, @tangential can you verify if this resolves both distortion and quality in Icon layer. If not we have work on any remaining issues in icon layer.