1个不稳定版本
0.1.0 | 2020年2月5日 |
---|
#618 in WebAssembly
56KB
1.5K SLoC
webgl-matrix
入门指南
这是一个轻量级的矩阵/向量库,旨在与WebGL一起使用。
在核心上,这个库只包含 Matrix
和 Vector
特性。所有实现都是可选特性,可以根据需要添加。
可用特性
Matrix4
:4x4矩阵操作(包括 Vector4)Matrix3
:3x3矩阵操作(包括 Vector3)Vector4
:4维向量操作Vector3
:3维向量操作SliceOps
:低级切片操作,如加法、减法、缩放等。
示例
所有类型都是简单的数组。您也可以仅使用切片作为操作数。
use webgl_matrix::{Matrix, Vector, ProjectionMatrix, Mat4, Vec4, Mat3, Vec3};
fn main() {
// all the default operations available
let mut B = [1., 2., 3.,
4., 5., 6.,
7., 8., 9.];
let b = Vec3::ones();
// Matrix operations are in-place
B.inverse();
B.transpose();
// ..
// Some basic vector operations
let c = B.mul_vector_left(&b);
let mag = c.mag(); // magnitude
let d = c.scale(5.);
let e = c.add(&b);
// Or fancier transformations
B.translate(&[1., 2., 3.]);
let A = Mat4::identity();
// operate on slices
let b = [1., 2., 3., 4., 5., 6., 7.];
// with automatic homogenous coordinate expansion
let c = A.mul_vector(&b[0..=2]);
// or using all four coordinates
let d = A.mul_vector(&b[3..]);
// create projection matrices (left, right, bot, top, near, far)
let P = Mat4::create_perspective_from_viewport(0., 1., 0., 1., 0.1, 10.);
}