MaterialAsset.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 MATERIALASSET_H
  23. #include "MaterialAsset.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 "console/script.h"
  38. #include "T3D/assets/assetImporter.h"
  39. StringTableEntry MaterialAsset::smNoMaterialAssetFallback = NULL;
  40. const String MaterialAsset::mErrCodeStrings[] =
  41. {
  42. "ScriptLoaded",
  43. "DefinitionAlreadyExists",
  44. "EmbeddedDefinition",
  45. "UnKnown"
  46. };
  47. //-----------------------------------------------------------------------------
  48. IMPLEMENT_CONOBJECT(MaterialAsset);
  49. ConsoleType(MaterialAssetPtr, TypeMaterialAssetPtr, MaterialAsset, ASSET_ID_FIELD_PREFIX)
  50. //-----------------------------------------------------------------------------
  51. ConsoleGetType(TypeMaterialAssetPtr)
  52. {
  53. // Fetch asset Id.
  54. return (*((AssetPtr<MaterialAsset>*)dptr)).getAssetId();
  55. }
  56. //-----------------------------------------------------------------------------
  57. ConsoleSetType(TypeMaterialAssetPtr)
  58. {
  59. // Was a single argument specified?
  60. if (argc == 1)
  61. {
  62. // Yes, so fetch field value.
  63. const char* pFieldValue = argv[0];
  64. // Fetch asset pointer.
  65. AssetPtr<MaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<MaterialAsset>*>((AssetPtrBase*)(dptr));
  66. // Is the asset pointer the correct type?
  67. if (pAssetPtr == NULL)
  68. {
  69. // No, so fail.
  70. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  71. return;
  72. }
  73. // Set asset.
  74. pAssetPtr->setAssetId(pFieldValue);
  75. return;
  76. }
  77. // Warn.
  78. Con::warnf("(TypeMaterialAssetPtr) - Cannot set multiple args to a single asset.");
  79. }
  80. ConsoleType(assetIdString, TypeMaterialAssetId, const char*, ASSET_ID_FIELD_PREFIX)
  81. ConsoleGetType(TypeMaterialAssetId)
  82. {
  83. // Fetch asset Id.
  84. return *((const char**)(dptr));
  85. }
  86. ConsoleSetType(TypeMaterialAssetId)
  87. {
  88. // Was a single argument specified?
  89. if (argc == 1)
  90. {
  91. // Yes, so fetch field value.
  92. const char* pFieldValue = argv[0];
  93. // Fetch asset Id.
  94. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  95. // Update asset value.
  96. *assetId = StringTable->insert(pFieldValue);
  97. return;
  98. }
  99. // Warn.
  100. Con::warnf("(TypeMaterialAssetId) - Cannot set multiple args to a single asset.");
  101. }
  102. //-----------------------------------------------------------------------------
  103. MaterialAsset::MaterialAsset()
  104. {
  105. mShaderGraphFile = "";
  106. mScriptFile = StringTable->EmptyString();
  107. mScriptPath = StringTable->EmptyString();
  108. mMatDefinitionName = StringTable->EmptyString();
  109. mMaterialDefinition = nullptr;
  110. }
  111. //-----------------------------------------------------------------------------
  112. MaterialAsset::~MaterialAsset()
  113. {
  114. //SAFE_DELETE(mMaterialDefinition);
  115. }
  116. //-----------------------------------------------------------------------------
  117. void MaterialAsset::consoleInit()
  118. {
  119. Parent::consoleInit();
  120. Con::addVariable("$Core::NoMaterialAssetFallback", TypeString, &smNoMaterialAssetFallback,
  121. "The assetId of the material to display when the requested material asset is missing.\n"
  122. "@ingroup GFX\n");
  123. smNoMaterialAssetFallback = StringTable->insert(Con::getVariable("$Core::NoMaterialAssetFallback"));
  124. }
  125. void MaterialAsset::initPersistFields()
  126. {
  127. docsURL;
  128. // Call parent.
  129. Parent::initPersistFields();
  130. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), "");
  131. //addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset),
  132. // &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  133. addField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset), "");
  134. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for.");
  135. }
  136. void MaterialAsset::initializeAsset()
  137. {
  138. // Call parent.
  139. Parent::initializeAsset();
  140. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  141. if (mMatDefinitionName == StringTable->EmptyString())
  142. {
  143. mLoadedState = Failed;
  144. return;
  145. }
  146. if (size() != 0 && mScriptPath == StringTable->EmptyString())
  147. {
  148. mLoadedState = EmbeddedDefinition;
  149. }
  150. else if (Con::isScriptFile(mScriptPath))
  151. {
  152. if (!Sim::findObject(mMatDefinitionName))
  153. {
  154. if (Con::executeFile(mScriptPath, false, false))
  155. {
  156. mLoadedState = ScriptLoaded;
  157. }
  158. else
  159. {
  160. mLoadedState = Failed;
  161. }
  162. }
  163. else
  164. {
  165. mLoadedState = DefinitionAlreadyExists;
  166. }
  167. }
  168. load();
  169. }
  170. void MaterialAsset::onAssetRefresh()
  171. {
  172. // Ignore if not yet added to the sim.
  173. if (!isProperlyAdded())
  174. return;
  175. // Call parent.
  176. Parent::onAssetRefresh();
  177. if (mMaterialDefinition)
  178. {
  179. mMaterialDefinition->flush();
  180. mMaterialDefinition->reload();
  181. }
  182. else
  183. {
  184. load();
  185. }
  186. }
  187. void MaterialAsset::setScriptFile(const char* pScriptFile)
  188. {
  189. // Sanity!
  190. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  191. pScriptFile = StringTable->insert(pScriptFile, true);
  192. // Update.
  193. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  194. // Refresh the asset.
  195. refreshAsset();
  196. }
  197. //------------------------------------------------------------------------------
  198. U32 MaterialAsset::load()
  199. {
  200. if (mMaterialDefinition)
  201. {
  202. mMaterialDefinition->safeDeleteObject();
  203. }
  204. if (mLoadedState == EmbeddedDefinition)
  205. {
  206. if (size() != 0)
  207. {
  208. for (U32 i = 0; i < size(); i++)
  209. {
  210. mMaterialDefinition = dynamic_cast<Material*>(getObject(i));
  211. if (mMaterialDefinition)
  212. {
  213. mLoadedState = Ok;
  214. mMaterialDefinition->setInternalName(getAssetId());
  215. mMaterialDefinition->reload();
  216. return mLoadedState;
  217. }
  218. }
  219. }
  220. }
  221. else if ((mLoadedState == ScriptLoaded || mLoadedState == DefinitionAlreadyExists) && mMatDefinitionName != StringTable->EmptyString())
  222. {
  223. Material* matDef;
  224. if (!Sim::findObject(mMatDefinitionName, matDef))
  225. {
  226. Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  227. mLoadedState = BadFileReference;
  228. return mLoadedState;
  229. }
  230. mMaterialDefinition = matDef;
  231. mLoadedState = Ok;
  232. mMaterialDefinition->setInternalName(getAssetId());
  233. mMaterialDefinition->reload();
  234. return mLoadedState;
  235. }
  236. mLoadedState = Failed;
  237. return mLoadedState;
  238. }
  239. //------------------------------------------------------------------------------
  240. void MaterialAsset::copyTo(SimObject* object)
  241. {
  242. // Call to parent.
  243. Parent::copyTo(object);
  244. }
  245. //------------------------------------------------------------------------------
  246. U32 MaterialAsset::getAssetByMaterialName(StringTableEntry matName, AssetPtr<MaterialAsset>* matAsset)
  247. {
  248. AssetQuery query;
  249. U32 foundAssetcount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  250. if (foundAssetcount == 0)
  251. {
  252. //Didn't work, so have us fall back to a placeholder asset
  253. matAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  254. if (matAsset->isNull())
  255. {
  256. //Well that's bad, loading the fallback failed.
  257. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of asset associated with material name %s failed with no fallback asset", matName);
  258. return AssetErrCode::Failed;
  259. }
  260. //handle noshape not being loaded itself
  261. if ((*matAsset)->mLoadedState == BadFileReference)
  262. {
  263. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, and fallback asset reported error of Bad File Reference.", matName);
  264. return AssetErrCode::BadFileReference;
  265. }
  266. Con::warnf("MaterialAsset::getAssetByMaterialName - Finding of associated with aterial name %s failed, utilizing fallback asset", matName);
  267. (*matAsset)->mLoadedState = AssetErrCode::UsingFallback;
  268. return AssetErrCode::UsingFallback;
  269. }
  270. else
  271. {
  272. for (U32 i = 0; i < foundAssetcount; i++)
  273. {
  274. MaterialAsset* tMatAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  275. if (tMatAsset && tMatAsset->getMaterialDefinitionName() == matName)
  276. {
  277. matAsset->setAssetId(query.mAssetList[i]);
  278. AssetDatabase.releaseAsset(query.mAssetList[i]);
  279. return (*matAsset)->mLoadedState;
  280. }
  281. AssetDatabase.releaseAsset(query.mAssetList[i]); //cleanup if that's not the one we needed
  282. }
  283. }
  284. //Somehow we failed to bind an asset, so just use the fallback and mark the failure
  285. matAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  286. (*matAsset)->mLoadedState = AssetErrCode::UsingFallback;
  287. return AssetErrCode::UsingFallback;
  288. }
  289. StringTableEntry MaterialAsset::getAssetIdByMaterialName(StringTableEntry matName)
  290. {
  291. if (matName == StringTable->EmptyString())
  292. return StringTable->EmptyString();
  293. StringTableEntry materialAssetId = MaterialAsset::smNoMaterialAssetFallback;
  294. AssetQuery query;
  295. U32 foundCount = AssetDatabase.findAssetType(&query, "MaterialAsset");
  296. if (foundCount != 0)
  297. {
  298. for (U32 i = 0; i < foundCount && materialAssetId == MaterialAsset::smNoMaterialAssetFallback; i++)
  299. {
  300. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(query.mAssetList[i]);
  301. if (matAsset)
  302. {
  303. if (matAsset->getMaterialDefinitionName() == matName)
  304. materialAssetId = matAsset->getAssetId();
  305. AssetDatabase.releaseAsset(query.mAssetList[i]);
  306. }
  307. }
  308. }
  309. return materialAssetId;
  310. }
  311. U32 MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset)
  312. {
  313. (*materialAsset) = assetId;
  314. if (materialAsset->notNull())
  315. {
  316. return (*materialAsset)->mLoadedState;
  317. }
  318. else
  319. {
  320. //Didn't work, so have us fall back to a placeholder asset
  321. materialAsset->setAssetId(MaterialAsset::smNoMaterialAssetFallback);
  322. if (materialAsset->isNull())
  323. {
  324. //Well that's bad, loading the fallback failed.
  325. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  326. return AssetErrCode::Failed;
  327. }
  328. //handle noshape not being loaded itself
  329. if ((*materialAsset)->mLoadedState == BadFileReference)
  330. {
  331. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, and fallback asset reported error of Bad File Reference.", assetId);
  332. return AssetErrCode::BadFileReference;
  333. }
  334. Con::warnf("MaterialAsset::getAssetById - Finding of asset with id %s failed, utilizing fallback asset", assetId);
  335. (*materialAsset)->mLoadedState = AssetErrCode::UsingFallback;
  336. return AssetErrCode::UsingFallback;
  337. }
  338. }
  339. SimObjectPtr<Material> MaterialAsset::findMaterialDefinitionByAssetId(StringTableEntry assetId)
  340. {
  341. SimSet* matSet = MATMGR->getMaterialSet();
  342. if (matSet)
  343. {
  344. SimObjectPtr<Material> matDef = dynamic_cast<Material*>(matSet->findObjectByInternalName(assetId));
  345. return matDef;
  346. }
  347. return nullptr;
  348. }
  349. #ifdef TORQUE_TOOLS
  350. DefineEngineStaticMethod(MaterialAsset, getAssetIdByMaterialName, const char*, (const char* materialName), (""),
  351. "Queries the Asset Database to see if any asset exists that is associated with the provided material name.\n"
  352. "@return The AssetId of the associated asset, if any.")
  353. {
  354. return MaterialAsset::getAssetIdByMaterialName(StringTable->insert(materialName));
  355. }
  356. //MaterialAsset::findMaterialDefinitionByAssetId("Prototyping:Detail")
  357. DefineEngineStaticMethod(MaterialAsset, findMaterialDefinitionByAssetId, S32, (const char* assetId), (""),
  358. "Queries the MaterialSet to see if any MaterialDefinition exists that is associated to the provided assetId.\n"
  359. "@return The MaterialDefinition Id associated to the assetId, if any")
  360. {
  361. SimObjectPtr<Material> matDef = MaterialAsset::findMaterialDefinitionByAssetId(StringTable->insert(assetId));
  362. if (matDef.isNull())
  363. return SimObjectId(0);
  364. else
  365. return matDef->getId();
  366. }
  367. DefineEngineMethod(MaterialAsset, getScriptPath, const char*, (), ,
  368. "Gets the script file path for the asset.")
  369. {
  370. return object->getScriptPath();
  371. }
  372. #endif
  373. #ifdef TORQUE_TOOLS
  374. //-----------------------------------------------------------------------------
  375. // GuiInspectorTypeAssetId
  376. //-----------------------------------------------------------------------------
  377. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  378. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  379. "@brief Inspector field type for Shapes\n\n"
  380. "Editor use only.\n\n"
  381. "@internal"
  382. );
  383. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  384. {
  385. Parent::consoleInit();
  386. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  387. }
  388. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  389. {
  390. // Create base filename edit controls
  391. GuiControl* retCtrl = Parent::constructEditControl();
  392. if (retCtrl == NULL)
  393. return retCtrl;
  394. // Change filespec
  395. char szBuffer[512];
  396. const char* previewImage;
  397. if (mInspector->getInspectObject() != nullptr)
  398. {
  399. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  400. mInspector->getIdString(), mCaption);
  401. mBrowseButton->setField("Command", szBuffer);
  402. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  403. previewImage = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  404. }
  405. else
  406. {
  407. //if we don't have a target object, we'll be manipulating the desination value directly
  408. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, \"%s\");",
  409. mInspector->getIdString(), mVariableName);
  410. mBrowseButton->setField("Command", szBuffer);
  411. previewImage = Con::getVariable(mVariableName);
  412. }
  413. mLabel = new GuiTextCtrl();
  414. mLabel->registerObject();
  415. mLabel->setControlProfile(mProfile);
  416. mLabel->setText(mCaption);
  417. addObject(mLabel);
  418. //
  419. GuiTextEditCtrl* editTextCtrl = static_cast<GuiTextEditCtrl*>(retCtrl);
  420. GuiControlProfile* toolEditProfile;
  421. if (Sim::findObject("ToolsGuiTextEditProfile", toolEditProfile))
  422. editTextCtrl->setControlProfile(toolEditProfile);
  423. GuiControlProfile* toolDefaultProfile = nullptr;
  424. Sim::findObject("ToolsGuiDefaultProfile", toolDefaultProfile);
  425. //
  426. mPreviewImage = new GuiBitmapCtrl();
  427. mPreviewImage->registerObject();
  428. if(toolDefaultProfile)
  429. mPreviewImage->setControlProfile(toolDefaultProfile);
  430. updatePreviewImage();
  431. addObject(mPreviewImage);
  432. //
  433. mPreviewBorderButton = new GuiBitmapButtonCtrl();
  434. mPreviewBorderButton->registerObject();
  435. if(toolDefaultProfile)
  436. mPreviewBorderButton->setControlProfile(toolDefaultProfile);
  437. mPreviewBorderButton->_setBitmap(StringTable->insert("ToolsModule:cubemapBtnBorder_n_image"));
  438. mPreviewBorderButton->setField("Command", szBuffer); //clicking the preview does the same thing as the edit button, for simplicity
  439. addObject(mPreviewBorderButton);
  440. //
  441. // Create "Open in Editor" button
  442. mEditButton = new GuiBitmapButtonCtrl();
  443. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  444. mEditButton->setField("Command", szBuffer);
  445. mEditButton->setText("Edit");
  446. mEditButton->setSizing(horizResizeLeft, vertResizeAspectTop);
  447. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "ToolsGuiButtonProfile");
  448. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  449. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  450. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this asset in the Material Editor");
  451. mEditButton->registerObject();
  452. addObject(mEditButton);
  453. //
  454. mUseHeightOverride = true;
  455. mHeightOverride = 72;
  456. return retCtrl;
  457. }
  458. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  459. {
  460. S32 rowSize = 18;
  461. S32 dividerPos, dividerMargin;
  462. mInspector->getDivider(dividerPos, dividerMargin);
  463. Point2I fieldExtent = getExtent();
  464. Point2I fieldPos = getPosition();
  465. mEditCtrlRect.set(0, 0, fieldExtent.x, fieldExtent.y);
  466. mLabel->resize(Point2I(mProfile->mTextOffset.x, 0), Point2I(fieldExtent.x, rowSize));
  467. RectI previewRect = RectI(Point2I(mProfile->mTextOffset.x, rowSize), Point2I(50, 50));
  468. mPreviewBorderButton->resize(previewRect.point, previewRect.extent);
  469. mPreviewImage->resize(previewRect.point, previewRect.extent);
  470. S32 editPos = previewRect.point.x + previewRect.extent.x + 10;
  471. mEdit->resize(Point2I(editPos, rowSize * 1.5), Point2I(fieldExtent.x - editPos - 5, rowSize));
  472. mEditButton->resize(Point2I(fieldExtent.x - 105, previewRect.point.y + previewRect.extent.y - rowSize), Point2I(100, rowSize));
  473. mBrowseButton->setHidden(true);
  474. return true;
  475. }
  476. void GuiInspectorTypeMaterialAssetPtr::updateValue()
  477. {
  478. Parent::updateValue();
  479. updatePreviewImage();
  480. }
  481. void GuiInspectorTypeMaterialAssetPtr::updatePreviewImage()
  482. {
  483. const char* matAssetId;
  484. if (mInspector->getInspectObject() != nullptr)
  485. matAssetId = mInspector->getInspectObject()->getDataField(mCaption, NULL);
  486. else
  487. matAssetId = Con::getVariable(mVariableName);
  488. //if what we're working with isn't even a valid asset, don't present like we found a good one
  489. if (!AssetDatabase.isDeclaredAsset(matAssetId))
  490. {
  491. mPreviewImage->_setBitmap(StringTable->insert("Core_Rendering:NoMaterial"));
  492. return;
  493. }
  494. AssetPtr<MaterialAsset> matAssetDef = matAssetId;
  495. if (matAssetDef.isNull() || matAssetDef->getStatus() != AssetBase::AssetErrCode::Ok ||
  496. matAssetDef->getMaterialDefinitionName() == StringTable->EmptyString())
  497. {
  498. mPreviewImage->_setBitmap(StringTable->insert("Core_Rendering:WarningMaterial"));
  499. return;
  500. }
  501. Material* matDef;
  502. if (!Sim::findObject(matAssetDef->getMaterialDefinitionName(), matDef))
  503. {
  504. mPreviewImage->_setBitmap(StringTable->insert("Core_Rendering:WarningMaterial"));
  505. return;
  506. }
  507. AssetPtr<ImageAsset> difMapAsset = matDef->getDiffuseMapAsset(0);
  508. if (difMapAsset.isNull() || difMapAsset->getStatus() != AssetBase::AssetErrCode::Ok)
  509. {
  510. mPreviewImage->_setBitmap(StringTable->insert("Core_Rendering:WarningMaterial"));
  511. return;
  512. }
  513. String matPreviewAssetId = String(difMapAsset->getAssetName()) + "_PreviewImage";
  514. matPreviewAssetId.replace(":", "_");
  515. matPreviewAssetId = "ToolsModule:" + matPreviewAssetId;
  516. if (AssetDatabase.isDeclaredAsset(matPreviewAssetId.c_str()))
  517. {
  518. mPreviewImage->setBitmap(StringTable->insert(matPreviewAssetId.c_str()));
  519. }
  520. else
  521. {
  522. if (AssetDatabase.isDeclaredAsset(difMapAsset->getAssetId()))
  523. {
  524. mPreviewImage->setBitmap(difMapAsset->getAssetId());
  525. }
  526. }
  527. if (mPreviewImage->getBitmapAsset().isNull())
  528. mPreviewImage->setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  529. }
  530. void GuiInspectorTypeMaterialAssetPtr::setPreviewImage(StringTableEntry assetId)
  531. {
  532. //if what we're working with isn't even a valid asset, don't present like we found a good one
  533. if (!AssetDatabase.isDeclaredAsset(assetId))
  534. {
  535. mPreviewImage->_setBitmap(StringTable->EmptyString());
  536. return;
  537. }
  538. String matPreviewAssetId = String(assetId) + "_PreviewImage";
  539. matPreviewAssetId.replace(":", "_");
  540. matPreviewAssetId = "ToolsModule:" + matPreviewAssetId;
  541. if (AssetDatabase.isDeclaredAsset(matPreviewAssetId.c_str()))
  542. {
  543. mPreviewImage->setBitmap(StringTable->insert(matPreviewAssetId.c_str()));
  544. }
  545. else
  546. {
  547. if (AssetDatabase.isDeclaredAsset(assetId))
  548. {
  549. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(assetId);
  550. if (matAsset && matAsset->getMaterialDefinition())
  551. {
  552. mPreviewImage->_setBitmap(matAsset->getMaterialDefinition()->_getDiffuseMap(0));
  553. }
  554. }
  555. }
  556. if (mPreviewImage->getBitmapAsset().isNull())
  557. mPreviewImage->_setBitmap(StringTable->insert("ToolsModule:genericAssetIcon_image"));
  558. }
  559. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId);
  560. ConsoleDocClass(GuiInspectorTypeMaterialAssetId,
  561. "@brief Inspector field type for Material Assets\n\n"
  562. "Editor use only.\n\n"
  563. "@internal"
  564. );
  565. void GuiInspectorTypeMaterialAssetId::consoleInit()
  566. {
  567. Parent::consoleInit();
  568. ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId");
  569. }
  570. #endif