base/matrix4
4x4 변환 행렬. (원본 base/matrix4.js)
- 원본과 같이 열 우선이다. 요소 자리는 elements[column * 4 + row] 이고, glUniformMatrix4fv 에 전치 없이 그대로 올린다.
- 점 변환은 transformed = matrix x (x, y, z, 1) 이다. (열벡터를 오른쪽에 곱한다)
- multiply / translate / rotate_x ... / scale 은 원본처럼 자기 자신을 바꾼다. (this = this x other, 로컬 공간 오른쪽 곱) create... 정적 함수는 원본처럼 새 행렬을 돌려준다.
- 좌표계는 원본과 같이 오른손이다. create_perspective 는 OpenGL 식(카메라는 -Z 를 보고, 깊이는 -1 ~ 1), create_look_at 은 눈에서 대상 반대쪽이 +Z(뒤쪽 축)인 뷰 행렬, 회전은 라디안이고 축을 바라볼 때 반시계가 양수다.
- 원본 getElements() 는 Float32Array 를 내주고 호출자가 그 안을 직접 고친다. 러스트 판은 elements 를 공개 필드로 두고, get_elements() 는 읽기 참조를, write_to_array / write_to_slice 는 유니폼 배열에 옮겨 쓰는 일을 맡는다.
- 원본처럼 칸은 Float32Array 와 같은 f32 에 담고, 셈은 칸마다 f64 로 한 뒤 f32 로 담는다. (원본과 같은 비트, 셈에 쓸 때는 read_elements 로 f64 로 읽는다)
- 원본 transformPoint 의 { x, y, z, w } 는 TransformedPoint 로, transform_direction 의 { x, y, z } 는 Vector3 로 돌려준다.
- 원본 clone 은 Copy 로 갈음한다. 원본에 없는 전치(transpose)는 만들지 않았다.
4x4 transform matrix. (the original base/matrix4.js)
- Column-major as in the original. Element positions are elements[column * 4 + row], uploaded as is to glUniformMatrix4fv without transposing.
- Point transformation is transformed = matrix x (x, y, z, 1). (column vector multiplied on the right)
- multiply / translate / rotate_x ... / scale change the matrix itself as in the original. (this = this x other, right multiplication in local space) The create... static functions return a new matrix as in the original.
- The coordinate system is right-handed as in the original. create_perspective is OpenGL style (the camera looks down -Z, depth -1 ~ 1), create_look_at is a view matrix whose +Z (back axis) points away from the target as seen from the eye, rotations are in radians, and counterclockwise when looking down the axis is positive.
- The original getElements() hands out a Float32Array and the caller edits it directly. The Rust version makes elements a public field; get_elements() gives a read reference, and write_to_array / write_to_slice copy into uniform arrays.
- Like the original, elements are stored in f32 like a Float32Array, and math is done per element in f64 and then stored as f32. (the same bits as the original; read them as f64 with read_elements when computing)
- The original transformPoint's { x, y, z, w } is returned as TransformedPoint, and transform_direction's { x, y, z } as Vector3.
- The original clone is replaced by Copy. A transpose, which the original does not have, was not added.
TransformedPointstruct traits Clone, Copy, Debug, Default, PartialEq
동차 좌표 점. (원본 transformPoint 가 돌려주는 { x, y, z, w }, 원근 나누기는 호출자가 한다)
Homogeneous coordinate point. (the { x, y, z, w } the original transformPoint returns; the caller does the perspective divide)
use vanilla::{math, Matrix4};
let projection = Matrix4::create_perspective(math::PI * 0.5, 2.0, 1.0, 3.0);
let clip_point = projection.transform_point(0.0, 0.0, -3.0);
assert_eq!(clip_point.w, 3.0);
assert_eq!(clip_point.z / clip_point.w, 1.0);동차 좌표 w. (원근 나누기에 쓴다, 아핀 변환이면 1)
Homogeneous coordinate w. (used for the perspective divide, 1 for affine transforms)
반환Returnsf64
Matrix4struct traits Clone, Copy, Debug, PartialEq, Default
4x4 변환 행렬. (열 우선, 오른손 좌표계, 기본값은 단위 행렬)
- translate, rotate_x, scale 같은 누적 함수는 자신을 바꾸고, create_... 함수는 새 행렬을 돌려준다.
4x4 transform matrix. (column-major, right-handed coordinate system, defaults to the identity matrix)
- Accumulating functions like translate, rotate_x and scale change the matrix itself, and create_... functions return a new matrix.
use vanilla::{Matrix4, Vector3};
let mut model = Matrix4::create_translation(1.0, 2.0, 3.0);
model.scale(2.0, 2.0, 2.0);
let point = model.transform_point(1.0, 1.0, 1.0);
assert_eq!((point.x, point.y, point.z, point.w), (3.0, 4.0, 5.0, 1.0));
let inverse = Matrix4::create_inverse(&model);
let back = inverse.transform_point(point.x, point.y, point.z);
assert_eq!((back.x, back.y, back.z), (1.0, 1.0, 1.0));
let view = Matrix4::create_look_at(Vector3::create(0.0, 0.0, 5.0), Vector3::zero(), Vector3::up());
assert_eq!(view, Matrix4::create_translation(0.0, 0.0, -5.0));단위 행렬을 만든다.
Creates an identity matrix.
반환ReturnsMatrix4 단위 행렬.Identity matrix.
짝Counterpart: vanilla.js Matrix4.createIdentity
이동 행렬을 만든다.
Creates a translation matrix.
- xf64
- x 이동량.x translation.
- yf64
- y 이동량.y translation.
- zf64
- z 이동량.z translation.
반환ReturnsMatrix4 이동 행렬. (elements 12, 13, 14 에 x, y, z)Translation matrix. (x, y, z in elements 12, 13, 14)
짝Counterpart: vanilla.js Matrix4.createTranslation
X 축 회전 행렬을 만든다. (라디안)
Creates an X axis rotation matrix. (radians)
- radianf64
- 회전 각. (라디안, 양수면 +Y 를 +Z 쪽으로 돌린다)Rotation angle. (radians, positive turns +Y toward +Z)
반환ReturnsMatrix4 X 축 회전 행렬.X axis rotation matrix.
짝Counterpart: vanilla.js Matrix4.createRotationX
Y 축 회전 행렬을 만든다. (라디안)
Creates a Y axis rotation matrix. (radians)
- radianf64
- 회전 각. (라디안, 양수면 +Z 를 +X 쪽으로 돌린다)Rotation angle. (radians, positive turns +Z toward +X)
반환ReturnsMatrix4 Y 축 회전 행렬.Y axis rotation matrix.
짝Counterpart: vanilla.js Matrix4.createRotationY
Z 축 회전 행렬을 만든다. (라디안)
Creates a Z axis rotation matrix. (radians)
- radianf64
- 회전 각. (라디안, 양수면 +X 를 +Y 쪽으로 돌린다)Rotation angle. (radians, positive turns +X toward +Y)
반환ReturnsMatrix4 Z 축 회전 행렬.Z axis rotation matrix.
짝Counterpart: vanilla.js Matrix4.createRotationZ
use vanilla::{math, Matrix4};
let rotation = Matrix4::create_rotation_z(math::degree_to_radian(90.0));
let rotated = rotation.transform_direction(1.0, 0.0, 0.0);
assert!(rotated.x.abs() < 1e-6);
assert!((rotated.y - 1.0).abs() < 1e-6);쿼터니언 회전 행렬을 만든다. (단위 쿼터니언을 가정한다)
Creates a quaternion rotation matrix. (assumes a unit quaternion)
- quaternionQuaternion
- 회전 쿼터니언. (길이 1 로 정규화한 것)Rotation quaternion. (normalized to length 1)
반환ReturnsMatrix4 회전 행렬. (이동 성분은 0)Rotation matrix. (translation part is 0)
짝Counterpart: vanilla.js Matrix4.createRotationFromQuaternion
use vanilla::{Matrix4, Quaternion};
let quaternion = Quaternion::create_from_euler(0.0, 0.0, 0.5);
let from_quaternion = Matrix4::create_rotation_from_quaternion(quaternion);
let from_angle = Matrix4::create_rotation_z(0.5);
for index in 0..16 {
assert!((from_quaternion.elements[index] - from_angle.elements[index]).abs() < 1e-5);
}크기 행렬을 만든다.
Creates a scale matrix.
- xf64
- x 배율.x scale.
- yf64
- y 배율.y scale.
- zf64
- z 배율.z scale.
반환ReturnsMatrix4 크기 행렬.Scale matrix.
짝Counterpart: vanilla.js Matrix4.createScale
pub fn create_perspective(field_of_view_radian: f64, aspect_ratio: f64, near_distance: f64, far_distance: f64) -> Matrix4#원근 투영 행렬을 만든다. (세로 시야각 라디안, OpenGL 식 깊이 -1 ~ 1)
Creates a perspective projection matrix. (vertical field of view in radians, OpenGL style depth -1 ~ 1)
- field_of_view_radianf64
- 세로 시야각. (라디안)Vertical field of view. (radians)
- aspect_ratiof64
- 가로 나누기 세로 비.Width divided by height ratio.
- near_distancef64
- 카메라에서 가까운 면까지 거리. (카메라는 -Z 를 본다)Distance from the camera to the near plane. (the camera looks down -Z)
- far_distancef64
- 카메라에서 먼 면까지 거리.Distance from the camera to the far plane.
반환ReturnsMatrix4 원근 투영 행렬. (가까운 면은 깊이 -1, 먼 면은 깊이 1 로 간다)Perspective projection matrix. (the near plane goes to depth -1, the far plane to depth 1)
짝Counterpart: vanilla.js Matrix4.createPerspective
use vanilla::{math, Matrix4};
let projection = Matrix4::create_perspective(math::degree_to_radian(90.0), 2.0, 1.0, 3.0);
let near_point = projection.transform_point(0.0, 0.0, -1.0);
assert_eq!(near_point.z / near_point.w, -1.0);pub fn create_orthographic(left: f64, right: f64, bottom: f64, top: f64, near_distance: f64, far_distance: f64) -> Matrix4#직교 투영 행렬을 만든다. (OpenGL glOrtho 와 같은 식)
Creates an orthographic projection matrix. (the same formula as OpenGL glOrtho)
- leftf64
- 왼쪽 면의 x.x of the left plane.
- rightf64
- 오른쪽 면의 x.x of the right plane.
- bottomf64
- 아래 면의 y.y of the bottom plane.
- topf64
- 위 면의 y.y of the top plane.
- near_distancef64
- 카메라에서 가까운 면까지 거리. (카메라는 -Z 를 본다)Distance from the camera to the near plane. (the camera looks down -Z)
- far_distancef64
- 카메라에서 먼 면까지 거리.Distance from the camera to the far plane.
반환ReturnsMatrix4 직교 투영 행렬. (상자 안을 -1 ~ 1 로 옮긴다)Orthographic projection matrix. (maps the inside of the box to -1 ~ 1)
짝Counterpart: vanilla.js Matrix4.createOrthographic
use vanilla::Matrix4;
let projection = Matrix4::create_orthographic(0.0, 4.0, 0.0, 2.0, 1.0, 5.0);
let corner = projection.transform_point(4.0, 2.0, -5.0);
assert_eq!((corner.x, corner.y, corner.z, corner.w), (1.0, 1.0, 1.0, 1.0));pub fn create_look_at(eye_position: Vector3, target_position: Vector3, up_direction: Vector3) -> Matrix4#시점 행렬을 만든다. (eye_position 에서 target_position 을 바라보는 뷰 행렬, 뒤쪽 축 = 눈 - 대상)
Creates a view matrix. (a view matrix looking from eye_position at target_position, back axis = eye - target)
- eye_positionVector3
- 눈(카메라) 위치.Eye (camera) position.
- target_positionVector3
- 바라볼 점. (eye_position 과 같으면 쓸 수 없는 행렬이 된다)Point to look at. (the matrix becomes unusable if it equals eye_position)
- up_directionVector3
- 위쪽 방향. (보통 Vector3::up(), 시선과 나란하면 오른쪽 축이 (0, 0, 0) 이 되어 쓸 수 없는 행렬이 된다)Up direction. (usually Vector3::up(); if parallel to the line of sight, the right axis becomes (0, 0, 0) and the matrix is unusable)
반환ReturnsMatrix4 월드 좌표를 뷰 좌표로 바꾸는 행렬. (대상은 -Z 쪽에 놓인다)Matrix that converts world coordinates to view coordinates. (the target lies toward -Z)
짝Counterpart: vanilla.js Matrix4.createLookAt
use vanilla::{Matrix4, Vector3};
let view = Matrix4::create_look_at(Vector3::create(0.0, 0.0, 5.0), Vector3::zero(), Vector3::up());
let target_in_view = view.transform_point(0.0, 0.0, 0.0);
assert_eq!((target_in_view.x, target_in_view.y, target_in_view.z), (0.0, 0.0, -5.0));역행렬을 만든다. (일반 4x4 여인수 전개, 행렬식이 0 이면 단위 행렬)
Creates an inverse matrix. (general 4x4 cofactor expansion, the identity matrix if the determinant is 0)
- matrix&Matrix4
- 뒤집을 행렬.Matrix to invert.
반환ReturnsMatrix4 역행렬. 행렬식이 0 이면 단위 행렬.The inverse matrix. The identity matrix if the determinant is 0.
짝Counterpart: vanilla.js Matrix4.createInverse
use vanilla::Matrix4;
let mut matrix = Matrix4::create_translation(1.0, 2.0, 3.0);
matrix.scale(2.0, 4.0, 8.0);
let inverse = Matrix4::create_inverse(&matrix);
let point = inverse.transform_point(1.0, 2.0, 3.0);
assert_eq!((point.x, point.y, point.z), (0.0, 0.0, 0.0));
let singular = Matrix4 { elements: [0.0; 16] };
assert_eq!(Matrix4::create_inverse(&singular), Matrix4::create_identity());열 우선 요소 배열. (elements[column * 4 + row])
Column-major element array. (elements[column * 4 + row])
반환Returns[f32; 16]
단위 행렬로 초기화.
Resets to the identity matrix.
짝Counterpart: vanilla.js Matrix4.setIdentity
다른 행렬로부터 복사.
Copies from another matrix.
- other&Matrix4
- 복사해 올 행렬.Matrix to copy from.
짝Counterpart: vanilla.js Matrix4.copyFrom
다른 행렬을 오른쪽에 곱해 누적한다. (this = this x other)
Accumulates by multiplying another matrix on the right. (this = this x other)
- other&Matrix4
- 오른쪽에 곱할 행렬. (점에는 other 변환이 먼저 걸린다)Matrix to multiply on the right. (the other transform applies to points first)
짝Counterpart: vanilla.js Matrix4.multiply
use vanilla::Matrix4;
let mut matrix = Matrix4::create_translation(1.0, 2.0, 3.0);
matrix.multiply(&Matrix4::create_scale(2.0, 2.0, 2.0));
let point = matrix.transform_point(1.0, 0.0, 0.0);
assert_eq!((point.x, point.y, point.z), (3.0, 2.0, 3.0));이동 변환을 누적한다. (로컬 공간 오른쪽 곱)
Accumulates a translation. (right multiplication in local space)
- xf64
- x 이동량.x translation.
- yf64
- y 이동량.y translation.
- zf64
- z 이동량.z translation.
짝Counterpart: vanilla.js Matrix4.translate
X 축 회전 변환을 누적한다. (라디안, 로컬 공간 오른쪽 곱)
Accumulates an X axis rotation. (radians, right multiplication in local space)
- radianf64
- 회전 각. (라디안, 양수면 +Y 를 +Z 쪽으로 돌린다)Rotation angle. (radians, positive turns +Y toward +Z)
짝Counterpart: vanilla.js Matrix4.rotateX
Y 축 회전 변환을 누적한다. (라디안, 로컬 공간 오른쪽 곱)
Accumulates a Y axis rotation. (radians, right multiplication in local space)
- radianf64
- 회전 각. (라디안, 양수면 +Z 를 +X 쪽으로 돌린다)Rotation angle. (radians, positive turns +Z toward +X)
짝Counterpart: vanilla.js Matrix4.rotateY
Z 축 회전 변환을 누적한다. (라디안, 로컬 공간 오른쪽 곱)
Accumulates a Z axis rotation. (radians, right multiplication in local space)
- radianf64
- 회전 각. (라디안, 양수면 +X 를 +Y 쪽으로 돌린다)Rotation angle. (radians, positive turns +X toward +Y)
짝Counterpart: vanilla.js Matrix4.rotateZ
크기 변환을 누적한다. (로컬 공간 오른쪽 곱)
Accumulates a scale. (right multiplication in local space)
- xf64
- x 배율. (1 이면 그대로)x scale. (1 leaves it unchanged)
- yf64
- y 배율.y scale.
- zf64
- z 배율.z scale.
짝Counterpart: vanilla.js Matrix4.scale
점을 변환한다. (w 성분까지 돌려주고, 원근 나누기는 호출자가 한다)
Transforms a point. (returns up to the w component; the caller does the perspective divide)
- xf64
- 점의 x.x of the point.
- yf64
- 점의 y.y of the point.
- zf64
- 점의 z.z of the point.
반환ReturnsTransformedPoint matrix x (x, y, z, 1) 의 동차 좌표. (w 로 나누지 않은 값)Homogeneous coordinates of matrix x (x, y, z, 1). (not divided by w)
짝Counterpart: vanilla.js Matrix4.transformPoint
방향을 변환한다. (이동 성분은 빼고 회전과 크기만 적용한다)
Transforms a direction. (applies only rotation and scale, without the translation part)
- xf64
- 방향의 x.x of the direction.
- yf64
- 방향의 y.y of the direction.
- zf64
- 방향의 z.z of the direction.
반환ReturnsVector3 변환한 방향. (길이를 다시 맞추지 않는다)The transformed direction. (the length is not renormalized)
짝Counterpart: vanilla.js Matrix4.transformDirection
use vanilla::{Matrix4, Vector3};
let mut matrix = Matrix4::create_translation(10.0, 0.0, 0.0);
matrix.scale(2.0, 2.0, 2.0);
assert_eq!(matrix.transform_direction(0.0, 1.0, 0.0), Vector3::create(0.0, 2.0, 0.0));열 우선 요소를 수(f64)로 읽은 배열. (원본이 Float32Array 칸을 읽으면 자바스크립트 수가 되는 것과 같다, 셈은 이것으로 한다)
Column-major elements read as numbers (f64). (the same as reading a Float32Array element in the original, which gives a JavaScript number; math uses this)
반환Returns[f64; 16] 열 우선 16 칸을 f64 로 넓힌 배열. (칸마다 f32 값 그대로)The 16 column-major elements widened to f64. (each keeps its f32 value)
use vanilla::Matrix4;
let matrix = Matrix4::create_translation(1.5, 2.0, 0.1);
let elements = matrix.read_elements();
assert_eq!(elements[12], 1.5);
assert_eq!(elements[14], 0.1f32 as f64);열 우선 요소 배열을 돌려준다. (고칠 때는 elements 필드를 직접 쓴다)
Returns the column-major element array. (to edit, use the elements field directly)
반환Returns&[f32; 16] 열 우선 요소 16 개의 읽기 참조.Read reference to the 16 column-major elements.
짝Counterpart: vanilla.js Matrix4.getElements
셰이더 mat4 (열 우선) 배열로 기록한다.
Writes to a shader mat4 (column-major) array.
- array&mut [f32; 16]
- 옮겨 쓸 배열. (열 우선 16 개)Array to copy into. (16 column-major values)
짝Counterpart: vanilla.js Matrix4.writeToArray
큰 배열의 offset 자리부터 16 개를 기록한다. (원본 jointMatrixArray.set(elements, joint_index * 16) 자리)
Writes 16 values starting at offset in a large array. (in place of the original jointMatrixArray.set(elements, joint_index * 16))
- array&mut [f32]
- 옮겨 쓸 큰 배열. (길이가 offset + 16 보다 짧으면 패닉)Large array to copy into. (panics if shorter than offset + 16)
- offsetusize
- 쓰기 시작할 자리. (요소 번호, 관절 번호 x 16 같은)Position to start writing. (element index, such as joint index x 16)
짝Counterpart: vanilla.js Matrix4.writeToSlice
use vanilla::Matrix4;
let joint_matrix = Matrix4::create_translation(7.0, 8.0, 9.0);
let mut joint_matrix_array = vec![0.0f32; 32];
joint_matrix.write_to_slice(&mut joint_matrix_array, 16);
assert_eq!(&joint_matrix_array[16..32], &joint_matrix.elements[..]);
assert_eq!(joint_matrix_array[0], 0.0);