| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- // Copyright (C) 2009-2022, Panagiotis Christopoulos Charitos and contributors.
- // All rights reserved.
- // Code licensed under the BSD License.
- // http://www.anki3d.org/LICENSE
- #include <AnKi/Importer/GltfImporter.h>
- #include <AnKi/Resource/ImageLoader.h>
- #include <AnKi/Util/WeakArray.h>
- namespace anki {
- const char* MATERIAL_TEMPLATE = R"(<!-- This file is auto generated by ImporterMaterial.cpp -->
- <material shadows="1">
- <shaderPrograms>
- <shaderProgram filename="ShaderBinaries/GBufferGeneric.ankiprogbin">
- <mutation>
- <mutator name="DIFFUSE_TEX" value="%diffTexMutator%"/>
- <mutator name="SPECULAR_TEX" value="%specTexMutator%"/>
- <mutator name="ROUGHNESS_TEX" value="%roughnessTexMutator%"/>
- <mutator name="METAL_TEX" value="%metalTexMutator%"/>
- <mutator name="NORMAL_TEX" value="%normalTexMutator%"/>
- <mutator name="PARALLAX" value="%parallaxMutator%"/>
- <mutator name="EMISSIVE_TEX" value="%emissiveTexMutator%"/>
- <mutator name="ALPHA_TEST" value="%alphaTestMutator%"/>
- </mutation>
- </shaderProgram>
- %rayTracing%
- </shaderPrograms>
- <inputs>
- %parallaxInput%
- %diff%
- %spec%
- %roughness%
- %metallic%
- %normal%
- %emission%
- %subsurface%
- %height%
- </inputs>
- </material>
- )";
- const char* RT_MATERIAL_TEMPLATE = R"(
- <shaderProgram filename="ShaderBinaries/RtShadowsHit.ankiprogbin">
- <mutation>
- <mutator name="ALPHA_TEXTURE" value="0"/>
- </mutation>
- </shaderProgram>
- )";
- static CString getTextureUri(const cgltf_texture_view& view)
- {
- ANKI_ASSERT(view.texture);
- ANKI_ASSERT(view.texture->image);
- ANKI_ASSERT(view.texture->image->uri);
- return view.texture->image->uri;
- }
- /// Read the texture and find out if
- static Error findConstantColorsInImage(CString fname, Vec4& constantColor, GenericMemoryPoolAllocator<U8>& alloc)
- {
- ImageLoader iloader(alloc);
- ANKI_CHECK(iloader.load(fname));
- ANKI_ASSERT(iloader.getColorFormat() == ImageBinaryColorFormat::RGBA8);
- ANKI_ASSERT(iloader.getCompression() == ImageBinaryDataCompression::RAW);
- const U8Vec4* data = reinterpret_cast<const U8Vec4*>(&iloader.getSurface(0, 0, 0).m_data[0]);
- ConstWeakArray<U8Vec4> pixels(data, iloader.getWidth() * iloader.getHeight());
- const F32 epsilon = 1.0f / 255.0f;
- for(U32 y = 0; y < iloader.getHeight(); ++y)
- {
- for(U32 x = 0; x < iloader.getWidth(); ++x)
- {
- const U8Vec4& pixel = pixels[y * iloader.getWidth() + x];
- const Vec4 pixelf = Vec4(pixel) / 255.0f;
- if(x == 0 && y == 0)
- {
- // Initialize
- constantColor = pixelf;
- }
- else
- {
- for(U32 i = 0; i < 4; ++i)
- {
- if(absolute(pixelf[i] - constantColor[i]) > epsilon)
- {
- constantColor[i] = -1.0f;
- }
- }
- }
- }
- }
- return Error::NONE;
- }
- Error GltfImporter::writeMaterial(const cgltf_material& mtl, Bool writeRayTracing)
- {
- StringAuto fname(m_alloc);
- fname.sprintf("%s%s", m_outDir.cstr(), computeMaterialResourceFilename(mtl).cstr());
- ANKI_IMPORTER_LOGV("Importing material %s", fname.cstr());
- if(!mtl.has_pbr_metallic_roughness)
- {
- ANKI_IMPORTER_LOGE("Expecting PBR metallic roughness");
- return Error::USER_DATA;
- }
- HashMapAuto<CString, StringAuto> extras(m_alloc);
- ANKI_CHECK(getExtras(mtl.extras, extras));
- StringAuto xml(m_alloc);
- xml.append(XML_HEADER);
- xml.append("\n");
- xml.append(MATERIAL_TEMPLATE);
- if(writeRayTracing)
- {
- xml.replaceAll("%rayTracing%", RT_MATERIAL_TEMPLATE);
- }
- // Diffuse
- if(mtl.pbr_metallic_roughness.base_color_texture.texture)
- {
- const CString fname = getTextureUri(mtl.pbr_metallic_roughness.base_color_texture);
- StringAuto uri(m_alloc);
- uri.sprintf("%s%s", m_texrpath.cstr(), fname.cstr());
- xml.replaceAll("%diff%", StringAuto(m_alloc).sprintf("<input name=\"m_diffTex\" value=\"%s\"/>", uri.cstr()));
- xml.replaceAll("%diffTexMutator%", "1");
- Vec4 constantColor;
- ANKI_CHECK(findConstantColorsInImage(fname, constantColor, m_alloc));
- const Bool constantAlpha = constantColor.w() >= 0.0f;
- xml.replaceAll("%alphaTestMutator%", (constantAlpha) ? "0" : "1");
- }
- else
- {
- const F32* diffCol = &mtl.pbr_metallic_roughness.base_color_factor[0];
- xml.replaceAll("%diff%", StringAuto(m_alloc).sprintf("<input name=\"m_diffColor\" value=\"%f %f %f\"/>",
- diffCol[0], diffCol[1], diffCol[2]));
- xml.replaceAll("%diffTexMutator%", "0");
- xml.replaceAll("%alphaTestMutator%", "0");
- }
- // Specular color (freshnel)
- {
- Vec3 specular;
- auto it = extras.find("specular");
- if(it != extras.getEnd())
- {
- StringListAuto tokens(m_alloc);
- tokens.splitString(it->toCString(), ' ');
- if(tokens.getSize() != 3)
- {
- ANKI_IMPORTER_LOGE("Wrong specular: %s", it->cstr());
- return Error::USER_DATA;
- }
- auto token = tokens.getBegin();
- ANKI_CHECK(token->toNumber(specular.x()));
- ++token;
- ANKI_CHECK(token->toNumber(specular.y()));
- ++token;
- ANKI_CHECK(token->toNumber(specular.z()));
- }
- else
- {
- specular = Vec3(0.04f);
- }
- xml.replaceAll("%spec%", StringAuto(m_alloc).sprintf("<input name=\"m_specColor\" value=\"%f %f %f\"/>",
- specular.x(), specular.y(), specular.z()));
- xml.replaceAll("%specTexMutator%", "0");
- }
- // Identify metallic/roughness texture
- F32 constantMetaliness = -1.0f, constantRoughness = -1.0f;
- if(mtl.pbr_metallic_roughness.metallic_roughness_texture.texture)
- {
- const CString fname = getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture);
- Vec4 constantColor;
- ANKI_CHECK(findConstantColorsInImage(fname, constantColor, m_alloc));
- constantRoughness = constantColor.y();
- constantMetaliness = constantColor.z();
- }
- // Roughness
- if(mtl.pbr_metallic_roughness.metallic_roughness_texture.texture && constantRoughness < 0.0f)
- {
- StringAuto uri(m_alloc);
- uri.sprintf("%s%s", m_texrpath.cstr(),
- getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
- xml.replaceAll("%roughness%",
- StringAuto(m_alloc).sprintf("<input name=\"m_roughnessTex\" value=\"%s\"/>", uri.cstr()));
- xml.replaceAll("%roughnessTexMutator%", "1");
- }
- else
- {
- const F32 roughness = (constantRoughness >= 0.0f)
- ? constantRoughness * mtl.pbr_metallic_roughness.roughness_factor
- : mtl.pbr_metallic_roughness.roughness_factor;
- xml.replaceAll("%roughness%",
- StringAuto(m_alloc).sprintf("<input name=\"m_roughness\" value=\"%f\"/>", roughness));
- xml.replaceAll("%roughnessTexMutator%", "0");
- }
- // Metallic
- if(mtl.pbr_metallic_roughness.metallic_roughness_texture.texture && constantMetaliness < 0.0f)
- {
- StringAuto uri(m_alloc);
- uri.sprintf("%s%s", m_texrpath.cstr(),
- getTextureUri(mtl.pbr_metallic_roughness.metallic_roughness_texture).cstr());
- xml.replaceAll("%metallic%",
- StringAuto(m_alloc).sprintf("<input name=\"m_metallicTex\" value=\"%s\"/>", uri.cstr()));
- xml.replaceAll("%metalTexMutator%", "1");
- }
- else
- {
- const F32 metalines = (constantMetaliness >= 0.0f)
- ? constantMetaliness * mtl.pbr_metallic_roughness.metallic_factor
- : mtl.pbr_metallic_roughness.metallic_factor;
- xml.replaceAll("%metallic%",
- StringAuto(m_alloc).sprintf("<input name=\"m_metallic\" value=\"%f\"/>", metalines));
- xml.replaceAll("%metalTexMutator%", "0");
- }
- // Normal texture
- if(mtl.normal_texture.texture)
- {
- 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 name=\"m_normalTex\" value=\"%s\"/>", uri.cstr()));
- xml.replaceAll("%normalTexMutator%", "1");
- }
- else
- {
- xml.replaceAll("%normal%", "");
- xml.replaceAll("%normalTexMutator%", "0");
- }
- }
- else
- {
- xml.replaceAll("%normal%", "");
- xml.replaceAll("%normalTexMutator%", "0");
- }
- // Emissive texture
- if(mtl.emissive_texture.texture)
- {
- StringAuto uri(m_alloc);
- uri.sprintf("%s%s", m_texrpath.cstr(), getTextureUri(mtl.emissive_texture).cstr());
- xml.replaceAll("%emission%",
- StringAuto(m_alloc).sprintf("<input name=\"m_emissiveTex\" value=\"%s\"/>", uri.cstr()));
- xml.replaceAll("%emissiveTexMutator%", "1");
- }
- else
- {
- const F32* emissionCol = &mtl.emissive_factor[0];
- xml.replaceAll("%emission%", StringAuto(m_alloc).sprintf("<input name=\"m_emission\" value=\"%f %f %f\"/>",
- emissionCol[0], emissionCol[1], emissionCol[2]));
- xml.replaceAll("%emissiveTexMutator%", "0");
- }
- // Subsurface
- {
- F32 subsurface;
- auto it = extras.find("subsurface");
- if(it != extras.getEnd())
- {
- ANKI_CHECK(it->toNumber(subsurface));
- }
- else
- {
- subsurface = 0.0f;
- }
- xml.replaceAll("%subsurface%",
- StringAuto(m_alloc).sprintf("<input name=\"m_subsurface\" value=\"%f\"/>", subsurface));
- }
- // Height texture
- auto it = extras.find("height_map");
- if(it != extras.getEnd())
- {
- StringAuto uri(m_alloc);
- uri.sprintf("%s%s", m_texrpath.cstr(), it->cstr());
- xml.replaceAll("%height%", StringAuto(m_alloc).sprintf("<input name=\"m_heightTex\" value=\"%s\" \"/>\n"
- "\t\t<input name=\"m_heightmapScale\" value=\"0.05\"/>",
- uri.cstr()));
- xml.replaceAll("%parallaxMutator%", "1");
- }
- else
- {
- xml.replaceAll("%height%", "");
- xml.replaceAll("%parallaxInput%", "");
- xml.replaceAll("%parallaxMutator%", "0");
- }
- // Replace texture extensions with .anki
- xml.replaceAll(".tga", ".ankitex");
- xml.replaceAll(".png", ".ankitex");
- xml.replaceAll(".jpg", ".ankitex");
- xml.replaceAll(".jpeg", ".ankitex");
- // Write file
- File file;
- ANKI_CHECK(file.open(fname.toCString(), FileOpenFlag::WRITE));
- ANKI_CHECK(file.writeText("%s", xml.cstr()));
- return Error::NONE;
- }
- } // end namespace anki
|