Skip to content

Commit f92ba50

Browse files
committed
fix matrixVecMul4 implementation
1 parent 6001892 commit f92ba50

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

package/src/renderer/__tests__/e2e/Matrix4.spec.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010
rotateX,
1111
translate,
1212
toMatrix3,
13+
Matrix4,
14+
mapPoint3d,
1315
} from "../../../skia/types";
1416

1517
const ckPerspective = (d: number) => [
@@ -225,4 +227,17 @@ describe("Matrix4", () => {
225227
0.1
226228
);
227229
});
230+
it("should correctly transform a point with an identity matrix", () => {
231+
const identityMatrix = Matrix4();
232+
const point = [100, -100, 200] as const; // Define some test point
233+
const result = mapPoint3d(identityMatrix, point);
234+
expect(result).toEqual(point);
235+
});
236+
it("should correctly transform a point with a translation matrix", () => {
237+
const translationMatrix = translate(100, 100, 100);
238+
const point = [100, -100, 200] as const; // Define some test point
239+
const expectedResult = [200, 0, 300] as const;
240+
const result = mapPoint3d(translationMatrix, point);
241+
expect(result).toEqual(expectedResult);
242+
});
228243
});

package/src/skia/types/Matrix4.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,16 @@ const rotatedUnitSinCos = (
150150
];
151151
};
152152

153-
const matrixVecMul4 = (m: Matrix4, v: Vec4) => {
153+
/**
154+
* @worklet
155+
*/
156+
export const matrixVecMul4 = (m: Matrix4, v: Vec4): Vec4 => {
154157
"worklet";
155-
const [vx, vy, vz, vw] = v;
156158
return [
157-
vx * m[0] + vy * m[4] + vz * m[8] + vw * m[12],
158-
vx * m[1] + vy * m[5] + vz * m[9] + vw * m[13],
159-
vx * m[2] + vy * m[6] + vz * m[10] + vw * m[14],
160-
vx * m[3] + vy * m[7] + vz * m[11] + vw * m[15],
159+
m[0] * v[0] + m[1] * v[1] + m[2] * v[2] + m[3] * v[3],
160+
m[4] * v[0] + m[5] * v[1] + m[6] * v[2] + m[7] * v[3],
161+
m[8] * v[0] + m[9] * v[1] + m[10] * v[2] + m[11] * v[3],
162+
m[12] * v[0] + m[13] * v[1] + m[14] * v[2] + m[15] * v[3],
161163
];
162164
};
163165

0 commit comments

Comments
 (0)