1/**************************************************************************/
2/* gltf_document.h */
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#ifndef GLTF_DOCUMENT_H
32#define GLTF_DOCUMENT_H
33
34#include "extensions/gltf_document_extension.h"
35
36#include "modules/modules_enabled.gen.h" // For csg, gridmap.
37
38class GLTFDocument : public Resource {
39 GDCLASS(GLTFDocument, Resource);
40 static Vector<Ref<GLTFDocumentExtension>> all_document_extensions;
41 Vector<Ref<GLTFDocumentExtension>> document_extensions;
42
43private:
44 const float BAKE_FPS = 30.0f;
45 String _image_format = "PNG";
46 float _lossy_quality = 0.75f;
47 Ref<GLTFDocumentExtension> _image_save_extension;
48
49public:
50 const int32_t JOINT_GROUP_SIZE = 4;
51
52 enum {
53 ARRAY_BUFFER = 34962,
54 ELEMENT_ARRAY_BUFFER = 34963,
55
56 TYPE_BYTE = 5120,
57 TYPE_UNSIGNED_BYTE = 5121,
58 TYPE_SHORT = 5122,
59 TYPE_UNSIGNED_SHORT = 5123,
60 TYPE_UNSIGNED_INT = 5125,
61 TYPE_FLOAT = 5126,
62
63 COMPONENT_TYPE_BYTE = 5120,
64 COMPONENT_TYPE_UNSIGNED_BYTE = 5121,
65 COMPONENT_TYPE_SHORT = 5122,
66 COMPONENT_TYPE_UNSIGNED_SHORT = 5123,
67 COMPONENT_TYPE_INT = 5125,
68 COMPONENT_TYPE_FLOAT = 5126,
69 };
70 enum {
71 TEXTURE_TYPE_GENERIC = 0,
72 TEXTURE_TYPE_NORMAL = 1,
73 };
74
75protected:
76 static void _bind_methods();
77
78public:
79 static void register_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension, bool p_first_priority = false);
80 static void unregister_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension);
81 static void unregister_all_gltf_document_extensions();
82
83 void set_image_format(const String &p_image_format);
84 String get_image_format() const;
85 void set_lossy_quality(float p_lossy_quality);
86 float get_lossy_quality() const;
87
88private:
89 void _build_parent_hierachy(Ref<GLTFState> p_state);
90 double _filter_number(double p_float);
91 String _get_component_type_name(const uint32_t p_component);
92 int _get_component_type_size(const int p_component_type);
93 Error _parse_scenes(Ref<GLTFState> p_state);
94 Error _parse_nodes(Ref<GLTFState> p_state);
95 String _get_type_name(const GLTFType p_component);
96 String _get_accessor_type_name(const GLTFType p_type);
97 String _gen_unique_name(Ref<GLTFState> p_state, const String &p_name);
98 String _sanitize_animation_name(const String &p_name);
99 String _gen_unique_animation_name(Ref<GLTFState> p_state, const String &p_name);
100 String _sanitize_bone_name(const String &p_name);
101 String _gen_unique_bone_name(Ref<GLTFState> p_state,
102 const GLTFSkeletonIndex p_skel_i,
103 const String &p_name);
104 GLTFTextureIndex _set_texture(Ref<GLTFState> p_state, Ref<Texture2D> p_texture,
105 StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
106 Ref<Texture2D> _get_texture(Ref<GLTFState> p_state,
107 const GLTFTextureIndex p_texture, int p_texture_type);
108 GLTFTextureSamplerIndex _set_sampler_for_mode(Ref<GLTFState> p_state,
109 StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
110 Ref<GLTFTextureSampler> _get_sampler_for_texture(Ref<GLTFState> p_state,
111 const GLTFTextureIndex p_texture);
112 Error _parse_json(const String &p_path, Ref<GLTFState> p_state);
113 Error _parse_glb(Ref<FileAccess> p_file, Ref<GLTFState> p_state);
114 void _compute_node_heights(Ref<GLTFState> p_state);
115 Error _parse_buffers(Ref<GLTFState> p_state, const String &p_base_path);
116 Error _parse_buffer_views(Ref<GLTFState> p_state);
117 GLTFType _get_type_from_str(const String &p_string);
118 Error _parse_accessors(Ref<GLTFState> p_state);
119 Error _decode_buffer_view(Ref<GLTFState> p_state, double *p_dst,
120 const GLTFBufferViewIndex p_buffer_view,
121 const int p_skip_every, const int p_skip_bytes,
122 const int p_element_size, const int p_count,
123 const GLTFType p_type, const int p_component_count,
124 const int p_component_type, const int p_component_size,
125 const bool p_normalized, const int p_byte_offset,
126 const bool p_for_vertex);
127 Vector<double> _decode_accessor(Ref<GLTFState> p_state,
128 const GLTFAccessorIndex p_accessor,
129 const bool p_for_vertex);
130 Vector<float> _decode_accessor_as_floats(Ref<GLTFState> p_state,
131 const GLTFAccessorIndex p_accessor,
132 const bool p_for_vertex);
133 Vector<int> _decode_accessor_as_ints(Ref<GLTFState> p_state,
134 const GLTFAccessorIndex p_accessor,
135 const bool p_for_vertex);
136 Vector<Vector2> _decode_accessor_as_vec2(Ref<GLTFState> p_state,
137 const GLTFAccessorIndex p_accessor,
138 const bool p_for_vertex);
139 Vector<Vector3> _decode_accessor_as_vec3(Ref<GLTFState> p_state,
140 const GLTFAccessorIndex p_accessor,
141 const bool p_for_vertex);
142 Vector<Color> _decode_accessor_as_color(Ref<GLTFState> p_state,
143 const GLTFAccessorIndex p_accessor,
144 const bool p_for_vertex);
145 Vector<Quaternion> _decode_accessor_as_quaternion(Ref<GLTFState> p_state,
146 const GLTFAccessorIndex p_accessor,
147 const bool p_for_vertex);
148 Vector<Transform2D> _decode_accessor_as_xform2d(Ref<GLTFState> p_state,
149 const GLTFAccessorIndex p_accessor,
150 const bool p_for_vertex);
151 Vector<Basis> _decode_accessor_as_basis(Ref<GLTFState> p_state,
152 const GLTFAccessorIndex p_accessor,
153 const bool p_for_vertex);
154 Vector<Transform3D> _decode_accessor_as_xform(Ref<GLTFState> p_state,
155 const GLTFAccessorIndex p_accessor,
156 const bool p_for_vertex);
157 Error _parse_meshes(Ref<GLTFState> p_state);
158 Error _serialize_textures(Ref<GLTFState> p_state);
159 Error _serialize_texture_samplers(Ref<GLTFState> p_state);
160 Error _serialize_images(Ref<GLTFState> p_state);
161 Error _serialize_lights(Ref<GLTFState> p_state);
162 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);
163 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);
164 Error _parse_images(Ref<GLTFState> p_state, const String &p_base_path);
165 Error _parse_textures(Ref<GLTFState> p_state);
166 Error _parse_texture_samplers(Ref<GLTFState> p_state);
167 Error _parse_materials(Ref<GLTFState> p_state);
168 void _set_texture_transform_uv1(const Dictionary &d, Ref<BaseMaterial3D> p_material);
169 void spec_gloss_to_rough_metal(Ref<GLTFSpecGloss> r_spec_gloss,
170 Ref<BaseMaterial3D> p_material);
171 static void spec_gloss_to_metal_base_color(const Color &p_specular_factor,
172 const Color &p_diffuse,
173 Color &r_base_color,
174 float &r_metallic);
175 GLTFNodeIndex _find_highest_node(Ref<GLTFState> p_state,
176 const Vector<GLTFNodeIndex> &p_subset);
177 void _recurse_children(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index,
178 RBSet<GLTFNodeIndex> &p_all_skin_nodes, HashSet<GLTFNodeIndex> &p_child_visited_set);
179 bool _capture_nodes_in_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin,
180 const GLTFNodeIndex p_node_index);
181 void _capture_nodes_for_multirooted_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin);
182 Error _expand_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin);
183 Error _verify_skin(Ref<GLTFState> p_state, Ref<GLTFSkin> p_skin);
184 Error _parse_skins(Ref<GLTFState> p_state);
185 Error _determine_skeletons(Ref<GLTFState> p_state);
186 Error _reparent_non_joint_skeleton_subtrees(
187 Ref<GLTFState> p_state, Ref<GLTFSkeleton> p_skeleton,
188 const Vector<GLTFNodeIndex> &p_non_joints);
189 Error _determine_skeleton_roots(Ref<GLTFState> p_state,
190 const GLTFSkeletonIndex p_skel_i);
191 Error _create_skeletons(Ref<GLTFState> p_state);
192 Error _map_skin_joints_indices_to_skeleton_bone_indices(Ref<GLTFState> p_state);
193 Error _serialize_skins(Ref<GLTFState> p_state);
194 Error _create_skins(Ref<GLTFState> p_state);
195 bool _skins_are_same(const Ref<Skin> p_skin_a, const Ref<Skin> p_skin_b);
196 void _remove_duplicate_skins(Ref<GLTFState> p_state);
197 Error _serialize_cameras(Ref<GLTFState> p_state);
198 Error _parse_cameras(Ref<GLTFState> p_state);
199 Error _parse_lights(Ref<GLTFState> p_state);
200 Error _parse_animations(Ref<GLTFState> p_state);
201 Error _serialize_animations(Ref<GLTFState> p_state);
202 BoneAttachment3D *_generate_bone_attachment(Ref<GLTFState> p_state,
203 Skeleton3D *p_skeleton,
204 const GLTFNodeIndex p_node_index,
205 const GLTFNodeIndex p_bone_index);
206 ImporterMeshInstance3D *_generate_mesh_instance(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
207 Camera3D *_generate_camera(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
208 Light3D *_generate_light(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
209 Node3D *_generate_spatial(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
210 void _assign_node_names(Ref<GLTFState> p_state);
211 template <class T>
212 T _interpolate_track(const Vector<real_t> &p_times, const Vector<T> &p_values,
213 const float p_time,
214 const GLTFAnimation::Interpolation p_interp);
215 GLTFAccessorIndex _encode_accessor_as_quaternions(Ref<GLTFState> p_state,
216 const Vector<Quaternion> p_attribs,
217 const bool p_for_vertex);
218 GLTFAccessorIndex _encode_accessor_as_weights(Ref<GLTFState> p_state,
219 const Vector<Color> p_attribs,
220 const bool p_for_vertex);
221 GLTFAccessorIndex _encode_accessor_as_joints(Ref<GLTFState> p_state,
222 const Vector<Color> p_attribs,
223 const bool p_for_vertex);
224 GLTFAccessorIndex _encode_accessor_as_floats(Ref<GLTFState> p_state,
225 const Vector<real_t> p_attribs,
226 const bool p_for_vertex);
227 GLTFAccessorIndex _encode_accessor_as_vec2(Ref<GLTFState> p_state,
228 const Vector<Vector2> p_attribs,
229 const bool p_for_vertex);
230
231 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) {
232 if (p_i == 0) {
233 for (int32_t type_i = 0; type_i < p_element_count; type_i++) {
234 p_type_max.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
235 p_type_min.write[type_i] = p_attribs[(p_i * p_element_count) + type_i];
236 }
237 }
238 for (int32_t type_i = 0; type_i < p_element_count; type_i++) {
239 p_type_max.write[type_i] = MAX(p_attribs[(p_i * p_element_count) + type_i], p_type_max[type_i]);
240 p_type_min.write[type_i] = MIN(p_attribs[(p_i * p_element_count) + type_i], p_type_min[type_i]);
241 p_type_max.write[type_i] = _filter_number(p_type_max.write[type_i]);
242 p_type_min.write[type_i] = _filter_number(p_type_min.write[type_i]);
243 }
244 }
245
246 GLTFAccessorIndex _encode_accessor_as_vec3(Ref<GLTFState> p_state,
247 const Vector<Vector3> p_attribs,
248 const bool p_for_vertex);
249 GLTFAccessorIndex _encode_accessor_as_color(Ref<GLTFState> p_state,
250 const Vector<Color> p_attribs,
251 const bool p_for_vertex);
252
253 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);
254
255 GLTFAccessorIndex _encode_accessor_as_ints(Ref<GLTFState> p_state,
256 const Vector<int32_t> p_attribs,
257 const bool p_for_vertex);
258 GLTFAccessorIndex _encode_accessor_as_xform(Ref<GLTFState> p_state,
259 const Vector<Transform3D> p_attribs,
260 const bool p_for_vertex);
261 Error _encode_buffer_view(Ref<GLTFState> p_state, const double *p_src,
262 const int p_count, const GLTFType p_type,
263 const int p_component_type, const bool p_normalized,
264 const int p_byte_offset, const bool p_for_vertex,
265 GLTFBufferViewIndex &r_accessor);
266 Error _encode_accessors(Ref<GLTFState> p_state);
267 Error _encode_buffer_views(Ref<GLTFState> p_state);
268 Error _serialize_materials(Ref<GLTFState> p_state);
269 Error _serialize_meshes(Ref<GLTFState> p_state);
270 Error _serialize_nodes(Ref<GLTFState> p_state);
271 Error _serialize_scenes(Ref<GLTFState> p_state);
272 String interpolation_to_string(const GLTFAnimation::Interpolation p_interp);
273 GLTFAnimation::Track _convert_animation_track(Ref<GLTFState> p_state,
274 GLTFAnimation::Track p_track,
275 Ref<Animation> p_animation,
276 int32_t p_track_i,
277 GLTFNodeIndex p_node_i);
278 Error _encode_buffer_bins(Ref<GLTFState> p_state, const String &p_path);
279 Error _encode_buffer_glb(Ref<GLTFState> p_state, const String &p_path);
280 PackedByteArray _serialize_glb_buffer(Ref<GLTFState> p_state, Error *r_err);
281 Dictionary _serialize_texture_transform_uv1(Ref<BaseMaterial3D> p_material);
282 Dictionary _serialize_texture_transform_uv2(Ref<BaseMaterial3D> p_material);
283 Error _serialize_asset_header(Ref<GLTFState> p_state);
284 Error _serialize_file(Ref<GLTFState> p_state, const String p_path);
285 Error _serialize_gltf_extensions(Ref<GLTFState> p_state) const;
286
287public:
288 // https://www.itu.int/rec/R-REC-BT.601
289 // https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf
290 static constexpr float R_BRIGHTNESS_COEFF = 0.299f;
291 static constexpr float G_BRIGHTNESS_COEFF = 0.587f;
292 static constexpr float B_BRIGHTNESS_COEFF = 0.114f;
293
294private:
295 // https://github.com/microsoft/glTF-SDK/blob/master/GLTFSDK/Source/PBRUtils.cpp#L9
296 // https://bghgary.github.io/glTF/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
297 static float solve_metallic(float p_dielectric_specular, float p_diffuse,
298 float p_specular,
299 float p_one_minus_specular_strength);
300 static float get_perceived_brightness(const Color p_color);
301 static float get_max_component(const Color &p_color);
302
303public:
304 Error append_from_file(String p_path, Ref<GLTFState> p_state, uint32_t p_flags = 0, String p_base_path = String());
305 Error append_from_buffer(PackedByteArray p_bytes, String p_base_path, Ref<GLTFState> p_state, uint32_t p_flags = 0);
306 Error append_from_scene(Node *p_node, Ref<GLTFState> p_state, uint32_t p_flags = 0);
307
308public:
309 Node *generate_scene(Ref<GLTFState> p_state, float p_bake_fps = 30.0f, bool p_trimming = false, bool p_remove_immutable_tracks = true);
310 PackedByteArray generate_buffer(Ref<GLTFState> p_state);
311 Error write_to_filesystem(Ref<GLTFState> p_state, const String &p_path);
312
313public:
314 Error _parse_gltf_state(Ref<GLTFState> p_state, const String &p_search_path);
315 Error _parse_asset_header(Ref<GLTFState> p_state);
316 Error _parse_gltf_extensions(Ref<GLTFState> p_state);
317 void _process_mesh_instances(Ref<GLTFState> p_state);
318 Node *_generate_scene_node_tree(Ref<GLTFState> p_state);
319 void _generate_scene_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
320 void _generate_skeleton_bone_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
321 void _import_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player,
322 const GLTFAnimationIndex p_index, const float p_bake_fps, const bool p_trimming, const bool p_remove_immutable_tracks);
323 void _convert_mesh_instances(Ref<GLTFState> p_state);
324 GLTFCameraIndex _convert_camera(Ref<GLTFState> p_state, Camera3D *p_camera);
325 void _convert_light_to_gltf(Light3D *p_light, Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node);
326 GLTFLightIndex _convert_light(Ref<GLTFState> p_state, Light3D *p_light);
327 void _convert_spatial(Ref<GLTFState> p_state, Node3D *p_spatial, Ref<GLTFNode> p_node);
328 void _convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
329 const GLTFNodeIndex p_gltf_current,
330 const GLTFNodeIndex p_gltf_root);
331
332#ifdef MODULE_CSG_ENABLED
333 void _convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeIndex p_gltf_parent, Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
334#endif // MODULE_CSG_ENABLED
335
336 void _create_gltf_node(Ref<GLTFState> p_state,
337 Node *p_scene_parent,
338 GLTFNodeIndex p_current_node_i,
339 GLTFNodeIndex p_parent_node_index,
340 GLTFNodeIndex p_root_gltf_node,
341 Ref<GLTFNode> p_gltf_node);
342 void _convert_animation_player_to_gltf(
343 AnimationPlayer *p_animation_player, Ref<GLTFState> p_state,
344 GLTFNodeIndex p_gltf_current,
345 GLTFNodeIndex p_gltf_root_index,
346 Ref<GLTFNode> p_gltf_node, Node *p_scene_parent);
347 void _check_visibility(Node *p_node, bool &r_retflag);
348 void _convert_camera_to_gltf(Camera3D *p_camera, Ref<GLTFState> p_state,
349 Ref<GLTFNode> p_gltf_node);
350#ifdef MODULE_GRIDMAP_ENABLED
351 void _convert_grid_map_to_gltf(
352 GridMap *p_grid_map,
353 GLTFNodeIndex p_parent_node_index,
354 GLTFNodeIndex p_root_node_index,
355 Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
356#endif // MODULE_GRIDMAP_ENABLED
357 void _convert_multi_mesh_instance_to_gltf(
358 MultiMeshInstance3D *p_multi_mesh_instance,
359 GLTFNodeIndex p_parent_node_index,
360 GLTFNodeIndex p_root_node_index,
361 Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
362 void _convert_skeleton_to_gltf(
363 Skeleton3D *p_scene_parent, Ref<GLTFState> p_state,
364 GLTFNodeIndex p_parent_node_index,
365 GLTFNodeIndex p_root_node_index,
366 Ref<GLTFNode> p_gltf_node);
367 void _convert_bone_attachment_to_gltf(BoneAttachment3D *p_bone_attachment,
368 Ref<GLTFState> p_state,
369 GLTFNodeIndex p_parent_node_index,
370 GLTFNodeIndex p_root_node_index,
371 Ref<GLTFNode> p_gltf_node);
372 void _convert_mesh_instance_to_gltf(MeshInstance3D *p_mesh_instance,
373 Ref<GLTFState> p_state,
374 Ref<GLTFNode> p_gltf_node);
375 GLTFMeshIndex _convert_mesh_to_gltf(Ref<GLTFState> p_state,
376 MeshInstance3D *p_mesh_instance);
377 void _convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player, String p_animation_track_name);
378 Error _serialize(Ref<GLTFState> p_state);
379 Error _parse(Ref<GLTFState> p_state, String p_path, Ref<FileAccess> p_file);
380};
381
382#endif // GLTF_DOCUMENT_H
383