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