2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* camera.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2017-04-07 22:11:42 +00:00
|
|
|
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "camera.h"
|
|
|
|
|
|
|
|
#include "camera_matrix.h"
|
|
|
|
#include "scene/resources/material.h"
|
|
|
|
#include "scene/resources/surface_tool.h"
|
|
|
|
|
|
|
|
void Camera::_update_audio_listener_state() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::_request_camera_update() {
|
|
|
|
|
|
|
|
_update_camera();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::_update_camera_mode() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
force_change = true;
|
|
|
|
switch (mode) {
|
2014-02-10 01:10:30 +00:00
|
|
|
case PROJECTION_PERSPECTIVE: {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
set_perspective(fov, near, far);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case PROJECTION_ORTHOGONAL: {
|
2017-03-05 15:44:50 +00:00
|
|
|
set_orthogonal(size, near, far);
|
2016-03-08 23:00:52 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Camera::_set(const StringName &p_name, const Variant &p_value) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool changed_all = false;
|
|
|
|
if (p_name == "projection") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int proj = p_value;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (proj == PROJECTION_PERSPECTIVE)
|
|
|
|
mode = PROJECTION_PERSPECTIVE;
|
|
|
|
if (proj == PROJECTION_ORTHOGONAL)
|
|
|
|
mode = PROJECTION_ORTHOGONAL;
|
|
|
|
|
|
|
|
changed_all = true;
|
|
|
|
} else if (p_name == "fov" || p_name == "fovy" || p_name == "fovx")
|
|
|
|
fov = p_value;
|
|
|
|
else if (p_name == "size" || p_name == "sizex" || p_name == "sizey")
|
|
|
|
size = p_value;
|
|
|
|
else if (p_name == "near")
|
|
|
|
near = p_value;
|
|
|
|
else if (p_name == "far")
|
|
|
|
far = p_value;
|
|
|
|
else if (p_name == "keep_aspect")
|
2014-09-15 23:06:37 +00:00
|
|
|
set_keep_aspect_mode(KeepAspect(int(p_value)));
|
2017-03-05 15:44:50 +00:00
|
|
|
else if (p_name == "vaspect")
|
|
|
|
set_keep_aspect_mode(p_value ? KEEP_WIDTH : KEEP_HEIGHT);
|
|
|
|
else if (p_name == "h_offset")
|
|
|
|
h_offset = p_value;
|
|
|
|
else if (p_name == "v_offset")
|
|
|
|
v_offset = p_value;
|
|
|
|
else if (p_name == "current") {
|
2014-02-10 01:10:30 +00:00
|
|
|
if (p_value.operator bool()) {
|
|
|
|
make_current();
|
|
|
|
} else {
|
|
|
|
clear_current();
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (p_name == "cull_mask") {
|
2016-10-03 19:33:42 +00:00
|
|
|
set_cull_mask(p_value);
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (p_name == "environment") {
|
2014-02-10 01:10:30 +00:00
|
|
|
set_environment(p_value);
|
2017-07-15 04:23:10 +00:00
|
|
|
} else if (p_name == "doppler/tracking") {
|
|
|
|
set_doppler_tracking(DopplerTracking(int(p_value)));
|
2014-02-10 01:10:30 +00:00
|
|
|
} else
|
|
|
|
return false;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_update_camera_mode();
|
|
|
|
if (changed_all)
|
|
|
|
_change_notify();
|
|
|
|
return true;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Camera::_get(const StringName &p_name, Variant &r_ret) const {
|
|
|
|
|
|
|
|
if (p_name == "projection") {
|
|
|
|
r_ret = mode;
|
|
|
|
} else if (p_name == "fov" || p_name == "fovy" || p_name == "fovx")
|
|
|
|
r_ret = fov;
|
|
|
|
else if (p_name == "size" || p_name == "sizex" || p_name == "sizey")
|
|
|
|
r_ret = size;
|
|
|
|
else if (p_name == "near")
|
|
|
|
r_ret = near;
|
|
|
|
else if (p_name == "far")
|
|
|
|
r_ret = far;
|
|
|
|
else if (p_name == "keep_aspect")
|
|
|
|
r_ret = int(keep_aspect);
|
|
|
|
else if (p_name == "current") {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
if (is_inside_tree() && get_tree()->is_node_being_edited(this)) {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_ret = current;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
r_ret = is_current();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
} else if (p_name == "cull_mask") {
|
|
|
|
r_ret = get_cull_mask();
|
|
|
|
} else if (p_name == "h_offset") {
|
|
|
|
r_ret = get_h_offset();
|
|
|
|
} else if (p_name == "v_offset") {
|
|
|
|
r_ret = get_v_offset();
|
|
|
|
} else if (p_name == "environment") {
|
|
|
|
r_ret = get_environment();
|
2017-07-15 04:23:10 +00:00
|
|
|
} else if (p_name == "doppler/tracking") {
|
|
|
|
r_ret = get_doppler_tracking();
|
2014-02-10 01:10:30 +00:00
|
|
|
} else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera::_get_property_list(List<PropertyInfo> *p_list) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "projection", PROPERTY_HINT_ENUM, "Perspective,Orthogonal"));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (mode) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
case PROJECTION_PERSPECTIVE: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "fov", PROPERTY_HINT_RANGE, "1,179,0.1", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
if (keep_aspect == KEEP_WIDTH)
|
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "fovx", PROPERTY_HINT_RANGE, "1,179,0.1", PROPERTY_USAGE_EDITOR));
|
2014-09-15 23:06:37 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "fovy", PROPERTY_HINT_RANGE, "1,179,0.1", PROPERTY_USAGE_EDITOR));
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case PROJECTION_ORTHOGONAL: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "size", PROPERTY_HINT_RANGE, "1,16384,0.01", PROPERTY_USAGE_NOEDITOR));
|
|
|
|
if (keep_aspect == KEEP_WIDTH)
|
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "sizex", PROPERTY_HINT_RANGE, "0.1,16384,0.01", PROPERTY_USAGE_EDITOR));
|
2014-09-15 23:06:37 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "sizey", PROPERTY_HINT_RANGE, "0.1,16384,0.01", PROPERTY_USAGE_EDITOR));
|
2014-09-15 23:06:37 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "near", PROPERTY_HINT_EXP_RANGE, "0.01,4096.0,0.01"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "far", PROPERTY_HINT_EXP_RANGE, "0.01,4096.0,0.01"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "keep_aspect", PROPERTY_HINT_ENUM, "Keep Width,Keep Height"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::BOOL, "current"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "cull_mask", PROPERTY_HINT_LAYERS_3D_RENDER));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "Environment"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "h_offset"));
|
|
|
|
p_list->push_back(PropertyInfo(Variant::REAL, "v_offset"));
|
2017-07-15 04:23:10 +00:00
|
|
|
p_list->push_back(PropertyInfo(Variant::INT, "doppler/tracking", PROPERTY_HINT_ENUM, "Disabled,Idle,Fixed"));
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::_update_camera() {
|
|
|
|
|
|
|
|
Transform tr = get_camera_transform();
|
2017-03-05 15:44:50 +00:00
|
|
|
tr.origin += tr.basis.get_axis(1) * v_offset;
|
|
|
|
tr.origin += tr.basis.get_axis(0) * h_offset;
|
|
|
|
VisualServer::get_singleton()->camera_set_transform(camera, tr);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-14 11:26:56 +00:00
|
|
|
// here goes listener stuff
|
|
|
|
/*
|
|
|
|
if (viewport_ptr && is_inside_scene() && is_current())
|
|
|
|
get_viewport()->_camera_transform_changed_notify();
|
|
|
|
*/
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (is_inside_tree() && is_current()) {
|
2016-01-02 20:18:45 +00:00
|
|
|
get_viewport()->_camera_transform_changed_notify();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (is_current() && get_world().is_valid()) {
|
|
|
|
get_world()->_update_camera(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::_notification(int p_what) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_what) {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_ENTER_WORLD: {
|
|
|
|
|
2016-01-31 18:09:52 +00:00
|
|
|
bool first_camera = get_viewport()->_camera_add(this);
|
2016-01-02 20:18:45 +00:00
|
|
|
if (!get_tree()->is_node_being_edited(this) && (current || first_camera))
|
2014-02-10 01:10:30 +00:00
|
|
|
make_current();
|
|
|
|
|
2016-03-08 23:00:52 +00:00
|
|
|
} break;
|
2014-02-10 01:10:30 +00:00
|
|
|
case NOTIFICATION_TRANSFORM_CHANGED: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
_request_camera_update();
|
2017-07-15 04:23:10 +00:00
|
|
|
if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
|
|
|
|
velocity_tracker->update_position(get_global_transform().origin);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_EXIT_WORLD: {
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
if (!get_tree()->is_node_being_edited(this)) {
|
|
|
|
if (is_current()) {
|
|
|
|
clear_current();
|
2017-03-05 15:44:50 +00:00
|
|
|
current = true; //keep it true
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
current = false;
|
2016-01-02 20:18:45 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
2016-01-02 20:18:45 +00:00
|
|
|
|
2016-01-31 18:09:52 +00:00
|
|
|
get_viewport()->_camera_remove(this);
|
2014-10-12 05:13:22 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
} break;
|
|
|
|
case NOTIFICATION_BECAME_CURRENT: {
|
|
|
|
if (get_world().is_valid()) {
|
|
|
|
get_world()->_register_camera(this);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case NOTIFICATION_LOST_CURRENT: {
|
|
|
|
if (get_world().is_valid()) {
|
|
|
|
get_world()->_remove_camera(this);
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Transform Camera::get_camera_transform() const {
|
|
|
|
|
2014-10-03 03:10:51 +00:00
|
|
|
return get_global_transform().orthonormalized();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::set_perspective(float p_fovy_degrees, float p_z_near, float p_z_far) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!force_change && fov == p_fovy_degrees && p_z_near == near && p_z_far == far && mode == PROJECTION_PERSPECTIVE)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
fov = p_fovy_degrees;
|
|
|
|
near = p_z_near;
|
|
|
|
far = p_z_far;
|
|
|
|
mode = PROJECTION_PERSPECTIVE;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualServer::get_singleton()->camera_set_perspective(camera, fov, near, far);
|
2014-02-10 01:10:30 +00:00
|
|
|
update_gizmo();
|
2017-03-05 15:44:50 +00:00
|
|
|
force_change = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
void Camera::set_orthogonal(float p_size, float p_z_near, float p_z_far) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!force_change && size == p_size && p_z_near == near && p_z_far == far && mode == PROJECTION_ORTHOGONAL)
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
size = p_size;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
near = p_z_near;
|
|
|
|
far = p_z_far;
|
|
|
|
mode = PROJECTION_ORTHOGONAL;
|
|
|
|
force_change = false;
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
VisualServer::get_singleton()->camera_set_orthogonal(camera, size, near, far);
|
2014-02-10 01:10:30 +00:00
|
|
|
update_gizmo();
|
|
|
|
}
|
|
|
|
|
|
|
|
RID Camera::get_camera() const {
|
|
|
|
|
|
|
|
return camera;
|
|
|
|
};
|
|
|
|
|
|
|
|
void Camera::make_current() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
current = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2016-01-31 18:09:52 +00:00
|
|
|
get_viewport()->_camera_set(this);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//get_scene()->call_group(SceneMainLoop::GROUP_CALL_REALTIME,camera_group,"_camera_make_current",this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::clear_current() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
current = false;
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree())
|
2014-02-10 01:10:30 +00:00
|
|
|
return;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (get_viewport()->get_camera() == this) {
|
2016-01-31 18:09:52 +00:00
|
|
|
get_viewport()->_camera_set(NULL);
|
|
|
|
get_viewport()->_camera_make_next_current(this);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera::is_current() const {
|
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
if (is_inside_tree() && !get_tree()->is_node_being_edited(this)) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return get_viewport()->get_camera() == this;
|
2014-02-10 01:10:30 +00:00
|
|
|
} else
|
|
|
|
return current;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Camera::_can_gizmo_scale() const {
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 Camera::project_ray_normal(const Point2 &p_pos) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector3 ray = project_local_ray_normal(p_pos);
|
|
|
|
return get_camera_transform().basis.xform(ray).normalized();
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 Camera::project_local_ray_normal(const Point2 &p_pos) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_EXPLAIN("Camera is not inside scene.");
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!is_inside_tree(), Vector3());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 01:54:32 +00:00
|
|
|
#if 0
|
2016-01-02 20:18:45 +00:00
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
2014-10-28 01:54:32 +00:00
|
|
|
Vector2 cpos = p_pos;
|
|
|
|
#else
|
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
Size2 viewport_size = get_viewport()->get_camera_rect_size();
|
|
|
|
Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
|
2014-10-28 01:54:32 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector3 ray;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (mode == PROJECTION_ORTHOGONAL) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ray = Vector3(0, 0, -1);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
|
|
|
CameraMatrix cm;
|
2017-03-05 15:44:50 +00:00
|
|
|
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
|
|
|
|
float screen_w, screen_h;
|
|
|
|
cm.get_viewport_size(screen_w, screen_h);
|
|
|
|
ray = Vector3(((cpos.x / viewport_size.width) * 2.0 - 1.0) * screen_w, ((1.0 - (cpos.y / viewport_size.height)) * 2.0 - 1.0) * screen_h, -near).normalized();
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ray;
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 Camera::project_ray_origin(const Point2 &p_pos) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_EXPLAIN("Camera is not inside scene.");
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!is_inside_tree(), Vector3());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 01:54:32 +00:00
|
|
|
#if 0
|
2016-01-02 20:18:45 +00:00
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
2014-10-28 01:54:32 +00:00
|
|
|
Vector2 cpos = p_pos;
|
|
|
|
#else
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
Size2 viewport_size = get_viewport()->get_camera_rect_size();
|
|
|
|
Vector2 cpos = get_viewport()->get_camera_coords(p_pos);
|
2014-10-28 01:54:32 +00:00
|
|
|
#endif
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(viewport_size.y == 0, Vector3());
|
2017-01-14 11:26:56 +00:00
|
|
|
//float aspect = viewport_size.x / viewport_size.y;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (mode == PROJECTION_PERSPECTIVE) {
|
|
|
|
|
|
|
|
return get_camera_transform().origin;
|
|
|
|
} else {
|
|
|
|
|
2014-10-28 01:54:32 +00:00
|
|
|
Vector2 pos = cpos / viewport_size;
|
2017-03-05 15:44:50 +00:00
|
|
|
float vsize, hsize;
|
|
|
|
if (keep_aspect == KEEP_WIDTH) {
|
|
|
|
vsize = size / viewport_size.aspect();
|
2014-02-10 01:10:30 +00:00
|
|
|
hsize = size;
|
|
|
|
} else {
|
2017-03-05 15:44:50 +00:00
|
|
|
hsize = size * viewport_size.aspect();
|
2014-02-10 01:10:30 +00:00
|
|
|
vsize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 ray;
|
2017-03-05 15:44:50 +00:00
|
|
|
ray.x = pos.x * (hsize)-hsize / 2;
|
|
|
|
ray.y = (1.0 - pos.y) * (vsize)-vsize / 2;
|
2014-02-10 01:10:30 +00:00
|
|
|
ray.z = -near;
|
|
|
|
ray = get_camera_transform().xform(ray);
|
|
|
|
return ray;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool Camera::is_position_behind(const Vector3 &p_pos) const {
|
2015-04-29 01:05:01 +00:00
|
|
|
|
|
|
|
Transform t = get_global_transform();
|
|
|
|
Vector3 eyedir = -get_global_transform().basis.get_axis(2).normalized();
|
2017-03-05 15:44:50 +00:00
|
|
|
return eyedir.dot(p_pos) < (eyedir.dot(t.origin) + near);
|
2015-04-29 01:05:01 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Point2 Camera::unproject_position(const Vector3 &p_pos) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_EXPLAIN("Camera is not inside scene.");
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!is_inside_tree(), Vector2());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
CameraMatrix cm;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (mode == PROJECTION_ORTHOGONAL)
|
|
|
|
cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Plane p(get_camera_transform().xform_inv(p_pos), 1.0);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
p = cm.xform4(p);
|
|
|
|
p.normal /= p.d;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Point2 res;
|
2017-03-05 15:44:50 +00:00
|
|
|
res.x = (p.normal.x * 0.5 + 0.5) * viewport_size.x;
|
|
|
|
res.y = (-p.normal.y * 0.5 + 0.5) * viewport_size.y;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 Camera::project_position(const Point2 &p_point) const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-11-06 00:20:42 +00:00
|
|
|
if (!is_inside_tree()) {
|
2014-02-10 01:10:30 +00:00
|
|
|
ERR_EXPLAIN("Camera is not inside scene.");
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!is_inside_tree(), Vector3());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
CameraMatrix cm;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (mode == PROJECTION_ORTHOGONAL)
|
|
|
|
cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Size2 vp_size;
|
2017-03-05 15:44:50 +00:00
|
|
|
cm.get_viewport_size(vp_size.x, vp_size.y);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Vector2 point;
|
2017-03-05 15:44:50 +00:00
|
|
|
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;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 p(point.x, point.y, -near);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return get_camera_transform().xform(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
void Camera::_camera_make_current(Node *p_camera) {
|
|
|
|
|
|
|
|
|
|
|
|
if (p_camera==this) {
|
|
|
|
VisualServer::get_singleton()->viewport_attach_camera(viewport_id,camera);
|
|
|
|
active=true;
|
|
|
|
} else {
|
|
|
|
if (active && p_camera==NULL) {
|
|
|
|
//detech camera because no one else will claim it
|
|
|
|
VisualServer::get_singleton()->viewport_attach_camera(viewport_id,RID());
|
|
|
|
}
|
|
|
|
active=false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void Camera::set_environment(const Ref<Environment> &p_environment) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
environment = p_environment;
|
2014-02-10 01:10:30 +00:00
|
|
|
if (environment.is_valid())
|
2017-03-05 15:44:50 +00:00
|
|
|
VS::get_singleton()->camera_set_environment(camera, environment->get_rid());
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
VS::get_singleton()->camera_set_environment(camera, RID());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ref<Environment> Camera::get_environment() const {
|
|
|
|
|
|
|
|
return environment;
|
|
|
|
}
|
|
|
|
|
2014-09-15 23:06:37 +00:00
|
|
|
void Camera::set_keep_aspect_mode(KeepAspect p_aspect) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
keep_aspect = p_aspect;
|
|
|
|
VisualServer::get_singleton()->camera_set_use_vertical_aspect(camera, p_aspect == KEEP_WIDTH);
|
2014-09-15 23:06:37 +00:00
|
|
|
|
|
|
|
_change_notify();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Camera::KeepAspect Camera::get_keep_aspect_mode() const {
|
2014-09-15 23:06:37 +00:00
|
|
|
|
|
|
|
return keep_aspect;
|
|
|
|
}
|
|
|
|
|
2017-07-15 04:23:10 +00:00
|
|
|
void Camera::set_doppler_tracking(DopplerTracking p_tracking) {
|
|
|
|
|
|
|
|
if (doppler_tracking == p_tracking)
|
|
|
|
return;
|
|
|
|
|
|
|
|
doppler_tracking = p_tracking;
|
|
|
|
if (p_tracking != DOPPLER_TRACKING_DISABLED) {
|
|
|
|
velocity_tracker->set_track_fixed_step(doppler_tracking == DOPPLER_TRACKING_FIXED_STEP);
|
|
|
|
velocity_tracker->reset(get_global_transform().origin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera::DopplerTracking Camera::get_doppler_tracking() const {
|
|
|
|
return doppler_tracking;
|
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
void Camera::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("project_ray_normal", "screen_point"), &Camera::project_ray_normal);
|
|
|
|
ClassDB::bind_method(D_METHOD("project_local_ray_normal", "screen_point"), &Camera::project_local_ray_normal);
|
|
|
|
ClassDB::bind_method(D_METHOD("project_ray_origin", "screen_point"), &Camera::project_ray_origin);
|
|
|
|
ClassDB::bind_method(D_METHOD("unproject_position", "world_point"), &Camera::unproject_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_position_behind", "world_point"), &Camera::is_position_behind);
|
|
|
|
ClassDB::bind_method(D_METHOD("project_position", "screen_point"), &Camera::project_position);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_perspective", "fov", "z_near", "z_far"), &Camera::set_perspective);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_orthogonal", "size", "z_near", "z_far"), &Camera::set_orthogonal);
|
|
|
|
ClassDB::bind_method(D_METHOD("make_current"), &Camera::make_current);
|
|
|
|
ClassDB::bind_method(D_METHOD("clear_current"), &Camera::clear_current);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_current"), &Camera::is_current);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_camera_transform"), &Camera::get_camera_transform);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_fov"), &Camera::get_fov);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_size"), &Camera::get_size);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_zfar"), &Camera::get_zfar);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_znear"), &Camera::get_znear);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_projection"), &Camera::get_projection);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_h_offset", "ofs"), &Camera::set_h_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_h_offset"), &Camera::get_h_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_v_offset", "ofs"), &Camera::set_v_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_v_offset"), &Camera::get_v_offset);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_cull_mask", "mask"), &Camera::set_cull_mask);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cull_mask"), &Camera::get_cull_mask);
|
2017-08-09 11:19:41 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_environment", "env"), &Camera::set_environment);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_environment"), &Camera::get_environment);
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_keep_aspect_mode", "mode"), &Camera::set_keep_aspect_mode);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_keep_aspect_mode"), &Camera::get_keep_aspect_mode);
|
2017-07-15 04:23:10 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_doppler_tracking", "mode"), &Camera::set_doppler_tracking);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_doppler_tracking"), &Camera::get_doppler_tracking);
|
2017-02-13 11:47:24 +00:00
|
|
|
//ClassDB::bind_method(D_METHOD("_camera_make_current"),&Camera::_camera_make_current );
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(PROJECTION_PERSPECTIVE);
|
|
|
|
BIND_ENUM_CONSTANT(PROJECTION_ORTHOGONAL);
|
2014-09-15 23:06:37 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(KEEP_WIDTH);
|
|
|
|
BIND_ENUM_CONSTANT(KEEP_HEIGHT);
|
2017-07-15 04:23:10 +00:00
|
|
|
|
2017-08-20 15:45:01 +00:00
|
|
|
BIND_ENUM_CONSTANT(DOPPLER_TRACKING_DISABLED)
|
|
|
|
BIND_ENUM_CONSTANT(DOPPLER_TRACKING_IDLE_STEP)
|
|
|
|
BIND_ENUM_CONSTANT(DOPPLER_TRACKING_FIXED_STEP)
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float Camera::get_fov() const {
|
|
|
|
|
|
|
|
return fov;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Camera::get_size() const {
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Camera::get_znear() const {
|
|
|
|
|
|
|
|
return near;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Camera::get_zfar() const {
|
|
|
|
|
|
|
|
return far;
|
|
|
|
}
|
|
|
|
|
|
|
|
Camera::Projection Camera::get_projection() const {
|
|
|
|
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
void Camera::set_cull_mask(uint32_t p_layers) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
layers = p_layers;
|
|
|
|
VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t Camera::get_cull_mask() const {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return layers;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Plane> Camera::get_frustum() const {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND_V(!is_inside_world(), Vector<Plane>());
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2016-01-02 20:18:45 +00:00
|
|
|
Size2 viewport_size = get_viewport()->get_visible_rect().size;
|
2014-02-10 01:10:30 +00:00
|
|
|
CameraMatrix cm;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (mode == PROJECTION_PERSPECTIVE)
|
|
|
|
cm.set_perspective(fov, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
cm.set_orthogonal(size, viewport_size.aspect(), near, far, keep_aspect == KEEP_WIDTH);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-10-03 03:10:51 +00:00
|
|
|
return cm.get_projection_planes(get_camera_transform());
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2015-01-03 14:06:53 +00:00
|
|
|
void Camera::set_v_offset(float p_offset) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
v_offset = p_offset;
|
2017-01-14 17:03:38 +00:00
|
|
|
_update_camera();
|
2015-01-03 14:06:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float Camera::get_v_offset() const {
|
|
|
|
|
|
|
|
return v_offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Camera::set_h_offset(float p_offset) {
|
2017-03-05 15:44:50 +00:00
|
|
|
h_offset = p_offset;
|
2015-01-03 14:06:53 +00:00
|
|
|
_update_camera();
|
|
|
|
}
|
|
|
|
|
|
|
|
float Camera::get_h_offset() const {
|
|
|
|
|
|
|
|
return h_offset;
|
|
|
|
}
|
|
|
|
|
2017-07-15 04:23:10 +00:00
|
|
|
Vector3 Camera::get_doppler_tracked_velocity() const {
|
|
|
|
|
|
|
|
if (doppler_tracking != DOPPLER_TRACKING_DISABLED) {
|
|
|
|
return velocity_tracker->get_tracked_linear_velocity();
|
|
|
|
} else {
|
|
|
|
return Vector3();
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
Camera::Camera() {
|
|
|
|
|
|
|
|
camera = VisualServer::get_singleton()->camera_create();
|
2017-03-05 15:44:50 +00:00
|
|
|
size = 1;
|
|
|
|
fov = 0;
|
|
|
|
near = 0;
|
|
|
|
far = 0;
|
|
|
|
current = false;
|
|
|
|
force_change = false;
|
|
|
|
mode = PROJECTION_PERSPECTIVE;
|
2017-06-16 10:07:07 +00:00
|
|
|
set_perspective(65.0, 0.1, 100.0);
|
2017-03-05 15:44:50 +00:00
|
|
|
keep_aspect = KEEP_HEIGHT;
|
|
|
|
layers = 0xfffff;
|
|
|
|
v_offset = 0;
|
|
|
|
h_offset = 0;
|
|
|
|
VisualServer::get_singleton()->camera_set_cull_mask(camera, layers);
|
2014-02-10 01:10:30 +00:00
|
|
|
//active=false;
|
2017-07-15 04:23:10 +00:00
|
|
|
velocity_tracker.instance();
|
|
|
|
doppler_tracking = DOPPLER_TRACKING_DISABLED;
|
2017-01-12 23:35:46 +00:00
|
|
|
set_notify_transform(true);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Camera::~Camera() {
|
|
|
|
|
|
|
|
VisualServer::get_singleton()->free(camera);
|
|
|
|
}
|