Merge branch 'master' into master
This commit is contained in:
@@ -2203,7 +2203,65 @@ void FBXExporter::WriteObjects ()
|
||||
bpnode.Dump(outstream, binary, indent);
|
||||
}*/
|
||||
|
||||
// TODO: cameras, lights
|
||||
// lights
|
||||
indent = 1;
|
||||
lights_uids.clear();
|
||||
for (size_t li = 0; li < mScene->mNumLights; ++li) {
|
||||
aiLight* l = mScene->mLights[li];
|
||||
|
||||
int64_t uid = generate_uid();
|
||||
const std::string lightNodeAttributeName = l->mName.C_Str() + FBX::SEPARATOR + "NodeAttribute";
|
||||
|
||||
FBX::Node lna("NodeAttribute");
|
||||
lna.AddProperties(uid, lightNodeAttributeName, "Light");
|
||||
FBX::Node lnap("Properties70");
|
||||
|
||||
// Light color.
|
||||
lnap.AddP70colorA("Color", l->mColorDiffuse.r, l->mColorDiffuse.g, l->mColorDiffuse.b);
|
||||
|
||||
// TODO Assimp light description is quite concise and do not handle light intensity.
|
||||
// Default value to 1000W.
|
||||
lnap.AddP70numberA("Intensity", 1000);
|
||||
|
||||
// FBXLight::EType conversion
|
||||
switch (l->mType) {
|
||||
case aiLightSource_POINT:
|
||||
lnap.AddP70enum("LightType", 0);
|
||||
break;
|
||||
case aiLightSource_DIRECTIONAL:
|
||||
lnap.AddP70enum("LightType", 1);
|
||||
break;
|
||||
case aiLightSource_SPOT:
|
||||
lnap.AddP70enum("LightType", 2);
|
||||
lnap.AddP70numberA("InnerAngle", AI_RAD_TO_DEG(l->mAngleInnerCone));
|
||||
lnap.AddP70numberA("OuterAngle", AI_RAD_TO_DEG(l->mAngleOuterCone));
|
||||
break;
|
||||
// TODO Assimp do not handle 'area' nor 'volume' lights, but FBX does.
|
||||
/*case aiLightSource_AREA:
|
||||
lnap.AddP70enum("LightType", 3);
|
||||
lnap.AddP70enum("AreaLightShape", 0); // 0=Rectangle, 1=Sphere
|
||||
break;
|
||||
case aiLightSource_VOLUME:
|
||||
lnap.AddP70enum("LightType", 4);
|
||||
break;*/
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Did not understood how to configure the decay so disabling attenuation.
|
||||
lnap.AddP70enum("DecayType", 0);
|
||||
|
||||
// Dump to FBX stream
|
||||
lna.AddChild(lnap);
|
||||
lna.AddChild("TypeFlags", FBX::FBXExportProperty("Light"));
|
||||
lna.AddChild("GeometryVersion", FBX::FBXExportProperty(int32_t(124)));
|
||||
lna.Dump(outstream, binary, indent);
|
||||
|
||||
// Store name and uid (will be used later when parsing scene nodes)
|
||||
lights_uids[l->mName.C_Str()] = uid;
|
||||
}
|
||||
|
||||
// TODO: cameras
|
||||
|
||||
// write nodes (i.e. model hierarchy)
|
||||
// start at root node
|
||||
@@ -2607,10 +2665,19 @@ void FBXExporter::WriteModelNodes(
|
||||
// and connect them
|
||||
connections.emplace_back("C", "OO", node_attribute_uid, node_uid);
|
||||
} else {
|
||||
// generate a null node so we can add children to it
|
||||
WriteModelNode(
|
||||
outstream, binary, node, node_uid, "Null", transform_chain
|
||||
);
|
||||
const auto& lightIt = lights_uids.find(node->mName.C_Str());
|
||||
if(lightIt != lights_uids.end()) {
|
||||
// Node has a light connected to it.
|
||||
WriteModelNode(
|
||||
outstream, binary, node, node_uid, "Light", transform_chain
|
||||
);
|
||||
connections.emplace_back("C", "OO", lightIt->second, node_uid);
|
||||
} else {
|
||||
// generate a null node so we can add children to it
|
||||
WriteModelNode(
|
||||
outstream, binary, node, node_uid, "Null", transform_chain
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// if more than one child mesh, make nodes for each mesh
|
||||
|
||||
@@ -63,10 +63,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
struct aiScene;
|
||||
struct aiNode;
|
||||
//struct aiMaterial;
|
||||
struct aiLight;
|
||||
|
||||
namespace Assimp
|
||||
{
|
||||
namespace Assimp {
|
||||
class IOSystem;
|
||||
class IOStream;
|
||||
class ExportProperties;
|
||||
@@ -95,6 +94,7 @@ namespace Assimp
|
||||
std::vector<int64_t> mesh_uids;
|
||||
std::vector<int64_t> material_uids;
|
||||
std::map<const aiNode*,int64_t> node_uids;
|
||||
std::map<std::string,int64_t> lights_uids;
|
||||
|
||||
// this crude unique-ID system is actually fine
|
||||
int64_t last_uid = 999999;
|
||||
|
||||
@@ -513,7 +513,7 @@ void ProcessPolygonalBoundedBooleanHalfSpaceDifference(const Schema_2x3::IfcPoly
|
||||
}
|
||||
|
||||
// we got a list of in-out-combinations of intersections. That should be an even number of intersections, or
|
||||
// we're fucked.
|
||||
// we are facing a non-recoverable error.
|
||||
if ((intersections.size() & 1) != 0) {
|
||||
IFCImporter::LogWarn("Odd number of intersections, can't work with that. Omitting half space boundary check.");
|
||||
continue;
|
||||
|
||||
@@ -118,14 +118,14 @@ glTF2Exporter::glTF2Exporter(const char* filename, IOSystem* pIOSystem, const ai
|
||||
ExportScene();
|
||||
|
||||
ExportAnimations();
|
||||
|
||||
|
||||
// export extras
|
||||
if(mProperties->HasPropertyCallback("extras"))
|
||||
{
|
||||
std::function<void*(void*)> ExportExtras = mProperties->GetPropertyCallback("extras");
|
||||
mAsset->extras = (rapidjson::Value*)ExportExtras(0);
|
||||
}
|
||||
|
||||
|
||||
AssetWriter writer(*mAsset);
|
||||
|
||||
if (isBinary) {
|
||||
@@ -436,11 +436,11 @@ inline void SetSamplerWrap(SamplerWrap& wrap, aiTextureMapMode map)
|
||||
};
|
||||
}
|
||||
|
||||
void glTF2Exporter::GetTexSampler(const aiMaterial* mat, Ref<Texture> texture, aiTextureType tt, unsigned int slot)
|
||||
void glTF2Exporter::GetTexSampler(const aiMaterial& mat, Ref<Texture> texture, aiTextureType tt, unsigned int slot)
|
||||
{
|
||||
aiString aId;
|
||||
std::string id;
|
||||
if (aiGetMaterialString(mat, AI_MATKEY_GLTF_MAPPINGID(tt, slot), &aId) == AI_SUCCESS) {
|
||||
if (aiGetMaterialString(&mat, AI_MATKEY_GLTF_MAPPINGID(tt, slot), &aId) == AI_SUCCESS) {
|
||||
id = aId.C_Str();
|
||||
}
|
||||
|
||||
@@ -455,49 +455,52 @@ void glTF2Exporter::GetTexSampler(const aiMaterial* mat, Ref<Texture> texture, a
|
||||
SamplerMagFilter filterMag;
|
||||
SamplerMinFilter filterMin;
|
||||
|
||||
if (aiGetMaterialInteger(mat, AI_MATKEY_MAPPINGMODE_U(tt, slot), (int*)&mapU) == AI_SUCCESS) {
|
||||
if (aiGetMaterialInteger(&mat, AI_MATKEY_MAPPINGMODE_U(tt, slot), (int*)&mapU) == AI_SUCCESS) {
|
||||
SetSamplerWrap(texture->sampler->wrapS, mapU);
|
||||
}
|
||||
|
||||
if (aiGetMaterialInteger(mat, AI_MATKEY_MAPPINGMODE_V(tt, slot), (int*)&mapV) == AI_SUCCESS) {
|
||||
if (aiGetMaterialInteger(&mat, AI_MATKEY_MAPPINGMODE_V(tt, slot), (int*)&mapV) == AI_SUCCESS) {
|
||||
SetSamplerWrap(texture->sampler->wrapT, mapV);
|
||||
}
|
||||
|
||||
if (aiGetMaterialInteger(mat, AI_MATKEY_GLTF_MAPPINGFILTER_MAG(tt, slot), (int*)&filterMag) == AI_SUCCESS) {
|
||||
if (aiGetMaterialInteger(&mat, AI_MATKEY_GLTF_MAPPINGFILTER_MAG(tt, slot), (int*)&filterMag) == AI_SUCCESS) {
|
||||
texture->sampler->magFilter = filterMag;
|
||||
}
|
||||
|
||||
if (aiGetMaterialInteger(mat, AI_MATKEY_GLTF_MAPPINGFILTER_MIN(tt, slot), (int*)&filterMin) == AI_SUCCESS) {
|
||||
if (aiGetMaterialInteger(&mat, AI_MATKEY_GLTF_MAPPINGFILTER_MIN(tt, slot), (int*)&filterMin) == AI_SUCCESS) {
|
||||
texture->sampler->minFilter = filterMin;
|
||||
}
|
||||
|
||||
aiString name;
|
||||
if (aiGetMaterialString(mat, AI_MATKEY_GLTF_MAPPINGNAME(tt, slot), &name) == AI_SUCCESS) {
|
||||
if (aiGetMaterialString(&mat, AI_MATKEY_GLTF_MAPPINGNAME(tt, slot), &name) == AI_SUCCESS) {
|
||||
texture->sampler->name = name.C_Str();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int slot)
|
||||
void glTF2Exporter::GetMatTexProp(const aiMaterial& mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int slot)
|
||||
{
|
||||
std::string textureKey = std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName;
|
||||
|
||||
mat->Get(textureKey.c_str(), tt, slot, prop);
|
||||
mat.Get(textureKey.c_str(), tt, slot, prop);
|
||||
}
|
||||
|
||||
void glTF2Exporter::GetMatTexProp(const aiMaterial* mat, float& prop, const char* propName, aiTextureType tt, unsigned int slot)
|
||||
void glTF2Exporter::GetMatTexProp(const aiMaterial& mat, float& prop, const char* propName, aiTextureType tt, unsigned int slot)
|
||||
{
|
||||
std::string textureKey = std::string(_AI_MATKEY_TEXTURE_BASE) + "." + propName;
|
||||
|
||||
mat->Get(textureKey.c_str(), tt, slot, prop);
|
||||
mat.Get(textureKey.c_str(), tt, slot, prop);
|
||||
}
|
||||
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTextureType tt, unsigned int slot = 0)
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial& mat, Ref<Texture>& texture, unsigned int &texCoord, aiTextureType tt, unsigned int slot = 0)
|
||||
{
|
||||
if (mat->GetTextureCount(tt) > 0) {
|
||||
if (mat.GetTextureCount(tt) > 0) {
|
||||
aiString tex;
|
||||
|
||||
if (mat->Get(AI_MATKEY_TEXTURE(tt, slot), tex) == AI_SUCCESS) {
|
||||
// Read texcoord (UV map index)
|
||||
mat.Get(AI_MATKEY_UVWSRC(tt, slot), texCoord);
|
||||
|
||||
if (mat.Get(AI_MATKEY_TEXTURE(tt, slot), tex) == AI_SUCCESS) {
|
||||
std::string path = tex.C_Str();
|
||||
|
||||
if (path.size() > 0) {
|
||||
@@ -515,11 +518,10 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTe
|
||||
std::string imgId = mAsset->FindUniqueID("", "image");
|
||||
texture->source = mAsset->images.Create(imgId);
|
||||
|
||||
if (path[0] == '*') { // embedded
|
||||
aiTexture* curTex = mScene->mTextures[atoi(&path[1])];
|
||||
|
||||
const aiTexture* curTex = mScene->GetEmbeddedTexture(path.c_str());
|
||||
if (curTex != nullptr) { // embedded
|
||||
texture->source->name = curTex->mFilename.C_Str();
|
||||
|
||||
|
||||
//basisu: embedded ktx2, bu
|
||||
if (curTex->achFormatHint[0]) {
|
||||
std::string mimeType = "image/";
|
||||
@@ -541,7 +543,7 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTe
|
||||
mimeType += curTex->achFormatHint;
|
||||
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);
|
||||
@@ -554,7 +556,7 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTe
|
||||
useBasisUniversal = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//basisu
|
||||
if(useBasisUniversal) {
|
||||
mAsset->extensionsUsed.KHR_texture_basisu = true;
|
||||
@@ -568,45 +570,45 @@ void glTF2Exporter::GetMatTex(const aiMaterial* mat, Ref<Texture>& texture, aiTe
|
||||
}
|
||||
}
|
||||
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial* mat, TextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial& mat, TextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
|
||||
{
|
||||
Ref<Texture>& texture = prop.texture;
|
||||
|
||||
GetMatTex(mat, texture, tt, slot);
|
||||
GetMatTex(mat, texture, prop.texCoord, tt, slot);
|
||||
|
||||
if (texture) {
|
||||
GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
||||
}
|
||||
//if (texture) {
|
||||
// GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
||||
//}
|
||||
}
|
||||
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial* mat, NormalTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial& mat, NormalTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
|
||||
{
|
||||
Ref<Texture>& texture = prop.texture;
|
||||
|
||||
GetMatTex(mat, texture, tt, slot);
|
||||
GetMatTex(mat, texture, prop.texCoord, tt, slot);
|
||||
|
||||
if (texture) {
|
||||
GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
||||
//GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
||||
GetMatTexProp(mat, prop.scale, "scale", tt, slot);
|
||||
}
|
||||
}
|
||||
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial* mat, OcclusionTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
|
||||
void glTF2Exporter::GetMatTex(const aiMaterial& mat, OcclusionTextureInfo& prop, aiTextureType tt, unsigned int slot = 0)
|
||||
{
|
||||
Ref<Texture>& texture = prop.texture;
|
||||
|
||||
GetMatTex(mat, texture, tt, slot);
|
||||
GetMatTex(mat, texture, prop.texCoord, tt, slot);
|
||||
|
||||
if (texture) {
|
||||
GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
||||
//GetMatTexProp(mat, prop.texCoord, "texCoord", tt, slot);
|
||||
GetMatTexProp(mat, prop.strength, "strength", tt, slot);
|
||||
}
|
||||
}
|
||||
|
||||
aiReturn glTF2Exporter::GetMatColor(const aiMaterial* mat, vec4& prop, const char* propName, int type, int idx)
|
||||
aiReturn glTF2Exporter::GetMatColor(const aiMaterial& mat, vec4& prop, const char* propName, int type, int idx) const
|
||||
{
|
||||
aiColor4D col;
|
||||
aiReturn result = mat->Get(propName, type, idx, col);
|
||||
aiReturn result = mat.Get(propName, type, idx, col);
|
||||
|
||||
if (result == AI_SUCCESS) {
|
||||
prop[0] = col.r; prop[1] = col.g; prop[2] = col.b; prop[3] = col.a;
|
||||
@@ -615,37 +617,116 @@ aiReturn glTF2Exporter::GetMatColor(const aiMaterial* mat, vec4& prop, const cha
|
||||
return result;
|
||||
}
|
||||
|
||||
aiReturn glTF2Exporter::GetMatColor(const aiMaterial* mat, vec3& prop, const char* propName, int type, int idx)
|
||||
aiReturn glTF2Exporter::GetMatColor(const aiMaterial& mat, vec3& prop, const char* propName, int type, int idx) const
|
||||
{
|
||||
aiColor3D col;
|
||||
aiReturn result = mat->Get(propName, type, idx, col);
|
||||
aiReturn result = mat.Get(propName, type, idx, col);
|
||||
|
||||
if (result == AI_SUCCESS) {
|
||||
prop[0] = col.r; prop[1] = col.g; prop[2] = col.b;
|
||||
prop[0] = col.r;
|
||||
prop[1] = col.g;
|
||||
prop[2] = col.b;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool glTF2Exporter::GetMatSpecGloss(const aiMaterial &mat, glTF2::PbrSpecularGlossiness &pbrSG) {
|
||||
bool result = false;
|
||||
// If has Glossiness, a Specular Color or Specular Texture, use the KHR_materials_pbrSpecularGlossiness extension
|
||||
// NOTE: This extension is being considered for deprecation (Dec 2020), may be replaced by KHR_material_specular
|
||||
|
||||
if (mat.Get(AI_MATKEY_GLOSSINESS_FACTOR, pbrSG.glossinessFactor) == AI_SUCCESS) {
|
||||
result = true;
|
||||
} else {
|
||||
// Don't have explicit glossiness, convert from pbr roughness or legacy shininess
|
||||
float shininess;
|
||||
if (mat.Get(AI_MATKEY_ROUGHNESS_FACTOR, shininess) == AI_SUCCESS) {
|
||||
pbrSG.glossinessFactor = 1.0f - shininess; // Extension defines this way
|
||||
} else if (mat.Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) {
|
||||
pbrSG.glossinessFactor = shininess / 1000;
|
||||
}
|
||||
}
|
||||
|
||||
if (GetMatColor(mat, pbrSG.specularFactor, AI_MATKEY_COLOR_SPECULAR) == AI_SUCCESS) {
|
||||
result = true;
|
||||
}
|
||||
// Add any appropriate textures
|
||||
GetMatTex(mat, pbrSG.specularGlossinessTexture, aiTextureType_SPECULAR);
|
||||
|
||||
result = result || pbrSG.specularGlossinessTexture.texture;
|
||||
|
||||
if (result) {
|
||||
// Likely to always have diffuse
|
||||
GetMatTex(mat, pbrSG.diffuseTexture, aiTextureType_DIFFUSE);
|
||||
GetMatColor(mat, pbrSG.diffuseFactor, AI_MATKEY_COLOR_DIFFUSE);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool glTF2Exporter::GetMatSheen(const aiMaterial &mat, glTF2::MaterialSheen &sheen) {
|
||||
// Return true if got any valid Sheen properties or textures
|
||||
if (GetMatColor(mat, sheen.sheenColorFactor, AI_MATKEY_SHEEN_COLOR_FACTOR) != aiReturn_SUCCESS)
|
||||
return false;
|
||||
|
||||
// Default Sheen color factor {0,0,0} disables Sheen, so do not export
|
||||
if (sheen.sheenColorFactor == defaultSheenFactor)
|
||||
return false;
|
||||
|
||||
mat.Get(AI_MATKEY_SHEEN_ROUGHNESS_FACTOR, sheen.sheenRoughnessFactor);
|
||||
|
||||
GetMatTex(mat, sheen.sheenColorTexture, AI_MATKEY_SHEEN_COLOR_TEXTURE);
|
||||
GetMatTex(mat, sheen.sheenRoughnessTexture, AI_MATKEY_SHEEN_ROUGHNESS_TEXTURE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool glTF2Exporter::GetMatClearcoat(const aiMaterial &mat, glTF2::MaterialClearcoat &clearcoat) {
|
||||
if (mat.Get(AI_MATKEY_CLEARCOAT_FACTOR, clearcoat.clearcoatFactor) != aiReturn_SUCCESS) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Clearcoat factor of zero disables Clearcoat, so do not export
|
||||
if (clearcoat.clearcoatFactor == 0.0f)
|
||||
return false;
|
||||
|
||||
mat.Get(AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR, clearcoat.clearcoatRoughnessFactor);
|
||||
|
||||
GetMatTex(mat, clearcoat.clearcoatTexture, AI_MATKEY_CLEARCOAT_TEXTURE);
|
||||
GetMatTex(mat, clearcoat.clearcoatRoughnessTexture, AI_MATKEY_CLEARCOAT_ROUGHNESS_TEXTURE);
|
||||
GetMatTex(mat, clearcoat.clearcoatNormalTexture, AI_MATKEY_CLEARCOAT_NORMAL_TEXTURE);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool glTF2Exporter::GetMatTransmission(const aiMaterial &mat, glTF2::MaterialTransmission &transmission) {
|
||||
bool result = mat.Get(AI_MATKEY_TRANSMISSION_FACTOR, transmission.transmissionFactor) == aiReturn_SUCCESS;
|
||||
GetMatTex(mat, transmission.transmissionTexture, AI_MATKEY_TRANSMISSION_TEXTURE);
|
||||
return result || transmission.transmissionTexture.texture;
|
||||
}
|
||||
|
||||
void glTF2Exporter::ExportMaterials()
|
||||
{
|
||||
aiString aiName;
|
||||
for (unsigned int i = 0; i < mScene->mNumMaterials; ++i) {
|
||||
const aiMaterial* mat = mScene->mMaterials[i];
|
||||
ai_assert(mScene->mMaterials[i] != nullptr);
|
||||
|
||||
const aiMaterial & mat = *(mScene->mMaterials[i]);
|
||||
|
||||
std::string id = "material_" + ai_to_string(i);
|
||||
|
||||
Ref<Material> m = mAsset->materials.Create(id);
|
||||
|
||||
std::string name;
|
||||
if (mat->Get(AI_MATKEY_NAME, aiName) == AI_SUCCESS) {
|
||||
if (mat.Get(AI_MATKEY_NAME, aiName) == AI_SUCCESS) {
|
||||
name = aiName.C_Str();
|
||||
}
|
||||
name = mAsset->FindUniqueID(name, "material");
|
||||
|
||||
m->name = name;
|
||||
|
||||
GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE);
|
||||
GetMatTex(mat, m->pbrMetallicRoughness.baseColorTexture, aiTextureType_BASE_COLOR);
|
||||
|
||||
if (!m->pbrMetallicRoughness.baseColorTexture.texture) {
|
||||
//if there wasn't a baseColorTexture defined in the source, fallback to any diffuse texture
|
||||
@@ -654,26 +735,26 @@ void glTF2Exporter::ExportMaterials()
|
||||
|
||||
GetMatTex(mat, m->pbrMetallicRoughness.metallicRoughnessTexture, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE);
|
||||
|
||||
if (GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_FACTOR) != AI_SUCCESS) {
|
||||
if (GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_BASE_COLOR) != AI_SUCCESS) {
|
||||
// if baseColorFactor wasn't defined, then the source is likely not a metallic roughness material.
|
||||
//a fallback to any diffuse color should be used instead
|
||||
GetMatColor(mat, m->pbrMetallicRoughness.baseColorFactor, AI_MATKEY_COLOR_DIFFUSE);
|
||||
}
|
||||
|
||||
if (mat->Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR, m->pbrMetallicRoughness.metallicFactor) != AI_SUCCESS) {
|
||||
if (mat.Get(AI_MATKEY_METALLIC_FACTOR, m->pbrMetallicRoughness.metallicFactor) != AI_SUCCESS) {
|
||||
//if metallicFactor wasn't defined, then the source is likely not a PBR file, and the metallicFactor should be 0
|
||||
m->pbrMetallicRoughness.metallicFactor = 0;
|
||||
}
|
||||
|
||||
// get roughness if source is gltf2 file
|
||||
if (mat->Get(AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR, m->pbrMetallicRoughness.roughnessFactor) != AI_SUCCESS) {
|
||||
if (mat.Get(AI_MATKEY_ROUGHNESS_FACTOR, m->pbrMetallicRoughness.roughnessFactor) != AI_SUCCESS) {
|
||||
// otherwise, try to derive and convert from specular + shininess values
|
||||
aiColor4D specularColor;
|
||||
ai_real shininess;
|
||||
|
||||
if (
|
||||
mat->Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS &&
|
||||
mat->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS
|
||||
mat.Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS &&
|
||||
mat.Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS
|
||||
) {
|
||||
// convert specular color to luminance
|
||||
float specularIntensity = specularColor[0] * 0.2125f + specularColor[1] * 0.7154f + specularColor[2] * 0.0721f;
|
||||
@@ -694,17 +775,17 @@ void glTF2Exporter::ExportMaterials()
|
||||
GetMatTex(mat, m->emissiveTexture, aiTextureType_EMISSIVE);
|
||||
GetMatColor(mat, m->emissiveFactor, AI_MATKEY_COLOR_EMISSIVE);
|
||||
|
||||
mat->Get(AI_MATKEY_TWOSIDED, m->doubleSided);
|
||||
mat->Get(AI_MATKEY_GLTF_ALPHACUTOFF, m->alphaCutoff);
|
||||
mat.Get(AI_MATKEY_TWOSIDED, m->doubleSided);
|
||||
mat.Get(AI_MATKEY_GLTF_ALPHACUTOFF, m->alphaCutoff);
|
||||
|
||||
aiString alphaMode;
|
||||
|
||||
if (mat->Get(AI_MATKEY_GLTF_ALPHAMODE, alphaMode) == AI_SUCCESS) {
|
||||
if (mat.Get(AI_MATKEY_GLTF_ALPHAMODE, alphaMode) == AI_SUCCESS) {
|
||||
m->alphaMode = alphaMode.C_Str();
|
||||
} else {
|
||||
float opacity;
|
||||
|
||||
if (mat->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) {
|
||||
if (mat.Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) {
|
||||
if (opacity < 1) {
|
||||
m->alphaMode = "BLEND";
|
||||
m->pbrMetallicRoughness.baseColorFactor[3] *= opacity;
|
||||
@@ -712,85 +793,44 @@ void glTF2Exporter::ExportMaterials()
|
||||
}
|
||||
}
|
||||
|
||||
bool hasPbrSpecularGlossiness = false;
|
||||
mat->Get(AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS, hasPbrSpecularGlossiness);
|
||||
|
||||
if (hasPbrSpecularGlossiness) {
|
||||
|
||||
if (!mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness) {
|
||||
mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness = true;
|
||||
}
|
||||
|
||||
{
|
||||
// KHR_materials_pbrSpecularGlossiness extension
|
||||
// NOTE: This extension is being considered for deprecation (Dec 2020)
|
||||
PbrSpecularGlossiness pbrSG;
|
||||
|
||||
GetMatColor(mat, pbrSG.diffuseFactor, AI_MATKEY_COLOR_DIFFUSE);
|
||||
GetMatColor(mat, pbrSG.specularFactor, AI_MATKEY_COLOR_SPECULAR);
|
||||
|
||||
if (mat->Get(AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS_GLOSSINESS_FACTOR, pbrSG.glossinessFactor) != AI_SUCCESS) {
|
||||
float shininess;
|
||||
|
||||
if (mat->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) {
|
||||
pbrSG.glossinessFactor = shininess / 1000;
|
||||
}
|
||||
if (GetMatSpecGloss(mat, pbrSG)) {
|
||||
mAsset->extensionsUsed.KHR_materials_pbrSpecularGlossiness = true;
|
||||
m->pbrSpecularGlossiness = Nullable<PbrSpecularGlossiness>(pbrSG);
|
||||
}
|
||||
|
||||
GetMatTex(mat, pbrSG.diffuseTexture, aiTextureType_DIFFUSE);
|
||||
GetMatTex(mat, pbrSG.specularGlossinessTexture, aiTextureType_SPECULAR);
|
||||
|
||||
m->pbrSpecularGlossiness = Nullable<PbrSpecularGlossiness>(pbrSG);
|
||||
}
|
||||
|
||||
bool unlit;
|
||||
if (mat->Get(AI_MATKEY_GLTF_UNLIT, unlit) == AI_SUCCESS && unlit) {
|
||||
// glTFv2 is either PBR or Unlit
|
||||
aiShadingMode shadingMode = aiShadingMode_PBR_BRDF;
|
||||
mat.Get(AI_MATKEY_SHADING_MODEL, shadingMode);
|
||||
if (shadingMode == aiShadingMode_Unlit) {
|
||||
mAsset->extensionsUsed.KHR_materials_unlit = true;
|
||||
m->unlit = true;
|
||||
}
|
||||
} else {
|
||||
// These extensions are not compatible with KHR_materials_unlit or KHR_materials_pbrSpecularGlossiness
|
||||
if (!m->pbrSpecularGlossiness.isPresent) {
|
||||
// Sheen
|
||||
MaterialSheen sheen;
|
||||
if (GetMatSheen(mat, sheen)) {
|
||||
mAsset->extensionsUsed.KHR_materials_sheen = true;
|
||||
m->materialSheen = Nullable<MaterialSheen>(sheen);
|
||||
}
|
||||
|
||||
bool hasMaterialSheen = false;
|
||||
mat->Get(AI_MATKEY_GLTF_MATERIAL_SHEEN, hasMaterialSheen);
|
||||
MaterialClearcoat clearcoat;
|
||||
if (GetMatClearcoat(mat, clearcoat)) {
|
||||
mAsset->extensionsUsed.KHR_materials_clearcoat = true;
|
||||
m->materialClearcoat = Nullable<MaterialClearcoat>(clearcoat);
|
||||
}
|
||||
|
||||
if (hasMaterialSheen) {
|
||||
mAsset->extensionsUsed.KHR_materials_sheen = true;
|
||||
|
||||
MaterialSheen sheen;
|
||||
|
||||
GetMatColor(mat, sheen.sheenColorFactor, AI_MATKEY_GLTF_MATERIAL_SHEEN_COLOR_FACTOR);
|
||||
mat->Get(AI_MATKEY_GLTF_MATERIAL_SHEEN_ROUGHNESS_FACTOR, sheen.sheenRoughnessFactor);
|
||||
GetMatTex(mat, sheen.sheenColorTexture, AI_MATKEY_GLTF_MATERIAL_SHEEN_COLOR_TEXTURE);
|
||||
GetMatTex(mat, sheen.sheenRoughnessTexture, AI_MATKEY_GLTF_MATERIAL_SHEEN_ROUGHNESS_TEXTURE);
|
||||
|
||||
m->materialSheen = Nullable<MaterialSheen>(sheen);
|
||||
}
|
||||
|
||||
bool hasMaterialClearcoat = false;
|
||||
mat->Get(AI_MATKEY_GLTF_MATERIAL_CLEARCOAT, hasMaterialClearcoat);
|
||||
|
||||
if (hasMaterialClearcoat) {
|
||||
mAsset->extensionsUsed.KHR_materials_clearcoat= true;
|
||||
|
||||
MaterialClearcoat clearcoat;
|
||||
|
||||
mat->Get(AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_FACTOR, clearcoat.clearcoatFactor);
|
||||
mat->Get(AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_FACTOR, clearcoat.clearcoatRoughnessFactor);
|
||||
GetMatTex(mat, clearcoat.clearcoatTexture, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_TEXTURE);
|
||||
GetMatTex(mat, clearcoat.clearcoatRoughnessTexture, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_TEXTURE);
|
||||
GetMatTex(mat, clearcoat.clearcoatNormalTexture, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_NORMAL_TEXTURE);
|
||||
|
||||
m->materialClearcoat = Nullable<MaterialClearcoat>(clearcoat);
|
||||
}
|
||||
|
||||
bool hasMaterialTransmission = false;
|
||||
mat->Get(AI_MATKEY_GLTF_MATERIAL_TRANSMISSION, hasMaterialTransmission);
|
||||
|
||||
if (hasMaterialTransmission) {
|
||||
mAsset->extensionsUsed.KHR_materials_transmission = true;
|
||||
|
||||
MaterialTransmission transmission;
|
||||
|
||||
mat->Get(AI_MATKEY_GLTF_MATERIAL_TRANSMISSION_FACTOR, transmission.transmissionFactor);
|
||||
GetMatTex(mat, transmission.transmissionTexture, AI_MATKEY_GLTF_MATERIAL_TRANSMISSION_TEXTURE);
|
||||
|
||||
m->materialTransmission = Nullable<MaterialTransmission>(transmission);
|
||||
MaterialTransmission transmission;
|
||||
if (GetMatTransmission(mat, transmission)) {
|
||||
mAsset->extensionsUsed.KHR_materials_transmission = true;
|
||||
m->materialTransmission = Nullable<MaterialTransmission>(transmission);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,6 +72,10 @@ namespace glTF2
|
||||
struct OcclusionTextureInfo;
|
||||
struct Node;
|
||||
struct Texture;
|
||||
struct PbrSpecularGlossiness;
|
||||
struct MaterialSheen;
|
||||
struct MaterialClearcoat;
|
||||
struct MaterialTransmission;
|
||||
|
||||
// Vec/matrix types, as raw float arrays
|
||||
typedef float (vec2)[2];
|
||||
@@ -97,15 +101,19 @@ namespace Assimp
|
||||
|
||||
protected:
|
||||
void WriteBinaryData(IOStream* outfile, std::size_t sceneLength);
|
||||
void GetTexSampler(const aiMaterial* mat, glTF2::Ref<glTF2::Texture> texture, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTexProp(const aiMaterial* mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int idx);
|
||||
void GetMatTexProp(const aiMaterial* mat, float& prop, const char* propName, aiTextureType tt, unsigned int idx);
|
||||
void GetMatTex(const aiMaterial* mat, glTF2::Ref<glTF2::Texture>& texture, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTex(const aiMaterial* mat, glTF2::TextureInfo& prop, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTex(const aiMaterial* mat, glTF2::NormalTextureInfo& prop, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTex(const aiMaterial* mat, glTF2::OcclusionTextureInfo& prop, aiTextureType tt, unsigned int slot);
|
||||
aiReturn GetMatColor(const aiMaterial* mat, glTF2::vec4& prop, const char* propName, int type, int idx);
|
||||
aiReturn GetMatColor(const aiMaterial* mat, glTF2::vec3& prop, const char* propName, int type, int idx);
|
||||
void GetTexSampler(const aiMaterial& mat, glTF2::Ref<glTF2::Texture> texture, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTexProp(const aiMaterial& mat, unsigned int& prop, const char* propName, aiTextureType tt, unsigned int idx);
|
||||
void GetMatTexProp(const aiMaterial& mat, float& prop, const char* propName, aiTextureType tt, unsigned int idx);
|
||||
void GetMatTex(const aiMaterial& mat, glTF2::Ref<glTF2::Texture>& texture, unsigned int &texCoord, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTex(const aiMaterial& mat, glTF2::TextureInfo& prop, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTex(const aiMaterial& mat, glTF2::NormalTextureInfo& prop, aiTextureType tt, unsigned int slot);
|
||||
void GetMatTex(const aiMaterial& mat, glTF2::OcclusionTextureInfo& prop, aiTextureType tt, unsigned int slot);
|
||||
aiReturn GetMatColor(const aiMaterial& mat, glTF2::vec4& prop, const char* propName, int type, int idx) const;
|
||||
aiReturn GetMatColor(const aiMaterial& mat, glTF2::vec3& prop, const char* propName, int type, int idx) const;
|
||||
bool GetMatSpecGloss(const aiMaterial& mat, glTF2::PbrSpecularGlossiness& pbrSG);
|
||||
bool GetMatSheen(const aiMaterial& mat, glTF2::MaterialSheen& sheen);
|
||||
bool GetMatClearcoat(const aiMaterial& mat, glTF2::MaterialClearcoat& clearcoat);
|
||||
bool GetMatTransmission(const aiMaterial& mat, glTF2::MaterialTransmission& transmission);
|
||||
void ExportMetadata();
|
||||
void ExportMaterials();
|
||||
void ExportMeshes();
|
||||
|
||||
@@ -165,7 +165,8 @@ inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset
|
||||
}
|
||||
|
||||
mat->AddProperty(&uri, AI_MATKEY_TEXTURE(texType, texSlot));
|
||||
mat->AddProperty(&prop.texCoord, 1, AI_MATKEY_GLTF_TEXTURE_TEXCOORD(texType, texSlot));
|
||||
const int uvIndex = static_cast<int>(prop.texCoord);
|
||||
mat->AddProperty(&uvIndex, 1, AI_MATKEY_UVWSRC(texType, texSlot));
|
||||
|
||||
if (prop.textureTransformSupported) {
|
||||
aiUVTransform transform;
|
||||
@@ -208,6 +209,11 @@ inline void SetMaterialTextureProperty(std::vector<int> &embeddedTexIdxs, Asset
|
||||
if (sampler->minFilter != SamplerMinFilter::UNSET) {
|
||||
mat->AddProperty(&sampler->minFilter, 1, AI_MATKEY_GLTF_MAPPINGFILTER_MIN(texType, texSlot));
|
||||
}
|
||||
} else {
|
||||
// Use glTFv2 default sampler
|
||||
const aiTextureMapMode default_wrap = aiTextureMapMode_Wrap;
|
||||
mat->AddProperty(&default_wrap, 1, AI_MATKEY_MAPPINGMODE_U(texType, texSlot));
|
||||
mat->AddProperty(&default_wrap, 1, AI_MATKEY_MAPPINGMODE_V(texType, texSlot));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,16 +244,18 @@ static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, M
|
||||
aimat->AddProperty(&str, AI_MATKEY_NAME);
|
||||
}
|
||||
|
||||
// Set Assimp DIFFUSE and BASE COLOR to the pbrMetallicRoughness base color and texture for backwards compatibility
|
||||
// Technically should not load any pbrMetallicRoughness if extensionsRequired contains KHR_materials_pbrSpecularGlossiness
|
||||
SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
|
||||
SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_FACTOR);
|
||||
SetMaterialColorProperty(r, mat.pbrMetallicRoughness.baseColorFactor, aimat, AI_MATKEY_BASE_COLOR);
|
||||
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, aiTextureType_DIFFUSE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_BASE_COLOR_TEXTURE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.baseColorTexture, aimat, aiTextureType_BASE_COLOR);
|
||||
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, mat.pbrMetallicRoughness.metallicRoughnessTexture, aimat, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLICROUGHNESS_TEXTURE);
|
||||
|
||||
aimat->AddProperty(&mat.pbrMetallicRoughness.metallicFactor, 1, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_METALLIC_FACTOR);
|
||||
aimat->AddProperty(&mat.pbrMetallicRoughness.roughnessFactor, 1, AI_MATKEY_GLTF_PBRMETALLICROUGHNESS_ROUGHNESS_FACTOR);
|
||||
aimat->AddProperty(&mat.pbrMetallicRoughness.metallicFactor, 1, AI_MATKEY_METALLIC_FACTOR);
|
||||
aimat->AddProperty(&mat.pbrMetallicRoughness.roughnessFactor, 1, AI_MATKEY_ROUGHNESS_FACTOR);
|
||||
|
||||
float roughnessAsShininess = 1 - mat.pbrMetallicRoughness.roughnessFactor;
|
||||
roughnessAsShininess *= roughnessAsShininess * 1000;
|
||||
@@ -268,52 +276,58 @@ static aiMaterial *ImportMaterial(std::vector<int> &embeddedTexIdxs, Asset &r, M
|
||||
if (mat.pbrSpecularGlossiness.isPresent) {
|
||||
PbrSpecularGlossiness &pbrSG = mat.pbrSpecularGlossiness.value;
|
||||
|
||||
aimat->AddProperty(&mat.pbrSpecularGlossiness.isPresent, 1, AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS);
|
||||
SetMaterialColorProperty(r, pbrSG.diffuseFactor, aimat, AI_MATKEY_COLOR_DIFFUSE);
|
||||
SetMaterialColorProperty(r, pbrSG.specularFactor, aimat, AI_MATKEY_COLOR_SPECULAR);
|
||||
|
||||
float glossinessAsShininess = pbrSG.glossinessFactor * 1000.0f;
|
||||
aimat->AddProperty(&glossinessAsShininess, 1, AI_MATKEY_SHININESS);
|
||||
aimat->AddProperty(&pbrSG.glossinessFactor, 1, AI_MATKEY_GLTF_PBRSPECULARGLOSSINESS_GLOSSINESS_FACTOR);
|
||||
aimat->AddProperty(&pbrSG.glossinessFactor, 1, AI_MATKEY_GLOSSINESS_FACTOR);
|
||||
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.diffuseTexture, aimat, aiTextureType_DIFFUSE);
|
||||
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, pbrSG.specularGlossinessTexture, aimat, aiTextureType_SPECULAR);
|
||||
}
|
||||
|
||||
// glTFv2 is either PBR or Unlit
|
||||
aiShadingMode shadingMode = aiShadingMode_PBR_BRDF;
|
||||
if (mat.unlit) {
|
||||
aimat->AddProperty(&mat.unlit, 1, AI_MATKEY_GLTF_UNLIT);
|
||||
shadingMode = aiShadingMode_Unlit;
|
||||
}
|
||||
|
||||
//KHR_materials_sheen
|
||||
aimat->AddProperty(&shadingMode, 1, AI_MATKEY_SHADING_MODEL);
|
||||
|
||||
|
||||
// KHR_materials_sheen
|
||||
if (mat.materialSheen.isPresent) {
|
||||
MaterialSheen &sheen = mat.materialSheen.value;
|
||||
|
||||
aimat->AddProperty(&mat.materialSheen.isPresent, 1, AI_MATKEY_GLTF_MATERIAL_SHEEN);
|
||||
SetMaterialColorProperty(r, sheen.sheenColorFactor, aimat, AI_MATKEY_GLTF_MATERIAL_SHEEN_COLOR_FACTOR);
|
||||
aimat->AddProperty(&sheen.sheenRoughnessFactor, 1, AI_MATKEY_GLTF_MATERIAL_SHEEN_ROUGHNESS_FACTOR);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, sheen.sheenColorTexture, aimat, AI_MATKEY_GLTF_MATERIAL_SHEEN_COLOR_TEXTURE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, sheen.sheenRoughnessTexture, aimat, AI_MATKEY_GLTF_MATERIAL_SHEEN_ROUGHNESS_TEXTURE);
|
||||
// Default value {0,0,0} disables Sheen
|
||||
if (sheen.sheenColorFactor != defaultSheenFactor) {
|
||||
SetMaterialColorProperty(r, sheen.sheenColorFactor, aimat, AI_MATKEY_SHEEN_COLOR_FACTOR);
|
||||
aimat->AddProperty(&sheen.sheenRoughnessFactor, 1, AI_MATKEY_SHEEN_ROUGHNESS_FACTOR);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, sheen.sheenColorTexture, aimat, AI_MATKEY_SHEEN_COLOR_TEXTURE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, sheen.sheenRoughnessTexture, aimat, AI_MATKEY_SHEEN_ROUGHNESS_TEXTURE);
|
||||
}
|
||||
}
|
||||
|
||||
//KHR_materials_clearcoat
|
||||
// KHR_materials_clearcoat
|
||||
if (mat.materialClearcoat.isPresent) {
|
||||
MaterialClearcoat &clearcoat = mat.materialClearcoat.value;
|
||||
|
||||
aimat->AddProperty(&mat.materialClearcoat.isPresent, 1, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT);
|
||||
aimat->AddProperty(&clearcoat.clearcoatFactor, 1, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_FACTOR);
|
||||
aimat->AddProperty(&clearcoat.clearcoatRoughnessFactor, 1, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_FACTOR);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatTexture, aimat, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_TEXTURE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatRoughnessTexture, aimat, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_ROUGHNESS_TEXTURE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatNormalTexture, aimat, AI_MATKEY_GLTF_MATERIAL_CLEARCOAT_NORMAL_TEXTURE);
|
||||
// Default value 0.0 disables clearcoat
|
||||
if (clearcoat.clearcoatFactor != 0.0f) {
|
||||
aimat->AddProperty(&clearcoat.clearcoatFactor, 1, AI_MATKEY_CLEARCOAT_FACTOR);
|
||||
aimat->AddProperty(&clearcoat.clearcoatRoughnessFactor, 1, AI_MATKEY_CLEARCOAT_ROUGHNESS_FACTOR);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatTexture, aimat, AI_MATKEY_CLEARCOAT_TEXTURE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatRoughnessTexture, aimat, AI_MATKEY_CLEARCOAT_ROUGHNESS_TEXTURE);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, clearcoat.clearcoatNormalTexture, aimat, AI_MATKEY_CLEARCOAT_NORMAL_TEXTURE);
|
||||
}
|
||||
}
|
||||
|
||||
//KHR_materials_transmission
|
||||
// KHR_materials_transmission
|
||||
if (mat.materialTransmission.isPresent) {
|
||||
MaterialTransmission &transmission = mat.materialTransmission.value;
|
||||
|
||||
aimat->AddProperty(&mat.materialTransmission.isPresent, 1, AI_MATKEY_GLTF_MATERIAL_TRANSMISSION);
|
||||
aimat->AddProperty(&transmission.transmissionFactor, 1, AI_MATKEY_GLTF_MATERIAL_TRANSMISSION_FACTOR);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, transmission.transmissionTexture, aimat, AI_MATKEY_GLTF_MATERIAL_TRANSMISSION_TEXTURE);
|
||||
aimat->AddProperty(&transmission.transmissionFactor, 1, AI_MATKEY_TRANSMISSION_FACTOR);
|
||||
SetMaterialTextureProperty(embeddedTexIdxs, r, transmission.transmissionTexture, aimat, AI_MATKEY_TRANSMISSION_TEXTURE);
|
||||
}
|
||||
|
||||
return aimat;
|
||||
|
||||
@@ -406,11 +406,25 @@ void SceneCombiner::MergeScenes(aiScene **_dest, aiScene *master, std::vector<At
|
||||
// Check whether this texture is an embedded texture.
|
||||
// In this case the property looks like this: *<n>,
|
||||
// where n is the index of the texture.
|
||||
aiString &s = *((aiString *)prop->mData);
|
||||
// Copy here because we overwrite the string data in-place and the buffer inside of aiString
|
||||
// will be a lie if we just reinterpret from prop->mData. The size of mData is not guaranteed to be
|
||||
// MAXLEN in size.
|
||||
aiString s(*(aiString *)prop->mData);
|
||||
if ('*' == s.data[0]) {
|
||||
// Offset the index and write it back ..
|
||||
const unsigned int idx = strtoul10(&s.data[1]) + offset[n];
|
||||
ASSIMP_itoa10(&s.data[1], sizeof(s.data) - 1, idx);
|
||||
const unsigned int oldLen = s.length;
|
||||
|
||||
s.length = 1 + ASSIMP_itoa10(&s.data[1], sizeof(s.data) - 1, idx);
|
||||
|
||||
// The string changed in size so we need to reallocate the buffer for the property.
|
||||
if (oldLen < s.length) {
|
||||
prop->mDataLength += s.length - oldLen;
|
||||
delete[] prop->mData;
|
||||
prop->mData = new char[prop->mDataLength];
|
||||
}
|
||||
|
||||
memcpy(prop->mData, static_cast<void*>(&s), prop->mDataLength);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,10 +47,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
#include <assimp/material.h>
|
||||
|
||||
// -------------------------------------------------------------------------------
|
||||
const char* TextureTypeToString(aiTextureType in)
|
||||
{
|
||||
switch (in)
|
||||
{
|
||||
const char *TextureTypeToString(aiTextureType in) {
|
||||
switch (in) {
|
||||
case aiTextureType_NONE:
|
||||
return "n/a";
|
||||
case aiTextureType_DIFFUSE:
|
||||
@@ -87,6 +85,12 @@ const char* TextureTypeToString(aiTextureType in)
|
||||
return "DiffuseRoughness";
|
||||
case aiTextureType_AMBIENT_OCCLUSION:
|
||||
return "AmbientOcclusion";
|
||||
case aiTextureType_SHEEN:
|
||||
return "Sheen";
|
||||
case aiTextureType_CLEARCOAT:
|
||||
return "Clearcoat";
|
||||
case aiTextureType_TRANSMISSION:
|
||||
return "Transmission";
|
||||
case aiTextureType_UNKNOWN:
|
||||
return "Unknown";
|
||||
default:
|
||||
|
||||
@@ -4,7 +4,6 @@ Open Asset Import Library (assimp)
|
||||
|
||||
Copyright (c) 2006-2021, assimp team
|
||||
|
||||
|
||||
All rights reserved.
|
||||
|
||||
Redistribution and use of this software in source and binary forms,
|
||||
@@ -41,6 +40,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "EmbedTexturesProcess.h"
|
||||
#include <assimp/IOStream.hpp>
|
||||
#include <assimp/IOSystem.hpp>
|
||||
#include <assimp/ParsingUtils.h>
|
||||
#include "ProcessHelper.h"
|
||||
|
||||
@@ -48,11 +49,13 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
using namespace Assimp;
|
||||
|
||||
EmbedTexturesProcess::EmbedTexturesProcess()
|
||||
: BaseProcess() {
|
||||
EmbedTexturesProcess::EmbedTexturesProcess() :
|
||||
BaseProcess() {
|
||||
// empty
|
||||
}
|
||||
|
||||
EmbedTexturesProcess::~EmbedTexturesProcess() {
|
||||
// empty
|
||||
}
|
||||
|
||||
bool EmbedTexturesProcess::IsActive(unsigned int pFlags) const {
|
||||
@@ -62,15 +65,16 @@ bool EmbedTexturesProcess::IsActive(unsigned int pFlags) const {
|
||||
void EmbedTexturesProcess::SetupProperties(const Importer* pImp) {
|
||||
mRootPath = pImp->GetPropertyString("sourceFilePath");
|
||||
mRootPath = mRootPath.substr(0, mRootPath.find_last_of("\\/") + 1u);
|
||||
mIOHandler = pImp->GetIOHandler();
|
||||
}
|
||||
|
||||
void EmbedTexturesProcess::Execute(aiScene* pScene) {
|
||||
if (pScene == nullptr || pScene->mRootNode == nullptr) return;
|
||||
if (pScene == nullptr || pScene->mRootNode == nullptr || mIOHandler == nullptr){
|
||||
return;
|
||||
}
|
||||
|
||||
aiString path;
|
||||
|
||||
uint32_t embeddedTexturesCount = 0u;
|
||||
|
||||
for (auto matId = 0u; matId < pScene->mNumMaterials; ++matId) {
|
||||
auto material = pScene->mMaterials[matId];
|
||||
|
||||
@@ -101,27 +105,31 @@ bool EmbedTexturesProcess::addTexture(aiScene* pScene, std::string path) const {
|
||||
std::string imagePath = path;
|
||||
|
||||
// Test path directly
|
||||
std::ifstream file(imagePath, std::ios::binary | std::ios::ate);
|
||||
if ((imageSize = file.tellg()) == std::streampos(-1)) {
|
||||
if (!mIOHandler->Exists(imagePath)) {
|
||||
ASSIMP_LOG_WARN("EmbedTexturesProcess: Cannot find image: ", imagePath, ". Will try to find it in root folder.");
|
||||
|
||||
// Test path in root path
|
||||
imagePath = mRootPath + path;
|
||||
file.open(imagePath, std::ios::binary | std::ios::ate);
|
||||
if ((imageSize = file.tellg()) == std::streampos(-1)) {
|
||||
if (!mIOHandler->Exists(imagePath)) {
|
||||
// Test path basename in root path
|
||||
imagePath = mRootPath + path.substr(path.find_last_of("\\/") + 1u);
|
||||
file.open(imagePath, std::ios::binary | std::ios::ate);
|
||||
if ((imageSize = file.tellg()) == std::streampos(-1)) {
|
||||
if (!mIOHandler->Exists(imagePath)) {
|
||||
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
IOStream* pFile = mIOHandler->Open(imagePath);
|
||||
if (pFile == nullptr) {
|
||||
ASSIMP_LOG_ERROR("EmbedTexturesProcess: Unable to embed texture: ", path, ".");
|
||||
return false;
|
||||
}
|
||||
imageSize = pFile->FileSize();
|
||||
|
||||
aiTexel* imageContent = new aiTexel[ 1ul + static_cast<unsigned long>( imageSize ) / sizeof(aiTexel)];
|
||||
file.seekg(0, std::ios::beg);
|
||||
file.read(reinterpret_cast<char*>(imageContent), imageSize);
|
||||
pFile->Seek(0, aiOrigin_SET);
|
||||
pFile->Read(reinterpret_cast<char*>(imageContent), imageSize, 1);
|
||||
mIOHandler->Close(pFile);
|
||||
|
||||
// Enlarging the textures table
|
||||
unsigned int textureId = pScene->mNumTextures++;
|
||||
|
||||
@@ -48,6 +48,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
struct aiNode;
|
||||
|
||||
class IOSystem;
|
||||
|
||||
namespace Assimp {
|
||||
|
||||
/**
|
||||
@@ -80,6 +82,7 @@ private:
|
||||
|
||||
private:
|
||||
std::string mRootPath;
|
||||
IOSystem* mIOHandler = nullptr;
|
||||
};
|
||||
|
||||
} // namespace Assimp
|
||||
|
||||
Reference in New Issue
Block a user