vanilladocs
vanillapunch.com
API / base/matrix4

base/matrix4

4x4 변환 행렬. (열우선, WebGL uniformMatrix4fv 에 그대로 업로드 가능)

4x4 transform matrix. (Column-major, can be uploaded as is with WebGL uniformMatrix4fv)

Matrix4extends Object

짝Counterpart: vanilla-engine Matrix4

4x4 변환 행렬. (열우선, WebGL uniformMatrix4fv 에 그대로 업로드 가능)

  • 열우선 배열 인덱스: elements[column * 4 + row]
  • 점 변환: transformed = matrix x (x, y, z, 1)

4x4 transform matrix. (Column-major, can be uploaded as is with WebGL uniformMatrix4fv)

  • Column-major array index: elements[column * 4 + row]
  • Point transform: transformed = matrix x (x, y, z, 1)
예제Example모델 행렬로 점 옮기고 되돌리기Transform a point with a model matrix and back
import { Matrix4 } from "./build/vanilla.js";

const model = Matrix4.createTranslation(10, 0, 0);
model.rotateY(Math.PI / 2);
model.scale(2, 2, 2);
const point = model.transformPoint(1, 0, 0);
console.log(point.x, point.y, point.z, point.w);
const inverse = Matrix4.createInverse(model);
const restored = inverse.transformPoint(point.x, point.y, point.z);
console.log(restored.x, restored.y, restored.z);
생성자constructorconstructor()#0.3.0 부터since 0.3.0소스source

생성.

Constructor.

정적staticstatic createIdentity()#0.3.0 부터since 0.3.0소스source

단위 행렬 생성. (정적)

Creates an identity matrix. (static)

반환ReturnsMatrix4 단위 행렬.Identity matrix.

짝Counterpart: vanilla-engine Matrix4.create_identity

정적staticstatic createTranslation(x, y, z)#0.3.0 부터since 0.3.0소스source

이동 행렬 생성. (정적)

Creates a translation matrix. (static)

xnumber
X 이동량.X translation.
ynumber
Y 이동량.Y translation.
znumber
Z 이동량.Z translation.

반환ReturnsMatrix4 이동 행렬.Translation matrix.

짝Counterpart: vanilla-engine Matrix4.create_translation

정적staticstatic createRotationX(radian)#0.3.0 부터since 0.3.0소스source

X축 회전 행렬 생성. (정적)

Creates an X-axis rotation matrix. (static)

radiannumber
회전 각도. (라디안)Rotation angle. (radians)

반환ReturnsMatrix4 X 축 회전 행렬.X axis rotation matrix.

짝Counterpart: vanilla-engine Matrix4.create_rotation_x

정적staticstatic createRotationY(radian)#0.3.0 부터since 0.3.0소스source

Y축 회전 행렬 생성. (정적)

Creates a Y-axis rotation matrix. (static)

radiannumber
회전 각도. (라디안)Rotation angle. (radians)

반환ReturnsMatrix4 Y 축 회전 행렬.Y axis rotation matrix.

짝Counterpart: vanilla-engine Matrix4.create_rotation_y

정적staticstatic createRotationZ(radian)#0.3.0 부터since 0.3.0소스source

Z축 회전 행렬 생성. (정적)

Creates a Z-axis rotation matrix. (static)

radiannumber
회전 각도. (라디안)Rotation angle. (radians)

반환ReturnsMatrix4 Z 축 회전 행렬.Z axis rotation matrix.

짝Counterpart: vanilla-engine Matrix4.create_rotation_z

정적staticstatic createRotationFromQuaternion(quaternion)#0.3.0 부터since 0.3.0소스source

쿼터니언 회전 행렬 생성. (정적)

Creates a quaternion rotation matrix. (static)

quaternion{ x: number, y: number, z: number, w: number }
회전 쿼터니언. (Quaternion 이나 x, y, z, w 를 가진 객체, 길이가 1 이어야 한다)Rotation quaternion. (a Quaternion or an object with x, y, z, w, must have length 1)

반환ReturnsMatrix4 회전 행렬.Rotation matrix.

짝Counterpart: vanilla-engine Matrix4.create_rotation_from_quaternion

정적staticstatic createScale(x, y, z)#0.3.0 부터since 0.3.0소스source

크기 행렬 생성. (정적)

Creates a scale matrix. (static)

xnumber
X 배율.X scale.
ynumber
Y 배율.Y scale.
znumber
Z 배율.Z scale.

반환ReturnsMatrix4 크기 행렬.Scale matrix.

짝Counterpart: vanilla-engine Matrix4.create_scale

정적staticstatic createPerspective(fieldOfViewRadian, aspectRatio, nearDistance, farDistance)#0.3.0 부터since 0.3.0소스source

원근 투영 행렬 생성. (정적)

Creates a perspective projection matrix. (static)

fieldOfViewRadiannumber
세로 시야각. (라디안)Vertical field of view. (radians)
aspectRationumber
가로 나누기 세로 비율.Width divided by height.
nearDistancenumber
가까운 자르기 면까지 거리. (0 보다 커야 한다)Distance to the near clipping plane. (must be greater than 0)
farDistancenumber
먼 자르기 면까지 거리.Distance to the far clipping plane.

반환ReturnsMatrix4 원근 투영 행렬. (카메라는 -Z 를 보고, 깊이를 -1 ~ 1 로 옮긴다)Perspective projection matrix. (the camera looks down -Z, depth is mapped to -1 ~ 1)

짝Counterpart: vanilla-engine Matrix4.create_perspective

예제Example원근 카메라의 뷰 투영 행렬 만들기Build a view projection matrix for a perspective camera
import { Matrix4, Vector3 } from "./build/vanilla.js";

const projection = Matrix4.createPerspective(Math.PI / 3, 16 / 9, 0.1, 100);
const eyePosition = Vector3.create(0, 2, 5);
const targetPosition = Vector3.zero();
const upDirection = Vector3.up();
const view = Matrix4.createLookAt(eyePosition, targetPosition, upDirection);
const viewProjection = projection.clone();
viewProjection.multiply(view);
const clip = viewProjection.transformPoint(0, 0, 0);
console.log(clip.x / clip.w, clip.y / clip.w, clip.z / clip.w);
정적staticstatic createOrthographic(left, right, bottom, top, nearDistance, farDistance)#0.3.0 부터since 0.3.0소스source

직교 투영 행렬 생성. (정적)

Creates an orthographic projection matrix. (static)

leftnumber
왼쪽 경계.Left bound.
rightnumber
오른쪽 경계.Right bound.
bottomnumber
아래 경계.Bottom bound.
topnumber
위 경계.Top bound.
nearDistancenumber
가까운 자르기 면까지 거리.Distance to the near clipping plane.
farDistancenumber
먼 자르기 면까지 거리.Distance to the far clipping plane.

반환ReturnsMatrix4 직교 투영 행렬. (경계 상자 안을 -1 ~ 1 로 옮긴다)Orthographic projection matrix. (maps the bounding box to -1 ~ 1)

짝Counterpart: vanilla-engine Matrix4.create_orthographic

정적staticstatic createLookAt(eyePosition, targetPosition, upDirection)#0.3.0 부터since 0.3.0소스source

시점 행렬 생성. (정적, 지정 위치에서 대상을 바라보는 뷰 행렬)

Creates a look-at matrix. (static, a view matrix looking at a target from a given position)

eyePositionVector3
카메라 위치.Camera position.
targetPositionVector3
바라볼 점.Point to look at.
upDirectionVector3
위쪽 방향. (바라보는 방향과 나란하면 안 된다)Up direction. (must not be parallel to the viewing direction)

반환ReturnsMatrix4 뷰 행렬. (카메라가 -Z 를 보는 오른손 좌표계)View matrix. (right-handed, the camera looks down -Z)

짝Counterpart: vanilla-engine Matrix4.create_look_at

정적staticstatic createInverse(matrix)#0.3.0 부터since 0.3.0소스source

역행렬 생성. (정적, 일반 4x4 여인수 전개, 비가역 시 단위 행렬)

Creates an inverse matrix. (static, general 4x4 cofactor expansion, identity matrix if non-invertible)

matrixMatrix4
역행렬을 구할 행렬.Matrix to invert.

반환ReturnsMatrix4 역행렬. (역행렬이 없으면 단위 행렬)Inverse matrix. (identity if the matrix is not invertible)

짝Counterpart: vanilla-engine Matrix4.create_inverse

메서드methodsetIdentity()#0.3.0 부터since 0.3.0소스source

단위 행렬로 초기화.

Resets to the identity matrix.

짝Counterpart: vanilla-engine Matrix4.set_identity

메서드methodcopyFrom(other)#0.3.0 부터since 0.3.0소스source

다른 행렬로부터 복사.

Copies from another matrix.

otherMatrix4
복사해 올 행렬.Matrix to copy from.

짝Counterpart: vanilla-engine Matrix4.copy_from

메서드methodclone()#0.3.0 부터since 0.3.0소스source

복제.

Clone.

반환Returnsthis 요소가 같은 새 행렬.A new matrix with the same elements.

메서드methodmultiply(other)#0.3.0 부터since 0.3.0소스source

다른 행렬을 우측에 곱해 누적. (this = this x other)

Accumulates by multiplying another matrix on the right. (this = this x other)

otherMatrix4
오른쪽에 곱할 행렬.Matrix to multiply on the right.

짝Counterpart: vanilla-engine Matrix4.multiply

메서드methodtranslate(x, y, z)#0.3.0 부터since 0.3.0소스source

이동 변환 누적. (로컬 공간 우측 곱)

Accumulates a translation. (Right multiplication in local space)

xnumber
X 이동량.X translation.
ynumber
Y 이동량.Y translation.
znumber
Z 이동량.Z translation.

짝Counterpart: vanilla-engine Matrix4.translate

메서드methodrotateX(radian)#0.3.0 부터since 0.3.0소스source

X축 회전 변환 누적. (로컬 공간 우측 곱)

Accumulates an X-axis rotation. (Right multiplication in local space)

radiannumber
회전 각도. (라디안)Rotation angle. (radians)

짝Counterpart: vanilla-engine Matrix4.rotate_x

메서드methodrotateY(radian)#0.3.0 부터since 0.3.0소스source

Y축 회전 변환 누적. (로컬 공간 우측 곱)

Accumulates a Y-axis rotation. (Right multiplication in local space)

radiannumber
회전 각도. (라디안)Rotation angle. (radians)

짝Counterpart: vanilla-engine Matrix4.rotate_y

메서드methodrotateZ(radian)#0.3.0 부터since 0.3.0소스source

Z축 회전 변환 누적. (로컬 공간 우측 곱)

Accumulates a Z-axis rotation. (Right multiplication in local space)

radiannumber
회전 각도. (라디안)Rotation angle. (radians)

짝Counterpart: vanilla-engine Matrix4.rotate_z

메서드methodscale(x, y, z)#0.3.0 부터since 0.3.0소스source

크기 변환 누적. (로컬 공간 우측 곱)

Accumulates a scale. (Right multiplication in local space)

xnumber
X 배율.X scale.
ynumber
Y 배율.Y scale.
znumber
Z 배율.Z scale.

짝Counterpart: vanilla-engine Matrix4.scale

메서드methodtransformPoint(x, y, z)#0.3.0 부터since 0.3.0소스source

점 변환. (w 성분 포함, 원근 분할은 호출자가 수행)

Transforms a point. (Includes the w component, perspective division is done by the caller)

xnumber
점의 X 좌표.X coordinate of the point.
ynumber
점의 Y 좌표.Y coordinate of the point.
znumber
점의 Z 좌표.Z coordinate of the point.

반환Returns{ x: number, y: number, z: number, w: number } 변환한 동차 좌표. (w 로 나누지 않은 값)Transformed homogeneous coordinates. (not divided by w)

짝Counterpart: vanilla-engine Matrix4.transform_point

메서드methodtransformDirection(x, y, z)#0.3.0 부터since 0.3.0소스source

방향 변환. (이동 성분 무시, 회전/크기만 적용)

Transforms a direction. (Ignores the translation, applies only rotation/scale)

xnumber
방향의 X 성분.X component of the direction.
ynumber
방향의 Y 성분.Y component of the direction.
znumber
방향의 Z 성분.Z component of the direction.

반환Returns{ x: number, y: number, z: number } 변환한 방향. (정규화하지 않는다)Transformed direction. (not normalized)

짝Counterpart: vanilla-engine Matrix4.transform_direction

메서드methodgetElements()#0.3.0 부터since 0.3.0소스source

열우선 요소 배열 반환.

Returns the column-major element array.

반환ReturnsFloat32Array 열우선 요소 16 개. (복사본이 아니라 내부 배열이다)The 16 column-major elements. (the internal array, not a copy)

짝Counterpart: vanilla-engine Matrix4.get_elements

메서드methodwriteToArray(array)#0.4.0 부터since 0.4.0소스source

셰이더 mat4 (열 우선) 배열로 기록. (16 칸)

Writes as a shader mat4 (column-major) array. (16 slots)

arrayFloat32Array | number[]
적을 배열. (0 번 칸부터 16 칸)Array to write into. (16 slots from index 0)

짝Counterpart: vanilla-engine Matrix4.write_to_array

메서드methodwriteToSlice(array, offset)#0.4.0 부터since 0.4.0소스source

큰 배열의 offset 자리부터 16 개를 기록. (관절 행렬 배열처럼 여러 행렬을 이어 담을 때)

Writes 16 values starting at offset in a large array. (When packing several matrices back to back, like a joint matrix array)

arrayFloat32Array | number[]
적을 배열.Array to write into.
offsetnumber
적기 시작할 칸 번호.Index to start writing at.

짝Counterpart: vanilla-engine Matrix4.write_to_slice