graphics3d/camera3d
3D 카메라. (vanilla.js graphics3d/camera3d.js 의 Camera3D)
- 원근이나 직교 투영과 보는 자리를 들고, 보기와 투영 행렬을 바뀔 때만 다시 셈한다.
- 좌표는 엔진 Matrix4 와 같은 y 가 위인 오른손 좌표다. 카메라는 -Z 를 본다. (create_look_at, create_perspective)
- 원근: set_perspective(시야각, 가까운 거리, 먼 거리), 직교: set_orthographic(보이는 높이, 가까운 거리, 먼 거리). 가로 세로 비는 set_aspect_ratio 로 따로 준다. (그리는 사각형이 바뀔 때마다 고쳐 넣는다, 직교의 너비는 높이 x 비)
- 행렬은 Cell 에 담아 두고 &self 로 읽을 때 바뀐 값이 있으면 다시 셈한다. (원본은 getter 가 #isMatrixDirty 를 보고 셈한다)
- 같은 카메라를 렌더러(ToonRenderer::begin_frame_with_camera)와 2D 표시(project_to_view)가 함께 써, 3D 점 위에 글이나 단추를 얹는다.
- project_to_view(x, y, z, view_rect): 월드 점을 view_rect 안의 자리로. view_rect 는 3D 를 그리는 사각형을 부르는 쪽의 좌표(보통 뷰 좌표)로 준 것이다. 원본의 { x, y, depth, isInFront } 는 ProjectedPoint 로 돌려준다.
- unproject_to_plane(view_position, plane_height, view_rect): view_rect 안의 자리에서 나간 시선이 수평면(y = plane_height)과 만나는 점. 없으면 None.
- 원 행렬: set_matrices(보기, 투영) 로 게임이 셈한 행렬을 그대로 쓴다. (기울인 직교, 화면 변환을 곱한 투영 같은 것) 위 함수들이 모두 그 행렬로 돈다. 보기 행렬은 월드 → 카메라의 강체 변환(카메라는 -Z 를 봄)으로 준다. 눈, 보는 곳(눈 앞 1), 위쪽과 is_orthographic 은 그 행렬에서 뽑는다. 시야각, 보이는 높이, 가까운 거리, 먼 거리, 가로 세로 비는 마지막으로 준 값 그대로다. set_perspective, set_orthographic, look_at 을 부르면 원 행렬을 버리고 셈하는 길로 돌아간다.
- 보기 덧붙임: set_view_offset(자리, 돌림) 은 카메라 지역 축의 덧붙임을 마지막에 건다. (CameraShake 의 흔들림, 걸음 흔들림, 반동 같은 것, 셈하는 길과 원 행렬 모두) 보기 행렬과 그 뒤의 행렬, project_to_view, unproject_to_plane 은 덧붙인 카메라로 돈다. get_eye_position, get_target_position, get_up_direction 은 덧붙이기 앞 값이다.
3D camera. (Camera3D of vanilla.js graphics3d/camera3d.js)
- Holds a perspective or orthographic projection and the view position, and recomputes the view and projection matrices only when they change.
- Coordinates are right-handed with y up, the same as the engine Matrix4. The camera looks toward -Z. (create_look_at, create_perspective)
- Perspective: set_perspective(field of view, near distance, far distance), orthographic: set_orthographic(visible height, near distance, far distance). The aspect ratio is given separately with set_aspect_ratio. (update it whenever the drawing rectangle changes, the orthographic width is height x ratio)
- Matrices are kept in Cell and recomputed when read through &self if a value changed. (the original getters check #isMatrixDirty and compute)
- The renderer (ToonRenderer::begin_frame_with_camera) and 2D display (project_to_view) share the same camera to place text or buttons over 3D points.
- project_to_view(x, y, z, view_rect): a world point to a position inside view_rect. view_rect is the rectangle where 3D is drawn, given in the caller's coordinates (usually view coordinates). The original { x, y, depth, isInFront } is returned as ProjectedPoint.
- unproject_to_plane(view_position, plane_height, view_rect): the point where the line of sight from a position inside view_rect meets the horizontal plane (y = plane_height). None if there is none.
- Raw matrices: set_matrices(view, projection) uses matrices computed by the game as is. (such as a tilted orthographic, or a projection multiplied by a screen transform) All the functions above then work with those matrices. The view matrix is given as a rigid transform from world to camera (the camera looks toward -Z). The eye, target (1 in front of the eye), up and is_orthographic are extracted from that matrix. The field of view, visible height, near distance, far distance and aspect ratio stay as last given. Calling set_perspective, set_orthographic or look_at drops the raw matrices and returns to the computed path.
- View offset: set_view_offset(position, rotation) applies an offset in camera local axes last. (such as CameraShake shakes, walk bobbing and recoil, on both the computed path and raw matrices) The view matrix and the matrices after it, project_to_view and unproject_to_plane run with the offset camera. get_eye_position, get_target_position and get_up_direction are the values before the offset.
ProjectedPointstruct traits Clone, Copy, Debug, Default, PartialEq
투영한 점. (view_rect 의 좌표, 카메라 앞 깊이, 눈의 면보다 앞인지, 앞이 아니면 x, y 는 0)
Projected point. (view_rect coordinates, depth in front of the camera, whether it is in front of the eye plane, x and y are 0 when not in front)
use vanilla::{Camera3D, ProjectedPoint, Rect};
let camera = Camera3D::new();
let view_rect = Rect::create(0.0, 0.0, 640.0, 480.0);
let behind_point: ProjectedPoint = camera.project_to_view(0.0, 0.0, 20.0, view_rect);
assert!(!behind_point.is_in_front);
assert_eq!((behind_point.x, behind_point.y), (0.0, 0.0));
assert!((behind_point.depth + 10.0).abs() < 0.001);view_rect 좌표의 가로 자리. (앞이 아니면 0)
Horizontal position in view_rect coordinates. (0 when not in front)
반환Returnsf64
view_rect 좌표의 세로 자리. (위가 0 이고 아래로 늘어남, 앞이 아니면 0)
Vertical position in view_rect coordinates. (0 at the top, increasing downward, 0 when not in front)
반환Returnsf64
카메라 앞 깊이. (보는 쪽으로 잰 거리, 눈의 면보다 뒤면 음수, read_view_depth 와 같다)
Depth in front of the camera. (distance measured along the view direction, negative if behind the eye plane, same as read_view_depth)
반환Returnsf64
눈의 면보다 앞인지. (깊이가 0.0001 이상이고 클립 w 가 0 이 아니면 true)
Whether it is in front of the eye plane. (true if the depth is 0.0001 or more and clip w is not 0)
반환Returnsbool
Camera3Dstruct traits Clone, Debug, Default
3D 카메라. (원근이나 직교 투영과 보는 자리, 보기와 투영 행렬은 바뀐 뒤 처음 읽을 때 다시 셈한다)
- 기본은 원근 시야각 0.8 라디안, 가로 세로 비 1, 거리 0.1 ~ 1000, 눈 (0, 0, 10) 에서 원점을 본다.
3D camera. (perspective or orthographic projection and view position, the view and projection matrices are recomputed on the first read after a change)
- The default is a perspective field of view of 0.8 radians, aspect ratio 1, distance 0.1 ~ 1000, looking at the origin from eye (0, 0, 10).
use vanilla::{Camera3D, Rect, Vector2, Vector3};
let mut camera = Camera3D::new();
camera.set_perspective(0.8, 0.1, 100.0);
camera.set_aspect_ratio(2.0);
camera.look_at(Vector3::create(0.0, 5.0, 10.0), Vector3::zero(), Vector3::up());
let view_rect = Rect::create(0.0, 0.0, 400.0, 200.0);
let projected_point = camera.project_to_view(0.0, 0.0, 0.0, view_rect);
assert!(projected_point.is_in_front);
assert!((projected_point.x - 200.0).abs() < 0.01);
assert!((projected_point.y - 100.0).abs() < 0.01);
let ground_point = camera.unproject_to_plane(Vector2::create(projected_point.x, projected_point.y), 0.0, view_rect).unwrap();
assert!(ground_point.x.abs() < 0.01);
assert!(ground_point.z.abs() < 0.01);생성. (원근 시야각 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))
반환ReturnsCamera3D 원근 시야각 0.8 라디안, 가로 세로 비 1, 거리 0.1 ~ 1000, 눈 (0, 0, 10) 에서 원점을 보는(위쪽 (0, 1, 0)) 새 카메라.A new camera with a perspective field of view of 0.8 radians, aspect ratio 1, distance 0.1 ~ 1000, looking at the origin from eye (0, 0, 10) (up (0, 1, 0)).
짝Counterpart: vanilla.js Camera3D.constructor
pub fn set_perspective(&mut self, field_of_view_radian: f64, near_distance: f64, far_distance: f64)#원근 투영 설정. (세로 시야각)
- set_matrices 로 준 원 행렬이 있으면 버리고 셈하는 길로 돌아간다.
Sets a perspective projection. (vertical field of view)
- If there are raw matrices given with set_matrices, they are dropped and it returns to the computed path.
- field_of_view_radianf64
- 세로 시야각. (라디안)Vertical field of view. (radians)
- near_distancef64
- 가까운 거리. (카메라 앞, 월드 단위)Near distance. (in front of the camera, world units)
- far_distancef64
- 먼 거리. (월드 단위)Far distance. (world units)
짝Counterpart: vanilla.js Camera3D.setPerspective
pub fn set_orthographic(&mut self, orthographic_height: f64, near_distance: f64, far_distance: f64)#직교 투영 설정. (보이는 높이, 너비는 높이 x 가로 세로 비)
- set_matrices 로 준 원 행렬이 있으면 버리고 셈하는 길로 돌아간다.
Sets an orthographic projection. (visible height, the width is height x aspect ratio)
- If there are raw matrices given with set_matrices, they are dropped and it returns to the computed path.
- orthographic_heightf64
- 보이는 높이. (월드 단위, 가운데에서 위아래로 절반씩)Visible height. (world units, half above and half below the center)
- near_distancef64
- 가까운 거리. (카메라 앞, 월드 단위)Near distance. (in front of the camera, world units)
- far_distancef64
- 먼 거리. (월드 단위)Far distance. (world units)
짝Counterpart: vanilla.js Camera3D.setOrthographic
use vanilla::{Camera3D, Rect, Vector3};
let mut camera = Camera3D::new();
camera.set_orthographic(10.0, 0.1, 100.0);
camera.set_aspect_ratio(2.0);
camera.look_at(Vector3::create(0.0, 0.0, 10.0), Vector3::zero(), Vector3::up());
assert!(camera.is_orthographic());
let projected_point = camera.project_to_view(10.0, 5.0, 0.0, Rect::create(0.0, 0.0, 200.0, 100.0));
assert!((projected_point.x - 200.0).abs() < 0.01);
assert!(projected_point.y.abs() < 0.01);직교 투영인지.
Whether it is an orthographic projection.
반환Returnsbool 직교 투영이면 true. (원 행렬이면 투영 행렬에서 가린 값)true for an orthographic projection. (with raw matrices, the value detected from the projection matrix)
짝Counterpart: vanilla.js Camera3D.isOrthographic
가로 세로 비 설정. (너비 / 높이, 0 이하이거나 같으면 그대로)
Sets the aspect ratio. (width / height, unchanged if 0 or less or the same)
- aspect_ratiof64
- 가로 세로 비. (너비 / 높이, NaN 이나 0 이하는 무시한다)Aspect ratio. (width / height, NaN or 0 or less is ignored)
짝Counterpart: vanilla.js Camera3D.setAspectRatio
가로 세로 비 반환.
Returns the aspect ratio.
반환Returnsf64 가로 세로 비. (너비 / 높이, 기본 1)Aspect ratio. (width / height, default 1)
짝Counterpart: vanilla.js Camera3D.getAspectRatio
원근의 세로 시야각 반환.
Returns the vertical field of view of the perspective.
반환Returnsf64 세로 시야각. (라디안, 기본 0.8, 원 행렬을 써도 마지막으로 준 값)Vertical field of view. (radians, default 0.8, the last given value even when raw matrices are used)
짝Counterpart: vanilla.js Camera3D.getFieldOfView
직교의 보이는 높이 반환.
Returns the orthographic visible height.
반환Returnsf64 보이는 높이. (월드 단위, 기본 10)Visible height. (world units, default 10)
짝Counterpart: vanilla.js Camera3D.getOrthographicHeight
가까운 거리 반환.
Returns the near distance.
반환Returnsf64 가까운 거리. (월드 단위, 기본 0.1)Near distance. (world units, default 0.1)
짝Counterpart: vanilla.js Camera3D.getNearDistance
먼 거리 반환.
Returns the far distance.
반환Returnsf64 먼 거리. (월드 단위, 기본 1000)Far distance. (world units, default 1000)
짝Counterpart: vanilla.js Camera3D.getFarDistance
pub fn look_at(&mut self, eye_position: Vector3, target_position: Vector3, up_direction: Vector3)#보는 자리 설정.
- set_matrices 로 준 원 행렬이 있으면 버리고 셈하는 길로 돌아간다.
Sets the view position.
- If there are raw matrices given with set_matrices, they are dropped and it returns to the computed path.
- eye_positionVector3
- 눈의 자리. (월드)Eye position. (world)
- target_positionVector3
- 보는 곳. (월드)Target. (world)
- up_directionVector3
- 위쪽. (보통 Vector3::up())Up direction. (usually Vector3::up())
짝Counterpart: vanilla.js Camera3D.lookAt
눈의 자리 반환.
Returns the eye position.
반환ReturnsVector3 눈의 자리. (월드, 원 행렬이면 보기 행렬에서 뽑은 자리)Eye position. (world, with raw matrices the position extracted from the view matrix)
짝Counterpart: vanilla.js Camera3D.getEyePosition
보는 곳 반환.
Returns the target.
반환ReturnsVector3 보는 곳. (월드, 원 행렬이면 눈 앞 1 의 자리)Target. (world, with raw matrices the position 1 in front of the eye)
짝Counterpart: vanilla.js Camera3D.getTargetPosition
위쪽 반환.
Returns the up direction.
반환ReturnsVector3 위쪽. (원 행렬이면 보기 행렬의 위쪽 축)Up direction. (with raw matrices the up axis of the view matrix)
짝Counterpart: vanilla.js Camera3D.getUpDirection
원 행렬 설정. (게임이 셈한 보기와 투영을 그대로 쓴다, 머리말)
Sets the raw matrices. (uses the view and projection the game computed as is, see the header comment)
- view_matrix&Matrix4
- 보기 행렬. (월드 → 카메라의 강체 변환, 카메라는 -Z 를 본다)View matrix. (rigid transform from world to camera, the camera looks toward -Z)
- projection_matrix&Matrix4
- 투영 행렬. (카메라 → 클립,
elements[11]의 절댓값이 0.5 밑이면 직교로 본다)Projection matrix. (camera → clip, treated as orthographic when the absolute value ofelements[11]is below 0.5)
짝Counterpart: vanilla.js Camera3D.setMatrices
use vanilla::{Camera3D, Matrix4, Vector3};
let view_matrix = Matrix4::create_look_at(Vector3::create(0.0, 5.0, 10.0), Vector3::zero(), Vector3::up());
let mut projection_matrix = Matrix4::create_orthographic(-4.0, 4.0, -2.0, 2.0, 0.1, 50.0);
projection_matrix.elements[8] = 0.3;
let mut camera = Camera3D::new();
camera.set_matrices(&view_matrix, &projection_matrix);
assert!(camera.has_raw_matrices());
assert!(camera.is_orthographic());
assert!((camera.get_eye_position().y - 5.0).abs() < 0.001);
assert_eq!(camera.get_projection_matrix(), projection_matrix);
camera.set_perspective(0.8, 0.1, 100.0);
assert!(!camera.has_raw_matrices());
assert!(!camera.is_orthographic());원 행렬을 쓰는지.
Whether raw matrices are used.
반환Returnsbool set_matrices 로 준 원 행렬을 쓰고 있으면 true.true if the raw matrices given with set_matrices are in use.
짝Counterpart: vanilla.js Camera3D.hasRawMatrices
원 행렬 버리기. (셈하는 길로)
Discards the raw matrices. (back to the computed path)
짝Counterpart: vanilla.js Camera3D.clearRawMatrices
보기 덧붙임 설정. (카메라 지역 축의 자리와 돌림을 보기 행렬 마지막에 건다, 둘 다 영 벡터면 덧붙임 없음)
Set the view offset. (applies position and rotation in camera local axes last to the view matrix, 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, 원본 기본 영 벡터는 Vector3::zero() 를 준다)Rotation offset in camera local axes. (radians, x pitch, y yaw, z roll, pass Vector3::zero() for the original default zero vector)
짝Counterpart: vanilla.js Camera3D.setViewOffset
use vanilla::{Camera3D, CameraShake, CameraShakeOptions, Vector3};
let mut camera = Camera3D::new();
camera.look_at(Vector3::create(0.0, 4.0, 10.0), Vector3::zero(), Vector3::up());
let plain_view_matrix = camera.get_view_matrix();
let mut camera_shake = CameraShake::default();
camera_shake.add(CameraShakeOptions { amplitude: 0.1, rotation_amplitude: 0.02, duration: 0.3, ..Default::default() });
camera_shake.update(1.0 / 60.0);
let shake = camera_shake.read();
camera.set_view_offset(shake.position, shake.rotation);
let shaken_view_matrix = camera.get_view_matrix();
camera.set_view_offset(Vector3::zero(), Vector3::zero());
assert_ne!(shaken_view_matrix.get_elements()[12], plain_view_matrix.get_elements()[12]);
assert_eq!(camera.get_view_matrix().get_elements(), plain_view_matrix.get_elements());보기 행렬 반환. (월드 → 카메라)
Returns the view matrix. (world → camera)
반환ReturnsMatrix4 보기 행렬. (원 행렬이면 그 행렬, 아니면 눈, 보는 곳, 위쪽으로 셈한 것)View matrix. (with raw matrices that matrix, otherwise computed from the eye, target and up)
짝Counterpart: vanilla.js Camera3D.getViewMatrix
투영 행렬 반환. (카메라 → 클립)
Returns the projection matrix. (camera → clip)
반환ReturnsMatrix4 투영 행렬. (원 행렬이면 그 행렬, 아니면 원근이나 직교로 셈한 것)Projection matrix. (with raw matrices that matrix, otherwise computed as perspective or orthographic)
짝Counterpart: vanilla.js Camera3D.getProjectionMatrix
보기와 투영 행렬 반환. (월드 → 클립)
Returns the view-projection matrix. (world → clip)
반환ReturnsMatrix4 투영 x 보기 행렬. (월드 점을 클립 좌표로)Projection x view matrix. (world points to clip coordinates)
짝Counterpart: vanilla.js Camera3D.getViewProjectionMatrix
보기와 투영의 역행렬 반환. (클립 → 월드)
Returns the inverse view-projection matrix. (clip → world)
반환ReturnsMatrix4 보기와 투영 행렬의 역행렬. (클립 좌표를 월드로)Inverse of the view and projection matrix. (clip coordinates to world)
짝Counterpart: vanilla.js Camera3D.getInverseViewProjectionMatrix
월드 점의 카메라 앞 깊이. (보는 쪽으로 잰 거리, 눈의 면보다 뒤면 음수, 비치는 것을 먼 것부터 그릴 때)
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)
- xf64
- 월드 점의 x.x of the world point.
- yf64
- 월드 점의 y.y of the world point.
- zf64
- 월드 점의 z.z of the world point.
반환Returnsf64 카메라 앞 깊이. (보는 쪽으로 잰 거리, 눈의 면보다 뒤면 음수)Depth in front of the camera. (distance measured along the view direction, negative if behind the eye plane)
짝Counterpart: vanilla.js Camera3D.readViewDepth
use vanilla::{Camera3D, Vector3};
let camera = Camera3D::new();
let mut position_list = vec![Vector3::create(0.0, 0.0, 5.0), Vector3::create(0.0, 0.0, -5.0), Vector3::zero()];
position_list.sort_by(|left, right| camera.read_view_depth(right.x, right.y, right.z).total_cmp(&camera.read_view_depth(left.x, left.y, left.z)));
assert_eq!(position_list[0], Vector3::create(0.0, 0.0, -5.0));
assert!((camera.read_view_depth(0.0, 0.0, 0.0) - 10.0).abs() < 0.001);월드 점을 view_rect 안의 자리로. (view_rect 의 좌표, 왼쪽 위가 원점이고 아래로 늘어남, 화면 밖인지는 부르는 쪽이 정한다)
A world point to a position inside view_rect. (view_rect coordinates, origin at the top left and increasing downward, whether it is off-screen is up to the caller)
- xf64
- 월드 점의 x.x of the world point.
- yf64
- 월드 점의 y.y of the world point.
- zf64
- 월드 점의 z.z of the world point.
- view_rectRect
- 3D 를 그리는 사각형. (부르는 쪽의 좌표, 보통 뷰 좌표)Rectangle where 3D is drawn. (in the caller's coordinates, usually view coordinates)
반환ReturnsProjectedPoint view_rect 안의 자리와 깊이. 깊이가 0.0001 밑(눈의 면 뒤)이거나 클립 w 가 0 이면 is_in_front 가 false 이고 x, y 는 0.Position and depth inside view_rect. If the depth is below 0.0001 (behind the eye plane) or clip w is 0, is_in_front is false and x, y are 0.
짝Counterpart: vanilla.js Camera3D.projectToView
use vanilla::{Camera3D, Rect};
let camera = Camera3D::new();
let view_rect = Rect::create(640.0, 0.0, 640.0, 720.0);
let projected_point = camera.project_to_view(0.0, 0.0, 0.0, view_rect);
if projected_point.is_in_front {
let label_x = projected_point.x;
let label_y = projected_point.y - 24.0;
assert!((label_x - 960.0).abs() < 0.01);
assert!((label_y - 336.0).abs() < 0.01);
}pub fn unproject_to_plane(&self, view_position: Vector2, plane_height: f64, view_rect: Rect) -> Option<Vector3>#view_rect 안의 자리에서 나간 시선이 수평면 y = plane_height 와 만나는 점. (나란하거나 눈 뒤에서 만나면 None)
The point where the line of sight from a position inside view_rect meets the horizontal plane y = plane_height. (None if parallel or meeting behind the eye)
- view_positionVector2
- 시선이 나가는 자리. (view_rect 와 같은 좌표, 누른 자리 같은 것)Position the line of sight starts from. (same coordinates as view_rect, such as a pressed position)
- plane_heightf64
- 수평면의 높이. (월드 y)Height of the horizontal plane. (world y)
- view_rectRect
- 3D 를 그리는 사각형. (부르는 쪽의 좌표, 보통 뷰 좌표)Rectangle where 3D is drawn. (in the caller's coordinates, usually view coordinates)
반환ReturnsOption<Vector3> 시선이 수평면과 만나는 월드 점. (y 는 plane_height) view_rect 가 비었거나, 역투영의 w 가 0 이거나, 시선이 수평면과 나란하거나, 눈 뒤에서 만나면 None.World point where the line of sight meets the horizontal plane. (y is plane_height) None if view_rect is empty, the w of the unprojection is 0, the line of sight is parallel to the plane, or they meet behind the eye.
짝Counterpart: vanilla.js Camera3D.unprojectToPlane
use vanilla::{Camera3D, Rect, Vector2, Vector3};
let mut camera = Camera3D::new();
camera.look_at(Vector3::create(0.0, 1.0, 0.0), Vector3::create(0.0, 1.0, -10.0), Vector3::up());
let view_rect = Rect::create(0.0, 0.0, 400.0, 400.0);
let ground_point = camera.unproject_to_plane(Vector2::create(200.0, 400.0), 0.0, view_rect).unwrap();
assert_eq!(ground_point.y, 0.0);
assert!(ground_point.z < 0.0);
assert_eq!(camera.unproject_to_plane(Vector2::create(200.0, 0.0), 0.0, view_rect), None);