Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/gltf/extensions/gltf_document_extension.cpp
10278 views
1
/**************************************************************************/
2
/* gltf_document_extension.cpp */
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
#include "gltf_document_extension.h"
32
33
void GLTFDocumentExtension::_bind_methods() {
34
// Import process.
35
GDVIRTUAL_BIND(_import_preflight, "state", "extensions");
36
GDVIRTUAL_BIND(_get_supported_extensions);
37
GDVIRTUAL_BIND(_parse_node_extensions, "state", "gltf_node", "extensions");
38
GDVIRTUAL_BIND(_parse_image_data, "state", "image_data", "mime_type", "ret_image");
39
GDVIRTUAL_BIND(_get_image_file_extension);
40
GDVIRTUAL_BIND(_parse_texture_json, "state", "texture_json", "ret_gltf_texture");
41
GDVIRTUAL_BIND(_import_object_model_property, "state", "split_json_pointer", "partial_paths");
42
GDVIRTUAL_BIND(_import_post_parse, "state");
43
GDVIRTUAL_BIND(_import_pre_generate, "state");
44
GDVIRTUAL_BIND(_generate_scene_node, "state", "gltf_node", "scene_parent");
45
GDVIRTUAL_BIND(_import_node, "state", "gltf_node", "json", "node");
46
GDVIRTUAL_BIND(_import_post, "state", "root");
47
// Export process.
48
GDVIRTUAL_BIND(_export_preflight, "state", "root");
49
GDVIRTUAL_BIND(_convert_scene_node, "state", "gltf_node", "scene_node");
50
GDVIRTUAL_BIND(_export_post_convert, "state", "root");
51
GDVIRTUAL_BIND(_export_preserialize, "state");
52
GDVIRTUAL_BIND(_export_object_model_property, "state", "node_path", "godot_node", "gltf_node_index", "target_object", "target_depth");
53
GDVIRTUAL_BIND(_get_saveable_image_formats);
54
GDVIRTUAL_BIND(_serialize_image_to_bytes, "state", "image", "image_dict", "image_format", "lossy_quality");
55
GDVIRTUAL_BIND(_save_image_at_path, "state", "image", "file_path", "image_format", "lossy_quality");
56
GDVIRTUAL_BIND(_serialize_texture_json, "state", "texture_json", "gltf_texture", "image_format");
57
GDVIRTUAL_BIND(_export_node, "state", "gltf_node", "json", "node");
58
GDVIRTUAL_BIND(_export_post, "state");
59
}
60
61
// Import process.
62
Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions) {
63
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
64
Error err = OK;
65
GDVIRTUAL_CALL(_import_preflight, p_state, p_extensions, err);
66
return err;
67
}
68
69
Vector<String> GLTFDocumentExtension::get_supported_extensions() {
70
Vector<String> ret;
71
GDVIRTUAL_CALL(_get_supported_extensions, ret);
72
return ret;
73
}
74
75
Error GLTFDocumentExtension::parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions) {
76
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
77
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
78
Error err = OK;
79
GDVIRTUAL_CALL(_parse_node_extensions, p_state, p_gltf_node, p_extensions, err);
80
return err;
81
}
82
83
Error GLTFDocumentExtension::parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image) {
84
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
85
ERR_FAIL_COND_V(r_image.is_null(), ERR_INVALID_PARAMETER);
86
Error err = OK;
87
GDVIRTUAL_CALL(_parse_image_data, p_state, p_image_data, p_mime_type, r_image, err);
88
return err;
89
}
90
91
String GLTFDocumentExtension::get_image_file_extension() {
92
String ret;
93
GDVIRTUAL_CALL(_get_image_file_extension, ret);
94
return ret;
95
}
96
97
Error GLTFDocumentExtension::parse_texture_json(Ref<GLTFState> p_state, const Dictionary &p_texture_json, Ref<GLTFTexture> r_gltf_texture) {
98
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
99
ERR_FAIL_COND_V(r_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
100
Error err = OK;
101
GDVIRTUAL_CALL(_parse_texture_json, p_state, p_texture_json, r_gltf_texture, err);
102
return err;
103
}
104
105
Ref<GLTFObjectModelProperty> GLTFDocumentExtension::import_object_model_property(Ref<GLTFState> p_state, const PackedStringArray &p_split_json_pointer, const TypedArray<NodePath> &p_partial_paths) {
106
Ref<GLTFObjectModelProperty> ret;
107
ERR_FAIL_COND_V(p_state.is_null(), ret);
108
GDVIRTUAL_CALL(_import_object_model_property, p_state, p_split_json_pointer, p_partial_paths, ret);
109
return ret;
110
}
111
112
Error GLTFDocumentExtension::import_post_parse(Ref<GLTFState> p_state) {
113
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
114
Error err = OK;
115
GDVIRTUAL_CALL(_import_post_parse, p_state, err);
116
return err;
117
}
118
119
Error GLTFDocumentExtension::import_pre_generate(Ref<GLTFState> p_state) {
120
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
121
Error err = OK;
122
GDVIRTUAL_CALL(_import_pre_generate, p_state, err);
123
return err;
124
}
125
126
Node3D *GLTFDocumentExtension::generate_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_parent) {
127
ERR_FAIL_COND_V(p_state.is_null(), nullptr);
128
ERR_FAIL_COND_V(p_gltf_node.is_null(), nullptr);
129
Node3D *ret_node = nullptr;
130
GDVIRTUAL_CALL(_generate_scene_node, p_state, p_gltf_node, p_scene_parent, ret_node);
131
return ret_node;
132
}
133
134
Error GLTFDocumentExtension::import_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
135
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
136
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
137
ERR_FAIL_NULL_V(p_node, ERR_INVALID_PARAMETER);
138
Error err = OK;
139
GDVIRTUAL_CALL(_import_node, p_state, p_gltf_node, r_dict, p_node, err);
140
return err;
141
}
142
143
Error GLTFDocumentExtension::import_post(Ref<GLTFState> p_state, Node *p_root) {
144
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
145
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
146
Error err = OK;
147
GDVIRTUAL_CALL(_import_post, p_state, p_root, err);
148
return err;
149
}
150
151
// Export process.
152
Error GLTFDocumentExtension::export_preflight(Ref<GLTFState> p_state, Node *p_root) {
153
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
154
Error err = OK;
155
GDVIRTUAL_CALL(_export_preflight, p_state, p_root, err);
156
return err;
157
}
158
159
void GLTFDocumentExtension::convert_scene_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Node *p_scene_node) {
160
ERR_FAIL_COND(p_state.is_null());
161
ERR_FAIL_COND(p_gltf_node.is_null());
162
ERR_FAIL_NULL(p_scene_node);
163
GDVIRTUAL_CALL(_convert_scene_node, p_state, p_gltf_node, p_scene_node);
164
}
165
166
Error GLTFDocumentExtension::export_post_convert(Ref<GLTFState> p_state, Node *p_root) {
167
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
168
ERR_FAIL_NULL_V(p_root, ERR_INVALID_PARAMETER);
169
Error err = OK;
170
GDVIRTUAL_CALL(_export_post_convert, p_state, p_root, err);
171
return err;
172
}
173
174
Error GLTFDocumentExtension::export_preserialize(Ref<GLTFState> p_state) {
175
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
176
Error err = OK;
177
GDVIRTUAL_CALL(_export_preserialize, p_state, err);
178
return err;
179
}
180
181
Ref<GLTFObjectModelProperty> GLTFDocumentExtension::export_object_model_property(Ref<GLTFState> p_state, const NodePath &p_node_path, const Node *p_godot_node, GLTFNodeIndex p_gltf_node_index, const Object *p_target_object, int p_target_depth) {
182
Ref<GLTFObjectModelProperty> ret;
183
ERR_FAIL_COND_V(p_state.is_null(), ret);
184
ERR_FAIL_NULL_V(p_godot_node, ret);
185
ERR_FAIL_NULL_V(p_target_object, ret);
186
GDVIRTUAL_CALL(_export_object_model_property, p_state, p_node_path, p_godot_node, p_gltf_node_index, p_target_object, p_target_depth, ret);
187
return ret;
188
}
189
190
Vector<String> GLTFDocumentExtension::get_saveable_image_formats() {
191
Vector<String> ret;
192
GDVIRTUAL_CALL(_get_saveable_image_formats, ret);
193
return ret;
194
}
195
196
PackedByteArray GLTFDocumentExtension::serialize_image_to_bytes(Ref<GLTFState> p_state, Ref<Image> p_image, Dictionary p_image_dict, const String &p_image_format, float p_lossy_quality) {
197
PackedByteArray ret;
198
ERR_FAIL_COND_V(p_state.is_null(), ret);
199
ERR_FAIL_COND_V(p_image.is_null(), ret);
200
GDVIRTUAL_CALL(_serialize_image_to_bytes, p_state, p_image, p_image_dict, p_image_format, p_lossy_quality, ret);
201
return ret;
202
}
203
204
Error GLTFDocumentExtension::save_image_at_path(Ref<GLTFState> p_state, Ref<Image> p_image, const String &p_file_path, const String &p_image_format, float p_lossy_quality) {
205
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
206
ERR_FAIL_COND_V(p_image.is_null(), ERR_INVALID_PARAMETER);
207
Error ret = OK;
208
GDVIRTUAL_CALL(_save_image_at_path, p_state, p_image, p_file_path, p_image_format, p_lossy_quality, ret);
209
return ret;
210
}
211
212
Error GLTFDocumentExtension::serialize_texture_json(Ref<GLTFState> p_state, Dictionary p_texture_json, Ref<GLTFTexture> p_gltf_texture, const String &p_image_format) {
213
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
214
ERR_FAIL_COND_V(p_gltf_texture.is_null(), ERR_INVALID_PARAMETER);
215
Error err = OK;
216
GDVIRTUAL_CALL(_serialize_texture_json, p_state, p_texture_json, p_gltf_texture, p_image_format, err);
217
return err;
218
}
219
220
Error GLTFDocumentExtension::export_node(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &r_dict, Node *p_node) {
221
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
222
ERR_FAIL_COND_V(p_gltf_node.is_null(), ERR_INVALID_PARAMETER);
223
Error err = OK;
224
GDVIRTUAL_CALL(_export_node, p_state, p_gltf_node, r_dict, p_node, err);
225
return err;
226
}
227
228
Error GLTFDocumentExtension::export_post(Ref<GLTFState> p_state) {
229
ERR_FAIL_COND_V(p_state.is_null(), ERR_INVALID_PARAMETER);
230
Error err = OK;
231
GDVIRTUAL_CALL(_export_post, p_state, err);
232
return err;
233
}
234
235