MaterialAsset.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. //-----------------------------------------------------------------------------
  38. IMPLEMENT_CONOBJECT(MaterialAsset);
  39. ConsoleType(MaterialAssetPtr, TypeMaterialAssetPtr, MaterialAsset, ASSET_ID_FIELD_PREFIX)
  40. //-----------------------------------------------------------------------------
  41. ConsoleGetType(TypeMaterialAssetPtr)
  42. {
  43. // Fetch asset Id.
  44. return (*((AssetPtr<MaterialAsset>*)dptr)).getAssetId();
  45. }
  46. //-----------------------------------------------------------------------------
  47. ConsoleSetType(TypeMaterialAssetPtr)
  48. {
  49. // Was a single argument specified?
  50. if (argc == 1)
  51. {
  52. // Yes, so fetch field value.
  53. const char* pFieldValue = argv[0];
  54. // Fetch asset pointer.
  55. AssetPtr<MaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<MaterialAsset>*>((AssetPtrBase*)(dptr));
  56. // Is the asset pointer the correct type?
  57. if (pAssetPtr == NULL)
  58. {
  59. // No, so fail.
  60. //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue);
  61. return;
  62. }
  63. // Set asset.
  64. pAssetPtr->setAssetId(pFieldValue);
  65. return;
  66. }
  67. // Warn.
  68. Con::warnf("(TypeMaterialAssetPtr) - Cannot set multiple args to a single asset.");
  69. }
  70. ConsoleType(assetIdString, TypeMaterialAssetId, String, ASSET_ID_FIELD_PREFIX)
  71. ConsoleGetType(TypeMaterialAssetId)
  72. {
  73. // Fetch asset Id.
  74. return *((const char**)(dptr));
  75. }
  76. ConsoleSetType(TypeMaterialAssetId)
  77. {
  78. // Was a single argument specified?
  79. if (argc == 1)
  80. {
  81. // Yes, so fetch field value.
  82. const char* pFieldValue = argv[0];
  83. // Fetch asset Id.
  84. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  85. // Update asset value.
  86. *assetId = StringTable->insert(pFieldValue);
  87. return;
  88. }
  89. // Warn.
  90. Con::warnf("(TypeMaterialAssetId) - Cannot set multiple args to a single asset.");
  91. }
  92. //-----------------------------------------------------------------------------
  93. MaterialAsset::MaterialAsset()
  94. {
  95. mShaderGraphFile = "";
  96. mScriptFile = StringTable->EmptyString();
  97. mScriptPath = StringTable->EmptyString();
  98. mMatDefinitionName = StringTable->EmptyString();
  99. }
  100. //-----------------------------------------------------------------------------
  101. MaterialAsset::~MaterialAsset()
  102. {
  103. }
  104. //-----------------------------------------------------------------------------
  105. void MaterialAsset::initPersistFields()
  106. {
  107. // Call parent.
  108. Parent::initPersistFields();
  109. //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), "");
  110. addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset),
  111. &setScriptFile, &getScriptFile, "Path to the file containing the material definition.");
  112. addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for.");
  113. }
  114. void MaterialAsset::initializeAsset()
  115. {
  116. // Call parent.
  117. Parent::initializeAsset();
  118. compileShader();
  119. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  120. if (Platform::isFile(mScriptPath))
  121. Con::executeFile(mScriptPath, false, false);
  122. }
  123. void MaterialAsset::onAssetRefresh()
  124. {
  125. mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath;
  126. if (Platform::isFile(mScriptPath))
  127. Con::executeFile(mScriptPath, false, false);
  128. if (mMatDefinitionName != StringTable->EmptyString())
  129. {
  130. Material* matDef;
  131. if (!Sim::findObject(mMatDefinitionName, matDef))
  132. {
  133. Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName);
  134. return;
  135. }
  136. matDef->reload();
  137. }
  138. }
  139. void MaterialAsset::setScriptFile(const char* pScriptFile)
  140. {
  141. // Sanity!
  142. AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file.");
  143. // Fetch image file.
  144. pScriptFile = StringTable->insert(pScriptFile);
  145. // Update.
  146. mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile;
  147. // Refresh the asset.
  148. refreshAsset();
  149. }
  150. //------------------------------------------------------------------------------
  151. void MaterialAsset::compileShader()
  152. {
  153. }
  154. void MaterialAsset::copyTo(SimObject* object)
  155. {
  156. // Call to parent.
  157. Parent::copyTo(object);
  158. }
  159. DefineEngineMethod(MaterialAsset, compileShader, void, (), , "Compiles the material's generated shader, if any. Not yet implemented\n")
  160. {
  161. object->compileShader();
  162. }
  163. //------------------------------------------------------------------------------
  164. StringTableEntry MaterialAsset::getAssetIdByMaterialName(StringTableEntry matName)
  165. {
  166. StringTableEntry materialAssetId = StringTable->EmptyString();
  167. AssetQuery* query = new AssetQuery();
  168. U32 foundCount = AssetDatabase.findAssetType(query, "MaterialAsset");
  169. if (foundCount == 0)
  170. {
  171. //Didn't work, so have us fall back to a placeholder asset
  172. materialAssetId = StringTable->insert("Core_Rendering:noMaterial");
  173. }
  174. else
  175. {
  176. for (U32 i = 0; i < foundCount; i++)
  177. {
  178. MaterialAsset* matAsset = AssetDatabase.acquireAsset<MaterialAsset>(query->mAssetList[i]);
  179. if (matAsset && matAsset->getMaterialDefinitionName() == matName)
  180. {
  181. materialAssetId = matAsset->getAssetId();
  182. break;
  183. }
  184. }
  185. }
  186. return materialAssetId;
  187. }
  188. bool MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset)
  189. {
  190. (*materialAsset) = assetId;
  191. if (!materialAsset->isNull())
  192. return true;
  193. //Didn't work, so have us fall back to a placeholder asset
  194. StringTableEntry noImageId = StringTable->insert("Core_Rendering:noMaterial");
  195. materialAsset->setAssetId(noImageId);
  196. if (!materialAsset->isNull())
  197. return true;
  198. return false;
  199. }
  200. //-----------------------------------------------------------------------------
  201. // GuiInspectorTypeAssetId
  202. //-----------------------------------------------------------------------------
  203. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr);
  204. ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr,
  205. "@brief Inspector field type for Shapes\n\n"
  206. "Editor use only.\n\n"
  207. "@internal"
  208. );
  209. void GuiInspectorTypeMaterialAssetPtr::consoleInit()
  210. {
  211. Parent::consoleInit();
  212. ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr");
  213. }
  214. GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl()
  215. {
  216. // Create base filename edit controls
  217. GuiControl* retCtrl = Parent::constructEditControl();
  218. if (retCtrl == NULL)
  219. return retCtrl;
  220. // Change filespec
  221. char szBuffer[512];
  222. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  223. mInspector->getInspectObject()->getIdString(), mCaption);
  224. mBrowseButton->setField("Command", szBuffer);
  225. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  226. // Create "Open in Editor" button
  227. mEditButton = new GuiBitmapButtonCtrl();
  228. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId());
  229. mEditButton->setField("Command", szBuffer);
  230. char bitmapName[512] = "tools/worldEditor/images/toolbar/material-editor";
  231. mEditButton->setBitmap(bitmapName);
  232. mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  233. mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  234. mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  235. mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Material Editor");
  236. mEditButton->registerObject();
  237. addObject(mEditButton);
  238. return retCtrl;
  239. }
  240. bool GuiInspectorTypeMaterialAssetPtr::updateRects()
  241. {
  242. S32 dividerPos, dividerMargin;
  243. mInspector->getDivider(dividerPos, dividerMargin);
  244. Point2I fieldExtent = getExtent();
  245. Point2I fieldPos = getPosition();
  246. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  247. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  248. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  249. if (mBrowseButton != NULL)
  250. {
  251. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  252. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  253. }
  254. if (mEditButton != NULL)
  255. {
  256. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  257. resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent);
  258. }
  259. return resized;
  260. }
  261. IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId);
  262. ConsoleDocClass(GuiInspectorTypeMaterialAssetId,
  263. "@brief Inspector field type for Material Assets\n\n"
  264. "Editor use only.\n\n"
  265. "@internal"
  266. );
  267. void GuiInspectorTypeMaterialAssetId::consoleInit()
  268. {
  269. Parent::consoleInit();
  270. ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId");
  271. }