Merge pull request #70181 from quentinguidee/fix/gltf-crash-shader-material

glTF: Fix export crash with a ShaderMaterial
This commit is contained in:
Rémi Verschelde 2022-12-17 20:56:52 +01:00
commit fa345869f5
No known key found for this signature in database
GPG key ID: C3336907360768E1

View file

@ -3384,8 +3384,13 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
if (!material->get_name().is_empty()) { if (!material->get_name().is_empty()) {
d["name"] = _gen_unique_name(p_state, material->get_name()); d["name"] = _gen_unique_name(p_state, material->get_name());
} }
Ref<BaseMaterial3D> base_material = material; Ref<BaseMaterial3D> base_material = material;
if (base_material.is_valid()) { if (base_material.is_null()) {
materials.push_back(d);
continue;
}
Dictionary mr; Dictionary mr;
{ {
Array arr; Array arr;
@ -3398,7 +3403,6 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
} }
{ {
Dictionary bct; Dictionary bct;
if (base_material.is_valid()) {
Ref<Texture2D> albedo_texture = base_material->get_texture(BaseMaterial3D::TEXTURE_ALBEDO); Ref<Texture2D> albedo_texture = base_material->get_texture(BaseMaterial3D::TEXTURE_ALBEDO);
GLTFTextureIndex gltf_texture_index = -1; GLTFTextureIndex gltf_texture_index = -1;
@ -3416,8 +3420,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
mr["baseColorTexture"] = bct; mr["baseColorTexture"] = bct;
} }
} }
}
if (base_material.is_valid()) {
mr["metallicFactor"] = base_material->get_metallic(); mr["metallicFactor"] = base_material->get_metallic();
mr["roughnessFactor"] = base_material->get_roughness(); mr["roughnessFactor"] = base_material->get_roughness();
bool has_roughness = base_material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS).is_valid() && base_material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS)->get_image().is_valid(); bool has_roughness = base_material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS).is_valid() && base_material->get_texture(BaseMaterial3D::TEXTURE_ROUGHNESS)->get_image().is_valid();
@ -3552,9 +3555,8 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
mr["metallicRoughnessTexture"] = mrt; mr["metallicRoughnessTexture"] = mrt;
} }
} }
}
d["pbrMetallicRoughness"] = mr; d["pbrMetallicRoughness"] = mr;
}
if (base_material->get_feature(BaseMaterial3D::FEATURE_NORMAL_MAPPING)) { if (base_material->get_feature(BaseMaterial3D::FEATURE_NORMAL_MAPPING)) {
Dictionary nt; Dictionary nt;
Ref<ImageTexture> tex; Ref<ImageTexture> tex;
@ -3607,6 +3609,7 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
arr.push_back(c.b); arr.push_back(c.b);
d["emissiveFactor"] = arr; d["emissiveFactor"] = arr;
} }
if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION)) { if (base_material->get_feature(BaseMaterial3D::FEATURE_EMISSION)) {
Dictionary et; Dictionary et;
Ref<Texture2D> emission_texture = base_material->get_texture(BaseMaterial3D::TEXTURE_EMISSION); Ref<Texture2D> emission_texture = base_material->get_texture(BaseMaterial3D::TEXTURE_EMISSION);
@ -3621,16 +3624,19 @@ Error GLTFDocument::_serialize_materials(Ref<GLTFState> p_state) {
d["emissiveTexture"] = et; d["emissiveTexture"] = et;
} }
} }
const bool ds = base_material->get_cull_mode() == BaseMaterial3D::CULL_DISABLED; const bool ds = base_material->get_cull_mode() == BaseMaterial3D::CULL_DISABLED;
if (ds) { if (ds) {
d["doubleSided"] = ds; d["doubleSided"] = ds;
} }
if (base_material->get_transparency() == BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR) { if (base_material->get_transparency() == BaseMaterial3D::TRANSPARENCY_ALPHA_SCISSOR) {
d["alphaMode"] = "MASK"; d["alphaMode"] = "MASK";
d["alphaCutoff"] = base_material->get_alpha_scissor_threshold(); d["alphaCutoff"] = base_material->get_alpha_scissor_threshold();
} else if (base_material->get_transparency() != BaseMaterial3D::TRANSPARENCY_DISABLED) { } else if (base_material->get_transparency() != BaseMaterial3D::TRANSPARENCY_DISABLED) {
d["alphaMode"] = "BLEND"; d["alphaMode"] = "BLEND";
} }
materials.push_back(d); materials.push_back(d);
} }
if (!materials.size()) { if (!materials.size()) {