Path: blob/master/modules/gltf/structures/gltf_camera.cpp
10278 views
/**************************************************************************/1/* gltf_camera.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "gltf_camera.h"3132#include "gltf_object_model_property.h"33#include "scene/3d/camera_3d.h"3435void GLTFCamera::_bind_methods() {36ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_node", "camera_node"), &GLTFCamera::from_node);37ClassDB::bind_method(D_METHOD("to_node"), &GLTFCamera::to_node);3839ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_dictionary", "dictionary"), &GLTFCamera::from_dictionary);40ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFCamera::to_dictionary);4142ClassDB::bind_method(D_METHOD("get_perspective"), &GLTFCamera::get_perspective);43ClassDB::bind_method(D_METHOD("set_perspective", "perspective"), &GLTFCamera::set_perspective);44ClassDB::bind_method(D_METHOD("get_fov"), &GLTFCamera::get_fov);45ClassDB::bind_method(D_METHOD("set_fov", "fov"), &GLTFCamera::set_fov);46ClassDB::bind_method(D_METHOD("get_size_mag"), &GLTFCamera::get_size_mag);47ClassDB::bind_method(D_METHOD("set_size_mag", "size_mag"), &GLTFCamera::set_size_mag);48ClassDB::bind_method(D_METHOD("get_depth_far"), &GLTFCamera::get_depth_far);49ClassDB::bind_method(D_METHOD("set_depth_far", "zdepth_far"), &GLTFCamera::set_depth_far);50ClassDB::bind_method(D_METHOD("get_depth_near"), &GLTFCamera::get_depth_near);51ClassDB::bind_method(D_METHOD("set_depth_near", "zdepth_near"), &GLTFCamera::set_depth_near);5253ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective");54ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov"), "set_fov", "get_fov");55ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_mag"), "set_size_mag", "get_size_mag");56ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far");57ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near");58}5960void GLTFCamera::set_fov_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop) {61// Expression to convert glTF yfov in radians to Godot fov in degrees.62Ref<Expression> gltf_to_godot_expr;63gltf_to_godot_expr.instantiate();64PackedStringArray gltf_to_godot_args = { "yfov_rad" };65gltf_to_godot_expr->parse("rad_to_deg(yfov_rad)", gltf_to_godot_args);66r_obj_model_prop->set_gltf_to_godot_expression(gltf_to_godot_expr);67// Expression to convert Godot fov in degrees to glTF yfov in radians.68Ref<Expression> godot_to_gltf_expr;69godot_to_gltf_expr.instantiate();70PackedStringArray godot_to_gltf_args = { "fov_deg" };71godot_to_gltf_expr->parse("deg_to_rad(fov_deg)", godot_to_gltf_args);72r_obj_model_prop->set_godot_to_gltf_expression(godot_to_gltf_expr);73}7475Ref<GLTFCamera> GLTFCamera::from_node(const Camera3D *p_camera) {76Ref<GLTFCamera> c;77c.instantiate();78ERR_FAIL_NULL_V_MSG(p_camera, c, "Tried to create a GLTFCamera from a Camera3D node, but the given node was null.");79c->set_perspective(p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);80// glTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.81c->set_fov(Math::deg_to_rad(p_camera->get_fov()));82// glTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.83c->set_size_mag(p_camera->get_size() * 0.5f);84c->set_depth_far(p_camera->get_far());85c->set_depth_near(p_camera->get_near());86return c;87}8889Camera3D *GLTFCamera::to_node() const {90Camera3D *camera = memnew(Camera3D);91camera->set_projection(perspective ? Camera3D::PROJECTION_PERSPECTIVE : Camera3D::PROJECTION_ORTHOGONAL);92// glTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.93camera->set_fov(Math::rad_to_deg(fov));94// glTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.95camera->set_size(size_mag * 2.0f);96camera->set_near(depth_near);97camera->set_far(depth_far);98return camera;99}100101Ref<GLTFCamera> GLTFCamera::from_dictionary(const Dictionary p_dictionary) {102ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFCamera>(), "Failed to parse glTF camera, missing required field 'type'.");103Ref<GLTFCamera> camera;104camera.instantiate();105const String &type = p_dictionary["type"];106if (type == "perspective") {107camera->set_perspective(true);108if (p_dictionary.has("perspective")) {109const Dictionary &persp = p_dictionary["perspective"];110camera->set_fov(persp["yfov"]);111if (persp.has("zfar")) {112camera->set_depth_far(persp["zfar"]);113}114camera->set_depth_near(persp["znear"]);115}116} else if (type == "orthographic") {117camera->set_perspective(false);118if (p_dictionary.has("orthographic")) {119const Dictionary &ortho = p_dictionary["orthographic"];120camera->set_size_mag(ortho["ymag"]);121camera->set_depth_far(ortho["zfar"]);122camera->set_depth_near(ortho["znear"]);123}124} else {125ERR_PRINT("Error parsing glTF camera: Camera type '" + type + "' is unknown, should be perspective or orthographic.");126}127return camera;128}129130Dictionary GLTFCamera::to_dictionary() const {131Dictionary d;132if (perspective) {133Dictionary persp;134persp["yfov"] = fov;135persp["zfar"] = depth_far;136persp["znear"] = depth_near;137d["perspective"] = persp;138d["type"] = "perspective";139} else {140Dictionary ortho;141ortho["ymag"] = size_mag;142ortho["xmag"] = size_mag;143ortho["zfar"] = depth_far;144ortho["znear"] = depth_near;145d["orthographic"] = ortho;146d["type"] = "orthographic";147}148return d;149}150151152