Skip to content

shear_x(), shear_y() and apply_matrix() draw the wrong shape #269

Open

Description

@Akshats-git

Shapes drawn under a shear come out as a rotated rectangle instead of a parallelogram. The same thing happens when a non-uniform scale() is followed by rotate().

Reproduction (processing-py)

from processing_py import *

def setup():
    size(400, 400)

def draw():
    background(240)
    translate(100, 100)
    shear_x(PI / 6)
    rect(0, 0, 150, 150)

run()

Expected behavior

A parallelogram leaning to the right, same as in Processing.

Actual behavior

A rotated rectangle.

In the screenshot, blue is what libprocessing draws. Green is the correct shape. Red dots are what screen_x() / screen_y() return for the corners. They match the green shape, not the drawn one. The last panel (rotate, then scale) is drawn correctly.

shear_2d

It happens in 3D too. Here is scale(1.6, 0.6, 1) followed by rotate_y() on a box:

squashed_box_3d_overlay

Cause

Before each draw, the current matrix is turned into a Bevy Transform using to_scale_rotation_translation(). This happens in spawn_mesh in render/mod.rs and in TransformStack::to_bevy_transform. A Transform can only hold translation, rotation and scale. A shear doesn't fit, so it gets lost.

compute_global_transform in gltf.rs does the same thing, so glTF scenes with a non-uniformly scaled parent are probably affected too.

Proposed fix

When the matrix fits in a Transform keep the current path. When it doesn't, apply the matrix to the mesh vertices and normals and use a translation-only Transform. Existing sketches would render exactly the same. I would add unit tests and a visual test case.

I would like to work on this if the approach sounds good.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions