import_utils.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*************************************************************************/
  2. /* import_utils.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef IMPORT_UTILS_IMPORTER_ASSIMP_H
  31. #define IMPORT_UTILS_IMPORTER_ASSIMP_H
  32. #include "core/io/image_loader.h"
  33. #include "import_state.h"
  34. #include <assimp/SceneCombiner.h>
  35. #include <assimp/cexport.h>
  36. #include <assimp/cimport.h>
  37. #include <assimp/matrix4x4.h>
  38. #include <assimp/pbrmaterial.h>
  39. #include <assimp/postprocess.h>
  40. #include <assimp/scene.h>
  41. #include <assimp/DefaultLogger.hpp>
  42. #include <assimp/Importer.hpp>
  43. #include <assimp/LogStream.hpp>
  44. #include <assimp/Logger.hpp>
  45. #include <string>
  46. using namespace AssimpImporter;
  47. #define AI_PROPERTIES aiTextureType_UNKNOWN, 0
  48. #define AI_NULL 0, 0
  49. #define AI_MATKEY_FBX_MAYA_BASE_COLOR_FACTOR "$raw.Maya|baseColor"
  50. #define AI_MATKEY_FBX_MAYA_METALNESS_FACTOR "$raw.Maya|metalness"
  51. #define AI_MATKEY_FBX_MAYA_DIFFUSE_ROUGHNESS_FACTOR "$raw.Maya|diffuseRoughness"
  52. #define AI_MATKEY_FBX_MAYA_EMISSION_TEXTURE "$raw.Maya|emissionColor|file"
  53. #define AI_MATKEY_FBX_MAYA_EMISSIVE_FACTOR "$raw.Maya|emission"
  54. #define AI_MATKEY_FBX_MAYA_METALNESS_TEXTURE "$raw.Maya|metalness|file"
  55. #define AI_MATKEY_FBX_MAYA_METALNESS_UV_XFORM "$raw.Maya|metalness|uvtrafo"
  56. #define AI_MATKEY_FBX_MAYA_DIFFUSE_ROUGHNESS_TEXTURE "$raw.Maya|diffuseRoughness|file"
  57. #define AI_MATKEY_FBX_MAYA_DIFFUSE_ROUGHNESS_UV_XFORM "$raw.Maya|diffuseRoughness|uvtrafo"
  58. #define AI_MATKEY_FBX_MAYA_BASE_COLOR_TEXTURE "$raw.Maya|baseColor|file"
  59. #define AI_MATKEY_FBX_MAYA_BASE_COLOR_UV_XFORM "$raw.Maya|baseColor|uvtrafo"
  60. #define AI_MATKEY_FBX_MAYA_NORMAL_TEXTURE "$raw.Maya|normalCamera|file"
  61. #define AI_MATKEY_FBX_MAYA_NORMAL_UV_XFORM "$raw.Maya|normalCamera|uvtrafo"
  62. #define AI_MATKEY_FBX_NORMAL_TEXTURE "$raw.Maya|normalCamera|file"
  63. #define AI_MATKEY_FBX_NORMAL_UV_XFORM "$raw.Maya|normalCamera|uvtrafo"
  64. #define AI_MATKEY_FBX_MAYA_STINGRAY_DISPLACEMENT_SCALING_FACTOR "$raw.Maya|displacementscaling"
  65. #define AI_MATKEY_FBX_MAYA_STINGRAY_BASE_COLOR_FACTOR "$raw.Maya|base_color"
  66. #define AI_MATKEY_FBX_MAYA_STINGRAY_EMISSIVE_FACTOR "$raw.Maya|emissive"
  67. #define AI_MATKEY_FBX_MAYA_STINGRAY_METALLIC_FACTOR "$raw.Maya|metallic"
  68. #define AI_MATKEY_FBX_MAYA_STINGRAY_ROUGHNESS_FACTOR "$raw.Maya|roughness"
  69. #define AI_MATKEY_FBX_MAYA_STINGRAY_EMISSIVE_INTENSITY_FACTOR "$raw.Maya|emissive_intensity"
  70. #define AI_MATKEY_FBX_MAYA_STINGRAY_NORMAL_TEXTURE "$raw.Maya|TEX_normal_map|file"
  71. #define AI_MATKEY_FBX_MAYA_STINGRAY_NORMAL_UV_XFORM "$raw.Maya|TEX_normal_map|uvtrafo"
  72. #define AI_MATKEY_FBX_MAYA_STINGRAY_COLOR_TEXTURE "$raw.Maya|TEX_color_map|file"
  73. #define AI_MATKEY_FBX_MAYA_STINGRAY_COLOR_UV_XFORM "$raw.Maya|TEX_color_map|uvtrafo"
  74. #define AI_MATKEY_FBX_MAYA_STINGRAY_METALLIC_TEXTURE "$raw.Maya|TEX_metallic_map|file"
  75. #define AI_MATKEY_FBX_MAYA_STINGRAY_METALLIC_UV_XFORM "$raw.Maya|TEX_metallic_map|uvtrafo"
  76. #define AI_MATKEY_FBX_MAYA_STINGRAY_ROUGHNESS_TEXTURE "$raw.Maya|TEX_roughness_map|file"
  77. #define AI_MATKEY_FBX_MAYA_STINGRAY_ROUGHNESS_UV_XFORM "$raw.Maya|TEX_roughness_map|uvtrafo"
  78. #define AI_MATKEY_FBX_MAYA_STINGRAY_EMISSIVE_TEXTURE "$raw.Maya|TEX_emissive_map|file"
  79. #define AI_MATKEY_FBX_MAYA_STINGRAY_EMISSIVE_UV_XFORM "$raw.Maya|TEX_emissive_map|uvtrafo"
  80. #define AI_MATKEY_FBX_MAYA_STINGRAY_AO_TEXTURE "$raw.Maya|TEX_ao_map|file"
  81. #define AI_MATKEY_FBX_MAYA_STINGRAY_AO_UV_XFORM "$raw.Maya|TEX_ao_map|uvtrafo"
  82. /**
  83. * Assimp Utils
  84. * Conversion tools / glue code to convert from assimp to godot
  85. */
  86. class AssimpUtils {
  87. public:
  88. /**
  89. * calculate tangents for mesh data from assimp data
  90. */
  91. static void calc_tangent_from_mesh(const aiMesh *ai_mesh, int i, int tri_index, int index, PoolColorArray::Write &w) {
  92. const aiVector3D normals = ai_mesh->mAnimMeshes[i]->mNormals[tri_index];
  93. const Vector3 godot_normal = Vector3(normals.x, normals.y, normals.z);
  94. const aiVector3D tangent = ai_mesh->mAnimMeshes[i]->mTangents[tri_index];
  95. const Vector3 godot_tangent = Vector3(tangent.x, tangent.y, tangent.z);
  96. const aiVector3D bitangent = ai_mesh->mAnimMeshes[i]->mBitangents[tri_index];
  97. const Vector3 godot_bitangent = Vector3(bitangent.x, bitangent.y, bitangent.z);
  98. float d = godot_normal.cross(godot_tangent).dot(godot_bitangent) > 0.0f ? 1.0f : -1.0f;
  99. Color plane_tangent = Color(tangent.x, tangent.y, tangent.z, d);
  100. w[index] = plane_tangent;
  101. }
  102. struct AssetImportFbx {
  103. enum ETimeMode {
  104. TIME_MODE_DEFAULT = 0,
  105. TIME_MODE_120 = 1,
  106. TIME_MODE_100 = 2,
  107. TIME_MODE_60 = 3,
  108. TIME_MODE_50 = 4,
  109. TIME_MODE_48 = 5,
  110. TIME_MODE_30 = 6,
  111. TIME_MODE_30_DROP = 7,
  112. TIME_MODE_NTSC_DROP_FRAME = 8,
  113. TIME_MODE_NTSC_FULL_FRAME = 9,
  114. TIME_MODE_PAL = 10,
  115. TIME_MODE_CINEMA = 11,
  116. TIME_MODE_1000 = 12,
  117. TIME_MODE_CINEMA_ND = 13,
  118. TIME_MODE_CUSTOM = 14,
  119. TIME_MODE_TIME_MODE_COUNT = 15
  120. };
  121. enum UpAxis {
  122. UP_VECTOR_AXIS_X = 1,
  123. UP_VECTOR_AXIS_Y = 2,
  124. UP_VECTOR_AXIS_Z = 3
  125. };
  126. enum FrontAxis {
  127. FRONT_PARITY_EVEN = 1,
  128. FRONT_PARITY_ODD = 2,
  129. };
  130. enum CoordAxis {
  131. COORD_RIGHT = 0,
  132. COORD_LEFT = 1
  133. };
  134. };
  135. /** Get assimp string
  136. * automatically filters the string data
  137. */
  138. static String get_assimp_string(const aiString &p_string) {
  139. //convert an assimp String to a Godot String
  140. String name;
  141. name.parse_utf8(p_string.C_Str() /*,p_string.length*/);
  142. if (name.find(":") != -1) {
  143. String replaced_name = name.split(":")[1];
  144. print_verbose("Replacing " + name + " containing : with " + replaced_name);
  145. name = replaced_name;
  146. }
  147. return name;
  148. }
  149. static String get_anim_string_from_assimp(const aiString &p_string) {
  150. String name;
  151. name.parse_utf8(p_string.C_Str() /*,p_string.length*/);
  152. if (name.find(":") != -1) {
  153. String replaced_name = name.split(":")[1];
  154. print_verbose("Replacing " + name + " containing : with " + replaced_name);
  155. name = replaced_name;
  156. }
  157. return name;
  158. }
  159. /**
  160. * No filter logic get_raw_string_from_assimp
  161. * This just convers the aiString to a parsed utf8 string
  162. * Without removing special chars etc
  163. */
  164. static String get_raw_string_from_assimp(const aiString &p_string) {
  165. String name;
  166. name.parse_utf8(p_string.C_Str() /*,p_string.length*/);
  167. return name;
  168. }
  169. static Ref<Animation> import_animation(const String &p_path, uint32_t p_flags, int p_bake_fps) {
  170. return Ref<Animation>();
  171. }
  172. /**
  173. * Converts aiMatrix4x4 to godot Transform
  174. */
  175. static const Transform assimp_matrix_transform(const aiMatrix4x4 p_matrix) {
  176. aiMatrix4x4 matrix = p_matrix;
  177. Transform xform;
  178. xform.set(matrix.a1, matrix.a2, matrix.a3, matrix.b1, matrix.b2, matrix.b3, matrix.c1, matrix.c2, matrix.c3, matrix.a4, matrix.b4, matrix.c4);
  179. return xform;
  180. }
  181. /** Get fbx fps for time mode meta data
  182. */
  183. static float get_fbx_fps(int32_t time_mode, const aiScene *p_scene) {
  184. switch (time_mode) {
  185. case AssetImportFbx::TIME_MODE_DEFAULT: return 24; //hack
  186. case AssetImportFbx::TIME_MODE_120: return 120;
  187. case AssetImportFbx::TIME_MODE_100: return 100;
  188. case AssetImportFbx::TIME_MODE_60: return 60;
  189. case AssetImportFbx::TIME_MODE_50: return 50;
  190. case AssetImportFbx::TIME_MODE_48: return 48;
  191. case AssetImportFbx::TIME_MODE_30: return 30;
  192. case AssetImportFbx::TIME_MODE_30_DROP: return 30;
  193. case AssetImportFbx::TIME_MODE_NTSC_DROP_FRAME: return 29.9700262f;
  194. case AssetImportFbx::TIME_MODE_NTSC_FULL_FRAME: return 29.9700262f;
  195. case AssetImportFbx::TIME_MODE_PAL: return 25;
  196. case AssetImportFbx::TIME_MODE_CINEMA: return 24;
  197. case AssetImportFbx::TIME_MODE_1000: return 1000;
  198. case AssetImportFbx::TIME_MODE_CINEMA_ND: return 23.976f;
  199. case AssetImportFbx::TIME_MODE_CUSTOM:
  200. int32_t frame_rate = -1;
  201. p_scene->mMetaData->Get("FrameRate", frame_rate);
  202. return frame_rate;
  203. }
  204. return 0;
  205. }
  206. /**
  207. * Get global transform for the current node - so we can use world space rather than
  208. * local space coordinates
  209. * useful if you need global - although recommend using local wherever possible over global
  210. * as you could break fbx scaling :)
  211. */
  212. static Transform _get_global_assimp_node_transform(const aiNode *p_current_node) {
  213. aiNode const *current_node = p_current_node;
  214. Transform xform;
  215. while (current_node != NULL) {
  216. xform = assimp_matrix_transform(current_node->mTransformation) * xform;
  217. current_node = current_node->mParent;
  218. }
  219. return xform;
  220. }
  221. /**
  222. * Find hardcoded textures from assimp which could be in many different directories
  223. */
  224. static void find_texture_path(const String &p_path, _Directory &dir, String &path, bool &found, String extension) {
  225. Vector<String> paths;
  226. paths.push_back(path.get_basename() + extension);
  227. paths.push_back(path + extension);
  228. paths.push_back(path);
  229. paths.push_back(p_path.get_base_dir().plus_file(path.get_file().get_basename() + extension));
  230. paths.push_back(p_path.get_base_dir().plus_file(path.get_file() + extension));
  231. paths.push_back(p_path.get_base_dir().plus_file(path.get_file()));
  232. paths.push_back(p_path.get_base_dir().plus_file("textures/" + path.get_file().get_basename() + extension));
  233. paths.push_back(p_path.get_base_dir().plus_file("textures/" + path.get_file() + extension));
  234. paths.push_back(p_path.get_base_dir().plus_file("textures/" + path.get_file()));
  235. paths.push_back(p_path.get_base_dir().plus_file("Textures/" + path.get_file().get_basename() + extension));
  236. paths.push_back(p_path.get_base_dir().plus_file("Textures/" + path.get_file() + extension));
  237. paths.push_back(p_path.get_base_dir().plus_file("Textures/" + path.get_file()));
  238. paths.push_back(p_path.get_base_dir().plus_file("../Textures/" + path.get_file() + extension));
  239. paths.push_back(p_path.get_base_dir().plus_file("../Textures/" + path.get_file().get_basename() + extension));
  240. paths.push_back(p_path.get_base_dir().plus_file("../Textures/" + path.get_file()));
  241. paths.push_back(p_path.get_base_dir().plus_file("../textures/" + path.get_file().get_basename() + extension));
  242. paths.push_back(p_path.get_base_dir().plus_file("../textures/" + path.get_file() + extension));
  243. paths.push_back(p_path.get_base_dir().plus_file("../textures/" + path.get_file()));
  244. paths.push_back(p_path.get_base_dir().plus_file("texture/" + path.get_file().get_basename() + extension));
  245. paths.push_back(p_path.get_base_dir().plus_file("texture/" + path.get_file() + extension));
  246. paths.push_back(p_path.get_base_dir().plus_file("texture/" + path.get_file()));
  247. paths.push_back(p_path.get_base_dir().plus_file("Texture/" + path.get_file().get_basename() + extension));
  248. paths.push_back(p_path.get_base_dir().plus_file("Texture/" + path.get_file() + extension));
  249. paths.push_back(p_path.get_base_dir().plus_file("Texture/" + path.get_file()));
  250. paths.push_back(p_path.get_base_dir().plus_file("../Texture/" + path.get_file() + extension));
  251. paths.push_back(p_path.get_base_dir().plus_file("../Texture/" + path.get_file().get_basename() + extension));
  252. paths.push_back(p_path.get_base_dir().plus_file("../Texture/" + path.get_file()));
  253. paths.push_back(p_path.get_base_dir().plus_file("../texture/" + path.get_file().get_basename() + extension));
  254. paths.push_back(p_path.get_base_dir().plus_file("../texture/" + path.get_file() + extension));
  255. paths.push_back(p_path.get_base_dir().plus_file("../texture/" + path.get_file()));
  256. for (int i = 0; i < paths.size(); i++) {
  257. if (dir.file_exists(paths[i])) {
  258. found = true;
  259. path = paths[i];
  260. return;
  261. }
  262. }
  263. }
  264. /** find the texture path for the supplied fbx path inside godot
  265. * very simple lookup for subfolders etc for a texture which may or may not be in a directory
  266. */
  267. static void find_texture_path(const String &r_p_path, String &r_path, bool &r_found) {
  268. _Directory dir;
  269. List<String> exts;
  270. ImageLoader::get_recognized_extensions(&exts);
  271. Vector<String> split_path = r_path.get_basename().split("*");
  272. if (split_path.size() == 2) {
  273. r_found = true;
  274. return;
  275. }
  276. if (dir.file_exists(r_p_path.get_base_dir() + r_path.get_file())) {
  277. r_path = r_p_path.get_base_dir() + r_path.get_file();
  278. r_found = true;
  279. return;
  280. }
  281. for (int32_t i = 0; i < exts.size(); i++) {
  282. if (r_found) {
  283. return;
  284. }
  285. find_texture_path(r_p_path, dir, r_path, r_found, "." + exts[i]);
  286. }
  287. }
  288. /**
  289. * set_texture_mapping_mode
  290. * Helper to check the mapping mode of the texture (repeat, clamp and mirror)
  291. */
  292. static void set_texture_mapping_mode(aiTextureMapMode *map_mode, Ref<ImageTexture> texture) {
  293. ERR_FAIL_COND(texture.is_null());
  294. ERR_FAIL_COND(map_mode == NULL);
  295. aiTextureMapMode tex_mode = map_mode[0];
  296. int32_t flags = Texture::FLAGS_DEFAULT;
  297. if (tex_mode == aiTextureMapMode_Wrap) {
  298. //Default
  299. } else if (tex_mode == aiTextureMapMode_Clamp) {
  300. flags = flags & ~Texture::FLAG_REPEAT;
  301. } else if (tex_mode == aiTextureMapMode_Mirror) {
  302. flags = flags | Texture::FLAG_MIRRORED_REPEAT;
  303. }
  304. texture->set_flags(flags);
  305. }
  306. /**
  307. * Load or load from cache image :)
  308. */
  309. static Ref<Image> load_image(ImportState &state, const aiScene *p_scene, String p_path) {
  310. Map<String, Ref<Image> >::Element *match = state.path_to_image_cache.find(p_path);
  311. // if our cache contains this image then don't bother
  312. if (match) {
  313. return match->get();
  314. }
  315. Vector<String> split_path = p_path.get_basename().split("*");
  316. if (split_path.size() == 2) {
  317. size_t texture_idx = split_path[1].to_int();
  318. ERR_FAIL_COND_V(texture_idx >= p_scene->mNumTextures, Ref<Image>());
  319. aiTexture *tex = p_scene->mTextures[texture_idx];
  320. String filename = AssimpUtils::get_raw_string_from_assimp(tex->mFilename);
  321. filename = filename.get_file();
  322. print_verbose("Open Asset Import: Loading embedded texture " + filename);
  323. if (tex->mHeight == 0) {
  324. if (tex->CheckFormat("png")) {
  325. ERR_FAIL_COND_V(Image::_png_mem_loader_func == NULL, Ref<Image>());
  326. Ref<Image> img = Image::_png_mem_loader_func((uint8_t *)tex->pcData, tex->mWidth);
  327. ERR_FAIL_COND_V(img.is_null(), Ref<Image>());
  328. state.path_to_image_cache.insert(p_path, img);
  329. return img;
  330. } else if (tex->CheckFormat("jpg")) {
  331. ERR_FAIL_COND_V(Image::_jpg_mem_loader_func == NULL, Ref<Image>());
  332. Ref<Image> img = Image::_jpg_mem_loader_func((uint8_t *)tex->pcData, tex->mWidth);
  333. ERR_FAIL_COND_V(img.is_null(), Ref<Image>());
  334. state.path_to_image_cache.insert(p_path, img);
  335. return img;
  336. } else if (tex->CheckFormat("dds")) {
  337. ERR_FAIL_COND_V_MSG(true, Ref<Image>(), "Open Asset Import: Embedded dds not implemented");
  338. }
  339. } else {
  340. Ref<Image> img;
  341. img.instance();
  342. PoolByteArray arr;
  343. uint32_t size = tex->mWidth * tex->mHeight;
  344. arr.resize(size);
  345. memcpy(arr.write().ptr(), tex->pcData, size);
  346. ERR_FAIL_COND_V(arr.size() % 4 != 0, Ref<Image>());
  347. //ARGB8888 to RGBA8888
  348. for (int32_t i = 0; i < arr.size() / 4; i++) {
  349. arr.write().ptr()[(4 * i) + 3] = arr[(4 * i) + 0];
  350. arr.write().ptr()[(4 * i) + 0] = arr[(4 * i) + 1];
  351. arr.write().ptr()[(4 * i) + 1] = arr[(4 * i) + 2];
  352. arr.write().ptr()[(4 * i) + 2] = arr[(4 * i) + 3];
  353. }
  354. img->create(tex->mWidth, tex->mHeight, true, Image::FORMAT_RGBA8, arr);
  355. ERR_FAIL_COND_V(img.is_null(), Ref<Image>());
  356. state.path_to_image_cache.insert(p_path, img);
  357. return img;
  358. }
  359. return Ref<Image>();
  360. } else {
  361. Ref<Texture> texture = ResourceLoader::load(p_path);
  362. ERR_FAIL_COND_V(texture.is_null(), Ref<Image>());
  363. Ref<Image> image = texture->get_data();
  364. ERR_FAIL_COND_V(image.is_null(), Ref<Image>());
  365. state.path_to_image_cache.insert(p_path, image);
  366. return image;
  367. }
  368. return Ref<Image>();
  369. }
  370. /* create texture from assimp data, if found in path */
  371. static bool CreateAssimpTexture(
  372. AssimpImporter::ImportState &state,
  373. aiString texture_path,
  374. String &filename,
  375. String &path,
  376. AssimpImageData &image_state) {
  377. filename = get_raw_string_from_assimp(texture_path);
  378. path = state.path.get_base_dir().plus_file(filename.replace("\\", "/"));
  379. bool found = false;
  380. find_texture_path(state.path, path, found);
  381. if (found) {
  382. image_state.raw_image = AssimpUtils::load_image(state, state.assimp_scene, path);
  383. if (image_state.raw_image.is_valid()) {
  384. image_state.texture.instance();
  385. image_state.texture->create_from_image(image_state.raw_image);
  386. image_state.texture->set_storage(ImageTexture::STORAGE_COMPRESS_LOSSY);
  387. return true;
  388. }
  389. }
  390. return false;
  391. }
  392. /** GetAssimpTexture
  393. * Designed to retrieve textures for you
  394. */
  395. static bool GetAssimpTexture(
  396. AssimpImporter::ImportState &state,
  397. aiMaterial *ai_material,
  398. aiTextureType texture_type,
  399. String &filename,
  400. String &path,
  401. AssimpImageData &image_state) {
  402. aiString ai_filename = aiString();
  403. if (AI_SUCCESS == ai_material->GetTexture(texture_type, 0, &ai_filename, NULL, NULL, NULL, NULL, image_state.map_mode)) {
  404. return CreateAssimpTexture(state, ai_filename, filename, path, image_state);
  405. }
  406. return false;
  407. }
  408. };
  409. #endif // IMPORT_UTILS_IMPORTER_ASSIMP_H