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