Пример #1
0
Point2 Camera::unproject_position(const Vector3& p_pos) const {

	if (!is_inside_tree()) {
		ERR_EXPLAIN("Camera is not inside scene.");
		ERR_FAIL_COND_V(!is_inside_tree(),Vector2());
	}

	Size2 viewport_size = get_viewport()->get_visible_rect().size;

	CameraMatrix cm;


	if (mode==PROJECTION_ORTHOGONAL)
		cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
	else
		cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);

	Plane p(get_camera_transform().xform_inv(p_pos),1.0);

	p=cm.xform4(p);
	p.normal/=p.d;


	Point2 res;
	res.x = (p.normal.x * 0.5 + 0.5)  * viewport_size.x;
	res.y = (-p.normal.y * 0.5 + 0.5)  * viewport_size.y;

	return res;

}
Пример #2
0
Vector3 Camera::project_position(const Point2& p_point) const {

	if (!is_inside_tree()) {
		ERR_EXPLAIN("Camera is not inside scene.");
		ERR_FAIL_COND_V(!is_inside_tree(),Vector3());
	}

	Size2 viewport_size = get_viewport()->get_visible_rect().size;

	CameraMatrix cm;

	if (mode==PROJECTION_ORTHOGONAL)
		cm.set_orthogonal(size,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);
	else
		cm.set_perspective(fov,viewport_size.get_aspect(),near,far,keep_aspect==KEEP_WIDTH);

	Size2 vp_size;
	cm.get_viewport_size(vp_size.x,vp_size.y);

	Vector2 point;
	point.x = (p_point.x/viewport_size.x) * 2.0 - 1.0;
	point.y = (1.0-(p_point.y/viewport_size.y)) * 2.0 - 1.0;
	point*=vp_size;

	Vector3 p(point.x,point.y,-near);


	return get_camera_transform().xform(p);
}
Пример #3
0
Vector<Plane> Camera::get_frustum() const {

	ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());

	Size2 viewport_size = get_viewport()->get_visible_rect().size;
	CameraMatrix cm;
	if (mode == PROJECTION_PERSPECTIVE)
		cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
	else
		cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);

	return cm.get_projection_planes(get_camera_transform());
}