ImageAsset.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. //-----------------------------------------------------------------------------
  41. IMPLEMENT_CONOBJECT(ImageAsset);
  42. ConsoleType(ImageAssetPtr, TypeImageAssetPtr, String, ASSET_ID_FIELD_PREFIX)
  43. //-----------------------------------------------------------------------------
  44. ConsoleGetType(TypeImageAssetPtr)
  45. {
  46. // Fetch asset Id.
  47. return *((StringTableEntry*)dptr);
  48. }
  49. //-----------------------------------------------------------------------------
  50. ConsoleSetType(TypeImageAssetPtr)
  51. {
  52. // Was a single argument specified?
  53. if (argc == 1)
  54. {
  55. // Yes, so fetch field value.
  56. const char* pFieldValue = argv[0];
  57. // Fetch asset Id.
  58. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  59. // Update asset value.
  60. *assetId = StringTable->insert(pFieldValue);
  61. return;
  62. }
  63. // Warn.
  64. Con::warnf("(TypeImageAssetPtr) - Cannot set multiple args to a single asset.");
  65. }
  66. //-----------------------------------------------------------------------------
  67. ImplementEnumType(ImageAssetType,
  68. "Type of mesh data available in a shape.\n"
  69. "@ingroup gameObjects")
  70. { ImageAsset::Albedo, "Albedo", "" },
  71. { ImageAsset::Normal, "Normal", "" },
  72. { ImageAsset::Composite, "Composite", "" },
  73. { ImageAsset::GUI, "GUI", "" },
  74. { ImageAsset::Roughness, "Roughness", "" },
  75. { ImageAsset::AO, "AO", "" },
  76. { ImageAsset::Metalness, "Metalness", "" },
  77. { ImageAsset::Glow, "Glow", "" },
  78. { ImageAsset::Particle, "Particle", "" },
  79. { ImageAsset::Decal, "Decal", "" },
  80. { ImageAsset::Cubemap, "Cubemap", "" },
  81. EndImplementEnumType;
  82. //-----------------------------------------------------------------------------
  83. ImageAsset::ImageAsset() : AssetBase(), mImage(nullptr), mUseMips(true), mIsHDRImage(false), mIsValidImage(false)
  84. {
  85. mImageFileName = StringTable->EmptyString();
  86. }
  87. //-----------------------------------------------------------------------------
  88. ImageAsset::~ImageAsset()
  89. {
  90. }
  91. //-----------------------------------------------------------------------------
  92. void ImageAsset::initPersistFields()
  93. {
  94. // Call parent.
  95. Parent::initPersistFields();
  96. addProtectedField("imageFile", TypeAssetLooseFilePath, Offset(mImageFileName, ImageAsset),
  97. &setImageFileName, &getImageFileName, "Path to the image file.");
  98. addField("useMips", TypeBool, Offset(mUseMips, ImageAsset), "Should the image use mips? (Currently unused).");
  99. addField("isHDRImage", TypeBool, Offset(mIsHDRImage, ImageAsset), "Is the image in an HDR format? (Currently unused)");
  100. addField("imageType", TypeImageAssetType, Offset(mImageType, ImageAsset), "What the main use-case for the image is for.");
  101. }
  102. //------------------------------------------------------------------------------
  103. //Utility function to 'fill out' bindings and resources with a matching asset if one exists
  104. bool ImageAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ImageAsset>* imageAsset)
  105. {
  106. AssetQuery query;
  107. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  108. if (foundAssetcount == 0)
  109. {
  110. //Didn't find any assets, so have us fall back to a placeholder asset
  111. imageAsset->setAssetId(StringTable->insert("Core_Rendering:noshape"));
  112. if (!imageAsset->isNull())
  113. return true;
  114. //That didn't work, so fail out
  115. return false;
  116. }
  117. else
  118. {
  119. //acquire and bind the asset, and return it out
  120. imageAsset->setAssetId(query.mAssetList[0]);
  121. return true;
  122. }
  123. }
  124. //------------------------------------------------------------------------------
  125. void ImageAsset::copyTo(SimObject* object)
  126. {
  127. // Call to parent.
  128. Parent::copyTo(object);
  129. }
  130. void ImageAsset::loadImage()
  131. {
  132. SAFE_DELETE(mImage);
  133. if (mImageFileName)
  134. {
  135. if (!Platform::isFile(mImageFileName))
  136. {
  137. Con::errorf("ImageAsset::initializeAsset: Attempted to load file %s but it was not valid!", mImageFileName);
  138. return;
  139. }
  140. mImage.set(mImageFileName, &GFXStaticTextureSRGBProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  141. if (mImage)
  142. {
  143. mIsValidImage = true;
  144. return;
  145. }
  146. }
  147. mIsValidImage = false;
  148. }
  149. void ImageAsset::initializeAsset()
  150. {
  151. mImageFileName = expandAssetFilePath(mImageFileName);
  152. loadImage();
  153. }
  154. void ImageAsset::onAssetRefresh()
  155. {
  156. setImageFileName(mImageFileName);
  157. }
  158. void ImageAsset::setImageFileName(const char* pScriptFile)
  159. {
  160. // Sanity!
  161. AssertFatal(pScriptFile != NULL, "Cannot use a NULL image file.");
  162. // Update.
  163. mImageFileName = StringTable->insert(pScriptFile);
  164. }
  165. GFXTexHandle ImageAsset::getImage(GFXTextureProfile requestedProfile)
  166. {
  167. /*if (mResourceMap.contains(requestedProfile))
  168. {
  169. return mResourceMap.find(requestedProfile)->value;
  170. }
  171. else
  172. {
  173. //If we don't have an existing map case to the requested format, we'll just create it and insert it in
  174. GFXTexHandle newImage;
  175. newImage.set(mImageFileName, &requestedProfile, avar("%s() - mImage (line %d)", __FUNCTION__, __LINE__));
  176. mResourceMap.insert(requestedProfile, newImage);
  177. return newImage;
  178. }*/
  179. return nullptr;
  180. }
  181. const char* ImageAsset::getImageInfo()
  182. {
  183. if (mIsValidImage)
  184. {
  185. static const U32 bufSize = 2048;
  186. char* returnBuffer = Con::getReturnBuffer(bufSize);
  187. dSprintf(returnBuffer, bufSize, "%s %d %d %d", GFXStringTextureFormat[mImage.getFormat()], mImage.getHeight(), mImage.getWidth(), mImage.getDepth());
  188. return returnBuffer;
  189. }
  190. return "";
  191. }
  192. DefineEngineMethod(ImageAsset, getImageFilename, const char*, (), ,
  193. "Creates an instance of the given GameObject given the asset definition.\n"
  194. "@return The GameObject entity created from the asset.")
  195. {
  196. return object->getImageFileName();
  197. }
  198. DefineEngineMethod(ImageAsset, getImageInfo, const char*, (), ,
  199. "Creates an instance of the given GameObject given the asset definition.\n"
  200. "@return The GameObject entity created from the asset.")
  201. {
  202. return object->getImageInfo();
  203. }