Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/gltf_document.h
21085 views
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
#pragma once
32
33
#include "extensions/gltf_document_extension.h"
34
#include "extensions/gltf_spec_gloss.h"
35
#include "gltf_defines.h"
36
#include "gltf_state.h"
37
38
#include "scene/3d/mesh_instance_3d.h"
39
#include "scene/3d/multimesh_instance_3d.h"
40
41
class CSGShape3D;
42
class GridMap;
43
44
class GLTFDocument : public Resource {
45
GDCLASS(GLTFDocument, Resource);
46
47
public:
48
const int32_t JOINT_GROUP_SIZE = 4;
49
50
enum {
51
TEXTURE_TYPE_GENERIC = 0,
52
TEXTURE_TYPE_NORMAL = 1,
53
};
54
enum RootNodeMode {
55
ROOT_NODE_MODE_SINGLE_ROOT,
56
ROOT_NODE_MODE_KEEP_ROOT,
57
ROOT_NODE_MODE_MULTI_ROOT,
58
};
59
enum VisibilityMode {
60
VISIBILITY_MODE_INCLUDE_REQUIRED,
61
VISIBILITY_MODE_INCLUDE_OPTIONAL,
62
VISIBILITY_MODE_EXCLUDE,
63
};
64
65
private:
66
int _naming_version = 2;
67
String _image_format = "PNG";
68
float _lossy_quality = 0.75f;
69
String _fallback_image_format = "None";
70
float _fallback_image_quality = 0.25f;
71
Ref<GLTFDocumentExtension> _image_save_extension;
72
RootNodeMode _root_node_mode = RootNodeMode::ROOT_NODE_MODE_SINGLE_ROOT;
73
VisibilityMode _visibility_mode = VisibilityMode::VISIBILITY_MODE_INCLUDE_REQUIRED;
74
75
protected:
76
static void _bind_methods();
77
String _gen_unique_name(Ref<GLTFState> p_state, const String &p_name);
78
static Vector<Ref<GLTFDocumentExtension>> all_document_extensions;
79
Vector<Ref<GLTFDocumentExtension>> document_extensions;
80
81
public:
82
static void register_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension, bool p_first_priority = false);
83
static void unregister_gltf_document_extension(Ref<GLTFDocumentExtension> p_extension);
84
static void unregister_all_gltf_document_extensions();
85
static Vector<Ref<GLTFDocumentExtension>> get_all_gltf_document_extensions();
86
static Vector<String> get_supported_gltf_extensions();
87
static HashSet<String> get_supported_gltf_extensions_hashset();
88
89
static NodePath _find_material_node_path(Ref<GLTFState> p_state, const Ref<Material> &p_material);
90
static Ref<GLTFObjectModelProperty> import_object_model_property(Ref<GLTFState> p_state, const String &p_json_pointer);
91
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);
92
93
void set_naming_version(int p_version);
94
int get_naming_version() const;
95
void set_image_format(const String &p_image_format);
96
String get_image_format() const;
97
void set_lossy_quality(float p_lossy_quality);
98
float get_lossy_quality() const;
99
void set_fallback_image_format(const String &p_fallback_image_format);
100
String get_fallback_image_format() const;
101
void set_fallback_image_quality(float p_fallback_image_quality);
102
float get_fallback_image_quality() const;
103
void set_root_node_mode(RootNodeMode p_root_node_mode);
104
RootNodeMode get_root_node_mode() const;
105
void set_visibility_mode(VisibilityMode p_visibility_mode);
106
VisibilityMode get_visibility_mode() const;
107
static String _gen_unique_name_static(HashSet<String> &r_unique_names, const String &p_name);
108
109
private:
110
void _build_parent_hierarchy(Ref<GLTFState> p_state);
111
Error _parse_scenes(Ref<GLTFState> p_state);
112
Error _parse_nodes(Ref<GLTFState> p_state);
113
String _sanitize_animation_name(const String &p_name);
114
String _gen_unique_animation_name(Ref<GLTFState> p_state, const String &p_name);
115
String _sanitize_bone_name(const String &p_name);
116
String _gen_unique_bone_name(Ref<GLTFState> p_state,
117
const GLTFSkeletonIndex p_skel_i,
118
const String &p_name);
119
GLTFTextureIndex _set_texture(Ref<GLTFState> p_state, Ref<Texture2D> p_texture,
120
StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
121
Ref<Texture2D> _get_texture(Ref<GLTFState> p_state,
122
const GLTFTextureIndex p_texture, int p_texture_type);
123
GLTFTextureSamplerIndex _set_sampler_for_mode(Ref<GLTFState> p_state,
124
StandardMaterial3D::TextureFilter p_filter_mode, bool p_repeats);
125
Ref<GLTFTextureSampler> _get_sampler_for_texture(Ref<GLTFState> p_state,
126
const GLTFTextureIndex p_texture);
127
Error _parse_json(const String &p_path, Ref<GLTFState> p_state);
128
Error _parse_glb(Ref<FileAccess> p_file, Ref<GLTFState> p_state);
129
void _compute_node_heights(Ref<GLTFState> p_state);
130
Error _parse_buffers(Ref<GLTFState> p_state, const String &p_base_path);
131
Error _parse_buffer_views(Ref<GLTFState> p_state);
132
Error _parse_accessors(Ref<GLTFState> p_state);
133
template <typename T>
134
static T _decode_unpack_indexed_data(const T &p_source, const PackedInt32Array &p_indices);
135
PackedFloat32Array _decode_accessor_as_float32s(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index, const PackedInt32Array &p_packed_vertex_ids = PackedInt32Array());
136
PackedFloat64Array _decode_accessor_as_float64s(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index, const PackedInt32Array &p_packed_vertex_ids = PackedInt32Array());
137
PackedInt32Array _decode_accessor_as_int32s(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index, const PackedInt32Array &p_packed_vertex_ids = PackedInt32Array());
138
PackedVector2Array _decode_accessor_as_vec2(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index, const PackedInt32Array &p_packed_vertex_ids = PackedInt32Array());
139
PackedVector3Array _decode_accessor_as_vec3(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index, const PackedInt32Array &p_packed_vertex_ids = PackedInt32Array());
140
PackedColorArray _decode_accessor_as_color(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index, const PackedInt32Array &p_packed_vertex_ids = PackedInt32Array());
141
Vector<Quaternion> _decode_accessor_as_quaternion(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index);
142
Array _decode_accessor_as_variants(const Ref<GLTFState> p_gltf_state, GLTFAccessorIndex p_accessor_index, Variant::Type p_variant_type);
143
Error _parse_meshes(Ref<GLTFState> p_state);
144
Error _serialize_textures(Ref<GLTFState> p_state);
145
Error _serialize_texture_samplers(Ref<GLTFState> p_state);
146
Error _serialize_images(Ref<GLTFState> p_state);
147
Dictionary _serialize_image(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_image_format, float p_lossy_quality, Ref<GLTFDocumentExtension> p_image_save_extension);
148
Error _serialize_lights(Ref<GLTFState> p_state);
149
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);
150
void _parse_image_save_image(Ref<GLTFState> p_state, const Vector<uint8_t> &p_bytes, const String &p_resource_uri, const String &p_file_extension, int p_index, Ref<Image> p_image);
151
Error _parse_images(Ref<GLTFState> p_state, const String &p_base_path);
152
Error _parse_textures(Ref<GLTFState> p_state);
153
Error _parse_texture_samplers(Ref<GLTFState> p_state);
154
Error _parse_materials(Ref<GLTFState> p_state);
155
void _set_texture_transform_uv1(const Dictionary &d, Ref<BaseMaterial3D> p_material);
156
void spec_gloss_to_rough_metal(Ref<GLTFSpecGloss> r_spec_gloss,
157
Ref<BaseMaterial3D> p_material);
158
static void spec_gloss_to_metal_base_color(const Color &p_specular_factor,
159
const Color &p_diffuse,
160
Color &r_base_color,
161
float &r_metallic);
162
Error _parse_skins(Ref<GLTFState> p_state);
163
Error _serialize_skins(Ref<GLTFState> p_state);
164
Error _create_skins(Ref<GLTFState> p_state);
165
bool _skins_are_same(const Ref<Skin> &p_skin_a, const Ref<Skin> &p_skin_b);
166
void _remove_duplicate_skins(Ref<GLTFState> p_state);
167
Error _serialize_cameras(Ref<GLTFState> p_state);
168
Error _parse_cameras(Ref<GLTFState> p_state);
169
Error _parse_lights(Ref<GLTFState> p_state);
170
Error _parse_animations(Ref<GLTFState> p_state);
171
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);
172
Error _serialize_animations(Ref<GLTFState> p_state);
173
bool _does_skinned_mesh_require_placeholder_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node);
174
BoneAttachment3D *_generate_bone_attachment(Skeleton3D *p_godot_skeleton, const Ref<GLTFNode> &p_bone_node);
175
BoneAttachment3D *_generate_bone_attachment_compat_4pt4(Ref<GLTFState> p_state, Skeleton3D *p_skeleton, const GLTFNodeIndex p_node_index, const GLTFNodeIndex p_bone_index);
176
ImporterMeshInstance3D *_generate_mesh_instance(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
177
Camera3D *_generate_camera(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
178
Light3D *_generate_light(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
179
Node3D *_generate_spatial(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index);
180
void _assign_node_names(Ref<GLTFState> p_state);
181
template <typename T>
182
T _interpolate_track(const Vector<double> &p_times, const Vector<T> &p_values,
183
const float p_time,
184
const GLTFAnimation::Interpolation p_interp);
185
186
Error _encode_accessors(Ref<GLTFState> p_state);
187
Error _encode_buffer_views(Ref<GLTFState> p_state);
188
Error _serialize_materials(Ref<GLTFState> p_state);
189
Error _serialize_meshes(Ref<GLTFState> p_state);
190
Error _serialize_nodes(Ref<GLTFState> p_state);
191
Error _serialize_scenes(Ref<GLTFState> p_state);
192
String interpolation_to_string(const GLTFAnimation::Interpolation p_interp);
193
Error _encode_buffer_bins(Ref<GLTFState> p_state, const String &p_path);
194
Error _encode_buffer_glb(Ref<GLTFState> p_state, const String &p_path);
195
PackedByteArray _serialize_glb_buffer(Ref<GLTFState> p_state, Error *r_err);
196
Dictionary _serialize_texture_transform_uv1(const Ref<BaseMaterial3D> &p_material);
197
Dictionary _serialize_texture_transform_uv2(const Ref<BaseMaterial3D> &p_material);
198
Error _serialize_asset_header(Ref<GLTFState> p_state);
199
Error _serialize_file(Ref<GLTFState> p_state, const String p_path);
200
Error _serialize_gltf_extensions(Ref<GLTFState> p_state) const;
201
202
public:
203
// https://www.itu.int/rec/R-REC-BT.601
204
// https://www.itu.int/dms_pubrec/itu-r/rec/bt/R-REC-BT.601-7-201103-I!!PDF-E.pdf
205
static constexpr float R_BRIGHTNESS_COEFF = 0.299f;
206
static constexpr float G_BRIGHTNESS_COEFF = 0.587f;
207
static constexpr float B_BRIGHTNESS_COEFF = 0.114f;
208
209
private:
210
// https://github.com/microsoft/glTF-SDK/blob/master/GLTFSDK/Source/PBRUtils.cpp#L9
211
// https://bghgary.github.io/glTF/convert-between-workflows-bjs/js/babylon.pbrUtilities.js
212
static float solve_metallic(float p_dielectric_specular, float p_diffuse,
213
float p_specular,
214
float p_one_minus_specular_strength);
215
static float get_perceived_brightness(const Color p_color);
216
static float get_max_component(const Color &p_color);
217
218
public:
219
virtual Error append_from_file(const String &p_path, Ref<GLTFState> p_state, uint32_t p_flags = 0, const String &p_base_path = String());
220
virtual Error append_from_buffer(const PackedByteArray &p_bytes, const String &p_base_path, Ref<GLTFState> p_state, uint32_t p_flags = 0);
221
virtual Error append_from_scene(Node *p_node, Ref<GLTFState> p_state, uint32_t p_flags = 0);
222
223
virtual Node *generate_scene(Ref<GLTFState> p_state, float p_bake_fps = 30.0f, bool p_trimming = false, bool p_remove_immutable_tracks = true);
224
virtual PackedByteArray generate_buffer(Ref<GLTFState> p_state);
225
virtual Error write_to_filesystem(Ref<GLTFState> p_state, const String &p_path);
226
227
public:
228
Error _parse_gltf_state(Ref<GLTFState> p_state, const String &p_search_path);
229
Error _parse_asset_header(Ref<GLTFState> p_state);
230
Error _parse_gltf_extensions(Ref<GLTFState> p_state);
231
void _process_mesh_instances(Ref<GLTFState> p_state, Node *p_scene_root);
232
Node *_generate_scene_node_tree(Ref<GLTFState> p_state);
233
void _generate_scene_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
234
void _generate_skeleton_bone_node(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node3D *p_current_node, Node *p_scene_parent, Node *p_scene_root);
235
void _attach_node_to_skeleton(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node3D *p_current_node, Skeleton3D *p_godot_skeleton, Node *p_scene_root, GLTFNodeIndex p_bone_node_index = -1);
236
void _generate_scene_node_compat_4pt4(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
237
void _generate_skeleton_bone_node_compat_4pt4(Ref<GLTFState> p_state, const GLTFNodeIndex p_node_index, Node *p_scene_parent, Node *p_scene_root);
238
void _import_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player,
239
const GLTFAnimationIndex p_index, const bool p_trimming, const bool p_remove_immutable_tracks);
240
void _convert_mesh_instances(Ref<GLTFState> p_state);
241
GLTFCameraIndex _convert_camera(Ref<GLTFState> p_state, Camera3D *p_camera);
242
void _convert_light_to_gltf(Light3D *p_light, Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node);
243
GLTFLightIndex _convert_light(Ref<GLTFState> p_state, Light3D *p_light);
244
void _convert_spatial(Ref<GLTFState> p_state, Node3D *p_spatial, Ref<GLTFNode> p_node);
245
void _convert_scene_node(Ref<GLTFState> p_state, Node *p_current,
246
const GLTFNodeIndex p_gltf_current,
247
const GLTFNodeIndex p_gltf_root);
248
249
void _convert_csg_shape_to_gltf(CSGShape3D *p_current, GLTFNodeIndex p_gltf_parent, Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
250
251
void _check_visibility(Node *p_node, bool &r_retflag);
252
void _convert_camera_to_gltf(Camera3D *p_camera, Ref<GLTFState> p_state,
253
Ref<GLTFNode> p_gltf_node);
254
void _convert_grid_map_to_gltf(
255
GridMap *p_grid_map,
256
GLTFNodeIndex p_parent_node_index,
257
GLTFNodeIndex p_root_node_index,
258
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
259
void _convert_multi_mesh_instance_to_gltf(
260
MultiMeshInstance3D *p_multi_mesh_instance,
261
GLTFNodeIndex p_parent_node_index,
262
GLTFNodeIndex p_root_node_index,
263
Ref<GLTFNode> p_gltf_node, Ref<GLTFState> p_state);
264
void _convert_skeleton_to_gltf(
265
Skeleton3D *p_scene_parent, Ref<GLTFState> p_state,
266
GLTFNodeIndex p_parent_node_index,
267
GLTFNodeIndex p_root_node_index,
268
Ref<GLTFNode> p_gltf_node);
269
void _convert_bone_attachment_to_gltf(BoneAttachment3D *p_bone_attachment,
270
Ref<GLTFState> p_state,
271
GLTFNodeIndex p_parent_node_index,
272
GLTFNodeIndex p_root_node_index,
273
Ref<GLTFNode> p_gltf_node);
274
void _convert_mesh_instance_to_gltf(MeshInstance3D *p_mesh_instance,
275
Ref<GLTFState> p_state,
276
Ref<GLTFNode> p_gltf_node);
277
GLTFMeshIndex _convert_mesh_to_gltf(Ref<GLTFState> p_state,
278
MeshInstance3D *p_mesh_instance);
279
280
GLTFNodeIndex _node_and_or_bone_to_gltf_node_index(Ref<GLTFState> p_state, const Vector<StringName> &p_node_subpath, const Node *p_godot_node);
281
bool _convert_animation_node_track(Ref<GLTFState> p_state,
282
GLTFAnimation::NodeTrack &p_gltf_node_track,
283
const Ref<Animation> &p_godot_animation,
284
int32_t p_godot_anim_track_index,
285
Vector<double> &p_times);
286
void _convert_animation(Ref<GLTFState> p_state, AnimationPlayer *p_animation_player, const String &p_animation_track_name);
287
288
Error _serialize(Ref<GLTFState> p_state);
289
Error _parse(Ref<GLTFState> p_state, const String &p_path, Ref<FileAccess> p_file);
290
};
291
292
VARIANT_ENUM_CAST(GLTFDocument::RootNodeMode);
293
VARIANT_ENUM_CAST(GLTFDocument::VisibilityMode);
294
295