Libgdx 的 Matrix4#translate() 没有按预期工作

Libgdx's Matrix4#translate() doesn't work as expected

我正在尝试使用变换矩阵绘制一个 NinePatch,以便它可以缩放、旋转、移动等。所以我创建了一个继承自 LibGDX 的 class' s NinePatch class 负责矩阵。

这就是我计算变换矩阵的方式(每次以下值之一发生变化时我都会更新它):

this.transform
    .idt()
    .translate(originX, originY, 0)
    .rotate(0, 0, 1, rotation)
    .scale(scale, scale, 1)
    .translate(-originX, -originY, 0)
;

以及我如何渲染我的自定义 NinePatch class :

drawConfig.begin(Mode.BATCH);
this.oldTransform.set(drawConfig.getTransformMatrix());
drawConfig.setTransformMatrix(this.transform);
this.draw(drawConfig.getBatch(), this.x, this.y, this.width, this.height); // Libgdx's NinePatch#draw()
drawConfig.setTransformMatrix(this.oldTransform);

案例一

这是我渲染 4 个九个补丁时得到的结果: Position = 0,0 / Origin = 0,0 / Scale = 0.002 / Rotation = 每个 9patch 不同

我得到了我期望的。

案例二

现在相同的 4 个九补丁: 位置 = 0,0 / 原点 = 0.5,0.5 / 比例 = 相同 / 旋转 = 相同

你可以看到我的 9 个补丁不是在 0,0(它们的位置)处绘制,而是在 0.5,0.5(它们的原点)处绘制,就像我在计算变换矩阵时没有 .translate(-originX, -originY, 0) 一样。可以肯定的是,我评论了这条指令,我确实得到了相同的结果。那么为什么我的第二次翻译显然没有被考虑在内?

问题可能出在缩放比例上。因为它也缩小了翻译,你的第二个翻译实际上翻译了 (-originX*scale, -originY*scale, 0) 因为 scale=0.002,看起来根本没有翻译。例如对于 x 坐标,您计算:

x_final = originX + scale * (-originX + x_initial)

我必须更改计算我的变换矩阵的代码,以便在按照 Guillaume G 的指示进行回译时考虑比例。除了我的代码与他的代码不同:

this.transform
    .idt()
    .translate(originX, originY, 0)
    .rotate(0, 0, 1, rotation)
    .scale(scale, scale, 1)
    .translate(-originX / scale, -originY / scale, 0);
;