ImageAsset.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef IMAGE_ASSET_H
  23. #include "ImageAsset.h"
  24. #endif
  25. #ifndef _ASSET_MANAGER_H_
  26. #include "assets/assetManager.h"
  27. #endif
  28. #ifndef _CONSOLETYPES_H_
  29. #include "console/consoleTypes.h"
  30. #endif
  31. #ifndef _TAML_
  32. #include "persistence/taml/taml.h"
  33. #endif
  34. #ifndef _ASSET_PTR_H_
  35. #include "assets/assetPtr.h"
  36. #endif
  37. #include "gfx/gfxStringEnumTranslate.h"
  38. // Debug Profiling.
  39. #include "platform/profiler.h"
  40. #include "T3D/assets/assetImporter.h"
  41. //-----------------------------------------------------------------------------
  42. IMPLEMENT_CONOBJECT(ImageAsset);
  43. ConsoleType(ImageAssetPtr, TypeImageAssetPtr, String, ASSET_ID_FIELD_PREFIX)
  44. //-----------------------------------------------------------------------------
  45. ConsoleGetType(TypeImageAssetPtr)
  46. {
  47. // Fetch asset Id.
  48. return *((StringTableEntry*)dptr);
  49. }
  50. //-----------------------------------------------------------------------------
  51. ConsoleSetType(TypeImageAssetPtr)
  52. {
  53. // Was a single argument specified?
  54. if (argc == 1)
  55. {
  56. // Yes, so fetch field value.
  57. const char* pFieldValue = argv[0];
  58. // Fetch asset Id.
  59. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  60. // Update asset value.
  61. *assetId = StringTable->insert(pFieldValue);
  62. return;
  63. }
  64. // Warn.
  65. Con::warnf("(TypeImageAssetPtr) - Cannot set multiple args to a single asset.");
  66. }
  67. //-----------------------------------------------------------------------------
  68. ImplementEnumType(ImageAssetType,
  69. "Type of mesh data available in a shape.\n"
  70. "@ingroup gameObjects")
  71. { ImageAsset::Albedo, "Albedo", "" },
  72. { ImageAsset::Normal, "Normal", "" },
  73. { ImageAsset::PBRConfig, "PBRConfig", "" },
  74. { ImageAsset::GUI, "GUI", "" },
  75. { ImageAsset::Roughness, "Roughness", "" },
  76. { ImageAsset::AO, "AO", "" },
  77. { ImageAsset::Metalness, "Metalness", "" },
  78. { ImageAsset::Glow, "Glow", "" },
  79. { ImageAsset::Particle, "Particle", "" },
  80. { ImageAsset::Decal, "Decal", "" },
  81. { ImageAsset::Cubemap, "Cubemap", "" },
  82. EndImplementEnumType;
  83. //-----------------------------------------------------------------------------
  84. ImageAsset::ImageAsset() : AssetBase(), mImage(nullptr), mUseMips(true), mIsHDRImage(false), mIsValidImage(false), mImageType(Albedo)
  85. {
  86. mImageFileName = StringTable->EmptyString();
  87. mImagePath = StringTable->EmptyString();
  88. }
  89. //-----------------------------------------------------------------------------
  90. ImageAsset::~ImageAsset()
  91. {
  92. }
  93. //-----------------------------------------------------------------------------
  94. void ImageAsset::initPersistFields()
  95. {
  96. // Call parent.
  97. Parent::initPersistFields();
  98. addProtectedField("imageFile", TypeAssetLooseFilePath, Offset(mImageFileName, ImageAsset),
  99. &setImageFileName, &getImageFileName, "Path to the image file.");
  100. addField("useMips", TypeBool, Offset(mUseMips, ImageAsset), "Should the image use mips? (Currently unused).");
  101. addField("isHDRImage", TypeBool, Offset(mIsHDRImage, ImageAsset), "Is the image in an HDR format? (Currently unused)");
  102. addField("imageType", TypeImageAssetType, Offset(mImageType, ImageAsset), "What the main use-case for the image is for.");
  103. }
  104. //------------------------------------------------------------------------------
  105. //Utility function to 'fill out' bindings and resources with a matching asset if one exists
  106. bool ImageAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset)
  107. {
  108. AssetQuery query;
  109. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  110. if (foundAssetcount == 0)
  111. {
  112. //Didn't find any assets
  113. //If possible, see if we can run an in-place import and the get the asset from that
  114. #if TORQUE_DEBUG
  115. Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
  116. #endif
  117. AssetImporter* autoAssetImporter;
  118. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  119. {
  120. autoAssetImporter = new AssetImporter();
  121. autoAssetImporter->registerObject("autoAssetImporter");
  122. }
  123. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  124. if (resultingAssetId != StringTable->EmptyString())
  125. {
  126. imageAsset->setAssetId(resultingAssetId);
  127. if (!imageAsset->isNull())
  128. return true;
  129. }
  130. //Didn't work, so have us fall back to a placeholder asset
  131. imageAsset->setAssetId(StringTable->insert("Core_Rendering:noImage"));
  132. if (!imageAsset->isNull())
  133. return true;
  134. //That didn't work, so fail out
  135. return false;
  136. }
  137. else
  138. {
  139. //acquire and bind the asset, and return it out
  140. imageAsset->setAssetId(query.mAssetList[0]);
  141. return true;
  142. }
  143. }
  144. StringTableEntry ImageAsset::getAssetIdByFilename(StringTableEntry fileName)
  145. {
  146. StringTableEntry imageAssetId = StringTable->EmptyString();
  147. AssetQuery query;
  148. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  149. if (foundAssetcount == 0)
  150. {
  151. //Didn't find any assets
  152. //If possible, see if we can run an in-place import and the get the asset from that
  153. #if TORQUE_DEBUG
  154. Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
  155. #endif
  156. AssetImporter* autoAssetImporter;
  157. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  158. {
  159. autoAssetImporter = new AssetImporter();
  160. autoAssetImporter->registerObject("autoAssetImporter");
  161. }
  162. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  163. if (resultingAssetId != StringTable->EmptyString())
  164. {
  165. imageAssetId = resultingAssetId;
  166. return imageAssetId;
  167. }
  168. //Didn't work, so have us fall back to a placeholder asset
  169. imageAssetId = StringTable->insert("Core_Rendering:noImage");
  170. }
  171. else
  172. {
  173. //acquire and bind the asset, and return it out
  174. imageAssetId = query.mAssetList[0];
  175. }
  176. return imageAssetId;
  177. }
  178. //------------------------------------------------------------------------------
  179. void ImageAsset::copyTo(SimObject* object)
  180. {
  181. // Call to parent.
  182. Parent::copyTo(object);
  183. }
  184. void ImageAsset::loadImage()
  185. {
  186. SAFE_DELETE(mImage);
  187. if (mImageFileName)
  188. {
  189. if (!Platform::isFile(mImageFileName))
  190. {
  191. Con::errorf("ImageAsset::initializeAsset: Attempted to load file %s but it was not valid!", mImageFileName);
  192. return;
  193. }
  194. mImage.set(mImageFileName, &GFXStaticTextureSRGBProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  195. if (mImage)
  196. {
  197. mIsValidImage = true;
  198. return;
  199. }
  200. }
  201. mIsValidImage = false;
  202. }
  203. void ImageAsset::initializeAsset()
  204. {
  205. mImagePath = expandAssetFilePath(mImageFileName);
  206. loadImage();
  207. }
  208. void ImageAsset::onAssetRefresh()
  209. {
  210. mImagePath = expandAssetFilePath(mImageFileName);
  211. loadImage();
  212. }
  213. void ImageAsset::setImageFileName(const char* pScriptFile)
  214. {
  215. // Sanity!
  216. AssertFatal(pScriptFile != NULL, "Cannot use a NULL image file.");
  217. // Update.
  218. mImageFileName = StringTable->insert(pScriptFile);
  219. }
  220. GFXTexHandle ImageAsset::getImage(GFXTextureProfile requestedProfile)
  221. {
  222. /*if (mResourceMap.contains(requestedProfile))
  223. {
  224. return mResourceMap.find(requestedProfile)->value;
  225. }
  226. else
  227. {
  228. //If we don't have an existing map case to the requested format, we'll just create it and insert it in
  229. GFXTexHandle newImage;
  230. newImage.set(mImageFileName, &requestedProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  231. mResourceMap.insert(requestedProfile, newImage);
  232. return newImage;
  233. }*/
  234. return nullptr;
  235. }
  236. const char* ImageAsset::getImageInfo()
  237. {
  238. if (mIsValidImage)
  239. {
  240. static const U32 bufSize = 2048;
  241. char* returnBuffer = Con::getReturnBuffer(bufSize);
  242. dSprintf(returnBuffer, bufSize, "%s %d %d %d", GFXStringTextureFormat[mImage.getFormat()], mImage.getHeight(), mImage.getWidth(), mImage.getDepth());
  243. return returnBuffer;
  244. }
  245. return "";
  246. }
  247. const char* ImageAsset::getImageTypeNameFromType(ImageAsset::ImageTypes type)
  248. {
  249. // must match ImageTypes order
  250. static const char* _names[] = {
  251. "Albedo",
  252. "Normal",
  253. "PBRConfig",
  254. "GUI",
  255. "Roughness",
  256. "AO",
  257. "Metalness",
  258. "Glow",
  259. "Particle",
  260. "Decal",
  261. "Cubemap"
  262. };
  263. if (type < 0 || type >= ImageTypeCount)
  264. {
  265. Con::errorf("ImageAsset::getAdapterNameFromType - Invalid ImageType, defaulting to Albedo");
  266. return _names[Albedo];
  267. }
  268. return _names[type];
  269. }
  270. ImageAsset::ImageTypes ImageAsset::getImageTypeFromName(const char* name)
  271. {
  272. S32 ret = -1;
  273. for (S32 i = 0; i < ImageTypeCount; i++)
  274. {
  275. if (!dStricmp(getImageTypeNameFromType((ImageTypes)i), name))
  276. ret = i;
  277. }
  278. if (ret == -1)
  279. {
  280. Con::errorf("ImageAsset::getImageTypeFromName - Invalid ImageType name, defaulting to Albedo");
  281. ret = Albedo;
  282. }
  283. return (ImageTypes)ret;
  284. }
  285. DefineEngineMethod(ImageAsset, getImagePath, const char*, (), ,
  286. "Creates an instance of the given GameObject given the asset definition.\n"
  287. "@return The GameObject entity created from the asset.")
  288. {
  289. return object->getImagePath();
  290. }
  291. DefineEngineMethod(ImageAsset, getImageInfo, const char*, (), ,
  292. "Creates an instance of the given GameObject given the asset definition.\n"
  293. "@return The GameObject entity created from the asset.")
  294. {
  295. return object->getImageInfo();
  296. }