Ported: [3.x] Backport moving camera and light logic to GLTF subclasses

- aaronfranke
a887f48d1d
This commit is contained in:
Relintai 2022-12-22 16:58:19 +01:00
parent 711d08be2b
commit fd3c23e04e
7 changed files with 288 additions and 191 deletions

View File

@ -1,22 +1,45 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFCamera" inherits="Resource" version="3.10"> <class name="GLTFCamera" inherits="Resource" version="3.10">
<brief_description> <brief_description>
Represents a GLTF camera.
</brief_description> </brief_description>
<description> <description>
Represents a camera as defined by the base GLTF spec.
[b]Note:[/b] This class is only compiled in editor builds. Run-time glTF loading and saving is [i]not[/i] available in exported projects. References to [GLTFCamera] within a script will cause an error in an exported project. [b]Note:[/b] This class is only compiled in editor builds. Run-time glTF loading and saving is [i]not[/i] available in exported projects. References to [GLTFCamera] within a script will cause an error in an exported project.
</description> </description>
<tutorials> <tutorials>
<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>
</tutorials> </tutorials>
<methods> <methods>
<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="Camera" />
<description>
Converts this GLTFCamera instance into a Godot [Camera] node.
</description>
</method>
</methods> </methods>
<members> <members>
<member name="fov_size" type="float" setter="set_fov_size" getter="get_fov_size" default="75.0"> <member name="fov_size" type="float" setter="set_fov_size" getter="get_fov_size" default="1.309">
The FOV of the camera. This class and GLTF define the camera FOV in radians, while Godot uses degrees. This maps to GLTF's [code]yfov[/code] property. This value is only used for perspective cameras, when [member perspective] is true.
</member> </member>
<member name="perspective" type="bool" setter="set_perspective" getter="get_perspective" default="true"> <member name="perspective" type="bool" setter="set_perspective" getter="get_perspective" default="true">
Whether or not the camera is in perspective mode. If false, the camera is in orthographic/orthogonal mode. This maps to GLTF's camera [code]type[/code] property. See [member Camera.projection] and the GLTF spec for more information.
</member>
<member name="size_mag" type="float" setter="set_size_mag" getter="get_size_mag" default="0.5">
The size of the camera. This class and GLTF define the camera size magnitude as a radius in meters, while Godot defines it as a diameter in meters. This maps to GLTF's [code]ymag[/code] property. This value is only used for orthographic/orthogonal cameras, when [member perspective] is false.
</member> </member>
<member name="zfar" type="float" setter="set_zfar" getter="get_zfar" default="4000.0"> <member name="zfar" type="float" setter="set_zfar" getter="get_zfar" 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.
</member> </member>
<member name="znear" type="float" setter="set_znear" getter="get_znear" default="0.05"> <member name="znear" type="float" setter="set_znear" getter="get_znear" default="0.05">
The distance to the near culling boundary for this camera relative to its local Z axis, in meters. This maps to GLTF's [code]znear[/code] property.
</member> </member>
</members> </members>
<constants> <constants>

View File

@ -1,13 +1,28 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<class name="GLTFLight" inherits="Resource" version="3.10"> <class name="GLTFLight" inherits="Resource" version="3.10">
<brief_description> <brief_description>
Represents a GLTF light.
</brief_description> </brief_description>
<description> <description>
Represents a light as defined by the [code]KHR_lights_punctual[/code] GLTF extension.
[b]Note:[/b] This class is only compiled in editor builds. Run-time glTF loading and saving is [i]not[/i] available in exported projects. References to [GLTFLight] within a script will cause an error in an exported project. [b]Note:[/b] This class is only compiled in editor builds. Run-time glTF loading and saving is [i]not[/i] available in exported projects. References to [GLTFLight] within a script will cause an error in an exported project.
</description> </description>
<tutorials> <tutorials>
<link title="KHR_lights_punctual GLTF extension spec">https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual</link>
</tutorials> </tutorials>
<methods> <methods>
<method name="to_dictionary" qualifiers="const">
<return type="Dictionary" />
<description>
Serializes this GLTFLight instance into a [Dictionary].
</description>
</method>
<method name="to_node" qualifiers="const">
<return type="Light" />
<description>
Converts this GLTFLight instance into a Godot [Light] node.
</description>
</method>
</methods> </methods>
<members> <members>
<member name="color" type="Color" setter="set_color" getter="get_color" default="Color( 1, 1, 1, 1 )"> <member name="color" type="Color" setter="set_color" getter="get_color" default="Color( 1, 1, 1, 1 )">

View File

@ -30,7 +30,12 @@
#include "gltf_light.h" #include "gltf_light.h"
#include "scene/3d/light.h"
void GLTFLight::_bind_methods() { void GLTFLight::_bind_methods() {
ClassDB::bind_method(D_METHOD("to_node"), &GLTFLight::to_node);
ClassDB::bind_method(D_METHOD("to_dictionary"), &GLTFLight::to_dictionary);
ClassDB::bind_method(D_METHOD("get_color"), &GLTFLight::get_color); ClassDB::bind_method(D_METHOD("get_color"), &GLTFLight::get_color);
ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color); ClassDB::bind_method(D_METHOD("set_color", "color"), &GLTFLight::set_color);
ClassDB::bind_method(D_METHOD("get_intensity"), &GLTFLight::get_intensity); ClassDB::bind_method(D_METHOD("get_intensity"), &GLTFLight::get_intensity);
@ -99,3 +104,116 @@ float GLTFLight::get_outer_cone_angle() {
void GLTFLight::set_outer_cone_angle(float p_outer_cone_angle) { void GLTFLight::set_outer_cone_angle(float p_outer_cone_angle) {
outer_cone_angle = p_outer_cone_angle; outer_cone_angle = p_outer_cone_angle;
} }
Ref<GLTFLight> GLTFLight::from_node(const Light *p_light) {
Ref<GLTFLight> l;
l.instance();
ERR_FAIL_COND_V_MSG(!p_light, l, "Tried to create a GLTFLight from a Light node, but the given node was null.");
l->color = p_light->get_color();
if (cast_to<const DirectionalLight>(p_light)) {
l->type = "directional";
const DirectionalLight *light = cast_to<const DirectionalLight>(p_light);
l->intensity = light->get_param(DirectionalLight::PARAM_ENERGY);
l->range = FLT_MAX; // Range for directional lights is infinite in Godot.
} else if (cast_to<const OmniLight>(p_light)) {
l->type = "point";
const OmniLight *light = cast_to<const OmniLight>(p_light);
l->range = light->get_param(OmniLight::PARAM_RANGE);
l->intensity = light->get_param(OmniLight::PARAM_ENERGY);
} else if (cast_to<const SpotLight>(p_light)) {
l->type = "spot";
const SpotLight *light = cast_to<const SpotLight>(p_light);
l->range = light->get_param(SpotLight::PARAM_RANGE);
l->intensity = light->get_param(SpotLight::PARAM_ENERGY);
l->outer_cone_angle = Math::deg2rad(light->get_param(SpotLight::PARAM_SPOT_ANGLE));
// This equation is the inverse of the import equation (which has a desmos link).
float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight::PARAM_SPOT_ATTENUATION)));
angle_ratio = MAX(0, angle_ratio);
l->inner_cone_angle = l->outer_cone_angle * angle_ratio;
}
return l;
}
Light *GLTFLight::to_node() const {
if (type == "directional") {
DirectionalLight *light = memnew(DirectionalLight);
light->set_param(Light::PARAM_ENERGY, intensity);
light->set_color(color);
return light;
}
if (type == "point") {
OmniLight *light = memnew(OmniLight);
light->set_param(OmniLight::PARAM_ENERGY, intensity);
light->set_param(OmniLight::PARAM_RANGE, CLAMP(range, 0, 4096));
light->set_color(color);
return light;
}
if (type == "spot") {
SpotLight *light = memnew(SpotLight);
light->set_param(SpotLight::PARAM_ENERGY, intensity);
light->set_param(SpotLight::PARAM_RANGE, CLAMP(range, 0, 4096));
light->set_param(SpotLight::PARAM_SPOT_ANGLE, Math::rad2deg(outer_cone_angle));
light->set_color(color);
// Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b
// The points in desmos are not exact, except for (1, infinity).
float angle_ratio = inner_cone_angle / outer_cone_angle;
float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1;
light->set_param(SpotLight::PARAM_SPOT_ATTENUATION, angle_attenuation);
return light;
}
return memnew(Light);
}
Ref<GLTFLight> GLTFLight::from_dictionary(const Dictionary p_dictionary) {
ERR_FAIL_COND_V_MSG(!p_dictionary.has("type"), Ref<GLTFLight>(), "Failed to parse GLTF light, missing required field 'type'.");
Ref<GLTFLight> light;
light.instance();
const String &type = p_dictionary["type"];
light->type = type;
if (p_dictionary.has("color")) {
const Array &arr = p_dictionary["color"];
if (arr.size() == 3) {
light->color = Color(arr[0], arr[1], arr[2]).to_srgb();
} else {
ERR_PRINT("Error parsing GLTF light: The color must have exactly 3 numbers.");
}
}
if (p_dictionary.has("intensity")) {
light->intensity = p_dictionary["intensity"];
}
if (p_dictionary.has("range")) {
light->range = p_dictionary["range"];
}
if (type == "spot") {
const Dictionary &spot = p_dictionary["spot"];
light->inner_cone_angle = spot["innerConeAngle"];
light->outer_cone_angle = spot["outerConeAngle"];
if (light->inner_cone_angle >= light->outer_cone_angle) {
ERR_PRINT("Error parsing GLTF light: The inner angle must be smaller than the outer angle.");
}
} else if (type != "point" && type != "directional") {
ERR_PRINT("Error parsing GLTF light: Light type '" + type + "' is unknown.");
}
return light;
}
Dictionary GLTFLight::to_dictionary() const {
Dictionary d;
Array color_array;
color_array.resize(3);
color_array[0] = color.r;
color_array[1] = color.g;
color_array[2] = color.b;
d["color"] = color_array;
d["type"] = type;
if (type == "spot") {
Dictionary spot_dict;
spot_dict["innerConeAngle"] = inner_cone_angle;
spot_dict["outerConeAngle"] = outer_cone_angle;
d["spot"] = spot_dict;
}
d["intensity"] = intensity;
d["range"] = range;
return d;
}

View File

@ -32,6 +32,10 @@
#include "core/object/resource.h" #include "core/object/resource.h"
class Light;
// https://github.com/KhronosGroup/glTF/blob/main/extensions/2.0/Khronos/KHR_lights_punctual
class GLTFLight : public Resource { class GLTFLight : public Resource {
GDCLASS(GLTFLight, Resource) GDCLASS(GLTFLight, Resource)
friend class GLTFDocument; friend class GLTFDocument;
@ -65,6 +69,12 @@ public:
float get_outer_cone_angle(); float get_outer_cone_angle();
void set_outer_cone_angle(float p_outer_cone_angle); void set_outer_cone_angle(float p_outer_cone_angle);
static Ref<GLTFLight> from_node(const Light *p_light);
Light *to_node() const;
static Ref<GLTFLight> from_dictionary(const Dictionary p_dictionary);
Dictionary to_dictionary() const;
}; };
#endif // GLTF_LIGHT_H #endif // GLTF_LIGHT_H

View File

@ -4657,27 +4657,8 @@ Error GLTFDocument::_serialize_lights(Ref<GLTFState> p_state) {
} }
Array lights; Array lights;
for (GLTFLightIndex i = 0; i < p_state->lights.size(); i++) { for (GLTFLightIndex i = 0; i < p_state->lights.size(); i++) {
Dictionary d;
Ref<GLTFLight> light = p_state->lights[i]; Ref<GLTFLight> light = p_state->lights[i];
Array color; Dictionary d = light->to_dictionary();
color.resize(3);
color[0] = light->color.r;
color[1] = light->color.g;
color[2] = light->color.b;
d["color"] = color;
d["type"] = light->type;
if (light->type == "spot") {
Dictionary s;
float inner_cone_angle = light->inner_cone_angle;
s["innerConeAngle"] = inner_cone_angle;
float outer_cone_angle = light->outer_cone_angle;
s["outerConeAngle"] = outer_cone_angle;
d["spot"] = s;
}
float intensity = light->intensity;
d["intensity"] = intensity;
float range = light->range;
d["range"] = range;
lights.push_back(d); lights.push_back(d);
} }
@ -4700,27 +4681,8 @@ Error GLTFDocument::_serialize_cameras(Ref<GLTFState> p_state) {
Array cameras; Array cameras;
cameras.resize(p_state->cameras.size()); cameras.resize(p_state->cameras.size());
for (GLTFCameraIndex i = 0; i < p_state->cameras.size(); i++) { for (GLTFCameraIndex i = 0; i < p_state->cameras.size(); i++) {
Dictionary d;
Ref<GLTFCamera> camera = p_state->cameras[i]; Ref<GLTFCamera> camera = p_state->cameras[i];
Dictionary d = camera->to_dictionary();
if (camera->get_perspective() == false) {
Dictionary og;
og["ymag"] = Math::deg2rad(camera->get_fov_size());
og["xmag"] = Math::deg2rad(camera->get_fov_size());
og["zfar"] = camera->get_zfar();
og["znear"] = camera->get_znear();
d["orthographic"] = og;
d["type"] = "orthographic";
} else if (camera->get_perspective()) {
Dictionary ppt;
// GLTF spec is in radians, Pandemonium's camera is in degrees.
ppt["yfov"] = Math::deg2rad(camera->get_fov_size());
ppt["zfar"] = camera->get_zfar();
ppt["znear"] = camera->get_znear();
d["perspective"] = ppt;
d["type"] = "perspective";
}
cameras[i] = d; cameras[i] = d;
} }
@ -4751,33 +4713,9 @@ Error GLTFDocument::_parse_lights(Ref<GLTFState> p_state) {
const Array &lights = lights_punctual["lights"]; const Array &lights = lights_punctual["lights"];
for (GLTFLightIndex light_i = 0; light_i < lights.size(); light_i++) { for (GLTFLightIndex light_i = 0; light_i < lights.size(); light_i++) {
const Dictionary &d = lights[light_i]; Ref<GLTFLight> light = GLTFLight::from_dictionary(lights[light_i]);
if (light.is_null()) {
Ref<GLTFLight> light; return Error::ERR_PARSE_ERROR;
light.instance();
ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
const String &type = d["type"];
light->type = type;
if (d.has("color")) {
const Array &arr = d["color"];
ERR_FAIL_COND_V(arr.size() != 3, ERR_PARSE_ERROR);
const Color c = Color(arr[0], arr[1], arr[2]).to_srgb();
light->color = c;
}
if (d.has("intensity")) {
light->intensity = d["intensity"];
}
if (d.has("range")) {
light->range = d["range"];
}
if (type == "spot") {
const Dictionary &spot = d["spot"];
light->inner_cone_angle = spot["innerConeAngle"];
light->outer_cone_angle = spot["outerConeAngle"];
ERR_CONTINUE_MSG(light->inner_cone_angle >= light->outer_cone_angle, "The inner angle must be smaller than the outer angle.");
} else if (type != "point" && type != "directional") {
ERR_CONTINUE_MSG(true, "Light type is unknown.");
} }
p_state->lights.push_back(light); p_state->lights.push_back(light);
@ -4796,36 +4734,9 @@ Error GLTFDocument::_parse_cameras(Ref<GLTFState> p_state) {
const Array cameras = p_state->json["cameras"]; const Array cameras = p_state->json["cameras"];
for (GLTFCameraIndex i = 0; i < cameras.size(); i++) { for (GLTFCameraIndex i = 0; i < cameras.size(); i++) {
const Dictionary &d = cameras[i]; Ref<GLTFCamera> camera = GLTFCamera::from_dictionary(cameras[i]);
if (camera.is_null()) {
Ref<GLTFCamera> camera; return Error::ERR_PARSE_ERROR;
camera.instance();
ERR_FAIL_COND_V(!d.has("type"), ERR_PARSE_ERROR);
const String &type = d["type"];
if (type == "orthographic") {
camera->set_perspective(false);
if (d.has("orthographic")) {
const Dictionary &og = d["orthographic"];
// GLTF spec is in radians, Pandemonium's camera is in degrees.
camera->set_fov_size(Math::rad2deg(real_t(og["ymag"])));
camera->set_zfar(og["zfar"]);
camera->set_znear(og["znear"]);
} else {
camera->set_fov_size(10);
}
} else if (type == "perspective") {
camera->set_perspective(true);
if (d.has("perspective")) {
const Dictionary &ppt = d["perspective"];
// GLTF spec is in radians, Pandemonium's camera is in degrees.
camera->set_fov_size(Math::rad2deg(real_t(ppt["yfov"])));
camera->set_zfar(ppt["zfar"]);
camera->set_znear(ppt["znear"]);
} else {
camera->set_fov_size(10);
}
} else {
ERR_FAIL_V_MSG(ERR_PARSE_ERROR, "Camera should be in 'orthographic' or 'perspective'");
} }
p_state->cameras.push_back(camera); p_state->cameras.push_back(camera);
@ -5310,44 +5221,7 @@ Spatial *GLTFDocument::_generate_light(Ref<GLTFState> p_state, Node *scene_paren
Ref<GLTFLight> l = p_state->lights[gltf_node->light]; Ref<GLTFLight> l = p_state->lights[gltf_node->light];
float intensity = l->intensity; return l->to_node();
if (intensity > 10) {
// GLTF spec has the default around 1, but Blender defaults lights to 100.
// The only sane way to handle this is to check where it came from and
// handle it accordingly. If it's over 10, it probably came from Blender.
intensity /= 100;
}
if (l->type == "directional") {
DirectionalLight *light = memnew(DirectionalLight);
light->set_param(Light::PARAM_ENERGY, intensity);
light->set_color(l->color);
return light;
}
const float range = CLAMP(l->range, 0, 4096);
if (l->type == "point") {
OmniLight *light = memnew(OmniLight);
light->set_param(OmniLight::PARAM_ENERGY, intensity);
light->set_param(OmniLight::PARAM_RANGE, range);
light->set_color(l->color);
return light;
}
if (l->type == "spot") {
SpotLight *light = memnew(SpotLight);
light->set_param(SpotLight::PARAM_ENERGY, intensity);
light->set_param(SpotLight::PARAM_RANGE, range);
light->set_param(SpotLight::PARAM_SPOT_ANGLE, Math::rad2deg(l->outer_cone_angle));
light->set_color(l->color);
// Line of best fit derived from guessing, see https://www.desmos.com/calculator/biiflubp8b
// The points in desmos are not exact, except for (1, infinity).
float angle_ratio = l->inner_cone_angle / l->outer_cone_angle;
float angle_attenuation = 0.2 / (1 - angle_ratio) - 0.1;
light->set_param(SpotLight::PARAM_SPOT_ATTENUATION, angle_attenuation);
return light;
}
return memnew(Spatial);
} }
Camera *GLTFDocument::_generate_camera(Ref<GLTFState> p_state, Node *p_scene_parent, const GLTFNodeIndex p_node_index) { Camera *GLTFDocument::_generate_camera(Ref<GLTFState> p_state, Node *p_scene_parent, const GLTFNodeIndex p_node_index) {
@ -5355,35 +5229,16 @@ Camera *GLTFDocument::_generate_camera(Ref<GLTFState> p_state, Node *p_scene_par
ERR_FAIL_INDEX_V(gltf_node->camera, p_state->cameras.size(), nullptr); ERR_FAIL_INDEX_V(gltf_node->camera, p_state->cameras.size(), nullptr);
Camera *camera = memnew(Camera);
print_verbose("glTF: Creating camera for: " + gltf_node->get_name()); print_verbose("glTF: Creating camera for: " + gltf_node->get_name());
Ref<GLTFCamera> c = p_state->cameras[gltf_node->camera]; Ref<GLTFCamera> c = p_state->cameras[gltf_node->camera];
if (c->get_perspective()) { return c->to_node();
camera->set_perspective(c->get_fov_size(), c->get_znear(), c->get_zfar());
} else {
camera->set_orthogonal(c->get_fov_size(), c->get_znear(), c->get_zfar());
}
return camera;
} }
GLTFCameraIndex GLTFDocument::_convert_camera(Ref<GLTFState> p_state, Camera *p_camera) { GLTFCameraIndex GLTFDocument::_convert_camera(Ref<GLTFState> p_state, Camera *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.instance();
if (p_camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
c->set_perspective(true);
c->set_fov_size(p_camera->get_fov());
c->set_zfar(p_camera->get_zfar());
c->set_znear(p_camera->get_znear());
} else {
c->set_fov_size(p_camera->get_fov());
c->set_zfar(p_camera->get_zfar());
c->set_znear(p_camera->get_znear());
}
GLTFCameraIndex camera_index = p_state->cameras.size(); GLTFCameraIndex camera_index = p_state->cameras.size();
p_state->cameras.push_back(c); p_state->cameras.push_back(c);
return camera_index; return camera_index;
@ -5392,31 +5247,7 @@ GLTFCameraIndex GLTFDocument::_convert_camera(Ref<GLTFState> p_state, Camera *p_
GLTFLightIndex GLTFDocument::_convert_light(Ref<GLTFState> p_state, Light *p_light) { GLTFLightIndex GLTFDocument::_convert_light(Ref<GLTFState> p_state, Light *p_light) {
print_verbose("glTF: Converting light: " + p_light->get_name()); print_verbose("glTF: Converting light: " + p_light->get_name());
Ref<GLTFLight> l; Ref<GLTFLight> l = GLTFLight::from_node(p_light);
l.instance();
l->color = p_light->get_color();
if (cast_to<DirectionalLight>(p_light)) {
l->type = "directional";
DirectionalLight *light = cast_to<DirectionalLight>(p_light);
l->intensity = light->get_param(DirectionalLight::PARAM_ENERGY);
l->range = FLT_MAX; // Range for directional lights is infinite in Pandemonium.
} else if (cast_to<OmniLight>(p_light)) {
l->type = "point";
OmniLight *light = cast_to<OmniLight>(p_light);
l->range = light->get_param(OmniLight::PARAM_RANGE);
l->intensity = light->get_param(OmniLight::PARAM_ENERGY);
} else if (cast_to<SpotLight>(p_light)) {
l->type = "spot";
SpotLight *light = cast_to<SpotLight>(p_light);
l->range = light->get_param(SpotLight::PARAM_RANGE);
l->intensity = light->get_param(SpotLight::PARAM_ENERGY);
l->outer_cone_angle = Math::deg2rad(light->get_param(SpotLight::PARAM_SPOT_ANGLE));
// This equation is the inverse of the import equation (which has a desmos link).
float angle_ratio = 1 - (0.2 / (0.1 + light->get_param(SpotLight::PARAM_SPOT_ATTENUATION)));
angle_ratio = MAX(0, angle_ratio);
l->inner_cone_angle = l->outer_cone_angle * angle_ratio;
}
GLTFLightIndex light_index = p_state->lights.size(); GLTFLightIndex light_index = p_state->lights.size();
p_state->lights.push_back(l); p_state->lights.push_back(l);

View File

@ -30,11 +30,18 @@
#include "gltf_camera.h" #include "gltf_camera.h"
#include "scene/3d/camera.h"
void GLTFCamera::_bind_methods() { void GLTFCamera::_bind_methods() {
ClassDB::bind_method(D_METHOD("to_node"), &GLTFCamera::to_node);
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_size"), &GLTFCamera::get_fov_size); ClassDB::bind_method(D_METHOD("get_fov_size"), &GLTFCamera::get_fov_size);
ClassDB::bind_method(D_METHOD("set_fov_size", "fov_size"), &GLTFCamera::set_fov_size); ClassDB::bind_method(D_METHOD("set_fov_size", "fov_size"), &GLTFCamera::set_fov_size);
ClassDB::bind_method(D_METHOD("get_size_mag"), &GLTFCamera::get_size_mag);
ClassDB::bind_method(D_METHOD("set_size_mag", "size_mag"), &GLTFCamera::set_size_mag);
ClassDB::bind_method(D_METHOD("get_zfar"), &GLTFCamera::get_zfar); ClassDB::bind_method(D_METHOD("get_zfar"), &GLTFCamera::get_zfar);
ClassDB::bind_method(D_METHOD("set_zfar", "zfar"), &GLTFCamera::set_zfar); ClassDB::bind_method(D_METHOD("set_zfar", "zfar"), &GLTFCamera::set_zfar);
ClassDB::bind_method(D_METHOD("get_znear"), &GLTFCamera::get_znear); ClassDB::bind_method(D_METHOD("get_znear"), &GLTFCamera::get_znear);
@ -42,6 +49,83 @@ void GLTFCamera::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective"); // bool ADD_PROPERTY(PropertyInfo(Variant::BOOL, "perspective"), "set_perspective", "get_perspective"); // bool
ADD_PROPERTY(PropertyInfo(Variant::REAL, "fov_size"), "set_fov_size", "get_fov_size"); // float ADD_PROPERTY(PropertyInfo(Variant::REAL, "fov_size"), "set_fov_size", "get_fov_size"); // float
ADD_PROPERTY(PropertyInfo(Variant::REAL, "size_mag"), "set_size_mag", "get_size_mag"); // float
ADD_PROPERTY(PropertyInfo(Variant::REAL, "zfar"), "set_zfar", "get_zfar"); // float ADD_PROPERTY(PropertyInfo(Variant::REAL, "zfar"), "set_zfar", "get_zfar"); // float
ADD_PROPERTY(PropertyInfo(Variant::REAL, "znear"), "set_znear", "get_znear"); // float ADD_PROPERTY(PropertyInfo(Variant::REAL, "znear"), "set_znear", "get_znear"); // float
} }
Ref<GLTFCamera> GLTFCamera::from_node(const Camera *p_camera) {
Ref<GLTFCamera> c;
c.instance();
ERR_FAIL_COND_V_MSG(!p_camera, c, "Tried to create a GLTFCamera from a Camera node, but the given node was null.");
c->set_perspective(p_camera->get_projection() == Camera::PROJECTION_PERSPECTIVE);
// GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
c->set_fov_size(Math::deg2rad(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_zfar(p_camera->get_zfar());
c->set_znear(p_camera->get_znear());
return c;
}
Camera *GLTFCamera::to_node() const {
Camera *camera = memnew(Camera);
camera->set_projection(perspective ? Camera::PROJECTION_PERSPECTIVE : Camera::PROJECTION_ORTHOGONAL);
// GLTF spec (yfov) is in radians, Godot's camera (fov) is in degrees.
camera->set_fov(Math::rad2deg(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_znear(znear);
camera->set_zfar(zfar);
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.instance();
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_size(persp["yfov"]);
if (persp.has("zfar")) {
camera->set_zfar(persp["zfar"]);
}
camera->set_znear(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_zfar(ortho["zfar"]);
camera->set_znear(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"] = zfar;
persp["znear"] = znear;
d["perspective"] = persp;
d["type"] = "perspective";
} else {
Dictionary ortho;
ortho["ymag"] = size_mag;
ortho["xmag"] = size_mag;
ortho["zfar"] = zfar;
ortho["znear"] = znear;
d["orthographic"] = ortho;
d["type"] = "orthographic";
}
return d;
}

View File

@ -32,14 +32,22 @@
#include "core/object/resource.h" #include "core/object/resource.h"
class Camera;
// Reference and test file:
// https://github.com/KhronosGroup/glTF-Tutorials/blob/master/gltfTutorial/gltfTutorial_015_SimpleCameras.md
class GLTFCamera : public Resource { class GLTFCamera : public Resource {
GDCLASS(GLTFCamera, Resource); GDCLASS(GLTFCamera, Resource);
private: private:
// GLTF has no default camera values, they should always be specified in
// the GLTF file. Here we default to Godot's default camera settings.
bool perspective = true; bool perspective = true;
float fov_size = 75.0; real_t fov = Math::deg2rad(75.0);
float zfar = 4000.0; real_t size_mag = 0.5;
float znear = 0.05; real_t zfar = 4000.0;
real_t znear = 0.05;
protected: protected:
static void _bind_methods(); static void _bind_methods();
@ -47,12 +55,20 @@ protected:
public: public:
bool get_perspective() const { return perspective; } bool get_perspective() const { return perspective; }
void set_perspective(bool p_val) { perspective = p_val; } void set_perspective(bool p_val) { perspective = p_val; }
float get_fov_size() const { return fov_size; } real_t get_fov_size() const { return fov; }
void set_fov_size(float p_val) { fov_size = p_val; } void set_fov_size(real_t p_val) { fov = p_val; }
float get_zfar() const { return zfar; } real_t get_size_mag() const { return size_mag; }
void set_zfar(float p_val) { zfar = p_val; } void set_size_mag(real_t p_val) { size_mag = p_val; }
float get_znear() const { return znear; } real_t get_zfar() const { return zfar; }
void set_znear(float p_val) { znear = p_val; } void set_zfar(real_t p_val) { zfar = p_val; }
real_t get_znear() const { return znear; }
void set_znear(real_t p_val) { znear = p_val; }
static Ref<GLTFCamera> from_node(const Camera *p_camera);
Camera *to_node() const;
static Ref<GLTFCamera> from_dictionary(const Dictionary p_dictionary);
Dictionary to_dictionary() const;
}; };
#endif // GLTF_CAMERA_H #endif // GLTF_CAMERA_H