diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2021-10-26 08:32:11 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-26 08:32:11 +0200 |
commit | ba23ef9b292dc42051ee8d11fa1fe6818b018c8f (patch) | |
tree | cf013b7a95ecc3e05ae749da6f31e5868d3e80be | |
parent | 0409644afd2da270073c331e6983a1dbd391e42b (diff) | |
parent | cada1a47475d28b796d0c64ab58f2408488b0fc0 (diff) |
Merge pull request #54249 from lyuma/gltf_padding_fix
-rw-r--r-- | modules/gltf/gltf_document.cpp | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/modules/gltf/gltf_document.cpp b/modules/gltf/gltf_document.cpp index 0df08a7996..3a8cab7e7e 100644 --- a/modules/gltf/gltf_document.cpp +++ b/modules/gltf/gltf_document.cpp @@ -120,26 +120,26 @@ Error GLTFDocument::serialize(Ref<GLTFState> state, Node *p_root, const String & return Error::FAILED; } - /* STEP 7 SERIALIZE IMAGES */ - err = _serialize_images(state, p_path); + /* STEP 7 SERIALIZE ANIMATIONS */ + err = _serialize_animations(state); if (err != OK) { return Error::FAILED; } - /* STEP 8 SERIALIZE TEXTURES */ - err = _serialize_textures(state); + /* STEP 8 SERIALIZE ACCESSORS */ + err = _encode_accessors(state); if (err != OK) { return Error::FAILED; } - // /* STEP 9 SERIALIZE ANIMATIONS */ - err = _serialize_animations(state); + /* STEP 9 SERIALIZE IMAGES */ + err = _serialize_images(state, p_path); if (err != OK) { return Error::FAILED; } - /* STEP 10 SERIALIZE ACCESSORS */ - err = _encode_accessors(state); + /* STEP 10 SERIALIZE TEXTURES */ + err = _serialize_textures(state); if (err != OK) { return Error::FAILED; } @@ -4369,6 +4369,10 @@ Error GLTFDocument::_serialize_skins(Ref<GLTFState> state) { json_skin["name"] = gltf_skin->get_name(); json_skins.push_back(json_skin); } + if (!state->skins.size()) { + return OK; + } + state->json["skins"] = json_skins; return OK; } @@ -6756,33 +6760,36 @@ Error GLTFDocument::_serialize_file(Ref<GLTFState> state, const String p_path) { const uint32_t magic = 0x46546C67; // GLTF const int32_t header_size = 12; const int32_t chunk_header_size = 8; - - for (int32_t pad_i = 0; pad_i < (chunk_header_size + json.utf8().length()) % 4; pad_i++) { - json += " "; - } CharString cs = json.utf8(); - const uint32_t text_chunk_length = cs.length(); - + const uint32_t text_data_length = cs.length(); + const uint32_t text_chunk_length = ((text_data_length + 3) & (~3)); const uint32_t text_chunk_type = 0x4E4F534A; //JSON - int32_t binary_data_length = 0; + + uint32_t binary_data_length = 0; if (state->buffers.size()) { binary_data_length = state->buffers[0].size(); } - const int32_t binary_chunk_length = binary_data_length; - const int32_t binary_chunk_type = 0x004E4942; //BIN + const uint32_t binary_chunk_length = ((binary_data_length + 3) & (~3)); + const uint32_t binary_chunk_type = 0x004E4942; //BIN f->create(FileAccess::ACCESS_RESOURCES); f->store_32(magic); f->store_32(state->major_version); // version - f->store_32(header_size + chunk_header_size + text_chunk_length + chunk_header_size + binary_data_length); // length + f->store_32(header_size + chunk_header_size + text_chunk_length + chunk_header_size + binary_chunk_length); // length f->store_32(text_chunk_length); f->store_32(text_chunk_type); f->store_buffer((uint8_t *)&cs[0], cs.length()); + for (uint32_t pad_i = text_data_length; pad_i < text_chunk_length; pad_i++) { + f->store_8(' '); + } if (binary_chunk_length) { f->store_32(binary_chunk_length); f->store_32(binary_chunk_type); f->store_buffer(state->buffers[0].ptr(), binary_data_length); } + for (uint32_t pad_i = binary_data_length; pad_i < binary_chunk_length; pad_i++) { + f->store_8(0); + } f->close(); } else { |