private static void FromAssimpAlphaTexture(Module.Import.Assimp.Context context, Material material, aiMaterial material_data, aiScene scene, string unity_property, aiTextureType texture_type, Color default_color, Func <Color, float> op) { if (material_data.GetTextureCount(texture_type) > 0) { using (aiString texture_name = new aiString()) { if (material_data.GetTexturePath(texture_type, 0, texture_name)) { Texture alpha = new Texture(context, texture_name.ToString(), scene); Texture base_tex = null; Material.TextureParams param = material.GetTextureParams(unity_property); if (param != null) { CLARTE.Backport.Tuple <Texture, uint> res = context.scene.GetAssimpTexture(param.index); if (res != null) { base_tex = res.Item1; } else { Debug.LogErrorFormat("Invalid texture index. '{0}' was registered for material '{1}' as texture with index '{2}'. However no texture was found with this index.", unity_property, material.Name, param.index); } } else { CLARTE.Backport.Tuple <Texture, uint> assimp_tex = context.scene.GetAssimpTexture(Guid.NewGuid().ToString(), () => new Texture("**E", alpha.width, alpha.height, default_color)); material.AddTextureParams(unity_property, new Material.TextureParams(assimp_tex.Item2)); base_tex = assimp_tex.Item1; } if (base_tex != null) { base_tex.AddToAlpha(alpha, op); base_tex.filename = string.Format("**A{0}|{1}", base_tex.filename, texture_name.C_Str()); } } } } }
private static void FromAssimpNormalsFromHeightmap(Module.Import.Assimp.Context context, Material material, aiMaterial material_data, aiScene scene) { if (material_data.GetTextureCount(Assimp.Convert.textureTypes[Assimp.Convert.unityBumpName]) <= 0) { using (aiString texture_name = new aiString()) { if (material_data.GetTexturePath(aiTextureType.aiTextureType_HEIGHT, 0, texture_name)) { string filename = string.Format("**N{0}", texture_name.C_Str()); material.AddTextureParams(Assimp.Convert.unityBumpName, new Material.TextureParams(context.scene.GetAssimpTexture(filename, () => { Texture texture = new Texture(context, texture_name.C_Str(), scene).HeightmapToNormals(0.5).Blur(0.5); texture.filename = filename; return(texture); }).Item2)); } } } }
public static void FromAssimp(Module.Import.Assimp.Context context, Material material, aiScene scene, aiMaterial material_data, string texture_key, aiTextureType texture_type, float reflectivity) { if (texture_type != aiTextureType.aiTextureType_NONE && material_data.GetTextureCount(texture_type) > 0) { using (aiString texture_name = new aiString()) { if (material_data.GetTexturePath(texture_type, 0, texture_name)) { string filename = texture_name.C_Str(); uint index = context.scene.GetAssimpTexture(filename, () => new Texture(context, filename, scene)).Item2; material.AddTextureParams(texture_key, new Material.TextureParams(index)); context.progress.Update(ASSIMP_PROGRESS_FACTOR); } } } Color default_color; // Add textures as alpha channel of existing textures, or compute normal map from heightmap if not defined. switch (texture_key) { case Assimp.Convert.unityMainTexName: default_color = Color.white; Color?diffuse = material.GetColor(Assimp.Convert.unityDiffuseColorName); if (diffuse.HasValue) { default_color = diffuse.Value; } // Opacity as main texture alpha channel FromAssimpAlphaTexture(context, material, material_data, scene, Assimp.Convert.unityMainTexName, aiTextureType.aiTextureType_OPACITY, default_color, c => c.grayscale); break; case Assimp.Convert.unityMetallicGlossName: default_color = Color.black; float?metallic = material.GetFloat(Assimp.Convert.unityMetallicValueName); if (metallic.HasValue) { default_color = new Color(metallic.Value, metallic.Value, metallic.Value, 1f); } // Shininess as alpha channel of metallic gloss map FromAssimpAlphaTexture(context, material, material_data, scene, Assimp.Convert.unityMetallicGlossName, aiTextureType.aiTextureType_SHININESS, default_color, c => Material.Smoothness(c.grayscale, reflectivity)); break; case Assimp.Convert.unitySpecGlossName: default_color = Color.black; Color?specular = material.GetColor(Assimp.Convert.unitySpecularColorName); if (specular.HasValue) { default_color = specular.Value; } // Shininess as alpha channel of specular gloss map FromAssimpAlphaTexture(context, material, material_data, scene, Assimp.Convert.unitySpecGlossName, aiTextureType.aiTextureType_SHININESS, default_color, c => Material.Smoothness(c.grayscale, reflectivity)); break; case Assimp.Convert.unityBumpName: // Bump mapping from heightmap if not defined from normals FromAssimpNormalsFromHeightmap(context, material, material_data, scene); break; } }