MaterialAsset.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. //-----------------------------------------------------------------------------
  40. IMPLEMENT_CONOBJECT(MaterialAsset);
  41. ConsoleType(MaterialAssetPtr, TypeMaterialAssetPtr, MaterialAsset, ASSET_ID_FIELD_PREFIX)
  42. //-----------------------------------------------------------------------------
  43. ConsoleGetType(TypeMaterialAssetPtr)
  44. {
  45. // Fetch asset Id.
  46. return (*((AssetPtr<MaterialAsset>*)dptr)).getAssetId();
  47. }
  48. //-----------------------------------------------------------------------------
  49. ConsoleSetType(TypeMaterialAssetPtr)
  50. {
  51. // Was a single argument specified?
  52. if (argc == 1)
  53. {
  54. // Yes, so fetch field value.
  55. const char* pFieldValue = argv[0];
  56. // Fetch asset pointer.
  57. AssetPtr<MaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<MaterialAsset>*>((AssetPtrBase*)(dptr));
  58. // Is the asset pointer the correct type?
  59. if (pAssetPtr == NULL)
  60. {
  61. // No, so fail.
  62. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  63. return;
  64. }
  65. // Set asset.
  66. pAssetPtr->setAssetId(pFieldValue);
  67. return;
  68. }
  69. // Warn.
  70. Con::warnf("(TypeMaterialAssetPtr) - Cannot set multiple args to a single asset.");
  71. }
  72. ConsoleType(assetIdString, TypeMaterialAssetId, String, ASSET_ID_FIELD_PREFIX)
  73. ConsoleGetType(TypeMaterialAssetId)
  74. {
  75. // Fetch asset Id.
  76. return *((const char**)(dptr));
  77. }
  78. ConsoleSetType(TypeMaterialAssetId)
  79. {
  80. // Was a single argument specified?
  81. if (argc == 1)
  82. {
  83. // Yes, so fetch field value.
  84. const char* pFieldValue = argv[0];
  85. // Fetch asset Id.
  86. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  87. // Update asset value.
  88. *assetId = StringTable->insert(pFieldValue);
  89. return;
  90. }
  91. // Warn.
  92. Con::warnf("(TypeMaterialAssetId) - Cannot set multiple args to a single asset.");
  93. }
  94. //-----------------------------------------------------------------------------
  95. MaterialAsset::MaterialAsset()
  96. {
  97. mShaderGraphFile = "";
  98. mScriptFile = StringTable->EmptyString();
  99. mScriptPath = StringTable->EmptyString();
  100. mMatDefinitionName = StringTable->EmptyString();
  101. }
  102. //-----------------------------------------------------------------------------
  103. MaterialAsset::~MaterialAsset()
  104. {
  105. }
  106. //-----------------------------------------------------------------------------
  107. void MaterialAsset::initPersistFields()
  108. {
  109. // Call parent.
  110. Parent::initPersistFields();
  111. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), "");
  112. addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset),
  113. &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  114. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for.");
  115. }
  116. void MaterialAsset::initializeAsset()
  117. {
  118. // Call parent.
  119. Parent::initializeAsset();
  120. compileShader();
  121. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  122. if (Platform::isFile(mScriptPath))
  123. Con::executeFile(mScriptPath, false, false);
  124. }
  125. void MaterialAsset::onAssetRefresh()
  126. {
  127. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  128. if (Platform::isFile(mScriptPath))
  129. Con::executeFile(mScriptPath, false, false);
  130. if (mMatDefinitionName != StringTable->EmptyString())
  131. {
  132. Material* matDef;
  133. if (!Sim::findObject(mMatDefinitionName, matDef))
  134. {
  135. Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  136. return;
  137. }
  138. matDef->reload();
  139. }
  140. }
  141. void MaterialAsset::setScriptFile(const char* pScriptFile)
  142. {
  143. // Sanity!
  144. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  145. // Fetch image file.
  146. pScriptFile = StringTable->insert(pScriptFile);
  147. // Update.
  148. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  149. // Refresh the asset.
  150. refreshAsset();
  151. }
  152. //------------------------------------------------------------------------------
  153. void MaterialAsset::compileShader()
  154. {
  155. }
  156. void MaterialAsset::copyTo(SimObject* object)
  157. {
  158. // Call to parent.
  159. Parent::copyTo(object);
  160. }
  161. DefineEngineMethod(MaterialAsset, compileShader, void, (), , "Compiles the material's generated shader, if any. Not yet implemented\n")
  162. {
  163. object->compileShader();
  164. }
  165. //------------------------------------------------------------------------------
  166. StringTableEntry MaterialAsset::getAssetIdByMaterialName(StringTableEntry matName)
  167. {
  168. StringTableEntry materialAssetId = StringTable->EmptyString();
  169. AssetQuery* query = new AssetQuery();
  170. U32 foundCount = AssetDatabase.findAssetType(query, "MaterialAsset");
  171. if (foundCount == 0)
  172. {
  173. //Didn't work, so have us fall back to a placeholder asset
  174. materialAssetId = StringTable->insert("Core_Rendering:noMaterial");
  175. }
  176. else
  177. {
  178. for (U32 i = 0; i < foundCount; i++)
  179. {
  180. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(query->mAssetList[i]);
  181. if (matAsset && matAsset->getMaterialDefinitionName() == matName)
  182. {
  183. materialAssetId = matAsset->getAssetId();
  184. break;
  185. }
  186. AssetDatabase.releaseAsset(query->mAssetList[i]); //cleanup if that's not the one we needed
  187. }
  188. if (materialAssetId == StringTable->EmptyString())
  189. {
  190. //Try auto-importing it if it exists already
  191. BaseMaterialDefinition* baseMatDef;
  192. if (!Sim::findObject(matName, baseMatDef))
  193. {
  194. //Not even a real material, apparently?
  195. //return back a blank
  196. return StringTable->EmptyString();
  197. }
  198. //Ok, a real mat def, we can work with this
  199. #if TORQUE_DEBUG
  200. Con::warnf("MaterialAsset::getAssetIdByMaterialName - Attempted to in-place import a material(%s) that had no associated asset", matName);
  201. #endif
  202. AssetImporter* autoAssetImporter;
  203. if (!Sim::findObject("autoAssetImporter", autoAssetImporter))
  204. {
  205. autoAssetImporter = new AssetImporter();
  206. autoAssetImporter->registerObject("autoAssetImporter");
  207. }
  208. autoAssetImporter->resetImportSession(true);
  209. String originalMaterialDefFile = Torque::Path(baseMatDef->getFilename()).getPath();
  210. autoAssetImporter->setTargetPath(originalMaterialDefFile);
  211. autoAssetImporter->resetImportConfig();
  212. AssetImportObject* assetObj = autoAssetImporter->addImportingAsset("MaterialAsset", originalMaterialDefFile, nullptr, matName);
  213. //Find out if the filepath has an associated module to it. If we're importing in-place, it needs to be within a module's directory
  214. ModuleDefinition* targetModuleDef = AssetImporter::getModuleFromPath(originalMaterialDefFile);
  215. if (targetModuleDef == nullptr)
  216. {
  217. return StringTable->EmptyString();
  218. }
  219. else
  220. {
  221. autoAssetImporter->setTargetModuleId(targetModuleDef->getModuleId());
  222. }
  223. autoAssetImporter->processImportAssets();
  224. bool hasIssues = autoAssetImporter->validateAssets();
  225. if (hasIssues)
  226. {
  227. //log it
  228. Con::errorf("Error! Import process of Material(%s) has failed due to issues discovered during validation!", matName);
  229. return StringTable->EmptyString();
  230. }
  231. else
  232. {
  233. autoAssetImporter->importAssets();
  234. }
  235. #if TORQUE_DEBUG
  236. autoAssetImporter->dumpActivityLog();
  237. #endif
  238. if (hasIssues)
  239. {
  240. return StringTable->EmptyString();
  241. }
  242. else
  243. {
  244. String assetId = autoAssetImporter->getTargetModuleId() + ":" + assetObj->assetName;
  245. return StringTable->insert(assetId.c_str());
  246. }
  247. }
  248. }
  249. return materialAssetId;
  250. }
  251. bool MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset)
  252. {
  253. (*materialAsset) = assetId;
  254. if (!materialAsset->isNull())
  255. return true;
  256. //Didn't work, so have us fall back to a placeholder asset
  257. StringTableEntry noImageId = StringTable->insert("Core_Rendering:noMaterial");
  258. materialAsset->setAssetId(noImageId);
  259. if (!materialAsset->isNull())
  260. return true;
  261. return false;
  262. }
  263. //-----------------------------------------------------------------------------
  264. // GuiInspectorTypeAssetId
  265. //-----------------------------------------------------------------------------
  266. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  267. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  268. "@brief Inspector field type for Shapes\n\n"
  269. "Editor use only.\n\n"
  270. "@internal"
  271. );
  272. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  273. {
  274. Parent::consoleInit();
  275. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  276. }
  277. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  278. {
  279. // Create base filename edit controls
  280. GuiControl* retCtrl = Parent::constructEditControl();
  281. if (retCtrl == NULL)
  282. return retCtrl;
  283. // Change filespec
  284. char szBuffer[512];
  285. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, \"\");",
  286. getIdString());
  287. mBrowseButton->setField("Command", szBuffer);
  288. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  289. // Create "Open in Editor" button
  290. mEditButton = new GuiBitmapButtonCtrl();
  291. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  292. mEditButton->setField("Command", szBuffer);
  293. char bitmapName[512] = "tools/worldEditor/images/toolbar/material-editor";
  294. mEditButton->setBitmap(bitmapName);
  295. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  296. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  297. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  298. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Material Editor");
  299. mEditButton->registerObject();
  300. addObject(mEditButton);
  301. return retCtrl;
  302. }
  303. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  304. {
  305. S32 dividerPos, dividerMargin;
  306. mInspector->getDivider(dividerPos, dividerMargin);
  307. Point2I fieldExtent = getExtent();
  308. Point2I fieldPos = getPosition();
  309. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  310. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  311. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  312. if (mBrowseButton != NULL)
  313. {
  314. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  315. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  316. }
  317. if (mEditButton != NULL)
  318. {
  319. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  320. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  321. }
  322. return resized;
  323. }
  324. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId);
  325. ConsoleDocClass(GuiInspectorTypeMaterialAssetId,
  326. "@brief Inspector field type for Material Assets\n\n"
  327. "Editor use only.\n\n"
  328. "@internal"
  329. );
  330. void GuiInspectorTypeMaterialAssetId::consoleInit()
  331. {
  332. Parent::consoleInit();
  333. ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId");
  334. }