Formalize imported mesh materials

This commit is contained in:
2026-04-08 02:31:10 +08:00
parent 6e6a98a022
commit 75defb0a49
7 changed files with 12453 additions and 30913 deletions

View File

@@ -409,11 +409,11 @@ MeshBuffers CreateUvSphereMeshBuffers() {
const Core::uint32 i3 = i2 + 1;
buffers.indices.push_back(i0);
buffers.indices.push_back(i2);
buffers.indices.push_back(i1);
buffers.indices.push_back(i1);
buffers.indices.push_back(i2);
buffers.indices.push_back(i1);
buffers.indices.push_back(i3);
buffers.indices.push_back(i2);
}
}
@@ -731,7 +731,7 @@ Material* BuildDefaultPrimitiveMaterial(const Containers::String& path) {
material->SetRenderQueue(MaterialRenderQueue::Geometry);
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinForwardLitShaderPath()));
material->SetTexture(
Containers::String("baseColorTexture"),
Containers::String("_MainTex"),
ResourceManager::Get().Load<Texture>(GetBuiltinDefaultPrimitiveTexturePath()));
material->RecalculateMemorySize();
return material;

View File

@@ -402,59 +402,35 @@ bool HasMaterialTexture(
return false;
}
void ImportMaterialProperties(const aiMaterial& assimpMaterial, Material& material) {
Math::Vector4 ResolveImportedBaseColor(const aiMaterial& assimpMaterial, bool hasBaseColorTexture) {
float opacity = 1.0f;
if (assimpMaterial.Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) {
material.SetFloat("opacity", opacity);
}
const bool hasBaseColorTexture =
HasMaterialTexture(assimpMaterial, { aiTextureType_BASE_COLOR, aiTextureType_DIFFUSE });
assimpMaterial.Get(AI_MATKEY_OPACITY, opacity);
aiColor4D baseColor;
if (assimpMaterial.Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_FACTOR, baseColor) == AI_SUCCESS) {
material.SetFloat4("baseColor",
Math::Vector4(baseColor.r, baseColor.g, baseColor.b, baseColor.a));
} else {
aiColor3D diffuseColor;
if (assimpMaterial.Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor) == AI_SUCCESS) {
material.SetFloat4("baseColor",
hasBaseColorTexture
? Math::Vector4(1.0f, 1.0f, 1.0f, opacity)
: Math::Vector4(diffuseColor.r, diffuseColor.g, diffuseColor.b, opacity));
}
return Math::Vector4(baseColor.r, baseColor.g, baseColor.b, baseColor.a);
}
aiColor3D emissiveColor;
if (assimpMaterial.Get(AI_MATKEY_COLOR_EMISSIVE, emissiveColor) == AI_SUCCESS) {
material.SetFloat3("emissiveColor",
Math::Vector3(emissiveColor.r, emissiveColor.g, emissiveColor.b));
aiColor3D diffuseColor;
if (assimpMaterial.Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor) == AI_SUCCESS) {
return hasBaseColorTexture
? Math::Vector4(1.0f, 1.0f, 1.0f, opacity)
: Math::Vector4(diffuseColor.r, diffuseColor.g, diffuseColor.b, opacity);
}
aiColor3D specularColor;
if (assimpMaterial.Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS) {
material.SetFloat3("specularColor",
Math::Vector3(specularColor.r, specularColor.g, specularColor.b));
}
return Math::Vector4(1.0f, 1.0f, 1.0f, opacity);
}
float metallic = 0.0f;
if (assimpMaterial.Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR, metallic) == AI_SUCCESS) {
material.SetFloat("metallic", metallic);
}
float roughness = 0.0f;
if (assimpMaterial.Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR, roughness) == AI_SUCCESS) {
material.SetFloat("roughness", roughness);
}
float shininess = 0.0f;
if (assimpMaterial.Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) {
material.SetFloat("shininess", shininess);
}
void ImportMaterialProperties(const aiMaterial& assimpMaterial, Material& material) {
const bool hasBaseColorTexture =
HasMaterialTexture(assimpMaterial, { aiTextureType_BASE_COLOR, aiTextureType_DIFFUSE });
material.SetFloat4("_BaseColor", ResolveImportedBaseColor(assimpMaterial, hasBaseColorTexture));
int twoSided = 0;
if (assimpMaterial.Get(AI_MATKEY_TWOSIDED, twoSided) == AI_SUCCESS) {
material.SetBool("twoSided", twoSided != 0);
if (assimpMaterial.Get(AI_MATKEY_TWOSIDED, twoSided) == AI_SUCCESS && twoSided != 0) {
MaterialRenderState renderState = material.GetRenderState();
renderState.cullMode = MaterialCullMode::None;
material.SetRenderState(renderState);
}
}
@@ -470,14 +446,7 @@ void ImportMaterialTextures(const aiMaterial& assimpMaterial,
}
};
assignTexture("baseColorTexture", { aiTextureType_BASE_COLOR, aiTextureType_DIFFUSE });
assignTexture("normalTexture", { aiTextureType_NORMAL_CAMERA, aiTextureType_NORMALS, aiTextureType_HEIGHT });
assignTexture("specularTexture", { aiTextureType_SPECULAR });
assignTexture("emissiveTexture", { aiTextureType_EMISSION_COLOR, aiTextureType_EMISSIVE });
assignTexture("metallicTexture", { aiTextureType_METALNESS });
assignTexture("roughnessTexture", { aiTextureType_DIFFUSE_ROUGHNESS });
assignTexture("occlusionTexture", { aiTextureType_AMBIENT_OCCLUSION, aiTextureType_LIGHTMAP });
assignTexture("opacityTexture", { aiTextureType_OPACITY });
assignTexture("_MainTex", { aiTextureType_BASE_COLOR, aiTextureType_DIFFUSE });
}
Material* ImportSingleMaterial(const aiMaterial& assimpMaterial,
@@ -500,8 +469,10 @@ Material* ImportSingleMaterial(const aiMaterial& assimpMaterial,
params.memorySize = materialName.length() + params.path.Length();
material->Initialize(params);
material->SetShader(ResourceManager::Get().Load<Shader>(GetBuiltinForwardLitShaderPath()));
ImportMaterialProperties(assimpMaterial, *material);
ImportMaterialTextures(assimpMaterial, *material, context);
material->RecalculateMemorySize();
return material;
}