Skip to content
0.2.0-dev Download ZIP
152 lines ·7.4 KB Raw
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 "gltf_object_model_property.h"
34
35#include "core/object/class_db.h"
36#include "scene/3d/camera_3d.h"
37
38void GLTFCamera::_bind_methods() {
39 ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_node", "camera_node"), &GLTFCamera::from_node);
40 ClassDB::bind_method(D_METHOD("to_node"), &GLTFCamera::to_node);
41
42 ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_dictionary", "dictionary"), &GLTFCamera::from_dictionary);
43 ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFCamera::to_dictionary);
44
45 ClassDB::bind_method(D_METHOD("get_perspective"), &GLTFCamera::get_perspective);
46 ClassDB::bind_method(D_METHOD("set_perspective", "perspective"), &GLTFCamera::set_perspective);
47 ClassDB::bind_method(D_METHOD("get_fov"), &GLTFCamera::get_fov);
48 ClassDB::bind_method(D_METHOD("set_fov", "fov"), &GLTFCamera::set_fov);
49 ClassDB::bind_method(D_METHOD("get_size_mag"), &GLTFCamera::get_size_mag);
50 ClassDB::bind_method(D_METHOD("set_size_mag", "size_mag"), &GLTFCamera::set_size_mag);
51 ClassDB::bind_method(D_METHOD("get_depth_far"), &GLTFCamera::get_depth_far);
52 ClassDB::bind_method(D_METHOD("set_depth_far", "zdepth_far"), &GLTFCamera::set_depth_far);
53 ClassDB::bind_method(D_METHOD("get_depth_near"), &GLTFCamera::get_depth_near);
54 ClassDB::bind_method(D_METHOD("set_depth_near", "zdepth_near"), &GLTFCamera::set_depth_near);
55
56 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective");
57 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "fov"), "set_fov", "get_fov");
58 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "size_mag"), "set_size_mag", "get_size_mag");
59 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far");
60 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near");
61}
62
63void GLTFCamera::set_fov_conversion_expressions(Ref<GLTFObjectModelProperty> &r_obj_model_prop) {
64 // Expression to convert glTF yfov in radians to Godot fov in degrees.
65 Ref<Expression> gltf_to_godot_expr;
66 gltf_to_godot_expr.instantiate();
67 PackedStringArray gltf_to_godot_args = { "yfov_rad" };
68 gltf_to_godot_expr->parse("rad_to_deg(yfov_rad)", gltf_to_godot_args);
69 r_obj_model_prop->set_gltf_to_godot_expression(gltf_to_godot_expr);
70 // Expression to convert Godot fov in degrees to glTF yfov in radians.
71 Ref<Expression> godot_to_gltf_expr;
72 godot_to_gltf_expr.instantiate();
73 PackedStringArray godot_to_gltf_args = { "fov_deg" };
74 godot_to_gltf_expr->parse("deg_to_rad(fov_deg)", godot_to_gltf_args);
75 r_obj_model_prop->set_godot_to_gltf_expression(godot_to_gltf_expr);
76}
77
78Ref<GLTFCamera> GLTFCamera::from_node(const Camera3D *p_camera) {
79 Ref<GLTFCamera> c;
80 c.instantiate();
81 ERR_FAIL_NULL_V_MSG(p_camera, c, "Tried to create a GLTFCamera from a Camera3D node, but the given node was null.");
82 c->set_perspective(p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
83 // glTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
84 c->set_fov(Math::deg_to_rad(p_camera->get_fov()));
85 // glTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
86 c->set_size_mag(p_camera->get_size() * 0.5f);
87 c->set_depth_far(p_camera->get_far());
88 c->set_depth_near(p_camera->get_near());
89 return c;
90}
91
92Camera3D *GLTFCamera::to_node() const {
93 Camera3D *camera = memnew(Camera3D);
94 camera->set_projection(perspective ? Camera3D::PROJECTION_PERSPECTIVE : Camera3D::PROJECTION_ORTHOGONAL);
95 // glTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
96 camera->set_fov(Math::rad_to_deg(fov));
97 // glTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
98 camera->set_size(size_mag * 2.0f);
99 camera->set_near(depth_near);
100 camera->set_far(depth_far);
101 return camera;
102}
103
104Ref<GLTFCamera> GLTFCamera::from_dictionary(const Dictionary &p_dictionary) {
105 ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFCamera>(), "Failed to parse glTF camera, missing required field 'type'.");
106 Ref<GLTFCamera> camera;
107 camera.instantiate();
108 const String &type = p_dictionary["type"];
109 if (type == "perspective") {
110 camera->set_perspective(true);
111 if (p_dictionary.has("perspective")) {
112 const Dictionary &persp = p_dictionary["perspective"];
113 camera->set_fov(persp["yfov"]);
114 if (persp.has("zfar")) {
115 camera->set_depth_far(persp["zfar"]);
116 }
117 camera->set_depth_near(persp["znear"]);
118 }
119 } else if (type == "orthographic") {
120 camera->set_perspective(false);
121 if (p_dictionary.has("orthographic")) {
122 const Dictionary &ortho = p_dictionary["orthographic"];
123 camera->set_size_mag(ortho["ymag"]);
124 camera->set_depth_far(ortho["zfar"]);
125 camera->set_depth_near(ortho["znear"]);
126 }
127 } else {
128 ERR_PRINT("Error parsing glTF camera: Camera type '" + type + "' is unknown, should be perspective or orthographic.");
129 }
130 return camera;
131}
132
133Dictionary GLTFCamera::to_dictionary() const {
134 Dictionary d;
135 if (perspective) {
136 Dictionary persp;
137 persp["yfov"] = fov;
138 persp["zfar"] = depth_far;
139 persp["znear"] = depth_near;
140 d["perspective"] = persp;
141 d["type"] = "perspective";
142 } else {
143 Dictionary ortho;
144 ortho["ymag"] = size_mag;
145 ortho["xmag"] = size_mag;
146 ortho["zfar"] = depth_far;
147 ortho["znear"] = depth_near;
148 d["orthographic"] = ortho;
149 d["type"] = "orthographic";
150 }
151 return d;
152}