MaterialAsset.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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(StringTable->insert(Con::getVariable("$Core::NoMaterialAssetFallback")));
  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. }
  117. void MaterialAsset::initPersistFields()
  118. {
  119. // Call parent.
  120. Parent::initPersistFields();
  121. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), "");
  122. addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset),
  123. &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  124. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for.");
  125. }
  126. void MaterialAsset::initializeAsset()
  127. {
  128. // Call parent.
  129. Parent::initializeAsset();
  130. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  131. if (Torque::FS::IsScriptFile(mScriptPath))
  132. Con::executeFile(mScriptPath, false, false);
  133. loadMaterial();
  134. }
  135. void MaterialAsset::onAssetRefresh()
  136. {
  137. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  138. if (Torque::FS::IsScriptFile(mScriptPath))
  139. Con::executeFile(mScriptPath, false, false);
  140. loadMaterial();
  141. }
  142. void MaterialAsset::setScriptFile(const char* pScriptFile)
  143. {
  144. // Sanity!
  145. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  146. // Fetch image file.
  147. pScriptFile = StringTable->insert(pScriptFile);
  148. // Update.
  149. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  150. // Refresh the asset.
  151. refreshAsset();
  152. }
  153. //------------------------------------------------------------------------------
  154. void MaterialAsset::loadMaterial()
  155. {
  156. if (mMaterialDefinition)
  157. SAFE_DELETE(mMaterialDefinition);
  158. if (mMatDefinitionName != StringTable->EmptyString())
  159. {
  160. Material* matDef;
  161. if (!Sim::findObject(mMatDefinitionName, matDef))
  162. {
  163. Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  164. mLoadedState = BadFileReference;
  165. return;
  166. }
  167. mMaterialDefinition = matDef;
  168. mLoadedState = Ok;
  169. mMaterialDefinition->reload();
  170. return;
  171. }
  172. mLoadedState = Failed;
  173. }
  174. //------------------------------------------------------------------------------
  175. void MaterialAsset::copyTo(SimObject* object)
  176. {
  177. // Call to parent.
  178. Parent::copyTo(object);
  179. }
  180. //------------------------------------------------------------------------------
  181. U32 MaterialAsset::getAssetByMaterialName(StringTableEntry matName, AssetPtr<MaterialAsset>* matAsset)
  182. {
  183. AssetQuery query;
  184. U32 foundAssetcount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  185. if (foundAssetcount == 0)
  186. {
  187. //Didn't work, so have us fall back to a placeholder asset
  188. matAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  189. if (matAsset->isNull())
  190. {
  191. //Well that's bad, loading the fallback failed.
  192. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of asset associated with material name %s failed with no fallback asset", matName);
  193. return AssetErrCode::Failed;
  194. }
  195. //handle noshape not being loaded itself
  196. if ((*matAsset)->mLoadedState == BadFileReference)
  197. {
  198. Con::warnf("ShapeAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, and fallback asset reported error of Bad File Reference.", matName);
  199. return AssetErrCode::BadFileReference;
  200. }
  201. Con::warnf("ShapeAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, utilizing fallback asset", matName);
  202. (*matAsset)->mLoadedState = AssetErrCode::UsingFallback;
  203. return AssetErrCode::UsingFallback;
  204. }
  205. else
  206. {
  207. for (U32 i = 0; i < foundAssetcount; i++)
  208. {
  209. MaterialAsset* tMatAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  210. if (tMatAsset && tMatAsset->getMaterialDefinitionName() == matName)
  211. {
  212. matAsset->setAssetId(query.mAssetList[i]);
  213. AssetDatabase.releaseAsset(query.mAssetList[i]);
  214. return (*matAsset)->mLoadedState;
  215. }
  216. AssetDatabase.releaseAsset(query.mAssetList[i]); //cleanup if that's not the one we needed
  217. }
  218. }
  219. }
  220. StringTableEntry MaterialAsset::getAssetIdByMaterialName(StringTableEntry matName)
  221. {
  222. if (matName == StringTable->EmptyString())
  223. return StringTable->EmptyString();
  224. StringTableEntry materialAssetId = MaterialAsset::smNoMaterialAssetFallback;
  225. AssetQuery query;
  226. U32 foundCount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  227. if (foundCount != 0)
  228. {
  229. for (U32 i = 0; i < foundCount; i++)
  230. {
  231. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  232. if (matAsset && matAsset->getMaterialDefinitionName() == matName)
  233. {
  234. materialAssetId = matAsset->getAssetId();
  235. AssetDatabase.releaseAsset(query.mAssetList[i]);
  236. break;
  237. }
  238. AssetDatabase.releaseAsset(query.mAssetList[i]);
  239. }
  240. }
  241. return materialAssetId;
  242. }
  243. U32 MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset)
  244. {
  245. (*materialAsset) = assetId;
  246. if (materialAsset->notNull())
  247. {
  248. return (*materialAsset)->mLoadedState;
  249. }
  250. else
  251. {
  252. //Didn't work, so have us fall back to a placeholder asset
  253. materialAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  254. if (materialAsset->isNull())
  255. {
  256. //Well that's bad, loading the fallback failed.
  257. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  258. return AssetErrCode::Failed;
  259. }
  260. //handle noshape not being loaded itself
  261. if ((*materialAsset)->mLoadedState == BadFileReference)
  262. {
  263. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, and fallback asset reported error of Bad File Reference.", assetId);
  264. return AssetErrCode::BadFileReference;
  265. }
  266. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, utilizing fallback asset", assetId);
  267. (*materialAsset)->mLoadedState = AssetErrCode::UsingFallback;
  268. return AssetErrCode::UsingFallback;
  269. }
  270. }
  271. #ifdef TORQUE_TOOLS
  272. DefineEngineStaticMethod(MaterialAsset, getAssetIdByMaterialName, const char*, (const char* materialName), (""),
  273. "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
  274. "@return The AssetId of the associated asset, if any.")
  275. {
  276. return MaterialAsset::getAssetIdByMaterialName(StringTable->insert(materialName));
  277. }
  278. #endif
  279. //-----------------------------------------------------------------------------
  280. // GuiInspectorTypeAssetId
  281. //-----------------------------------------------------------------------------
  282. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  283. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  284. "@brief Inspector field type for Shapes\n\n"
  285. "Editor use only.\n\n"
  286. "@internal"
  287. );
  288. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  289. {
  290. Parent::consoleInit();
  291. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  292. }
  293. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  294. {
  295. // Create base filename edit controls
  296. GuiControl* retCtrl = Parent::constructEditControl();
  297. if (retCtrl == NULL)
  298. return retCtrl;
  299. // Change filespec
  300. char szBuffer[512];
  301. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, \"\");",
  302. getIdString());
  303. mBrowseButton->setField("Command", szBuffer);
  304. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  305. // Create "Open in Editor" button
  306. mEditButton = new GuiBitmapButtonCtrl();
  307. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  308. mEditButton->setField("Command", szBuffer);
  309. char bitmapName[512] = "tools/worldEditor/images/toolbar/material-editor";
  310. mEditButton->setBitmap(StringTable->insert(bitmapName));
  311. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  312. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  313. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  314. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Material Editor");
  315. mEditButton->registerObject();
  316. addObject(mEditButton);
  317. return retCtrl;
  318. }
  319. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  320. {
  321. S32 dividerPos, dividerMargin;
  322. mInspector->getDivider(dividerPos, dividerMargin);
  323. Point2I fieldExtent = getExtent();
  324. Point2I fieldPos = getPosition();
  325. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  326. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  327. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  328. if (mBrowseButton != NULL)
  329. {
  330. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  331. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  332. }
  333. if (mEditButton != NULL)
  334. {
  335. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  336. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  337. }
  338. return resized;
  339. }
  340. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId);
  341. ConsoleDocClass(GuiInspectorTypeMaterialAssetId,
  342. "@brief Inspector field type for Material Assets\n\n"
  343. "Editor use only.\n\n"
  344. "@internal"
  345. );
  346. void GuiInspectorTypeMaterialAssetId::consoleInit()
  347. {
  348. Parent::consoleInit();
  349. ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId");
  350. }