Move GLTF camera conversion code into GLTFCamera

This commit is contained in:
Aaron Franke 2022-08-13 16:27:15 -05:00
parent 3d76b91229
commit 03cd8097e1
No known key found for this signature in database
GPG Key ID: 40A1750B977E56BF
4 changed files with 119 additions and 68 deletions

View File

@ -10,6 +10,34 @@
<link title="GLTF camera detailed specification">https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-camera</link> <link title="GLTF camera detailed specification">https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#reference-camera</link>
<link title="GLTF camera spec and example file">https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_015_SimpleCameras.md</link> <link title="GLTF camera spec and example file">https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_015_SimpleCameras.md</link>
</tutorials> </tutorials>
<methods>
<method name="from_dictionary" qualifiers="static">
<return type="GLTFCamera" />
<param index="0" name="dictionary" type="Dictionary" />
<description>
Creates a new GLTFCamera instance by parsing the given [Dictionary].
</description>
</method>
<method name="from_node" qualifiers="static">
<return type="GLTFCamera" />
<param index="0" name="camera_node" type="Camera3D" />
<description>
Create a new GLTFCamera instance from the given Godot [Camera3D] node.
</description>
</method>
<method name="to_dictionary" qualifiers="const">
<return type="Dictionary" />
<description>
Serializes this GLTFCamera instance into a [Dictionary].
</description>
</method>
<method name="to_node" qualifiers="const">
<return type="Camera3D" />
<description>
Converts this GLTFCamera instance into a Godot [Camera3D] node.
</description>
</method>
</methods>
<members> <members>
<member name="depth_far" type="float" setter="set_depth_far" getter="get_depth_far" default="4000.0"> <member name="depth_far" type="float" setter="set_depth_far" getter="get_depth_far" default="4000.0">
The distance to the far culling boundary for this camera relative to its local Z axis, in meters. This maps to GLTF's [code]zfar[/code] property. The distance to the far culling boundary for this camera relative to its local Z axis, in meters. This maps to GLTF's [code]zfar[/code] property.

View File

@ -4556,27 +4556,7 @@ Error GLTFDocument::_serialize_cameras(Ref<GLTFState> state) {
Array cameras; Array cameras;
cameras.resize(state->cameras.size()); cameras.resize(state->cameras.size());
for (GLTFCameraIndex i = 0; i < state->cameras.size(); i++) { for (GLTFCameraIndex i = 0; i < state->cameras.size(); i++) {
Dictionary d; cameras[i] = state->cameras[i]->to_dictionary();
Ref<GLTFCamera> camera = state->cameras[i];
if (camera->get_perspective()) {
Dictionary persp;
persp["yfov"] = camera->get_fov();
persp["zfar"] = camera->get_depth_far();
persp["znear"] = camera->get_depth_near();
d["perspective"] = persp;
d["type"] = "perspective";
} else {
Dictionary ortho;
ortho["ymag"] = camera->get_size_mag();
ortho["xmag"] = camera->get_size_mag();
ortho["zfar"] = camera->get_depth_far();
ortho["znear"] = camera->get_depth_near();
d["orthographic"] = ortho;
d["type"] = "orthographic";
}
cameras[i] = d;
} }
if (!state->cameras.size()) { if (!state->cameras.size()) {
@ -4626,35 +4606,7 @@ Error GLTFDocument::_parse_cameras(Ref<GLTFState> state) {
const Array cameras = state->json["cameras"]; const Array cameras = state->json["cameras"];
for (GLTFCameraIndex i = 0; i < cameras.size(); i++) { for (GLTFCameraIndex i = 0; i < cameras.size(); i++) {
const Dictionary &d = cameras[i]; state->cameras.push_back(GLTFCamera::from_dictionary(cameras[i]));
Ref<GLTFCamera> camera;
camera.instantiate();
ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
const String &type = d["type"];
if (type == "perspective") {
camera->set_perspective(true);
if (d.has("perspective")) {
const Dictionary &persp = d["perspective"];
camera->set_fov(persp["yfov"]);
if (persp.has("zfar")) {
camera->set_depth_far(persp["zfar"]);
}
camera->set_depth_near(persp["znear"]);
}
} else if (type == "orthographic") {
camera->set_perspective(false);
if (d.has("orthographic")) {
const Dictionary &ortho = d["orthographic"];
camera->set_size_mag(ortho["ymag"]);
camera->set_depth_far(ortho["zfar"]);
camera->set_depth_near(ortho["znear"]);
}
} else {
ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "Camera3D should be in 'orthographic' or 'perspective'");
}
state->cameras.push_back(camera);
} }
print_verbose("glTF: Total cameras: " + itos(state->cameras.size())); print_verbose("glTF: Total cameras: " + itos(state->cameras.size()));
@ -5110,32 +5062,16 @@ Camera3D *GLTFDocument::_generate_camera(Ref<GLTFState> state, const GLTFNodeInd
ERR_FAIL_INDEX_V(gltf_node->camera, state->cameras.size(), nullptr); ERR_FAIL_INDEX_V(gltf_node->camera, state->cameras.size(), nullptr);
Camera3D *camera = memnew(Camera3D);
print_verbose("glTF: Creating camera for: " + gltf_node->get_name()); print_verbose("glTF: Creating camera for: " + gltf_node->get_name());
Ref<GLTFCamera> c = state->cameras[gltf_node->camera]; Ref<GLTFCamera> c = state->cameras[gltf_node->camera];
camera->set_projection(c->get_perspective() ? Camera3D::PROJECTION_PERSPECTIVE : Camera3D::PROJECTION_ORTHOGONAL); return c->to_node();
// GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
camera->set_fov(Math::rad_to_deg(c->get_fov()));
// GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
camera->set_size(c->get_size_mag() * 2.0f);
camera->set_near(c->get_depth_near());
camera->set_far(c->get_depth_far());
return camera;
} }
GLTFCameraIndex GLTFDocument::_convert_camera(Ref<GLTFState> state, Camera3D *p_camera) { GLTFCameraIndex GLTFDocument::_convert_camera(Ref<GLTFState> state, Camera3D *p_camera) {
print_verbose("glTF: Converting camera: " + p_camera->get_name()); print_verbose("glTF: Converting camera: " + p_camera->get_name());
Ref<GLTFCamera> c; Ref<GLTFCamera> c = GLTFCamera::from_node(p_camera);
c.instantiate();
c->set_perspective(p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
// GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
c->set_fov(Math::deg_to_rad(p_camera->get_fov()));
// GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
c->set_size_mag(p_camera->get_size() * 0.5f);
c->set_depth_far(p_camera->get_far());
c->set_depth_near(p_camera->get_near());
GLTFCameraIndex camera_index = state->cameras.size(); GLTFCameraIndex camera_index = state->cameras.size();
state->cameras.push_back(c); state->cameras.push_back(c);
return camera_index; return camera_index;

View File

@ -31,6 +31,12 @@
#include "gltf_camera.h" #include "gltf_camera.h"
void GLTFCamera::_bind_methods() { void GLTFCamera::_bind_methods() {
ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_node", "camera_node"), &GLTFCamera::from_node);
ClassDB::bind_method(D_METHOD("to_node"), &GLTFCamera::to_node);
ClassDB::bind_static_method("GLTFCamera", D_METHOD("from_dictionary", "dictionary"), &GLTFCamera::from_dictionary);
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFCamera::to_dictionary);
ClassDB::bind_method(D_METHOD("get_perspective"), &GLTFCamera::get_perspective); ClassDB::bind_method(D_METHOD("get_perspective"), &GLTFCamera::get_perspective);
ClassDB::bind_method(D_METHOD("set_perspective", "perspective"), &GLTFCamera::set_perspective); ClassDB::bind_method(D_METHOD("set_perspective", "perspective"), &GLTFCamera::set_perspective);
ClassDB::bind_method(D_METHOD("get_fov"), &GLTFCamera::get_fov); ClassDB::bind_method(D_METHOD("get_fov"), &GLTFCamera::get_fov);
@ -48,3 +54,78 @@ void GLTFCamera::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_far"), "set_depth_far", "get_depth_far");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near"); ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "depth_near"), "set_depth_near", "get_depth_near");
} }
Ref<GLTFCamera> GLTFCamera::from_node(const Camera3D *p_camera) {
Ref<GLTFCamera> c;
c.instantiate();
c->set_perspective(p_camera->get_projection() == Camera3D::ProjectionType::PROJECTION_PERSPECTIVE);
// GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
c->set_fov(Math::deg_to_rad(p_camera->get_fov()));
// GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
c->set_size_mag(p_camera->get_size() * 0.5f);
c->set_depth_far(p_camera->get_far());
c->set_depth_near(p_camera->get_near());
return c;
}
Camera3D *GLTFCamera::to_node() const {
Camera3D *camera = memnew(Camera3D);
camera->set_projection(perspective ? Camera3D::PROJECTION_PERSPECTIVE : Camera3D::PROJECTION_ORTHOGONAL);
// GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
camera->set_fov(Math::rad_to_deg(fov));
// GLTF spec (xmag and ymag) is a radius in meters, Godot's camera (size) is a diameter in meters.
camera->set_size(size_mag * 2.0f);
camera->set_near(depth_near);
camera->set_far(depth_far);
return camera;
}
Ref<GLTFCamera> GLTFCamera::from_dictionary(const Dictionary p_dictionary) {
ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFCamera>(), "Failed to parse GLTF camera, missing required field 'type'.");
Ref<GLTFCamera> camera;
camera.instantiate();
const String &type = p_dictionary["type"];
if (type == "perspective") {
camera->set_perspective(true);
if (p_dictionary.has("perspective")) {
const Dictionary &persp = p_dictionary["perspective"];
camera->set_fov(persp["yfov"]);
if (persp.has("zfar")) {
camera->set_depth_far(persp["zfar"]);
}
camera->set_depth_near(persp["znear"]);
}
} else if (type == "orthographic") {
camera->set_perspective(false);
if (p_dictionary.has("orthographic")) {
const Dictionary &ortho = p_dictionary["orthographic"];
camera->set_size_mag(ortho["ymag"]);
camera->set_depth_far(ortho["zfar"]);
camera->set_depth_near(ortho["znear"]);
}
} else {
ERR_PRINT("Error parsing GLTF camera: Camera type '" + type + "' is unknown, should be perspective or orthographic.");
}
return camera;
}
Dictionary GLTFCamera::to_dictionary() const {
Dictionary d;
if (perspective) {
Dictionary persp;
persp["yfov"] = fov;
persp["zfar"] = depth_far;
persp["znear"] = depth_near;
d["perspective"] = persp;
d["type"] = "perspective";
} else {
Dictionary ortho;
ortho["ymag"] = size_mag;
ortho["xmag"] = size_mag;
ortho["zfar"] = depth_far;
ortho["znear"] = depth_near;
d["orthographic"] = ortho;
d["type"] = "orthographic";
}
return d;
}

View File

@ -63,6 +63,12 @@ public:
void set_depth_far(real_t p_val) { depth_far = p_val; } void set_depth_far(real_t p_val) { depth_far = p_val; }
real_t get_depth_near() const { return depth_near; } real_t get_depth_near() const { return depth_near; }
void set_depth_near(real_t p_val) { depth_near = p_val; } void set_depth_near(real_t p_val) { depth_near = p_val; }
static Ref<GLTFCamera> from_node(const Camera3D *p_light);
Camera3D *to_node() const;
static Ref<GLTFCamera> from_dictionary(const Dictionary p_dictionary);
Dictionary to_dictionary() const;
}; };
#endif // GLTF_CAMERA_H #endif // GLTF_CAMERA_H