* Converts a 4×4 matrix to its 3×3 inverse transpose transform. * This is commonly used in MVMatrix to NMatrix conversions, particularly * in 3D graphics for transforming normal vectors. * * This method extracts the top-left 3×3 portion of a 4×4 matrix, inverts it, * and then transpos
({ mat4 })
| 742 | * } |
| 743 | */ |
| 744 | inverseTranspose4x4({ mat4 }) { |
| 745 | if (this.#sqDimention !== 3) { |
| 746 | throw new Error('This function only works with 3×3 matrices.'); |
| 747 | } else { |
| 748 | // Convert mat4 -> mat3 by extracting the top-left 3×3 portion |
| 749 | this.matrix[0] = mat4[0]; |
| 750 | this.matrix[1] = mat4[1]; |
| 751 | this.matrix[2] = mat4[2]; |
| 752 | this.matrix[3] = mat4[4]; |
| 753 | this.matrix[4] = mat4[5]; |
| 754 | this.matrix[5] = mat4[6]; |
| 755 | this.matrix[6] = mat4[8]; |
| 756 | this.matrix[7] = mat4[9]; |
| 757 | this.matrix[8] = mat4[10]; |
| 758 | } |
| 759 | |
| 760 | const inverse = this.invert(); |
| 761 | // Check if inversion succeeded |
| 762 | if (inverse) { |
| 763 | inverse.transpose(this.matrix); |
| 764 | } else { |
| 765 | // In case of singularity, zero out the matrix |
| 766 | for (let i = 0; i < 9; i++) { |
| 767 | this.matrix[i] = 0; |
| 768 | } |
| 769 | } |
| 770 | return this; |
| 771 | } |
| 772 | |
| 773 | /** |
| 774 | * Applies a transformation matrix to the current matrix. |
no test coverage detected