Skip to content

Commit 01fd5f6

Browse files
Fix visual glitches caused by weird Z basis vector
1 parent a363a59 commit 01fd5f6

1 file changed

Lines changed: 19 additions & 1 deletion

File tree

src/main.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ impl<const N: usize> Vector<N> {
116116
}
117117
}
118118

119+
impl Vector<3> {
120+
fn cross_product(self, rhs: Vector<3>) -> Vector<3> {
121+
Vector([
122+
self[1] * rhs[2] - self[2] * rhs[1],
123+
self[2] * rhs[0] - self[0] * rhs[2],
124+
self[0] * rhs[1] - self[1] * rhs[0],
125+
])
126+
}
127+
}
128+
119129
type Vertex = Vector<3>;
120130

121131
type Face = [Vertex; 3];
@@ -263,11 +273,19 @@ fn main() {
263273
let (width, x_basis_vector) = x_basis_vector.normalise();
264274
let (height, y_basis_vector) = y_basis_vector.normalise();
265275

276+
// Technically we don't need to change the Z basis vector from
277+
// (0, 0, 1, 0) if we assume the input Z co-ordinate is always going to
278+
// be zero, but in practice we get exciting visual glitches in Chrome
279+
// and Firefox if we leave it like that. They're probably trying to
280+
// decompose the matrix and getting weird results.
281+
// So: set the Z basis vector to the normal.
282+
let z_basis_vector = x_basis_vector.cross_product(y_basis_vector);
283+
266284
#[cfg_attr(rustfmt, rustfmt_skip)]
267285
let matrix: [f32; 16] = [
268286
x_basis_vector[0], x_basis_vector[1], x_basis_vector[2], 0f32,
269287
y_basis_vector[0], y_basis_vector[1], y_basis_vector[2], 0f32,
270-
0f32, 0f32, 1f32, 0f32,
288+
z_basis_vector[0], z_basis_vector[1], z_basis_vector[2], 0f32,
271289
translation[0], translation[1], translation[2], 1f32,
272290
];
273291

0 commit comments

Comments
 (0)