Skip to content

Commit

Permalink
Import custom attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
huwpascoe committed Oct 3, 2024
1 parent 2e14492 commit d58f372
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
6 changes: 6 additions & 0 deletions modules/gltf/editor/editor_scene_importer_blend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ Node *EditorSceneFormatImporterBlend::import_scene(const String &p_path, uint32_
} else {
parameters_map["export_def_bones"] = false;
}
if (p_options.has(SNAME("blender/meshes/export_attributes")) && p_options[SNAME("blender/meshes/export_attributes")]) {
parameters_map["export_attributes"] = true;
} else {
parameters_map["export_attributes"] = false;
}
if (p_options.has(SNAME("blender/nodes/modifiers")) && p_options[SNAME("blender/nodes/modifiers")]) {
parameters_map["export_apply"] = true;
} else {
Expand Down Expand Up @@ -373,6 +378,7 @@ void EditorSceneFormatImporterBlend::get_import_options(const String &p_path, Li
ADD_OPTION_BOOL("blender/meshes/tangents", true);
ADD_OPTION_ENUM("blender/meshes/skins", "None,4 Influences (Compatible),All Influences", BLEND_BONE_INFLUENCES_ALL);
ADD_OPTION_BOOL("blender/meshes/export_bones_deforming_mesh_only", false);
ADD_OPTION_BOOL("blender/meshes/export_attributes", false);
ADD_OPTION_BOOL("blender/materials/unpack_enabled", true);
ADD_OPTION_ENUM("blender/materials/export_materials", "Placeholder,Export", BLEND_MATERIAL_EXPORT_EXPORT);
ADD_OPTION_BOOL("blender/animation/limit_playback", true);
Expand Down
7 changes: 7 additions & 0 deletions modules/gltf/extensions/gltf_document_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ void GLTFDocumentExtension::_bind_methods() {
// Import process.
GDVIRTUAL_BIND(_import_preflight, "state", "extensions");
GDVIRTUAL_BIND(_get_supported_extensions);
GDVIRTUAL_BIND(_remap_custom_attribute, "name");
GDVIRTUAL_BIND(_parse_node_extensions, "state", "gltf_node", "extensions");
GDVIRTUAL_BIND(_parse_image_data, "state", "image_data", "mime_type", "ret_image");
GDVIRTUAL_BIND(_get_image_file_extension);
Expand Down Expand Up @@ -64,6 +65,12 @@ Error GLTFDocumentExtension::import_preflight(Ref<GLTFState> p_state, Vector<Str
return err;
}

int GLTFDocumentExtension::remap_custom_attribute(const String &p_name) {
int ret = -1;
GDVIRTUAL_CALL(_remap_custom_attribute, p_name, ret);
return ret;
}

Vector<String> GLTFDocumentExtension::get_supported_extensions() {
Vector<String> ret;
GDVIRTUAL_CALL(_get_supported_extensions, ret);
Expand Down
2 changes: 2 additions & 0 deletions modules/gltf/extensions/gltf_document_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class GLTFDocumentExtension : public Resource {
// Import process.
virtual Error import_preflight(Ref<GLTFState> p_state, Vector<String> p_extensions);
virtual Vector<String> get_supported_extensions();
virtual int remap_custom_attribute(const String &p_name);
virtual Error parse_node_extensions(Ref<GLTFState> p_state, Ref<GLTFNode> p_gltf_node, Dictionary &p_extensions);
virtual Error parse_image_data(Ref<GLTFState> p_state, const PackedByteArray &p_image_data, const String &p_mime_type, Ref<Image> r_image);
virtual String get_image_file_extension();
Expand All @@ -69,6 +70,7 @@ class GLTFDocumentExtension : public Resource {
// Import process.
GDVIRTUAL2R(Error, _import_preflight, Ref<GLTFState>, Vector<String>);
GDVIRTUAL0R(Vector<String>, _get_supported_extensions);
GDVIRTUAL1R(int, _remap_custom_attribute, String);
GDVIRTUAL3R(Error, _parse_node_extensions, Ref<GLTFState>, Ref<GLTFNode>, Dictionary);
GDVIRTUAL4R(Error, _parse_image_data, Ref<GLTFState>, PackedByteArray, String, Ref<Image>);
GDVIRTUAL0R(String, _get_image_file_extension);
Expand Down
58 changes: 58 additions & 0 deletions modules/gltf/gltf_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3136,6 +3136,64 @@ Error GLTFDocument::_parse_meshes(Ref<GLTFState> p_state) {
}
}

for (Ref<GLTFDocumentExtension> ext : document_extensions) {
for (const String &attrib_name : a.keys()) {

Check failure on line 3140 in modules/gltf/gltf_document.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with clang sanitizers (target=editor, tests=yes, dev_build=yes, use_asan=yes, use_ubsan=yes, use_llvm=yes, linker=lld)

loop variable 'attrib_name' of type 'const String &' binds to a temporary constructed from type 'Variant &' [-Werror,-Wrange-loop-construct]

Check failure on line 3140 in modules/gltf/gltf_document.cpp

View workflow job for this annotation

GitHub Actions / 🐧 Linux / Editor with ThreadSanitizer (target=editor, tests=yes, dev_build=yes, use_tsan=yes, use_llvm=yes, linker=lld)

loop variable 'attrib_name' of type 'const String &' binds to a temporary constructed from type 'Variant &' [-Werror,-Wrange-loop-construct]
if (!attrib_name.begins_with("_")) {
continue;
}

const int custom_i = ext->remap_custom_attribute(attrib_name);
if (custom_i == -1) {
continue;
}

ERR_CONTINUE_MSG(custom_i < Mesh::ARRAY_CUSTOM0 || custom_i > Mesh::ARRAY_CUSTOM3, "Invalid MESH_CUSTOM index, ignoring attribute.");
ERR_CONTINUE_MSG(array[custom_i].get_type() != Variant::NIL, "Data already exists for MESH_CUSTOM index, ignoring attribute.");

bool format_okay = true;
Mesh::ArrayCustomFormat format{};
const Ref<GLTFAccessor> accessor = p_state->accessors[a[attrib_name]];
switch (accessor->accessor_type) {
case GLTFAccessor::TYPE_SCALAR: {
array[custom_i] = _decode_accessor_as_floats(p_state, a[attrib_name], true, indices_mapping);
format = Mesh::ARRAY_CUSTOM_R_FLOAT;
} break;
case GLTFAccessor::TYPE_VEC2: {
array[custom_i] = _decode_accessor_as_vec2(p_state, a[attrib_name], true, indices_mapping);
format = Mesh::ARRAY_CUSTOM_RG_FLOAT;
} break;
case GLTFAccessor::TYPE_VEC3: {
array[custom_i] = _decode_accessor_as_vec3(p_state, a[attrib_name], true, indices_mapping);
format = Mesh::ARRAY_CUSTOM_RGB_FLOAT;
} break;
case GLTFAccessor::TYPE_VEC4: {
array[custom_i] = _decode_accessor_as_floats(p_state, a[attrib_name], true, indices_vec4_mapping);
format = Mesh::ARRAY_CUSTOM_RGBA_FLOAT;
} break;
default: {
format_okay = false;
} break;
}

ERR_CONTINUE_MSG(!format_okay, "Unknown format for MESH_CUSTOM, ignoring attribute.");

switch (custom_i) {
case Mesh::ARRAY_CUSTOM0: {
flags |= format << Mesh::ARRAY_FORMAT_CUSTOM0_SHIFT;
} break;
case Mesh::ARRAY_CUSTOM1: {
flags |= format << Mesh::ARRAY_FORMAT_CUSTOM1_SHIFT;
} break;
case Mesh::ARRAY_CUSTOM2: {
flags |= format << Mesh::ARRAY_FORMAT_CUSTOM2_SHIFT;
} break;
case Mesh::ARRAY_CUSTOM3: {
flags |= format << Mesh::ARRAY_FORMAT_CUSTOM3_SHIFT;
} break;
}
}
}

Array morphs;
// Blend shapes
if (p.has("targets")) {
Expand Down

0 comments on commit d58f372

Please sign in to comment.