ImageAsset.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. }
  88. //-----------------------------------------------------------------------------
  89. ImageAsset::~ImageAsset()
  90. {
  91. }
  92. //-----------------------------------------------------------------------------
  93. void ImageAsset::initPersistFields()
  94. {
  95. // Call parent.
  96. Parent::initPersistFields();
  97. addProtectedField("imageFile", TypeAssetLooseFilePath, Offset(mImageFileName, ImageAsset),
  98. &setImageFileName, &getImageFileName, "Path to the image file.");
  99. addField("useMips", TypeBool, Offset(mUseMips, ImageAsset), "Should the image use mips? (Currently unused).");
  100. addField("isHDRImage", TypeBool, Offset(mIsHDRImage, ImageAsset), "Is the image in an HDR format? (Currently unused)");
  101. addField("imageType", TypeImageAssetType, Offset(mImageType, ImageAsset), "What the main use-case for the image is for.");
  102. }
  103. //------------------------------------------------------------------------------
  104. //Utility function to 'fill out' bindings and resources with a matching asset if one exists
  105. bool ImageAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset)
  106. {
  107. AssetQuery query;
  108. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  109. if (foundAssetcount == 0)
  110. {
  111. //Didn't find any assets
  112. //If possible, see if we can run an in-place import and the get the asset from that
  113. #if TORQUE_DEBUG
  114. Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
  115. #endif
  116. AssetImporter* autoAssetImporter;
  117. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  118. {
  119. autoAssetImporter = new AssetImporter();
  120. autoAssetImporter->registerObject("autoAssetImporter");
  121. }
  122. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  123. if (resultingAssetId != StringTable->EmptyString())
  124. {
  125. imageAsset->setAssetId(resultingAssetId);
  126. if (!imageAsset->isNull())
  127. return true;
  128. }
  129. //Didn't work, so have us fall back to a placeholder asset
  130. imageAsset->setAssetId(StringTable->insert("Core_Rendering:noImage"));
  131. if (!imageAsset->isNull())
  132. return true;
  133. //That didn't work, so fail out
  134. return false;
  135. }
  136. else
  137. {
  138. //acquire and bind the asset, and return it out
  139. imageAsset->setAssetId(query.mAssetList[0]);
  140. return true;
  141. }
  142. }
  143. StringTableEntry ImageAsset::getAssetIdByFilename(StringTableEntry fileName)
  144. {
  145. StringTableEntry imageAssetId = StringTable->EmptyString();
  146. AssetQuery query;
  147. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  148. if (foundAssetcount == 0)
  149. {
  150. //Didn't find any assets
  151. //If possible, see if we can run an in-place import and the get the asset from that
  152. #if TORQUE_DEBUG
  153. Con::warnf("ImageAsset::getAssetByFilename - Attempted to in-place import a image file(%s) that had no associated asset", fileName);
  154. #endif
  155. AssetImporter* autoAssetImporter;
  156. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  157. {
  158. autoAssetImporter = new AssetImporter();
  159. autoAssetImporter->registerObject("autoAssetImporter");
  160. }
  161. StringTableEntry resultingAssetId = autoAssetImporter->autoImportFile(fileName);
  162. if (resultingAssetId != StringTable->EmptyString())
  163. {
  164. imageAssetId = resultingAssetId;
  165. return imageAssetId;
  166. }
  167. //Didn't work, so have us fall back to a placeholder asset
  168. imageAssetId = StringTable->insert("Core_Rendering:noImage");
  169. }
  170. else
  171. {
  172. //acquire and bind the asset, and return it out
  173. imageAssetId = query.mAssetList[0];
  174. }
  175. return imageAssetId;
  176. }
  177. //------------------------------------------------------------------------------
  178. void ImageAsset::copyTo(SimObject* object)
  179. {
  180. // Call to parent.
  181. Parent::copyTo(object);
  182. }
  183. void ImageAsset::loadImage()
  184. {
  185. SAFE_DELETE(mImage);
  186. if (mImageFileName)
  187. {
  188. if (!Platform::isFile(mImageFileName))
  189. {
  190. Con::errorf("ImageAsset::initializeAsset: Attempted to load file %s but it was not valid!", mImageFileName);
  191. return;
  192. }
  193. mImage.set(mImageFileName, &GFXStaticTextureSRGBProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  194. if (mImage)
  195. {
  196. mIsValidImage = true;
  197. return;
  198. }
  199. }
  200. mIsValidImage = false;
  201. }
  202. void ImageAsset::initializeAsset()
  203. {
  204. mImageFileName = expandAssetFilePath(mImageFileName);
  205. loadImage();
  206. }
  207. void ImageAsset::onAssetRefresh()
  208. {
  209. setImageFileName(mImageFileName);
  210. }
  211. void ImageAsset::setImageFileName(const char* pScriptFile)
  212. {
  213. // Sanity!
  214. AssertFatal(pScriptFile != NULL, "Cannot use a NULL image file.");
  215. // Update.
  216. mImageFileName = StringTable->insert(pScriptFile);
  217. }
  218. GFXTexHandle ImageAsset::getImage(GFXTextureProfile requestedProfile)
  219. {
  220. /*if (mResourceMap.contains(requestedProfile))
  221. {
  222. return mResourceMap.find(requestedProfile)->value;
  223. }
  224. else
  225. {
  226. //If we don't have an existing map case to the requested format, we'll just create it and insert it in
  227. GFXTexHandle newImage;
  228. newImage.set(mImageFileName, &requestedProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  229. mResourceMap.insert(requestedProfile, newImage);
  230. return newImage;
  231. }*/
  232. return nullptr;
  233. }
  234. const char* ImageAsset::getImageInfo()
  235. {
  236. if (mIsValidImage)
  237. {
  238. static const U32 bufSize = 2048;
  239. char* returnBuffer = Con::getReturnBuffer(bufSize);
  240. dSprintf(returnBuffer, bufSize, "%s %d %d %d", GFXStringTextureFormat[mImage.getFormat()], mImage.getHeight(), mImage.getWidth(), mImage.getDepth());
  241. return returnBuffer;
  242. }
  243. return "";
  244. }
  245. const char* ImageAsset::getImageTypeNameFromType(ImageAsset::ImageTypes type)
  246. {
  247. // must match ImageTypes order
  248. static const char* _names[] = {
  249. "Albedo",
  250. "Normal",
  251. "PBRConfig",
  252. "GUI",
  253. "Roughness",
  254. "AO",
  255. "Metalness",
  256. "Glow",
  257. "Particle",
  258. "Decal",
  259. "Cubemap"
  260. };
  261. if (type < 0 || type >= ImageTypeCount)
  262. {
  263. Con::errorf("ImageAsset::getAdapterNameFromType - Invalid ImageType, defaulting to Albedo");
  264. return _names[Albedo];
  265. }
  266. return _names[type];
  267. }
  268. ImageAsset::ImageTypes ImageAsset::getImageTypeFromName(const char* name)
  269. {
  270. S32 ret = -1;
  271. for (S32 i = 0; i < ImageTypeCount; i++)
  272. {
  273. if (!dStricmp(getImageTypeNameFromType((ImageTypes)i), name))
  274. ret = i;
  275. }
  276. if (ret == -1)
  277. {
  278. Con::errorf("ImageAsset::getImageTypeFromName - Invalid ImageType name, defaulting to Albedo");
  279. ret = Albedo;
  280. }
  281. return (ImageTypes)ret;
  282. }
  283. DefineEngineMethod(ImageAsset, getImageFilename, const char*, (), ,
  284. "Creates an instance of the given GameObject given the asset definition.\n"
  285. "@return The GameObject entity created from the asset.")
  286. {
  287. return object->getImageFileName();
  288. }
  289. DefineEngineMethod(ImageAsset, getImageInfo, const char*, (), ,
  290. "Creates an instance of the given GameObject given the asset definition.\n"
  291. "@return The GameObject entity created from the asset.")
  292. {
  293. return object->getImageInfo();
  294. }