Browse Source

Improved handling of transparency in collada importer
+ started preliminary support for RGB_ZERO collada transparency mode
+ added option to manually invert transparency values to deal with broken exporters that don't follow the specs

Léo Terziman 10 years ago
parent
commit
d9a2293491
5 changed files with 36 additions and 6 deletions
  1. 5 1
      code/ColladaHelper.h
  2. 20 5
      code/ColladaLoader.cpp
  3. 1 0
      code/ColladaLoader.h
  4. 8 0
      code/ColladaParser.cpp
  5. 2 0
      include/assimp/config.h

+ 5 - 1
code/ColladaHelper.h

@@ -502,6 +502,8 @@ struct Effect
 	// Scalar factory
 	float mShininess, mRefractIndex, mReflectivity;
 	float mTransparency;
+	bool mHasTransparency;
+	bool mRGBTransparency;
 
 	// local params referring to each other by their SID
 	typedef std::map<std::string, Collada::EffectParam> ParamLibrary;
@@ -522,7 +524,9 @@ struct Effect
 		, mShininess    (10.0f)
 		, mRefractIndex (1.f)
 		, mReflectivity (1.f)
-		, mTransparency (0.f)
+		, mTransparency (1.f)
+		, mHasTransparency (false)
+		, mRGBTransparency(false)
 		, mDoubleSided	(false)
 		, mWireframe    (false)
 		, mFaceted      (false)

+ 20 - 5
code/ColladaLoader.cpp

@@ -109,6 +109,7 @@ void ColladaLoader::SetupProperties(const Importer* pImp)
 {
 	noSkeletonMesh = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_NO_SKELETON_MESHES,0) != 0;
 	ignoreUpDirection = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION,0) != 0;
+	invertTransparency = pImp->GetPropertyInteger(AI_CONFIG_IMPORT_COLLADA_INVERT_TRANSPARENCY,0) != 0;
 }
 
 
@@ -1289,11 +1290,25 @@ void ColladaLoader::FillMaterials( const ColladaParser& pParser, aiScene* /*pSce
 		mat.AddProperty( &effect.mRefractIndex, 1, AI_MATKEY_REFRACTI);
 
 		// transparency, a very hard one. seemingly not all files are following the
-		// specification here .. but we can trick.
-		if (effect.mTransparency >= 0.f && effect.mTransparency < 1.f) {
-			effect.mTransparency = 1.f- effect.mTransparency;
-			mat.AddProperty( &effect.mTransparency, 1, AI_MATKEY_OPACITY );
-			mat.AddProperty( &effect.mTransparent, 1, AI_MATKEY_COLOR_TRANSPARENT );
+		// specification here (1.0 transparency => completly opaque)...
+		// therefore, we let the opportunity for the user to manually invert
+		// the transparency if necessary and we add preliminary support for RGB_ZERO mode
+		if(effect.mTransparency >= 0.f && effect.mTransparency <= 1.f) {
+			// Trying some support for RGB_ZERO mode
+			if(effect.mRGBTransparency) {
+				effect.mTransparency = 1.f - effect.mTransparent.a;
+			}
+			
+			// Global option
+			if(invertTransparency) {
+				effect.mTransparency = 1.f - effect.mTransparency;
+			}
+
+			// Is the material finally transparent ?
+			if (effect.mHasTransparency || effect.mTransparency < 1.f) {
+				mat.AddProperty( &effect.mTransparency, 1, AI_MATKEY_OPACITY );
+				mat.AddProperty( &effect.mTransparent, 1, AI_MATKEY_COLOR_TRANSPARENT );
+			}
 		}
 
 		// add textures, if given

+ 1 - 0
code/ColladaLoader.h

@@ -235,6 +235,7 @@ protected:
 
 	bool noSkeletonMesh;
 	bool ignoreUpDirection;
+	bool invertTransparency;
 
 	/** Used by FindNameForNode() to generate unique node names */
 	unsigned int mNodeNameCounter;

+ 8 - 0
code/ColladaParser.cpp

@@ -1199,6 +1199,14 @@ void ColladaParser::ReadEffectProfileCommon( Collada::Effect& pEffect)
 				ReadEffectColor( pEffect.mReflective, pEffect.mTexReflective);
 			}
 			else if( IsElement( "transparent")) {
+				pEffect.mHasTransparency = true;
+
+				// In RGB_ZERO mode, the transparency is interpreted in reverse, go figure...
+				if(::strcmp(mReader->getAttributeValueSafe("opaque"), "RGB_ZERO") == 0) {
+					// TODO: handle RGB_ZERO mode completely
+					pEffect.mRGBTransparency = true;
+				}
+
 				ReadEffectColor( pEffect.mTransparent,pEffect.mTexTransparent);
 			}
 			else if( IsElement( "shininess"))

+ 2 - 0
include/assimp/config.h

@@ -878,4 +878,6 @@ enum aiComponent
 
 #define AI_CONFIG_IMPORT_COLLADA_IGNORE_UP_DIRECTION "IMPORT_COLLADA_IGNORE_UP_DIRECTION"
 
+#define AI_CONFIG_IMPORT_COLLADA_INVERT_TRANSPARENCY "IMPORT_COLLADA_INVERT_TRANSPARENCY"
+
 #endif // !! AI_CONFIG_H_INC