MaterialAsset.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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. #pragma once
  23. #ifndef MATERIALASSET_H
  24. #include "MaterialAsset.h"
  25. #endif
  26. #ifndef _ASSET_MANAGER_H_
  27. #include "assets/assetManager.h"
  28. #endif
  29. #ifndef _CONSOLETYPES_H_
  30. #include "console/consoleTypes.h"
  31. #endif
  32. #ifndef _TAML_
  33. #include "persistence/taml/taml.h"
  34. #endif
  35. #ifndef _ASSET_PTR_H_
  36. #include "assets/assetPtr.h"
  37. #endif
  38. #include "T3D/assets/assetImporter.h"
  39. StringTableEntry MaterialAsset::smNoMaterialAssetFallback = NULL;
  40. //-----------------------------------------------------------------------------
  41. IMPLEMENT_CONOBJECT(MaterialAsset);
  42. ConsoleType(MaterialAssetPtr, TypeMaterialAssetPtr, MaterialAsset, ASSET_ID_FIELD_PREFIX)
  43. //-----------------------------------------------------------------------------
  44. ConsoleGetType(TypeMaterialAssetPtr)
  45. {
  46. // Fetch asset Id.
  47. return (*((AssetPtr<MaterialAsset>*)dptr)).getAssetId();
  48. }
  49. //-----------------------------------------------------------------------------
  50. ConsoleSetType(TypeMaterialAssetPtr)
  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 pointer.
  58. AssetPtr<MaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<MaterialAsset>*>((AssetPtrBase*)(dptr));
  59. // Is the asset pointer the correct type?
  60. if (pAssetPtr == NULL)
  61. {
  62. // No, so fail.
  63. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  64. return;
  65. }
  66. // Set asset.
  67. pAssetPtr->setAssetId(pFieldValue);
  68. return;
  69. }
  70. // Warn.
  71. Con::warnf("(TypeMaterialAssetPtr) - Cannot set multiple args to a single asset.");
  72. }
  73. ConsoleType(assetIdString, TypeMaterialAssetId, const char*, ASSET_ID_FIELD_PREFIX)
  74. ConsoleGetType(TypeMaterialAssetId)
  75. {
  76. // Fetch asset Id.
  77. return *((const char**)(dptr));
  78. }
  79. ConsoleSetType(TypeMaterialAssetId)
  80. {
  81. // Was a single argument specified?
  82. if (argc == 1)
  83. {
  84. // Yes, so fetch field value.
  85. const char* pFieldValue = argv[0];
  86. // Fetch asset Id.
  87. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  88. // Update asset value.
  89. *assetId = StringTable->insert(pFieldValue);
  90. return;
  91. }
  92. // Warn.
  93. Con::warnf("(TypeMaterialAssetId) - Cannot set multiple args to a single asset.");
  94. }
  95. //-----------------------------------------------------------------------------
  96. MaterialAsset::MaterialAsset()
  97. {
  98. mShaderGraphFile = "";
  99. mScriptFile = StringTable->EmptyString();
  100. mScriptPath = StringTable->EmptyString();
  101. mMatDefinitionName = StringTable->EmptyString();
  102. mMaterialDefinition = nullptr;
  103. }
  104. //-----------------------------------------------------------------------------
  105. MaterialAsset::~MaterialAsset()
  106. {
  107. //SAFE_DELETE(mMaterialDefinition);
  108. }
  109. //-----------------------------------------------------------------------------
  110. void MaterialAsset::consoleInit()
  111. {
  112. Parent::consoleInit();
  113. Con::addVariable("$Core::NoMaterialAssetFallback", TypeString, &smNoMaterialAssetFallback,
  114. "The assetId of the material to display when the requested material asset is missing.\n"
  115. "@ingroup GFX\n");
  116. smNoMaterialAssetFallback = StringTable->insert(Con::getVariable("$Core::NoMaterialAssetFallback"));
  117. }
  118. void MaterialAsset::initPersistFields()
  119. {
  120. // Call parent.
  121. Parent::initPersistFields();
  122. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), "");
  123. addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset),
  124. &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  125. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for.");
  126. }
  127. void MaterialAsset::initializeAsset()
  128. {
  129. // Call parent.
  130. Parent::initializeAsset();
  131. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  132. if (Torque::FS::IsScriptFile(mScriptPath))
  133. Con::executeFile(mScriptPath, false, false);
  134. loadMaterial();
  135. }
  136. void MaterialAsset::onAssetRefresh()
  137. {
  138. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  139. if (Torque::FS::IsScriptFile(mScriptPath))
  140. Con::executeFile(mScriptPath, false, false);
  141. loadMaterial();
  142. }
  143. void MaterialAsset::setScriptFile(const char* pScriptFile)
  144. {
  145. // Sanity!
  146. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  147. // Fetch image file.
  148. pScriptFile = StringTable->insert(pScriptFile, true);
  149. // Update.
  150. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  151. // Refresh the asset.
  152. refreshAsset();
  153. }
  154. //------------------------------------------------------------------------------
  155. void MaterialAsset::loadMaterial()
  156. {
  157. if (mMaterialDefinition)
  158. SAFE_DELETE(mMaterialDefinition);
  159. if (mMatDefinitionName != StringTable->EmptyString())
  160. {
  161. Material* matDef;
  162. if (!Sim::findObject(mMatDefinitionName, matDef))
  163. {
  164. Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  165. mLoadedState = BadFileReference;
  166. return;
  167. }
  168. mMaterialDefinition = matDef;
  169. mLoadedState = Ok;
  170. mMaterialDefinition->reload();
  171. return;
  172. }
  173. mLoadedState = Failed;
  174. }
  175. //------------------------------------------------------------------------------
  176. void MaterialAsset::copyTo(SimObject* object)
  177. {
  178. // Call to parent.
  179. Parent::copyTo(object);
  180. }
  181. //------------------------------------------------------------------------------
  182. U32 MaterialAsset::getAssetByMaterialName(StringTableEntry matName, AssetPtr<MaterialAsset>* matAsset)
  183. {
  184. AssetQuery query;
  185. U32 foundAssetcount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  186. if (foundAssetcount == 0)
  187. {
  188. //Didn't work, so have us fall back to a placeholder asset
  189. matAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  190. if (matAsset->isNull())
  191. {
  192. //Well that's bad, loading the fallback failed.
  193. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of asset associated with material name %s failed with no fallback asset", matName);
  194. return AssetErrCode::Failed;
  195. }
  196. //handle noshape not being loaded itself
  197. if ((*matAsset)->mLoadedState == BadFileReference)
  198. {
  199. Con::warnf("ShapeAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, and fallback asset reported error of Bad File Reference.", matName);
  200. return AssetErrCode::BadFileReference;
  201. }
  202. Con::warnf("ShapeAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, utilizing fallback asset", matName);
  203. (*matAsset)->mLoadedState = AssetErrCode::UsingFallback;
  204. return AssetErrCode::UsingFallback;
  205. }
  206. else
  207. {
  208. for (U32 i = 0; i < foundAssetcount; i++)
  209. {
  210. MaterialAsset* tMatAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  211. if (tMatAsset && tMatAsset->getMaterialDefinitionName() == matName)
  212. {
  213. matAsset->setAssetId(query.mAssetList[i]);
  214. AssetDatabase.releaseAsset(query.mAssetList[i]);
  215. return (*matAsset)->mLoadedState;
  216. }
  217. AssetDatabase.releaseAsset(query.mAssetList[i]); //cleanup if that's not the one we needed
  218. }
  219. }
  220. }
  221. StringTableEntry MaterialAsset::getAssetIdByMaterialName(StringTableEntry matName)
  222. {
  223. if (matName == StringTable->EmptyString())
  224. return StringTable->EmptyString();
  225. StringTableEntry materialAssetId = MaterialAsset::smNoMaterialAssetFallback;
  226. AssetQuery query;
  227. U32 foundCount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  228. if (foundCount != 0)
  229. {
  230. for (U32 i = 0; i < foundCount; i++)
  231. {
  232. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  233. if (matAsset && matAsset->getMaterialDefinitionName() == matName)
  234. {
  235. materialAssetId = matAsset->getAssetId();
  236. AssetDatabase.releaseAsset(query.mAssetList[i]);
  237. break;
  238. }
  239. AssetDatabase.releaseAsset(query.mAssetList[i]);
  240. }
  241. }
  242. return materialAssetId;
  243. }
  244. U32 MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset)
  245. {
  246. (*materialAsset) = assetId;
  247. if (materialAsset->notNull())
  248. {
  249. return (*materialAsset)->mLoadedState;
  250. }
  251. else
  252. {
  253. //Didn't work, so have us fall back to a placeholder asset
  254. materialAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  255. if (materialAsset->isNull())
  256. {
  257. //Well that's bad, loading the fallback failed.
  258. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  259. return AssetErrCode::Failed;
  260. }
  261. //handle noshape not being loaded itself
  262. if ((*materialAsset)->mLoadedState == BadFileReference)
  263. {
  264. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, and fallback asset reported error of Bad File Reference.", assetId);
  265. return AssetErrCode::BadFileReference;
  266. }
  267. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, utilizing fallback asset", assetId);
  268. (*materialAsset)->mLoadedState = AssetErrCode::UsingFallback;
  269. return AssetErrCode::UsingFallback;
  270. }
  271. }
  272. #ifdef TORQUE_TOOLS
  273. DefineEngineStaticMethod(MaterialAsset, getAssetIdByMaterialName, const char*, (const char* materialName), (""),
  274. "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
  275. "@return The AssetId of the associated asset, if any.")
  276. {
  277. return MaterialAsset::getAssetIdByMaterialName(StringTable->insert(materialName));
  278. }
  279. DefineEngineMethod(MaterialAsset, getScriptPath, const char*, (), ,
  280. "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
  281. "@return The AssetId of the associated asset, if any.")
  282. {
  283. return object->getScriptPath();
  284. }
  285. #endif
  286. //-----------------------------------------------------------------------------
  287. // GuiInspectorTypeAssetId
  288. //-----------------------------------------------------------------------------
  289. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  290. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  291. "@brief Inspector field type for Shapes\n\n"
  292. "Editor use only.\n\n"
  293. "@internal"
  294. );
  295. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  296. {
  297. Parent::consoleInit();
  298. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  299. }
  300. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  301. {
  302. // Create base filename edit controls
  303. GuiControl* retCtrl = Parent::constructEditControl();
  304. if (retCtrl == NULL)
  305. return retCtrl;
  306. // Change filespec
  307. char szBuffer[512];
  308. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, \"\");",
  309. getIdString());
  310. mBrowseButton->setField("Command", szBuffer);
  311. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  312. // Create "Open in Editor" button
  313. mEditButton = new GuiBitmapButtonCtrl();
  314. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  315. mEditButton->setField("Command", szBuffer);
  316. char bitmapName[512] = "ToolsModule:material_editor_n_image";
  317. mEditButton->setBitmap(StringTable->insert(bitmapName));
  318. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  319. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  320. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  321. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this asset in the Material Editor");
  322. mEditButton->registerObject();
  323. addObject(mEditButton);
  324. return retCtrl;
  325. }
  326. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  327. {
  328. S32 dividerPos, dividerMargin;
  329. mInspector->getDivider(dividerPos, dividerMargin);
  330. Point2I fieldExtent = getExtent();
  331. Point2I fieldPos = getPosition();
  332. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  333. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  334. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  335. if (mBrowseButton != NULL)
  336. {
  337. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  338. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  339. }
  340. if (mEditButton != NULL)
  341. {
  342. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  343. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  344. }
  345. return resized;
  346. }
  347. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId);
  348. ConsoleDocClass(GuiInspectorTypeMaterialAssetId,
  349. "@brief Inspector field type for Material Assets\n\n"
  350. "Editor use only.\n\n"
  351. "@internal"
  352. );
  353. void GuiInspectorTypeMaterialAssetId::consoleInit()
  354. {
  355. Parent::consoleInit();
  356. ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId");
  357. }