Skip to content

Commit

Permalink
porting over some functions from 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ogapo committed May 11, 2024
1 parent 5c7f084 commit d9585d7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
27 changes: 27 additions & 0 deletions modules/gltf/gltf_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "gltf_template_convert.h"

void GLTFState::_bind_methods() {
ClassDB::bind_method(D_METHOD("append_data_to_buffers", "data", "deduplication"), &GLTFState::append_data_to_buffers);
ClassDB::bind_method(D_METHOD("add_used_extension", "extension_name", "required"), &GLTFState::add_used_extension);
ClassDB::bind_method(D_METHOD("get_json"), &GLTFState::get_json);
ClassDB::bind_method(D_METHOD("set_json", "json"), &GLTFState::set_json);
Expand Down Expand Up @@ -399,3 +400,29 @@ Variant GLTFState::get_additional_data(const StringName &p_extension_name) {
void GLTFState::set_additional_data(const StringName &p_extension_name, Variant p_additional_data) {
additional_data[p_extension_name] = p_additional_data;
}

GLTFBufferViewIndex GLTFState::append_data_to_buffers(const Vector<uint8_t> &p_data, const bool p_deduplication = false) {
if (p_deduplication) {
for (int i = 0; i < buffer_views.size(); i++) {
Ref<GLTFBufferView> buffer_view = buffer_views[i];
Vector<uint8_t> buffer_view_data = buffer_view->load_buffer_view_data(this);
if (buffer_view_data == p_data) {
return i;
}
}
}
// Append the given data to a buffer and create a buffer view for it.
if (unlikely(buffers.is_empty())) {
buffers.push_back(Vector<uint8_t>());
}
Vector<uint8_t> &destination_buffer = buffers.write[0];
Ref<GLTFBufferView> buffer_view;
buffer_view.instantiate();
buffer_view->set_buffer(0);
buffer_view->set_byte_offset(destination_buffer.size());
buffer_view->set_byte_length(p_data.size());
destination_buffer.append_array(p_data);
const int new_index = buffer_views.size();
buffer_views.push_back(buffer_view);
return new_index;
}
1 change: 1 addition & 0 deletions modules/gltf/gltf_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class GLTFState : public Resource {

public:
void add_used_extension(const String &p_extension, bool p_required = false);
GLTFBufferViewIndex append_data_to_buffers(const Vector<uint8_t> &p_data, const bool p_deduplication);

enum GLTFHandleBinary {
HANDLE_BINARY_DISCARD_TEXTURES = 0,
Expand Down
12 changes: 12 additions & 0 deletions modules/gltf/structures/gltf_buffer_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@

#include "gltf_buffer_view.h"

#include "../gltf_state.h"

void GLTFBufferView::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_buffer"), &GLTFBufferView::get_buffer);
ClassDB::bind_method(D_METHOD("set_buffer", "buffer"), &GLTFBufferView::set_buffer);
Expand Down Expand Up @@ -88,3 +90,13 @@ bool GLTFBufferView::get_indices() {
void GLTFBufferView::set_indices(bool p_indices) {
indices = p_indices;
}

Vector<uint8_t> GLTFBufferView::load_buffer_view_data(const Ref<GLTFState> p_state) const {
ERR_FAIL_COND_V(p_state.is_null(), Vector<uint8_t>());
ERR_FAIL_COND_V_MSG(byte_stride > 0, Vector<uint8_t>(), "Buffer views with byte stride are not yet supported by this method.");
const TypedArray<Vector<uint8_t>> &buffers = p_state->get_buffers();
ERR_FAIL_INDEX_V(buffer, buffers.size(), Vector<uint8_t>());
const PackedByteArray &buffer_data = buffers[buffer];
const int64_t byte_end = byte_offset + byte_length;
return buffer_data.slice(byte_offset, byte_end);
}
2 changes: 2 additions & 0 deletions modules/gltf/structures/gltf_buffer_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class GLTFBufferView : public Resource {
bool get_indices();
void set_indices(bool p_indices);
// matrices need to be transformed to this

Vector<uint8_t> load_buffer_view_data(const Ref<GLTFState> p_state) const;
};

#endif // GLTF_BUFFER_VIEW_H

0 comments on commit d9585d7

Please sign in to comment.