public static ModelPack ConvertFromAssimpScene(string filePath, ModelPackConverterOptions options) { // For importing textures string baseDirectoryPath = Path.GetDirectoryName(filePath); // Import scene var aiScene = AssimpSceneImporter.ImportFile(filePath); // Build textures & Materials var model = new ModelPack(options.Version); model.Textures = new TextureDictionary(options.Version); model.Materials = new MaterialDictionary(options.Version); foreach (var aiSceneMaterial in aiScene.Materials) { var material = ConvertMaterialAndTextures(aiSceneMaterial, options, baseDirectoryPath, model.Textures); model.Materials.Add(material); } // Create scene var sceneConverterOptions = new ModelConverterOptions() { Version = options.Version, ConvertSkinToZUp = options.ConvertSkinToZUp, GenerateVertexColors = options.GenerateVertexColors, MinimalVertexAttributes = options.MinimalVertexAttributes, }; model.Model = ModelConverter.ConvertFromAssimpScene(aiScene, sceneConverterOptions); return(model); }
private static Material ConvertMaterialAndTextures(Ai.Material aiMaterial, ModelPackConverterOptions options, string baseDirectoryPath, TextureDictionary textureDictionary) { // Convert all textures TextureInfo diffuseTexture = null; if (aiMaterial.HasTextureDiffuse) { diffuseTexture = ConvertTexture(aiMaterial.TextureDiffuse, baseDirectoryPath); } TextureInfo lightmapTexture = null; if (aiMaterial.HasTextureLightMap) { lightmapTexture = ConvertTexture(aiMaterial.TextureLightMap, baseDirectoryPath); } TextureInfo displacementTexture = null; if (aiMaterial.HasTextureDisplacement) { displacementTexture = ConvertTexture(aiMaterial.TextureDisplacement, baseDirectoryPath); } TextureInfo opacityTexture = null; if (aiMaterial.HasTextureOpacity) { opacityTexture = ConvertTexture(aiMaterial.TextureOpacity, baseDirectoryPath); } TextureInfo normalTexture = null; if (aiMaterial.HasTextureNormal) { normalTexture = ConvertTexture(aiMaterial.TextureNormal, baseDirectoryPath); } TextureInfo heightTexture = null; if (aiMaterial.HasTextureHeight) { heightTexture = ConvertTexture(aiMaterial.TextureHeight, baseDirectoryPath); } TextureInfo emissiveTexture = null; if (aiMaterial.HasTextureEmissive) { emissiveTexture = ConvertTexture(aiMaterial.TextureEmissive, baseDirectoryPath); } TextureInfo ambientTexture = null; if (aiMaterial.HasTextureAmbient) { ambientTexture = ConvertTexture(aiMaterial.TextureAmbient, baseDirectoryPath); } TextureInfo specularTexture = null; if (aiMaterial.HasTextureSpecular) { specularTexture = ConvertTexture(aiMaterial.TextureSpecular, baseDirectoryPath); } TextureInfo reflectionTexture = null; if (aiMaterial.HasTextureReflection) { reflectionTexture = ConvertTexture(aiMaterial.TextureReflection, baseDirectoryPath); } // Convert material Material material = null; string materialName = AssimpConverterCommon.UnescapeName(aiMaterial.Name); switch (options.MaterialPreset) { case MaterialPreset.FieldTerrain: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateFieldTerrainMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; case MaterialPreset.FieldTerrainVertexColors: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateFieldTerrainVertexColorsMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; case MaterialPreset.FieldTerrainCastShadow: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateFieldTerrainCastShadowMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; case MaterialPreset.CharacterSkinP5: case MaterialPreset.CharacterSkinFB: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); string shadowTextureName = diffuseTexture.Name; if (ambientTexture != null) { textureDictionary.Add(ambientTexture.Texture); shadowTextureName = ambientTexture.Name; } // TODO: transparency var hasTransparency = HasAlpha(diffuseTexture.PixelFormat); if (options.MaterialPreset == MaterialPreset.CharacterSkinP5) { material = MaterialFactory.CreateCharacterSkinP5Material(materialName, diffuseTexture.Name, shadowTextureName, hasTransparency); } else { material = MaterialFactory.CreateCharacterSkinFBMaterial(materialName, diffuseTexture.Name, shadowTextureName, hasTransparency); } } } break; case MaterialPreset.PersonaSkinP5: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); string shadowTextureName = diffuseTexture.Name; string specularTextureName = diffuseTexture.Name; if (ambientTexture != null) { textureDictionary.Add(ambientTexture.Texture); shadowTextureName = ambientTexture.Name; } if (specularTexture != null) { textureDictionary.Add(specularTexture.Texture); specularTextureName = specularTexture.Name; } material = MaterialFactory.CreatePersonaSkinP5Material(materialName, diffuseTexture.Name, specularTextureName, shadowTextureName); } } break; case MaterialPreset.CharacterClothP4D: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateCharacterClothP4DMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; case MaterialPreset.CharacterSkinP3DP5D: { if (diffuseTexture != null) { textureDictionary.Add(diffuseTexture.Texture); material = MaterialFactory.CreateCharacterSkinP3DP5DMaterial(materialName, diffuseTexture.Name, HasAlpha(diffuseTexture.PixelFormat)); } } break; } // Create dummy material if none was created if (material == null) { material = new Material(materialName); } return(material); }