|
@@ -739,6 +739,9 @@ Error GLTFDocument::_encode_buffer_glb(Ref<GLTFState> state, const String &p_pat
|
|
|
gltf_buffer["byteLength"] = buffer_data.size();
|
|
|
buffers.push_back(gltf_buffer);
|
|
|
}
|
|
|
+ if (!buffers.size()) {
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
state->json["buffers"] = buffers;
|
|
|
|
|
|
return OK;
|
|
@@ -843,6 +846,9 @@ Error GLTFDocument::_encode_buffer_views(Ref<GLTFState> state) {
|
|
|
buffers.push_back(d);
|
|
|
}
|
|
|
print_verbose("glTF: Total buffer views: " + itos(state->buffer_views.size()));
|
|
|
+ if (!buffers.size()) {
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
state->json["bufferViews"] = buffers;
|
|
|
return OK;
|
|
|
}
|
|
@@ -942,6 +948,9 @@ Error GLTFDocument::_encode_accessors(Ref<GLTFState> state) {
|
|
|
accessors.push_back(d);
|
|
|
}
|
|
|
|
|
|
+ if (!accessors.size()) {
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
state->json["accessors"] = accessors;
|
|
|
ERR_FAIL_COND_V(!state->json.has("accessors"), ERR_FILE_CORRUPT);
|
|
|
print_verbose("glTF: Total accessors: " + itos(state->accessors.size()));
|
|
@@ -2287,6 +2296,7 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> state) {
|
|
|
if (import_mesh.is_null()) {
|
|
|
continue;
|
|
|
}
|
|
|
+ Array instance_materials = state->meshes.write[gltf_mesh_i]->get_instance_materials();
|
|
|
Array primitives;
|
|
|
Dictionary gltf_mesh;
|
|
|
Array target_names;
|
|
@@ -2531,8 +2541,14 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> state) {
|
|
|
targets.push_back(t);
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- Ref<SpatialMaterial> mat = import_mesh->surface_get_material(surface_i);
|
|
|
+ Variant v;
|
|
|
+ if (surface_i < instance_materials.size()) {
|
|
|
+ v = instance_materials.get(surface_i);
|
|
|
+ }
|
|
|
+ Ref<SpatialMaterial> mat = v;
|
|
|
+ if (!mat.is_valid()) {
|
|
|
+ mat = import_mesh->surface_get_material(surface_i);
|
|
|
+ }
|
|
|
if (mat.is_valid()) {
|
|
|
Map<Ref<Material>, GLTFMaterialIndex>::Element *material_cache_i = state->material_cache.find(mat);
|
|
|
if (material_cache_i && material_cache_i->get() != -1) {
|
|
@@ -2576,6 +2592,9 @@ Error GLTFDocument::_serialize_meshes(Ref<GLTFState> state) {
|
|
|
meshes.push_back(gltf_mesh);
|
|
|
}
|
|
|
|
|
|
+ if (!meshes.size()) {
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
state->json["meshes"] = meshes;
|
|
|
print_verbose("glTF: Total meshes: " + itos(meshes.size()));
|
|
|
|
|
@@ -3442,6 +3461,9 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> state) {
|
|
|
}
|
|
|
materials.push_back(d);
|
|
|
}
|
|
|
+ if (!materials.size()) {
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
state->json["materials"] = materials;
|
|
|
print_verbose("Total materials: " + itos(state->materials.size()));
|
|
|
|
|
@@ -4835,6 +4857,9 @@ Error GLTFDocument::_serialize_animations(Ref<GLTFState> state) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ if (!animations.size()) {
|
|
|
+ return OK;
|
|
|
+ }
|
|
|
state->json["animations"] = animations;
|
|
|
|
|
|
print_verbose("glTF: Total animations '" + itos(state->animations.size()) + "'.");
|
|
@@ -5066,6 +5091,18 @@ GLTFMeshIndex GLTFDocument::_convert_mesh_to_gltf(Ref<GLTFState> state, MeshInst
|
|
|
}
|
|
|
Ref<GLTFMesh> gltf_mesh;
|
|
|
gltf_mesh.instance();
|
|
|
+ Array instance_materials;
|
|
|
+ for (int32_t surface_i = 0; surface_i < import_mesh->get_surface_count(); surface_i++) {
|
|
|
+ Ref<Material> mat = import_mesh->surface_get_material(surface_i);
|
|
|
+ if (p_mesh_instance->get_surface_material(surface_i).is_valid()) {
|
|
|
+ mat = p_mesh_instance->get_surface_material(surface_i);
|
|
|
+ }
|
|
|
+ if (p_mesh_instance->get_material_override().is_valid()) {
|
|
|
+ mat = p_mesh_instance->get_material_override();
|
|
|
+ }
|
|
|
+ instance_materials.append(mat);
|
|
|
+ }
|
|
|
+ gltf_mesh->set_instance_materials(instance_materials);
|
|
|
gltf_mesh->set_mesh(import_mesh);
|
|
|
gltf_mesh->set_blend_weights(blend_weights);
|
|
|
GLTFMeshIndex mesh_i = state->meshes.size();
|