graphics3d/camera3d
3D 카메라. (원근이나 직교 투영과 보는 자리를 들고, 보기와 투영 행렬을 바뀔 때만 다시 셈한다)
3D camera. (holds a perspective or orthographic projection and the viewpoint, recomputes the view and projection matrices only when they change)
Camera3D
3D 카메라. (원근이나 직교 투영과 보는 자리를 들고, 보기와 투영 행렬을 바뀔 때만 다시 셈한다)
- 좌표는 엔진 Matrix4 와 같은 y 가 위인 오른손 좌표다. 카메라는 -Z 를 본다. (createLookAt, createPerspective)
- 원근: setPerspective(시야각, 가까운 거리, 먼 거리), 직교: setOrthographic(보이는 높이, 가까운 거리, 먼 거리). 가로 세로 비는 setAspectRatio 로 따로 준다. (그리는 사각형이 바뀔 때마다 고쳐 넣는다, 직교의 너비는 높이 x 비)
- lookAt(눈, 보는 곳, 위) 로 보는 자리를 정한다.
- getViewProjectionMatrix 는 담아 둔 행렬을 돌려준다. 값을 바꾸는 함수를 부른 뒤 처음 읽을 때만 다시 셈한다. 돌려받은 행렬을 고치지 않는다. (고칠 것은 clone)
- 같은 카메라를 렌더러(ToonRenderer.beginFrameWithCamera)와 2D 표시(projectToView)가 함께 써, 3D 점 위에 글이나 단추를 얹는다.
- projectToView(x, y, z, viewRect): 월드 점을 viewRect 안의 자리로. viewRect 는 3D 를 그리는 사각형을 부르는 쪽의 좌표(보통 뷰 좌표)로 준 것이다.
- unprojectToPlane(viewPosition, planeHeight, viewRect): viewRect 안의 자리에서 나간 시선이 높이 planeHeight 의 수평면(y = planeHeight)과 만나는 점.
- 원 행렬: setMatrices(보기, 투영) 로 게임이 셈한 행렬을 그대로 쓴다. (기울인 직교, 화면 변환을 곱한 투영 같은 것) 위 함수들이 모두 그 행렬로 돈다. 보기 행렬은 월드 → 카메라의 강체 변환(카메라는 -Z 를 봄)으로 준다. 눈, 보는 곳(눈 앞 1), 위쪽과 isOrthographic 은 그 행렬에서 뽑는다. 시야각, 보이는 높이, 가까운 거리, 먼 거리, 가로 세로 비는 마지막으로 준 값 그대로다. setPerspective, setOrthographic, lookAt 을 부르면 원 행렬을 버리고 셈하는 길로 돌아간다.
- 보기 덧붙임: setViewOffset(자리, 돌림) 은 카메라 지역 축의 덧붙임을 마지막에 건다. (CameraShake 의 흔들림, 걸음 흔들림, 반동 같은 것, 셈하는 길과 원 행렬 모두) 보기 행렬과 그 뒤의 행렬, projectToView, unprojectToPlane 은 덧붙인 카메라로 돈다. getEyePosition, getTargetPosition, getUpDirection 은 덧붙이기 앞 값이다.
3D camera. (holds a perspective or orthographic projection and the viewpoint, recomputes the view and projection matrices only when they change)
- Coordinates are right-handed with y up, same as the engine's Matrix4. The camera looks down -Z. (createLookAt, createPerspective)
- Perspective: setPerspective(field of view, near distance, far distance), orthographic: setOrthographic(visible height, near distance, far distance). The aspect ratio is given separately with setAspectRatio. (update it whenever the drawing rectangle changes, orthographic width is height x ratio)
- lookAt(eye, target, up) sets the viewpoint.
- getViewProjectionMatrix returns the cached matrix. It recomputes only on the first read after a function that changes values is called. Do not modify the returned matrix. (clone to modify)
- The renderer (ToonRenderer.beginFrameWithCamera) and the 2D display (projectToView) share the same camera, to put text or buttons over 3D points.
- projectToView(x, y, z, viewRect): a world point to a position in viewRect. viewRect is the rectangle the 3D is drawn in, given in the caller's coordinates (usually view coordinates).
- unprojectToPlane(viewPosition, planeHeight, viewRect): the point where the line of sight from a position in viewRect meets the horizontal plane at height planeHeight (y = planeHeight).
- Raw matrices: setMatrices(view, projection) uses the matrices the game computed as is. (oblique orthographic, a projection multiplied by a screen transform, and so on) All the functions above run on those matrices. The view matrix is given as a rigid world → camera transform (the camera looks down -Z). Eye, target (1 in front of the eye), up and isOrthographic are extracted from that matrix. Field of view, visible height, near distance, far distance and aspect ratio stay as last given. Calling setPerspective, setOrthographic or lookAt discards the raw matrices and returns to the computed path.
- View offset: setViewOffset(position, rotation) applies an offset in camera local axes last. (CameraShake shakes, head bob, recoil and the like, on both the computed path and raw matrices) The view matrix, the matrices after it, projectToView and unprojectToPlane run with the offset camera. getEyePosition, getTargetPosition and getUpDirection are the values before the offset.
import { Camera3D, Rect, Vector3 } from "./build/vanilla.js";
const camera = new Camera3D();
camera.setPerspective(Math.PI / 3, 0.1, 500);
camera.setAspectRatio(640 / 360);
const eyePosition = Vector3.create(0, 8, 12);
const targetPosition = Vector3.create(0, 0, 0);
camera.lookAt(eyePosition, targetPosition);
const viewRect = Rect.create(0, 0, 640, 360);
const screenPoint = camera.projectToView(0, 1, 0, viewRect);
console.log(screenPoint.x, screenPoint.y, screenPoint.isInFront);
const viewProjectionMatrix = camera.getViewProjectionMatrix();
const elements = viewProjectionMatrix.getElements();
console.log(elements.length);생성. (원근 시야각 0.8 라디안, 비 1, 거리 0.1 ~ 1000, 눈 (0, 0, 10) 에서 원점을 본다)
Constructor. (perspective field of view 0.8 radians, ratio 1, distance 0.1 ~ 1000, looking at the origin from eye (0, 0, 10))
짝Counterpart: vanilla-engine Camera3D.new
원근 투영 설정.
Sets a perspective projection.
- fieldOfViewRadiannumber
- 세로 시야각.Vertical field of view.
- nearDistancenumber
- 가까운 거리. (월드 단위)Near distance. (world units)
- farDistancenumber
- 먼 거리. (월드 단위)Far distance. (world units)
짝Counterpart: vanilla-engine Camera3D.set_perspective
직교 투영 설정. (보이는 높이, 너비는 높이 x 가로 세로 비)
Sets an orthographic projection. (visible height, width is height x aspect ratio)
- orthographicHeightnumber
- 보이는 높이. (월드 단위, 너비는 높이 x 가로 세로 비)Visible height. (world units, width is height x aspect ratio)
- nearDistancenumber
- 가까운 거리. (월드 단위)Near distance. (world units)
- farDistancenumber
- 먼 거리. (월드 단위)Far distance. (world units)
짝Counterpart: vanilla-engine Camera3D.set_orthographic
직교 투영인지.
Whether it is an orthographic projection.
반환Returnsboolean 직교 투영이면 참.True for orthographic projection.
짝Counterpart: vanilla-engine Camera3D.is_orthographic
가로 세로 비 설정. (너비 / 높이, 같으면 그대로)
Sets the aspect ratio. (width / height, unchanged if the same)
- aspectRationumber
- 가로 세로 비. (너비 / 높이, 0 이하나 NaN 이면 무시한다)Aspect ratio. (width / height, ignored when 0 or less or NaN)
짝Counterpart: vanilla-engine Camera3D.set_aspect_ratio
가로 세로 비 반환.
Returns the aspect ratio.
반환Returnsnumber 가로 세로 비. (너비 / 높이)Aspect ratio. (width / height)
짝Counterpart: vanilla-engine Camera3D.get_aspect_ratio
원근의 세로 시야각 반환.
Returns the vertical field of view of the perspective.
반환Returnsnumber 원근의 세로 시야각. (라디안)Vertical field of view of the perspective. (radians)
짝Counterpart: vanilla-engine Camera3D.get_field_of_view
직교의 보이는 높이 반환.
Returns the orthographic visible height.
반환Returnsnumber 직교의 보이는 높이. (월드 단위)Visible height of the orthographic projection. (world units)
짝Counterpart: vanilla-engine Camera3D.get_orthographic_height
가까운 거리 반환.
Returns the near distance.
반환Returnsnumber 가까운 거리. (월드 단위)Near distance. (world units)
짝Counterpart: vanilla-engine Camera3D.get_near_distance
먼 거리 반환.
Returns the far distance.
반환Returnsnumber 먼 거리. (월드 단위)Far distance. (world units)
짝Counterpart: vanilla-engine Camera3D.get_far_distance
보는 자리 설정. (값을 복사해 둔다)
Sets the viewpoint. (copies the values)
- eyePositionVector3
- 눈의 월드 자리.World position of the eye.
- targetPositionVector3
- 보는 곳의 월드 자리.World position being looked at.
- upDirectionVector3
- 기본 (0, 1, 0).Default (0, 1, 0).
짝Counterpart: vanilla-engine Camera3D.look_at
눈의 자리 반환.
Returns the eye position.
반환ReturnsVector3 눈의 월드 자리. (담아 둔 벡터, 고치지 않는다)World position of the eye. (the stored vector, do not modify)
짝Counterpart: vanilla-engine Camera3D.get_eye_position
보는 곳 반환.
Returns the target.
반환ReturnsVector3 보는 곳의 월드 자리. (담아 둔 벡터, 고치지 않는다)World position being looked at. (the stored vector, do not modify)
짝Counterpart: vanilla-engine Camera3D.get_target_position
위쪽 반환.
Returns the up direction.
반환ReturnsVector3 위쪽 방향. (담아 둔 벡터, 고치지 않는다)Up direction. (the stored vector, do not modify)
짝Counterpart: vanilla-engine Camera3D.get_up_direction
원 행렬 설정. (게임이 셈한 보기와 투영을 그대로 쓴다, 값을 복사해 둔다, 머리말)
Sets the raw matrices. (uses the view and projection the game computed as is, copies the values, header)
- viewMatrixMatrix4
- 월드 → 카메라.World → camera.
- projectionMatrixMatrix4
- 카메라 → 클립.Camera → clip.
짝Counterpart: vanilla-engine Camera3D.set_matrices
import { Camera3D, Matrix4, Vector3 } from "./build/vanilla.js";
const eyePosition = Vector3.create(10, 10, 10);
const targetPosition = Vector3.create(0, 0, 0);
const upDirection = Vector3.up();
const viewMatrix = Matrix4.createLookAt(eyePosition, targetPosition, upDirection);
const projectionMatrix = Matrix4.createOrthographic(-8, 8, -4.5, 4.5, 0.1, 100);
const camera = new Camera3D();
camera.setMatrices(viewMatrix, projectionMatrix);
const recoveredEyePosition = camera.getEyePosition();
const isOrthographic = camera.isOrthographic();
console.log(isOrthographic, recoveredEyePosition.x, recoveredEyePosition.y, recoveredEyePosition.z);
camera.setPerspective(0.8, 0.1, 100);
const hasRawMatrices = camera.hasRawMatrices();
console.log(hasRawMatrices);원 행렬을 쓰는지.
Whether raw matrices are used.
반환Returnsboolean setMatrices 로 받은 원 행렬을 쓰면 참.True when the raw matrices given to setMatrices are in use.
짝Counterpart: vanilla-engine Camera3D.has_raw_matrices
원 행렬 버리기. (셈하는 길로)
Discards the raw matrices. (back to the computed path)
짝Counterpart: vanilla-engine Camera3D.clear_raw_matrices
보기 덧붙임 설정. (카메라 지역 축의 자리와 돌림을 보기 행렬 마지막에 건다, 값을 복사해 둔다, 둘 다 영 벡터면 덧붙임 없음)
Sets the view offset. (applies position and rotation in camera local axes last to the view matrix, copies the values, no offset when both are zero vectors)
- positionVector3
- 카메라 지역 자리 덧붙임. (장면 길이, x 오른쪽, y 위, z 뒤)Position offset in camera local axes. (scene length, x right, y up, z back)
- rotationVector3
- 카메라 지역 돌림 덧붙임. (라디안, x pitch, y yaw, z roll, 기본 영 벡터)Rotation offset in camera local axes. (radians, x pitch, y yaw, z roll, zero vector by default)
짝Counterpart: vanilla-engine Camera3D.set_view_offset
import { Camera3D, CameraShake, Vector3 } from "./vanilla.js";
const camera = new Camera3D();
camera.lookAt(Vector3.create(0, 4, 10), Vector3.create(0, 0, 0));
const cameraShake = new CameraShake();
cameraShake.add({ amplitude: 0.1, rotationAmplitude: 0.02, duration: 0.3 });
cameraShake.update(1 / 60);
const shake = cameraShake.read();
camera.setViewOffset(shake.position, shake.rotation);
const shakenViewMatrix = camera.getViewMatrix().clone();
camera.setViewOffset(Vector3.zero(), Vector3.zero());
console.log(shakenViewMatrix.getElements()[12], camera.getViewMatrix().getElements()[12]);보기 행렬 반환. (월드 → 카메라)
Returns the view matrix. (world → camera)
반환ReturnsMatrix4 보기 행렬. (담아 둔 행렬, 고칠 것은 clone)View matrix. (the stored matrix, clone it to modify)
짝Counterpart: vanilla-engine Camera3D.get_view_matrix
투영 행렬 반환. (카메라 → 클립)
Returns the projection matrix. (camera → clip)
반환ReturnsMatrix4 투영 행렬. (담아 둔 행렬, 고칠 것은 clone)Projection matrix. (the stored matrix, clone it to modify)
짝Counterpart: vanilla-engine Camera3D.get_projection_matrix
보기와 투영 행렬 반환. (월드 → 클립)
Returns the view-projection matrix. (world → clip)
반환ReturnsMatrix4 투영 x 보기 행렬. (담아 둔 행렬, 고칠 것은 clone)Projection x view matrix. (the stored matrix, clone it to modify)
짝Counterpart: vanilla-engine Camera3D.get_view_projection_matrix
보기와 투영의 역행렬 반환. (클립 → 월드)
Returns the inverse view-projection matrix. (clip → world)
반환ReturnsMatrix4 보기와 투영의 역행렬. (담아 둔 행렬, 고칠 것은 clone)Inverse of the view and projection. (the stored matrix, clone it to modify)
짝Counterpart: vanilla-engine Camera3D.get_inverse_view_projection_matrix
월드 점의 카메라 앞 깊이. (보는 쪽으로 잰 거리, 눈의 면보다 뒤면 음수, 비치는 것을 먼 것부터 그릴 때)
Depth of a world point in front of the camera. (distance measured along the view direction, negative if behind the eye plane, for drawing transparent things far to near)
- xnumber
- 월드 X.World X.
- ynumber
- 월드 Y.World Y.
- znumber
- 월드 Z.World Z.
반환Returnsnumber 카메라가 보는 쪽으로 잰 깊이. (월드 단위, 눈의 면보다 뒤면 음수)Depth measured along the camera's view direction. (world units, negative behind the eye plane)
짝Counterpart: vanilla-engine Camera3D.read_view_depth
월드 점을 viewRect 안의 자리로. (viewRect 의 좌표, 왼쪽 위가 원점이고 아래로 늘어남)
- isInFront 는 점이 눈의 면보다 앞인지다. 뒤면 x, y 는 0 이다. 화면 밖인지는 부르는 쪽이 x, y 를 보고 정한다.
- result 를 주면 거기에 담아 돌려준다. (매 프레임 새 객체를 만들지 않게)
A world point to a position in viewRect. (viewRect coordinates, origin at top-left, increasing downward)
- isInFront is whether the point is in front of the eye plane. If behind, x and y are 0. Whether it is off-screen is decided by the caller from x, y.
- If result is given, it is filled and returned. (so a new object is not created every frame)
- xnumber
- 월드 X.World X.
- ynumber
- 월드 Y.World Y.
- znumber
- 월드 Z.World Z.
- viewRectRect
- 3D 를 그리는 사각형. (부르는 쪽 좌표, 보통 뷰 좌표)Rectangle where 3D is drawn. (in the caller's coordinates, usually view coordinates)
- resultobject
- { x, y, depth, isInFront }
반환Returnsobject { x, y, depth, isInFront }
짝Counterpart: vanilla-engine Camera3D.project_to_view
import { Camera3D, Rect } from "./build/vanilla.js";
const camera = new Camera3D();
const viewRect = Rect.create(0, 0, 640, 360);
const projected = { x: 0, y: 0, depth: 0, isInFront: false };
const pointList = [[0, 0, 0], [2, 1, 0], [0, 0, 20]];
for (const point of pointList) {
camera.projectToView(point[0], point[1], point[2], viewRect, projected);
console.log(projected.x, projected.y, projected.depth, projected.isInFront);
}viewRect 안의 자리에서 나간 시선이 수평면 y = planeHeight 와 만나는 점. (나란하거나 눈 뒤에서 만나면 null)
The point where the line of sight from a position in viewRect meets the horizontal plane y = planeHeight. (null if parallel or meeting behind the eye)
- viewPositionVector2
- viewRect 와 같은 좌표.Same coordinates as viewRect.
- planeHeightnumber
- 수평면의 높이. (월드 y)Height of the horizontal plane. (world y)
- viewRectRect
- 3D 를 그리는 사각형. (viewPosition 과 같은 좌표)Rectangle where 3D is drawn. (same coordinates as viewPosition)
반환ReturnsVector3 | null 시선과 수평면이 만나는 월드 점. (나란하거나 눈 뒤에서 만나거나 viewRect 크기가 0 이하면 null)World point where the ray meets the horizontal plane. (null when parallel, when it meets behind the eye, or when the viewRect size is 0 or less)
짝Counterpart: vanilla-engine Camera3D.unproject_to_plane
import { Camera3D, Rect, Vector2, Vector3 } from "./build/vanilla.js";
const camera = new Camera3D();
camera.setAspectRatio(640 / 360);
const eyePosition = Vector3.create(0, 10, 10);
const targetPosition = Vector3.create(0, 0, 0);
camera.lookAt(eyePosition, targetPosition);
const viewRect = Rect.create(0, 0, 640, 360);
const viewPosition = Vector2.create(320, 180);
const groundPoint = camera.unprojectToPlane(viewPosition, 0, viewRect);
if (groundPoint !== null) {
console.log(groundPoint.x, groundPoint.y, groundPoint.z);
}