Sort triangles with vertex transparency separately. (#32)

Lean on the excellent pre-existing support for creating multiple glTF
meshes from a single FBX mesh based on material type. All the triangles
with (at least one) non-opaque vertex get flagged as transparent
material. They will all go separately in their own mesh after the
CreateMaterialModels() gauntlet.

Fixes #25.
This commit is contained in:
Pär Winzell 2017-11-04 20:10:28 -07:00 committed by GitHub
parent 14150269a0
commit 5e0f05261c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 12 deletions

View File

@ -458,10 +458,11 @@ static bool TriangleTexturePolarity(const Vec2f &uv0, const Vec2f &uv1, const Ve
} }
static RawMaterialType static RawMaterialType
GetMaterialType(const RawModel &raw, const int textures[RAW_TEXTURE_USAGE_MAX], const bool skinned) GetMaterialType(const RawModel &raw, const int textures[RAW_TEXTURE_USAGE_MAX], const bool vertexTransparency, const bool skinned)
{ {
if ((raw.GetVertexAttributes() & RAW_VERTEX_ATTRIBUTE_COLOR) != 0) { // if there is vertex transparency, definitely transparent
return skinned ? RAW_MATERIAL_TYPE_SKINNED_VERTEX_COLORED : RAW_MATERIAL_TYPE_VERTEX_COLORED; if (vertexTransparency) {
return skinned ? RAW_MATERIAL_TYPE_SKINNED_TRANSPARENT : RAW_MATERIAL_TYPE_TRANSPARENT;
} }
// Determine material type based on texture occlusion. // Determine material type based on texture occlusion.
@ -588,12 +589,8 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std:
maybeAddTexture(matProps.texShininess, RAW_TEXTURE_USAGE_SHININESS); maybeAddTexture(matProps.texShininess, RAW_TEXTURE_USAGE_SHININESS);
} }
const RawMaterialType materialType = GetMaterialType(raw, textures, skinning.IsSkinned());
const int rawMaterialIndex = raw.AddMaterial(
materialName, shadingModel, materialType, textures,
toVec3f(ambient), toVec4f(diffuse), toVec3f(specular), toVec3f(emissive), shininess);
RawVertex rawVertices[3]; RawVertex rawVertices[3];
bool vertexTransparency = false;
for (int vertexIndex = 0; vertexIndex < 3; vertexIndex++, polygonVertexIndex++) { for (int vertexIndex = 0; vertexIndex < 3; vertexIndex++, polygonVertexIndex++) {
const int controlPointIndex = pMesh->GetPolygonVertex(polygonIndex, vertexIndex); const int controlPointIndex = pMesh->GetPolygonVertex(polygonIndex, vertexIndex);
@ -636,6 +633,9 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std:
vertex.jointWeights = skinning.GetVertexWeights(controlPointIndex); vertex.jointWeights = skinning.GetVertexWeights(controlPointIndex);
vertex.polarityUv0 = false; vertex.polarityUv0 = false;
// flag this triangle as transparent if any of its corner vertices substantially deviates from fully opaque
vertexTransparency |= (fabs(fbxColor.mAlpha - 1.0) > 1e-3);
rawSurface.bounds.AddPoint(vertex.position); rawSurface.bounds.AddPoint(vertex.position);
if (skinning.IsSkinned()) { if (skinning.IsSkinned()) {
@ -690,6 +690,11 @@ static void ReadMesh(RawModel &raw, FbxScene *pScene, FbxNode *pNode, const std:
rawVertexIndices[vertexIndex] = raw.AddVertex(rawVertices[vertexIndex]); rawVertexIndices[vertexIndex] = raw.AddVertex(rawVertices[vertexIndex]);
} }
const RawMaterialType materialType = GetMaterialType(raw, textures, vertexTransparency, skinning.IsSkinned());
const int rawMaterialIndex = raw.AddMaterial(
materialName, shadingModel, materialType, textures,
toVec3f(ambient), toVec4f(diffuse), toVec3f(specular), toVec3f(emissive), shininess);
raw.AddTriangle(rawVertexIndices[0], rawVertexIndices[1], rawVertexIndices[2], rawMaterialIndex, rawSurfaceIndex); raw.AddTriangle(rawVertexIndices[0], rawVertexIndices[1], rawVertexIndices[2], rawMaterialIndex, rawSurfaceIndex);
} }
} }

View File

@ -410,8 +410,6 @@ ModelData *Raw2Gltf(
for (int materialIndex = 0; materialIndex < raw.GetMaterialCount(); materialIndex++) { for (int materialIndex = 0; materialIndex < raw.GetMaterialCount(); materialIndex++) {
const RawMaterial &material = raw.GetMaterial(materialIndex); const RawMaterial &material = raw.GetMaterial(materialIndex);
const bool isTransparent = const bool isTransparent =
material.type == RAW_MATERIAL_TYPE_VERTEX_COLORED ||
material.type == RAW_MATERIAL_TYPE_SKINNED_VERTEX_COLORED ||
material.type == RAW_MATERIAL_TYPE_TRANSPARENT || material.type == RAW_MATERIAL_TYPE_TRANSPARENT ||
material.type == RAW_MATERIAL_TYPE_SKINNED_TRANSPARENT; material.type == RAW_MATERIAL_TYPE_SKINNED_TRANSPARENT;

View File

@ -136,10 +136,8 @@ enum RawMaterialType
{ {
RAW_MATERIAL_TYPE_OPAQUE, RAW_MATERIAL_TYPE_OPAQUE,
RAW_MATERIAL_TYPE_TRANSPARENT, RAW_MATERIAL_TYPE_TRANSPARENT,
RAW_MATERIAL_TYPE_VERTEX_COLORED,
RAW_MATERIAL_TYPE_SKINNED_OPAQUE, RAW_MATERIAL_TYPE_SKINNED_OPAQUE,
RAW_MATERIAL_TYPE_SKINNED_TRANSPARENT, RAW_MATERIAL_TYPE_SKINNED_TRANSPARENT,
RAW_MATERIAL_TYPE_SKINNED_VERTEX_COLORED
}; };
struct RawMaterial struct RawMaterial