2020-12-21 23:39:32 +08:00
|
|
|
/**************************************************************************/
|
|
|
|
/* gltf_document.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
|
|
|
|
|
|
|
#ifndef GLTF_DOCUMENT_H
|
|
|
|
#define GLTF_DOCUMENT_H
|
|
|
|
|
2022-09-19 09:35:13 +08:00
|
|
|
#include "extensions/gltf_document_extension.h"
|
2024-02-16 21:25:15 +08:00
|
|
|
#include "extensions/gltf_spec_gloss.h"
|
|
|
|
#include "gltf_defines.h"
|
|
|
|
#include "gltf_state.h"
|
|
|
|
|
|
|
|
#include "scene/3d/mesh_instance_3d.h"
|
|
|
|
#include "scene/3d/multimesh_instance_3d.h"
|
2020-12-21 23:39:32 +08:00
|
|
|
|
2021-11-12 20:42:58 +08:00
|
|
|
#include "modules/modules_enabled.gen.h" // For csg, gridmap.
|
2024-02-16 21:25:15 +08:00
|
|
|
#ifdef MODULE_CSG_ENABLED
|
|
|
|
#include "modules/csg/csg_shape.h"
|
|
|
|
#endif // MODULE_CSG_ENABLED
|
|
|
|
#ifdef MODULE_GRIDMAP_ENABLED
|
|
|
|
#include "modules/gridmap/grid_map.h"
|
|
|
|
#endif // MODULE_GRIDMAP_ENABLED
|
2021-11-12 20:42:58 +08:00
|
|
|
|
2020-12-21 23:39:32 +08:00
|
|
|
class GLTFDocument : public Resource {
|
|
|
|
GDCLASS(GLTFDocument, Resource);
|
|
|
|
|
|
|
|
public:
|
2020-12-29 03:51:48 +08:00
|
|
|
const int32_t JOINT_GROUP_SIZE = 4;
|
2020-12-21 23:39:32 +08:00
|
|
|
|
|
|
|
enum {
|
|
|
|
ARRAY_BUFFER = 34962,
|
|
|
|
ELEMENT_ARRAY_BUFFER = 34963,
|
|
|
|
};
|
2022-07-19 08:58:27 +08:00
|
|
|
enum {
|
|
|
|
TEXTURE_TYPE_GENERIC = 0,
|
|
|
|
TEXTURE_TYPE_NORMAL = 1,
|
|
|
|
};
|
2023-07-23 11:36:57 +08:00
|
|
|
enum RootNodeMode {
|
|
|
|
ROOT_NODE_MODE_SINGLE_ROOT,
|
|
|
|
ROOT_NODE_MODE_KEEP_ROOT,
|
|
|
|
ROOT_NODE_MODE_MULTI_ROOT,
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2023-11-01 06:55:15 +08:00
|
|
|
int _naming_version = 1;
|
2023-07-23 11:36:57 +08:00
|
|
|
String _image_format = "PNG";
|
|
|
|
float _lossy_quality = 0.75f;
|
|
|
|
Ref<GLTFDocumentExtension> _image_save_extension;
|
|
|
|
RootNodeMode _root_node_mode = RootNodeMode::ROOT_NODE_MODE_SINGLE_ROOT;
|
2020-12-21 23:39:32 +08:00
|
|
|
|
2021-09-10 15:54:10 +08:00
|
|
|
protected:
|
|
|
|
static void _bind_methods();
|
2024-02-16 21:25:15 +08:00
|
|
|
String _gen_unique_name(Ref<GLTFState> p_state, const String &p_name);
|
|
|
|
static Vector<Ref<GLTFDocumentExtension>> all_document_extensions;
|
|
|
|
Vector<Ref<GLTFDocumentExtension>> document_extensions;
|
2021-09-10 15:54:10 +08:00
|
|
|
|
|
|
|
public:
|
2022-09-19 09:35:13 +08:00
|
|
|
static void register_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension, bool p_first_priority = false);
|
2022-11-23 04:07:56 +08:00
|
|
|
static void unregister_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension);
|
2022-09-19 09:35:13 +08:00
|
|
|
static void unregister_all_gltf_document_extensions();
|
2023-09-27 01:01:26 +08:00
|
|
|
static Vector<Ref<GLTFDocumentExtension>> get_all_gltf_document_extensions();
|
2024-08-29 08:46:01 +08:00
|
|
|
static Vector<String> get_supported_gltf_extensions();
|
|
|
|
static HashSet<String> get_supported_gltf_extensions_hashset();
|
2021-09-10 15:54:10 +08:00
|
|
|
|
2024-10-04 15:19:12 +08:00
|
|
|
static NodePath _find_material_node_path(Ref<GLTFState> p_state, Ref<Material> p_material);
|
|
|
|
static Ref<GLTFObjectModelProperty> import_object_model_property(Ref<GLTFState> p_state, const String &p_json_pointer);
|
|
|
|
static Ref<GLTFObjectModelProperty> export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index);
|
|
|
|
|
2023-11-01 06:55:15 +08:00
|
|
|
void set_naming_version(int p_version);
|
|
|
|
int get_naming_version() const;
|
2023-07-16 14:03:49 +08:00
|
|
|
void set_image_format(const String &p_image_format);
|
|
|
|
String get_image_format() const;
|
|
|
|
void set_lossy_quality(float p_lossy_quality);
|
|
|
|
float get_lossy_quality() const;
|
2023-07-23 11:36:57 +08:00
|
|
|
void set_root_node_mode(RootNodeMode p_root_node_mode);
|
|
|
|
RootNodeMode get_root_node_mode() const;
|
2024-02-16 21:25:15 +08:00
|
|
|
static String _gen_unique_name_static(HashSet<String> &r_unique_names, const String &p_name);
|
2023-07-16 14:03:49 +08:00
|
|
|
|
2020-12-21 23:39:32 +08:00
|
|
|
private:
|
2022-12-11 05:05:13 +08:00
|
|
|
void _build_parent_hierachy(Ref<GLTFState> p_state);
|
2020-12-21 23:39:32 +08:00
|
|
|
double _filter_number(double p_float);
|
2024-03-10 15:51:52 +08:00
|
|
|
void _round_min_max_components(Vector<double> &r_type_min, Vector<double> &r_type_max);
|
2024-07-10 16:02:36 +08:00
|
|
|
String _get_component_type_name(const GLTFAccessor::GLTFComponentType p_component_type);
|
|
|
|
int _get_component_type_size(const GLTFAccessor::GLTFComponentType p_component_type);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_scenes(Ref<GLTFState> p_state);
|
|
|
|
Error _parse_nodes(Ref<GLTFState> p_state);
|
2024-07-04 10:57:17 +08:00
|
|
|
String _get_accessor_type_name(const GLTFAccessor::GLTFAccessorType p_accessor_type);
|
2022-12-11 05:05:13 +08:00
|
|
|
String _sanitize_animation_name(const String &p_name);
|
|
|
|
String _gen_unique_animation_name(Ref<GLTFState> p_state, const String &p_name);
|
|
|
|
String _sanitize_bone_name(const String &p_name);
|
|
|
|
String _gen_unique_bone_name(Ref<GLTFState> p_state,
|
|
|
|
const GLTFSkeletonIndex p_skel_i,
|
2020-12-21 23:39:32 +08:00
|
|
|
const String &p_name);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFTextureIndex _set_texture(Ref<GLTFState> p_state, Ref<Texture2D> p_texture,
|
2021-10-04 23:49:42 +08:00
|
|
|
StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<Texture2D> _get_texture(Ref<GLTFState> p_state,
|
2022-07-19 08:58:27 +08:00
|
|
|
const GLTFTextureIndex p_texture, int p_texture_type);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFTextureSamplerIndex _set_sampler_for_mode(Ref<GLTFState> p_state,
|
2021-10-04 23:49:42 +08:00
|
|
|
StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<GLTFTextureSampler> _get_sampler_for_texture(Ref<GLTFState> p_state,
|
2021-10-04 23:49:42 +08:00
|
|
|
const GLTFTextureIndex p_texture);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_json(const String &p_path, Ref<GLTFState> p_state);
|
|
|
|
Error _parse_glb(Ref<FileAccess> p_file, Ref<GLTFState> p_state);
|
|
|
|
void _compute_node_heights(Ref<GLTFState> p_state);
|
|
|
|
Error _parse_buffers(Ref<GLTFState> p_state, const String &p_base_path);
|
|
|
|
Error _parse_buffer_views(Ref<GLTFState> p_state);
|
2024-07-04 10:57:17 +08:00
|
|
|
GLTFAccessor::GLTFAccessorType _get_accessor_type_from_str(const String &p_string);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_accessors(Ref<GLTFState> p_state);
|
|
|
|
Error _decode_buffer_view(Ref<GLTFState> p_state, double *p_dst,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFBufferViewIndex p_buffer_view,
|
2022-12-11 05:05:13 +08:00
|
|
|
const int p_skip_every, const int p_skip_bytes,
|
|
|
|
const int p_element_size, const int p_count,
|
2024-07-04 10:57:17 +08:00
|
|
|
const GLTFAccessor::GLTFAccessorType p_accessor_type, const int p_component_count,
|
2024-07-10 16:02:36 +08:00
|
|
|
const GLTFAccessor::GLTFComponentType p_component_type, const int p_component_size,
|
2022-12-11 05:05:13 +08:00
|
|
|
const bool p_normalized, const int p_byte_offset,
|
|
|
|
const bool p_for_vertex);
|
|
|
|
Vector<double> _decode_accessor(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<float> _decode_accessor_as_floats(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
2024-03-12 19:50:54 +08:00
|
|
|
const bool p_for_vertex,
|
|
|
|
const Vector<int> &p_packed_vertex_ids = Vector<int>());
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<int> _decode_accessor_as_ints(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
2024-03-12 19:50:54 +08:00
|
|
|
const bool p_for_vertex,
|
|
|
|
const Vector<int> &p_packed_vertex_ids = Vector<int>());
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<Vector2> _decode_accessor_as_vec2(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
2024-03-12 19:50:54 +08:00
|
|
|
const bool p_for_vertex,
|
|
|
|
const Vector<int> &p_packed_vertex_ids = Vector<int>());
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<Vector3> _decode_accessor_as_vec3(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
2024-03-12 19:50:54 +08:00
|
|
|
const bool p_for_vertex,
|
|
|
|
const Vector<int> &p_packed_vertex_ids = Vector<int>());
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<Color> _decode_accessor_as_color(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
2024-03-12 19:50:54 +08:00
|
|
|
const bool p_for_vertex,
|
|
|
|
const Vector<int> &p_packed_vertex_ids = Vector<int>());
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<Quaternion> _decode_accessor_as_quaternion(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<Transform2D> _decode_accessor_as_xform2d(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<Basis> _decode_accessor_as_basis(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
Vector<Transform3D> _decode_accessor_as_xform(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFAccessorIndex p_accessor,
|
|
|
|
const bool p_for_vertex);
|
2024-06-30 02:14:09 +08:00
|
|
|
Vector<Variant> _decode_accessor_as_variant(Ref<GLTFState> p_state,
|
|
|
|
const GLTFAccessorIndex p_accessor,
|
|
|
|
Variant::Type p_variant_type,
|
|
|
|
GLTFAccessor::GLTFAccessorType p_accessor_type);
|
|
|
|
GLTFAccessorIndex _encode_accessor_as_variant(Ref<GLTFState> p_state,
|
|
|
|
Vector<Variant> p_attribs,
|
|
|
|
Variant::Type p_variant_type,
|
|
|
|
GLTFAccessor::GLTFAccessorType p_accessor_type,
|
|
|
|
GLTFAccessor::GLTFComponentType p_component_type = GLTFAccessor::COMPONENT_TYPE_SINGLE_FLOAT);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_meshes(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_textures(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_texture_samplers(Ref<GLTFState> p_state);
|
2023-07-19 03:42:22 +08:00
|
|
|
Error _serialize_images(Ref<GLTFState> p_state);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _serialize_lights(Ref<GLTFState> p_state);
|
2023-07-16 11:23:43 +08:00
|
|
|
Ref<Image> _parse_image_bytes_into_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_mime_type, int p_index, String &r_file_extension);
|
|
|
|
void _parse_image_save_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_file_extension, int p_index, Ref<Image> p_image);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_images(Ref<GLTFState> p_state, const String &p_base_path);
|
|
|
|
Error _parse_textures(Ref<GLTFState> p_state);
|
|
|
|
Error _parse_texture_samplers(Ref<GLTFState> p_state);
|
|
|
|
Error _parse_materials(Ref<GLTFState> p_state);
|
|
|
|
void _set_texture_transform_uv1(const Dictionary &d, Ref<BaseMaterial3D> p_material);
|
2020-12-21 23:39:32 +08:00
|
|
|
void spec_gloss_to_rough_metal(Ref<GLTFSpecGloss> r_spec_gloss,
|
|
|
|
Ref<BaseMaterial3D> p_material);
|
|
|
|
static void spec_gloss_to_metal_base_color(const Color &p_specular_factor,
|
|
|
|
const Color &p_diffuse,
|
|
|
|
Color &r_base_color,
|
|
|
|
float &r_metallic);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_skins(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_skins(Ref<GLTFState> p_state);
|
|
|
|
Error _create_skins(Ref<GLTFState> p_state);
|
|
|
|
bool _skins_are_same(const Ref<Skin> p_skin_a, const Ref<Skin> p_skin_b);
|
|
|
|
void _remove_duplicate_skins(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_cameras(Ref<GLTFState> p_state);
|
|
|
|
Error _parse_cameras(Ref<GLTFState> p_state);
|
|
|
|
Error _parse_lights(Ref<GLTFState> p_state);
|
|
|
|
Error _parse_animations(Ref<GLTFState> p_state);
|
2024-10-04 15:37:44 +08:00
|
|
|
void _parse_animation_pointer(Ref<GLTFState> p_state, const String &p_animation_json_pointer, const Ref<GLTFAnimation> p_gltf_animation, const GLTFAnimation::Interpolation p_interp, const Vector<double> &p_times, const int p_output_value_accessor_index);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _serialize_animations(Ref<GLTFState> p_state);
|
|
|
|
BoneAttachment3D *_generate_bone_attachment(Ref<GLTFState> p_state,
|
|
|
|
Skeleton3D *p_skeleton,
|
|
|
|
const GLTFNodeIndex p_node_index,
|
|
|
|
const GLTFNodeIndex p_bone_index);
|
|
|
|
ImporterMeshInstance3D *_generate_mesh_instance(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
|
|
|
|
Camera3D *_generate_camera(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
|
|
|
|
Light3D *_generate_light(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
|
|
|
|
Node3D *_generate_spatial(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
|
2023-07-22 11:06:50 +08:00
|
|
|
void _assign_node_names(Ref<GLTFState> p_state);
|
2020-12-21 23:39:32 +08:00
|
|
|
template <typename T>
|
2024-07-10 16:26:35 +08:00
|
|
|
T _interpolate_track(const Vector<double> &p_times, const Vector<T> &p_values,
|
2020-12-21 23:39:32 +08:00
|
|
|
const float p_time,
|
|
|
|
const GLTFAnimation::Interpolation p_interp);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_quaternions(Ref<GLTFState> p_state,
|
2021-01-20 15:02:02 +08:00
|
|
|
const Vector<Quaternion> p_attribs,
|
2020-12-21 23:39:32 +08:00
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_weights(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const Vector<Color> p_attribs,
|
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_joints(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const Vector<Color> p_attribs,
|
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_floats(Ref<GLTFState> p_state,
|
2024-07-10 16:26:35 +08:00
|
|
|
const Vector<double> p_attribs,
|
2020-12-21 23:39:32 +08:00
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_vec2(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const Vector<Vector2> p_attribs,
|
|
|
|
const bool p_for_vertex);
|
|
|
|
|
2022-12-11 05:05:13 +08:00
|
|
|
void _calc_accessor_vec2_min_max(int p_i, const int p_element_count, Vector<double> &p_type_max, Vector2 p_attribs, Vector<double> &p_type_min) {
|
|
|
|
if (p_i == 0) {
|
|
|
|
for (int32_t type_i = 0; type_i < p_element_count; type_i++) {
|
|
|
|
p_type_max.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
|
|
|
|
p_type_min.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
|
2020-12-21 23:39:32 +08:00
|
|
|
}
|
|
|
|
}
|
2022-12-11 05:05:13 +08:00
|
|
|
for (int32_t type_i = 0; type_i < p_element_count; type_i++) {
|
|
|
|
p_type_max.write[type_i] = MAX(p_attribs[(p_i * p_element_count) + type_i], p_type_max[type_i]);
|
|
|
|
p_type_min.write[type_i] = MIN(p_attribs[(p_i * p_element_count) + type_i], p_type_min[type_i]);
|
|
|
|
p_type_max.write[type_i] = _filter_number(p_type_max.write[type_i]);
|
|
|
|
p_type_min.write[type_i] = _filter_number(p_type_min.write[type_i]);
|
2020-12-21 23:39:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_vec3(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const Vector<Vector3> p_attribs,
|
|
|
|
const bool p_for_vertex);
|
2024-03-12 17:39:15 +08:00
|
|
|
GLTFAccessorIndex _encode_sparse_accessor_as_vec3(Ref<GLTFState> p_state, const Vector<Vector3> p_attribs, const Vector<Vector3> p_reference_attribs, const float p_reference_multiplier, const bool p_for_vertex, const GLTFAccessorIndex p_reference_accessor);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_color(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const Vector<Color> p_attribs,
|
|
|
|
const bool p_for_vertex);
|
|
|
|
|
|
|
|
void _calc_accessor_min_max(int p_i, const int p_element_count, Vector<double> &p_type_max, Vector<double> p_attribs, Vector<double> &p_type_min);
|
|
|
|
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_ints(Ref<GLTFState> p_state,
|
2020-12-21 23:39:32 +08:00
|
|
|
const Vector<int32_t> p_attribs,
|
2024-03-10 15:51:52 +08:00
|
|
|
const bool p_for_vertex,
|
|
|
|
const bool p_for_indices);
|
2022-12-11 05:05:13 +08:00
|
|
|
GLTFAccessorIndex _encode_accessor_as_xform(Ref<GLTFState> p_state,
|
2020-10-17 13:08:21 +08:00
|
|
|
const Vector<Transform3D> p_attribs,
|
2020-12-21 23:39:32 +08:00
|
|
|
const bool p_for_vertex);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _encode_buffer_view(Ref<GLTFState> p_state, const double *p_src,
|
2024-07-04 10:57:17 +08:00
|
|
|
const int p_count, const GLTFAccessor::GLTFAccessorType p_accessor_type,
|
2024-07-10 16:02:36 +08:00
|
|
|
const GLTFAccessor::GLTFComponentType p_component_type, const bool p_normalized,
|
2022-12-11 05:05:13 +08:00
|
|
|
const int p_byte_offset, const bool p_for_vertex,
|
2024-03-10 15:51:52 +08:00
|
|
|
GLTFBufferViewIndex &r_accessor, const bool p_for_indices = false);
|
2024-03-02 05:07:07 +08:00
|
|
|
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _encode_accessors(Ref<GLTFState> p_state);
|
|
|
|
Error _encode_buffer_views(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_materials(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_meshes(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_nodes(Ref<GLTFState> p_state);
|
|
|
|
Error _serialize_scenes(Ref<GLTFState> p_state);
|
2020-12-21 23:39:32 +08:00
|
|
|
String interpolation_to_string(const GLTFAnimation::Interpolation p_interp);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _encode_buffer_bins(Ref<GLTFState> p_state, const String &p_path);
|
|
|
|
Error _encode_buffer_glb(Ref<GLTFState> p_state, const String &p_path);
|
|
|
|
PackedByteArray _serialize_glb_buffer(Ref<GLTFState> p_state, Error *r_err);
|
2020-12-21 23:39:32 +08:00
|
|
|
Dictionary _serialize_texture_transform_uv1(Ref<BaseMaterial3D> p_material);
|
|
|
|
Dictionary _serialize_texture_transform_uv2(Ref<BaseMaterial3D> p_material);
|
2023-07-10 13:18:55 +08:00
|
|
|
Error _serialize_asset_header(Ref<GLTFState> p_state);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _serialize_file(Ref<GLTFState> p_state, const String p_path);
|
|
|
|
Error _serialize_gltf_extensions(Ref<GLTFState> p_state) const;
|
2020-12-21 23:39:32 +08:00
|
|
|
|
|
|
|
public:
|
2021-08-22 09:56:25 +08:00
|
|
|
// https://www.itu.int/rec/R-REC-BT.601
|
|
|
|
// https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf
|
2020-12-21 23:39:32 +08:00
|
|
|
static constexpr float R_BRIGHTNESS_COEFF = 0.299f;
|
|
|
|
static constexpr float G_BRIGHTNESS_COEFF = 0.587f;
|
|
|
|
static constexpr float B_BRIGHTNESS_COEFF = 0.114f;
|
|
|
|
|
|
|
|
private:
|
|
|
|
// https://github.com/microsoft/glTF-SDK/blob/master/GLTFSDK/Source/PBRUtils.cpp#L9
|
|
|
|
// https://bghgary.github.io/glTF/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
|
2022-12-11 05:05:13 +08:00
|
|
|
static float solve_metallic(float p_dielectric_specular, float p_diffuse,
|
|
|
|
float p_specular,
|
2020-12-21 23:39:32 +08:00
|
|
|
float p_one_minus_specular_strength);
|
|
|
|
static float get_perceived_brightness(const Color p_color);
|
|
|
|
static float get_max_component(const Color &p_color);
|
|
|
|
|
|
|
|
public:
|
2024-03-16 03:22:39 +08:00
|
|
|
virtual Error append_from_file(String p_path, Ref<GLTFState> p_state, uint32_t p_flags = 0, String p_base_path = String());
|
|
|
|
virtual Error append_from_buffer(PackedByteArray p_bytes, String p_base_path, Ref<GLTFState> p_state, uint32_t p_flags = 0);
|
|
|
|
virtual Error append_from_scene(Node *p_node, Ref<GLTFState> p_state, uint32_t p_flags = 0);
|
2021-10-20 00:06:23 +08:00
|
|
|
|
2024-03-16 03:22:39 +08:00
|
|
|
virtual Node *generate_scene(Ref<GLTFState> p_state, float p_bake_fps = 30.0f, bool p_trimming = false, bool p_remove_immutable_tracks = true);
|
|
|
|
virtual PackedByteArray generate_buffer(Ref<GLTFState> p_state);
|
|
|
|
virtual Error write_to_filesystem(Ref<GLTFState> p_state, const String &p_path);
|
2021-10-20 00:06:23 +08:00
|
|
|
|
|
|
|
public:
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_gltf_state(Ref<GLTFState> p_state, const String &p_search_path);
|
2023-07-10 13:18:55 +08:00
|
|
|
Error _parse_asset_header(Ref<GLTFState> p_state);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse_gltf_extensions(Ref<GLTFState> p_state);
|
2024-02-04 16:47:44 +08:00
|
|
|
void _process_mesh_instances(Ref<GLTFState> p_state, Node *p_scene_root);
|
2023-07-22 10:59:39 +08:00
|
|
|
Node *_generate_scene_node_tree(Ref<GLTFState> p_state);
|
2023-07-22 11:06:50 +08:00
|
|
|
void _generate_scene_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
|
|
|
|
void _generate_skeleton_bone_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
|
2022-12-11 05:05:13 +08:00
|
|
|
void _import_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player,
|
2024-05-23 23:40:26 +08:00
|
|
|
const GLTFAnimationIndex p_index, const bool p_trimming, const bool p_remove_immutable_tracks);
|
2022-12-11 05:05:13 +08:00
|
|
|
void _convert_mesh_instances(Ref<GLTFState> p_state);
|
|
|
|
GLTFCameraIndex _convert_camera(Ref<GLTFState> p_state, Camera3D *p_camera);
|
|
|
|
void _convert_light_to_gltf(Light3D *p_light, Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node);
|
|
|
|
GLTFLightIndex _convert_light(Ref<GLTFState> p_state, Light3D *p_light);
|
|
|
|
void _convert_spatial(Ref<GLTFState> p_state, Node3D *p_spatial, Ref<GLTFNode> p_node);
|
|
|
|
void _convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
|
2020-12-21 23:39:32 +08:00
|
|
|
const GLTFNodeIndex p_gltf_current,
|
|
|
|
const GLTFNodeIndex p_gltf_root);
|
|
|
|
|
2021-01-23 05:52:32 +08:00
|
|
|
#ifdef MODULE_CSG_ENABLED
|
2022-12-11 05:05:13 +08:00
|
|
|
void _convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeIndex p_gltf_parent, Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
|
2021-01-23 05:52:32 +08:00
|
|
|
#endif // MODULE_CSG_ENABLED
|
2020-12-21 23:39:32 +08:00
|
|
|
|
2022-12-11 05:05:13 +08:00
|
|
|
void _check_visibility(Node *p_node, bool &r_retflag);
|
|
|
|
void _convert_camera_to_gltf(Camera3D *p_camera, Ref<GLTFState> p_state,
|
|
|
|
Ref<GLTFNode> p_gltf_node);
|
2021-01-23 05:52:32 +08:00
|
|
|
#ifdef MODULE_GRIDMAP_ENABLED
|
2020-12-21 23:39:32 +08:00
|
|
|
void _convert_grid_map_to_gltf(
|
2021-09-04 09:00:59 +08:00
|
|
|
GridMap *p_grid_map,
|
|
|
|
GLTFNodeIndex p_parent_node_index,
|
|
|
|
GLTFNodeIndex p_root_node_index,
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
|
2021-01-23 05:52:32 +08:00
|
|
|
#endif // MODULE_GRIDMAP_ENABLED
|
2021-09-21 09:24:31 +08:00
|
|
|
void _convert_multi_mesh_instance_to_gltf(
|
|
|
|
MultiMeshInstance3D *p_multi_mesh_instance,
|
2021-09-04 09:00:59 +08:00
|
|
|
GLTFNodeIndex p_parent_node_index,
|
|
|
|
GLTFNodeIndex p_root_node_index,
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
|
2020-12-21 23:39:32 +08:00
|
|
|
void _convert_skeleton_to_gltf(
|
2022-12-11 05:05:13 +08:00
|
|
|
Skeleton3D *p_scene_parent, Ref<GLTFState> p_state,
|
2021-09-04 09:00:59 +08:00
|
|
|
GLTFNodeIndex p_parent_node_index,
|
|
|
|
GLTFNodeIndex p_root_node_index,
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<GLTFNode> p_gltf_node);
|
2021-09-04 09:00:59 +08:00
|
|
|
void _convert_bone_attachment_to_gltf(BoneAttachment3D *p_bone_attachment,
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<GLTFState> p_state,
|
2021-09-04 09:00:59 +08:00
|
|
|
GLTFNodeIndex p_parent_node_index,
|
|
|
|
GLTFNodeIndex p_root_node_index,
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<GLTFNode> p_gltf_node);
|
2021-09-04 09:00:59 +08:00
|
|
|
void _convert_mesh_instance_to_gltf(MeshInstance3D *p_mesh_instance,
|
2022-12-11 05:05:13 +08:00
|
|
|
Ref<GLTFState> p_state,
|
|
|
|
Ref<GLTFNode> p_gltf_node);
|
|
|
|
GLTFMeshIndex _convert_mesh_to_gltf(Ref<GLTFState> p_state,
|
2021-09-04 09:00:59 +08:00
|
|
|
MeshInstance3D *p_mesh_instance);
|
2024-07-10 16:26:35 +08:00
|
|
|
|
|
|
|
GLTFNodeIndex _node_and_or_bone_to_gltf_node_index(Ref<GLTFState> p_state, const Vector<StringName> &p_node_subpath, const Node *p_godot_node);
|
|
|
|
bool _convert_animation_node_track(Ref<GLTFState> p_state,
|
|
|
|
GLTFAnimation::NodeTrack &p_gltf_node_track,
|
|
|
|
const Ref<Animation> &p_godot_animation,
|
|
|
|
int32_t p_godot_anim_track_index,
|
|
|
|
Vector<double> &p_times);
|
|
|
|
void _convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player, const String &p_animation_track_name);
|
|
|
|
|
2023-07-19 03:42:22 +08:00
|
|
|
Error _serialize(Ref<GLTFState> p_state);
|
2022-12-11 05:05:13 +08:00
|
|
|
Error _parse(Ref<GLTFState> p_state, String p_path, Ref<FileAccess> p_file);
|
2020-12-21 23:39:32 +08:00
|
|
|
};
|
|
|
|
|
2023-07-23 11:36:57 +08:00
|
|
|
VARIANT_ENUM_CAST(GLTFDocument::RootNodeMode);
|
|
|
|
|
2020-12-21 23:39:32 +08:00
|
|
|
#endif // GLTF_DOCUMENT_H
|