|
@@ -53,8 +53,7 @@ static CString getTextureUri(const cgltf_texture_view& view)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// Read the texture and find out if
|
|
/// Read the texture and find out if
|
|
|
-static Error identifyMetallicRoughnessTexture(CString fname, F32& constantMetalines, F32& constantRoughness,
|
|
|
|
|
- GenericMemoryPoolAllocator<U8>& alloc)
|
|
|
|
|
|
|
+static Error findConstantColorsInImage(CString fname, Vec4& constantColor, GenericMemoryPoolAllocator<U8>& alloc)
|
|
|
{
|
|
{
|
|
|
ImageLoader iloader(alloc);
|
|
ImageLoader iloader(alloc);
|
|
|
ANKI_CHECK(iloader.load(fname));
|
|
ANKI_CHECK(iloader.load(fname));
|
|
@@ -69,25 +68,21 @@ static Error identifyMetallicRoughnessTexture(CString fname, F32& constantMetali
|
|
|
for(U32 x = 0; x < iloader.getHeight(); ++x)
|
|
for(U32 x = 0; x < iloader.getHeight(); ++x)
|
|
|
{
|
|
{
|
|
|
const U8Vec4& pixel = *(data + y * iloader.getWidth() + x);
|
|
const U8Vec4& pixel = *(data + y * iloader.getWidth() + x);
|
|
|
- const F32 m = F32(pixel.z()) / 255.0f;
|
|
|
|
|
- const F32 r = F32(pixel.y()) / 255.0f;
|
|
|
|
|
|
|
+ const Vec4 pixelf = Vec4(pixel) / 255.0f;
|
|
|
|
|
|
|
|
if(x == 0 && y == 0)
|
|
if(x == 0 && y == 0)
|
|
|
{
|
|
{
|
|
|
// Initialize
|
|
// Initialize
|
|
|
- constantMetalines = m;
|
|
|
|
|
- constantRoughness = r;
|
|
|
|
|
|
|
+ constantColor = pixelf;
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
- if(constantMetalines < 0.0f || absolute(m - constantMetalines) > epsilon)
|
|
|
|
|
|
|
+ for(U32 i = 0; i < 4; ++i)
|
|
|
{
|
|
{
|
|
|
- constantMetalines = -1.0f;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if(constantRoughness < 0.0f || absolute(r - constantRoughness) > epsilon)
|
|
|
|
|
- {
|
|
|
|
|
- constantRoughness = -1.0f;
|
|
|
|
|
|
|
+ if(absolute(pixelf[i] - constantColor[i]) > epsilon)
|
|
|
|
|
+ {
|
|
|
|
|
+ constantColor[i] = -1.0f;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
@@ -128,14 +123,14 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.pbr_metallic_roughness.base_color_texture).cstr());
|
|
uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.pbr_metallic_roughness.base_color_texture).cstr());
|
|
|
|
|
|
|
|
xml.replaceAll("%diff%",
|
|
xml.replaceAll("%diff%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"u_diffTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"u_diffTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
xml.replaceAll("%diffTexMutator%", "1");
|
|
xml.replaceAll("%diffTexMutator%", "1");
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
|
const F32* diffCol = &mtl.pbr_metallic_roughness.base_color_factor[0];
|
|
const F32* diffCol = &mtl.pbr_metallic_roughness.base_color_factor[0];
|
|
|
|
|
|
|
|
- xml.replaceAll("%diff%", StringAuto{m_alloc}.sprintf("<input shaderVar=\"m_diffColor\" value=\"%f %f %f\"/>",
|
|
|
|
|
|
|
+ xml.replaceAll("%diff%", StringAuto(m_alloc).sprintf("<input shaderVar=\"m_diffColor\" value=\"%f %f %f\"/>",
|
|
|
diffCol[0], diffCol[1], diffCol[2]));
|
|
diffCol[0], diffCol[1], diffCol[2]));
|
|
|
|
|
|
|
|
xml.replaceAll("%diffTexMutator%", "0");
|
|
xml.replaceAll("%diffTexMutator%", "0");
|
|
@@ -167,7 +162,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
specular = Vec3(0.04f);
|
|
specular = Vec3(0.04f);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- xml.replaceAll("%spec%", StringAuto{m_alloc}.sprintf("<input shaderVar=\"m_specColor\" value=\"%f %f %f\"/>",
|
|
|
|
|
|
|
+ xml.replaceAll("%spec%", StringAuto(m_alloc).sprintf("<input shaderVar=\"m_specColor\" value=\"%f %f %f\"/>",
|
|
|
specular.x(), specular.y(), specular.z()));
|
|
specular.x(), specular.y(), specular.z()));
|
|
|
|
|
|
|
|
xml.replaceAll("%specTexMutator%", "0");
|
|
xml.replaceAll("%specTexMutator%", "0");
|
|
@@ -179,7 +174,10 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
{
|
|
{
|
|
|
const CString fname = getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture);
|
|
const CString fname = getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture);
|
|
|
|
|
|
|
|
- ANKI_CHECK(identifyMetallicRoughnessTexture(fname, constantMetaliness, constantRoughness, m_alloc));
|
|
|
|
|
|
|
+ Vec4 constantColor;
|
|
|
|
|
+ ANKI_CHECK(findConstantColorsInImage(fname, constantColor, m_alloc));
|
|
|
|
|
+ constantRoughness = constantColor.y();
|
|
|
|
|
+ constantMetaliness = constantColor.z();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Roughness
|
|
// Roughness
|
|
@@ -190,7 +188,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
|
|
getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
|
|
|
|
|
|
|
|
xml.replaceAll("%roughness%",
|
|
xml.replaceAll("%roughness%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"u_roughnessTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"u_roughnessTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
|
xml.replaceAll("%roughnessTexMutator%", "1");
|
|
xml.replaceAll("%roughnessTexMutator%", "1");
|
|
|
}
|
|
}
|
|
@@ -201,7 +199,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
: mtl.pbr_metallic_roughness.roughness_factor;
|
|
: mtl.pbr_metallic_roughness.roughness_factor;
|
|
|
|
|
|
|
|
xml.replaceAll("%roughness%",
|
|
xml.replaceAll("%roughness%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"m_roughness\" value=\"%f\"/>", roughness));
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"m_roughness\" value=\"%f\"/>", roughness));
|
|
|
|
|
|
|
|
xml.replaceAll("%roughnessTexMutator%", "0");
|
|
xml.replaceAll("%roughnessTexMutator%", "0");
|
|
|
}
|
|
}
|
|
@@ -214,7 +212,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
|
|
getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
|
|
|
|
|
|
|
|
xml.replaceAll("%metallic%",
|
|
xml.replaceAll("%metallic%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"u_metallicTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"u_metallicTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
|
xml.replaceAll("%metalTexMutator%", "1");
|
|
xml.replaceAll("%metalTexMutator%", "1");
|
|
|
}
|
|
}
|
|
@@ -225,7 +223,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
: mtl.pbr_metallic_roughness.metallic_factor;
|
|
: mtl.pbr_metallic_roughness.metallic_factor;
|
|
|
|
|
|
|
|
xml.replaceAll("%metallic%",
|
|
xml.replaceAll("%metallic%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"m_metallic\" value=\"%f\"/>", metalines));
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"m_metallic\" value=\"%f\"/>", metalines));
|
|
|
|
|
|
|
|
xml.replaceAll("%metalTexMutator%", "0");
|
|
xml.replaceAll("%metalTexMutator%", "0");
|
|
|
}
|
|
}
|
|
@@ -233,13 +231,23 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
// Normal texture
|
|
// Normal texture
|
|
|
if(mtl.normal_texture.texture)
|
|
if(mtl.normal_texture.texture)
|
|
|
{
|
|
{
|
|
|
- StringAuto uri(m_alloc);
|
|
|
|
|
- uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.normal_texture).cstr());
|
|
|
|
|
|
|
+ Vec4 constantColor;
|
|
|
|
|
+ ANKI_CHECK(findConstantColorsInImage(getTextureUri(mtl.normal_texture).cstr(), constantColor, m_alloc));
|
|
|
|
|
+ if(constantColor.xyz() == -1.0f)
|
|
|
|
|
+ {
|
|
|
|
|
+ StringAuto uri(m_alloc);
|
|
|
|
|
+ uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.normal_texture).cstr());
|
|
|
|
|
|
|
|
- xml.replaceAll("%normal%",
|
|
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"u_normalTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
+ xml.replaceAll("%normal%",
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"u_normalTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
|
- xml.replaceAll("%normalTexMutator%", "1");
|
|
|
|
|
|
|
+ xml.replaceAll("%normalTexMutator%", "1");
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ xml.replaceAll("%normal%", "");
|
|
|
|
|
+ xml.replaceAll("%normalTexMutator%", "0");
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
else
|
|
else
|
|
|
{
|
|
{
|
|
@@ -254,7 +262,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.emissive_texture).cstr());
|
|
uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.emissive_texture).cstr());
|
|
|
|
|
|
|
|
xml.replaceAll("%emission%",
|
|
xml.replaceAll("%emission%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"u_emissiveTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"u_emissiveTex\" value=\"%s\"/>", uri.cstr()));
|
|
|
|
|
|
|
|
xml.replaceAll("%emissiveTexMutator%", "1");
|
|
xml.replaceAll("%emissiveTexMutator%", "1");
|
|
|
}
|
|
}
|
|
@@ -262,7 +270,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
{
|
|
{
|
|
|
const F32* emissionCol = &mtl.emissive_factor[0];
|
|
const F32* emissionCol = &mtl.emissive_factor[0];
|
|
|
|
|
|
|
|
- xml.replaceAll("%emission%", StringAuto{m_alloc}.sprintf("<input shaderVar=\"m_emission\" value=\"%f %f %f\"/>",
|
|
|
|
|
|
|
+ xml.replaceAll("%emission%", StringAuto(m_alloc).sprintf("<input shaderVar=\"m_emission\" value=\"%f %f %f\"/>",
|
|
|
emissionCol[0], emissionCol[1], emissionCol[2]));
|
|
emissionCol[0], emissionCol[1], emissionCol[2]));
|
|
|
|
|
|
|
|
xml.replaceAll("%emissiveTexMutator%", "0");
|
|
xml.replaceAll("%emissiveTexMutator%", "0");
|
|
@@ -282,7 +290,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
xml.replaceAll("%subsurface%",
|
|
xml.replaceAll("%subsurface%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"m_subsurface\" value=\"%f\"/>", subsurface));
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"m_subsurface\" value=\"%f\"/>", subsurface));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Height texture
|
|
// Height texture
|
|
@@ -293,7 +301,7 @@ Error GltfImporter::writeMaterial(const cgltf_material& mtl, RayTypeBit usedRayT
|
|
|
uri.sprintf("%s%s", m_texrpath.cstr(), it->cstr());
|
|
uri.sprintf("%s%s", m_texrpath.cstr(), it->cstr());
|
|
|
|
|
|
|
|
xml.replaceAll("%height%",
|
|
xml.replaceAll("%height%",
|
|
|
- StringAuto{m_alloc}.sprintf("<input shaderVar=\"u_heightTex\" value=\"%s\" \"/>\n"
|
|
|
|
|
|
|
+ StringAuto(m_alloc).sprintf("<input shaderVar=\"u_heightTex\" value=\"%s\" \"/>\n"
|
|
|
"\t\t<input shaderVar=\"m_heightmapScale\" value=\"0.05\"/>",
|
|
"\t\t<input shaderVar=\"m_heightmapScale\" value=\"0.05\"/>",
|
|
|
uri.cstr()));
|
|
uri.cstr()));
|
|
|
|
|
|