Merge pull request #3893 from thomasbiang/wangyi_basisu

Support basis universal to GLTF2 format
pull/3878/head^2
Kim Kulling 2021-05-25 17:28:19 +02:00 committed by GitHub
commit 6f24e873b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 6 deletions

View File

@ -1118,11 +1118,13 @@ public:
bool KHR_materials_transmission; bool KHR_materials_transmission;
bool KHR_draco_mesh_compression; bool KHR_draco_mesh_compression;
bool FB_ngon_encoding; bool FB_ngon_encoding;
bool KHR_texture_basisu;
} extensionsUsed; } extensionsUsed;
//! Keeps info about the required extensions //! Keeps info about the required extensions
struct RequiredExtensions { struct RequiredExtensions {
bool KHR_draco_mesh_compression; bool KHR_draco_mesh_compression;
bool KHR_texture_basisu;
} extensionsRequired; } extensionsRequired;
AssetMetadata asset; AssetMetadata asset;

View File

@ -1144,6 +1144,7 @@ inline Image::Image() :
} }
inline void Image::Read(Value &obj, Asset &r) { inline void Image::Read(Value &obj, Asset &r) {
//basisu: no need to handle .ktx2, .basis, load as is
if (!mDataLength) { if (!mDataLength) {
Value *curUri = FindString(obj, "uri"); Value *curUri = FindString(obj, "uri");
if (nullptr != curUri) { if (nullptr != curUri) {
@ -2124,6 +2125,7 @@ inline void Asset::ReadExtensionsUsed(Document &doc) {
CHECK_EXT(KHR_materials_clearcoat); CHECK_EXT(KHR_materials_clearcoat);
CHECK_EXT(KHR_materials_transmission); CHECK_EXT(KHR_materials_transmission);
CHECK_EXT(KHR_draco_mesh_compression); CHECK_EXT(KHR_draco_mesh_compression);
CHECK_EXT(KHR_texture_basisu);
#undef CHECK_EXT #undef CHECK_EXT
} }

View File

@ -250,6 +250,7 @@ namespace glTF2 {
inline void Write(Value& obj, Image& img, AssetWriter& w) inline void Write(Value& obj, Image& img, AssetWriter& w)
{ {
//basisu: no need to handle .ktx2, .basis, write as is
if (img.bufferView) { if (img.bufferView) {
obj.AddMember("bufferView", img.bufferView->index, w.mAl); obj.AddMember("bufferView", img.bufferView->index, w.mAl);
obj.AddMember("mimeType", Value(img.mimeType, w.mAl).Move(), w.mAl); obj.AddMember("mimeType", Value(img.mimeType, w.mAl).Move(), w.mAl);
@ -892,10 +893,22 @@ namespace glTF2 {
if (this->mAsset.extensionsUsed.FB_ngon_encoding) { if (this->mAsset.extensionsUsed.FB_ngon_encoding) {
exts.PushBack(StringRef("FB_ngon_encoding"), mAl); exts.PushBack(StringRef("FB_ngon_encoding"), mAl);
} }
if (this->mAsset.extensionsUsed.KHR_texture_basisu) {
exts.PushBack(StringRef("KHR_texture_basisu"), mAl);
}
} }
if (!exts.Empty()) if (!exts.Empty())
mDoc.AddMember("extensionsUsed", exts, mAl); mDoc.AddMember("extensionsUsed", exts, mAl);
//basisu extensionRequired
Value extsReq;
extsReq.SetArray();
if (this->mAsset.extensionsUsed.KHR_texture_basisu) {
extsReq.PushBack(StringRef("KHR_texture_basisu"), mAl);
mDoc.AddMember("extensionsRequired", extsReq, mAl);
}
} }
template<class T> template<class T>

View File

@ -494,7 +494,6 @@ void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, float& prop, const char
void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTextureType tt, unsigned int slot = 0) void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTextureType tt, unsigned int slot = 0)
{ {
if (mat->GetTextureCount(tt) > 0) { if (mat->GetTextureCount(tt) > 0) {
aiString tex; aiString tex;
@ -507,6 +506,7 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTe
texture = mAsset->textures.Get(it->second); texture = mAsset->textures.Get(it->second);
} }
bool useBasisUniversal = false;
if (!texture) { if (!texture) {
std::string texId = mAsset->FindUniqueID("", "texture"); std::string texId = mAsset->FindUniqueID("", "texture");
texture = mAsset->textures.Create(texId); texture = mAsset->textures.Create(texId);
@ -520,17 +520,45 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTe
texture->source->name = curTex->mFilename.C_Str(); texture->source->name = curTex->mFilename.C_Str();
// The asset has its own buffer, see Image::SetData //basisu: embedded ktx2, bu
texture->source->SetData(reinterpret_cast<uint8_t *>(curTex->pcData), curTex->mWidth, *mAsset);
if (curTex->achFormatHint[0]) { if (curTex->achFormatHint[0]) {
std::string mimeType = "image/"; std::string mimeType = "image/";
mimeType += (memcmp(curTex->achFormatHint, "jpg", 3) == 0) ? "jpeg" : curTex->achFormatHint; if(memcmp(curTex->achFormatHint, "jpg", 3) == 0)
mimeType += "jpeg";
else if(memcmp(curTex->achFormatHint, "ktx", 3) == 0) {
useBasisUniversal = true;
mimeType += "ktx";
}
else if(memcmp(curTex->achFormatHint, "kx2", 3) == 0) {
useBasisUniversal = true;
mimeType += "ktx2";
}
else if(memcmp(curTex->achFormatHint, "bu", 2) == 0) {
useBasisUniversal = true;
mimeType += "basis";
}
else
mimeType += curTex->achFormatHint;
texture->source->mimeType = mimeType; texture->source->mimeType = mimeType;
} }
// The asset has its own buffer, see Image::SetData
//basisu: "image/ktx2", "image/basis" as is
texture->source->SetData(reinterpret_cast<uint8_t *>(curTex->pcData), curTex->mWidth, *mAsset);
} }
else { else {
texture->source->uri = path; texture->source->uri = path;
if(texture->source->uri.find(".ktx")!=std::string::npos ||
texture->source->uri.find(".basis")!=std::string::npos)
{
useBasisUniversal = true;
}
}
//basisu
if(useBasisUniversal) {
mAsset->extensionsUsed.KHR_texture_basisu = true;
mAsset->extensionsRequired.KHR_texture_basisu = true;
} }
GetTexSampler(mat, texture, tt, slot); GetTexSampler(mat, texture, tt, slot);

View File

@ -1476,6 +1476,12 @@ void glTF2Importer::ImportEmbeddedTextures(glTF2::Asset &r) {
if (strcmp(ext, "jpeg") == 0) { if (strcmp(ext, "jpeg") == 0) {
ext = "jpg"; ext = "jpg";
} }
else if(strcmp(ext, "ktx2") == 0) { //basisu: ktx remains
ext = "kx2";
}
else if(strcmp(ext, "basis") == 0) { //basisu
ext = "bu";
}
size_t len = strlen(ext); size_t len = strlen(ext);
if (len <= 3) { if (len <= 3) {