MaterialAsset.cpp
Engine/source/T3D/assets/MaterialAsset.cpp
Public Functions
ConsoleDocClass(GuiInspectorTypeMaterialAssetId , "@brief Inspector field type <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/class/classmaterial/">Material</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Assets\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr , "@brief Inspector field type <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Shapes\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
ConsoleSetType(TypeMaterialAssetId )
ConsoleSetType(TypeMaterialAssetPtr )
ConsoleType(assetIdString , TypeMaterialAssetId , String , ASSET_ID_FIELD_PREFIX )
ConsoleType(MaterialAssetPtr , TypeMaterialAssetPtr , MaterialAsset , ASSET_ID_FIELD_PREFIX )
DefineEngineMethod(MaterialAsset , compileShader , void , () , "Compiles the material's generated shader, <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> any. Not yet <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">implemented\n</a>" )
Detailed Description
Public Functions
ConsoleDocClass(GuiInspectorTypeMaterialAssetId , "@brief Inspector field type <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/class/classmaterial/">Material</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Assets\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr , "@brief Inspector field type <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1a2732ab74fa0237854c2ba0f75f88a624">for</a> <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">Shapes\n\n</a>" "Editor use <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">only.\n\n</a>" "@internal" )
ConsoleSetType(TypeMaterialAssetId )
ConsoleSetType(TypeMaterialAssetPtr )
ConsoleType(assetIdString , TypeMaterialAssetId , String , ASSET_ID_FIELD_PREFIX )
ConsoleType(MaterialAssetPtr , TypeMaterialAssetPtr , MaterialAsset , ASSET_ID_FIELD_PREFIX )
DefineEngineMethod(MaterialAsset , compileShader , void , () , "Compiles the material's generated shader, <a href="/coding/file/tsmeshintrinsics_8cpp/#tsmeshintrinsics_8cpp_1a4e4fa7e3399708e0777b5308db01278c">if</a> any. Not yet <a href="/coding/file/cmdscan_8cpp/#cmdscan_8cpp_1aeab71244afb687f16d8c4f5ee9d6ef0e">implemented\n</a>" )
IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId )
IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr )
IMPLEMENT_CONOBJECT(MaterialAsset )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2013 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23#pragma once 24 25#ifndef MATERIALASSET_H 26#include "MaterialAsset.h" 27#endif 28 29#ifndef _ASSET_MANAGER_H_ 30#include "assets/assetManager.h" 31#endif 32 33#ifndef _CONSOLETYPES_H_ 34#include "console/consoleTypes.h" 35#endif 36 37#ifndef _TAML_ 38#include "persistence/taml/taml.h" 39#endif 40 41#ifndef _ASSET_PTR_H_ 42#include "assets/assetPtr.h" 43#endif 44 45#include "T3D/assets/assetImporter.h" 46 47//----------------------------------------------------------------------------- 48 49IMPLEMENT_CONOBJECT(MaterialAsset); 50 51ConsoleType(MaterialAssetPtr, TypeMaterialAssetPtr, MaterialAsset, ASSET_ID_FIELD_PREFIX) 52 53//----------------------------------------------------------------------------- 54 55ConsoleGetType(TypeMaterialAssetPtr) 56{ 57 // Fetch asset Id. 58 return (*((AssetPtr<MaterialAsset>*)dptr)).getAssetId(); 59} 60 61//----------------------------------------------------------------------------- 62 63ConsoleSetType(TypeMaterialAssetPtr) 64{ 65 // Was a single argument specified? 66 if (argc == 1) 67 { 68 // Yes, so fetch field value. 69 const char* pFieldValue = argv[0]; 70 71 // Fetch asset pointer. 72 AssetPtr<MaterialAsset>* pAssetPtr = dynamic_cast<AssetPtr<MaterialAsset>*>((AssetPtrBase*)(dptr)); 73 74 // Is the asset pointer the correct type? 75 if (pAssetPtr == NULL) 76 { 77 // No, so fail. 78 //Con::warnf("(TypeMaterialAssetPtr) - Failed to set asset Id '%d'.", pFieldValue); 79 return; 80 } 81 82 // Set asset. 83 pAssetPtr->setAssetId(pFieldValue); 84 85 return; 86 } 87 88 // Warn. 89 Con::warnf("(TypeMaterialAssetPtr) - Cannot set multiple args to a single asset."); 90} 91 92 93ConsoleType(assetIdString, TypeMaterialAssetId, String, ASSET_ID_FIELD_PREFIX) 94 95ConsoleGetType(TypeMaterialAssetId) 96{ 97 // Fetch asset Id. 98 return *((const char**)(dptr)); 99} 100 101ConsoleSetType(TypeMaterialAssetId) 102{ 103 // Was a single argument specified? 104 if (argc == 1) 105 { 106 // Yes, so fetch field value. 107 const char* pFieldValue = argv[0]; 108 109 // Fetch asset Id. 110 StringTableEntry* assetId = (StringTableEntry*)(dptr); 111 112 // Update asset value. 113 *assetId = StringTable->insert(pFieldValue); 114 115 return; 116 } 117 118 // Warn. 119 Con::warnf("(TypeMaterialAssetId) - Cannot set multiple args to a single asset."); 120} 121//----------------------------------------------------------------------------- 122 123MaterialAsset::MaterialAsset() 124{ 125 mShaderGraphFile = ""; 126 mScriptFile = StringTable->EmptyString(); 127 mScriptPath = StringTable->EmptyString(); 128 mMatDefinitionName = StringTable->EmptyString(); 129} 130 131//----------------------------------------------------------------------------- 132 133MaterialAsset::~MaterialAsset() 134{ 135} 136 137//----------------------------------------------------------------------------- 138 139void MaterialAsset::initPersistFields() 140{ 141 // Call parent. 142 Parent::initPersistFields(); 143 144 //addField("shaderGraph", TypeRealString, Offset(mShaderGraphFile, MaterialAsset), ""); 145 addProtectedField("scriptFile", TypeAssetLooseFilePath, Offset(mScriptFile, MaterialAsset), 146 &setScriptFile, &getScriptFile, "Path to the file containing the material definition."); 147 148 addField("materialDefinitionName", TypeString, Offset(mMatDefinitionName, MaterialAsset), "Name of the material definition this asset is for."); 149} 150 151void MaterialAsset::initializeAsset() 152{ 153 // Call parent. 154 Parent::initializeAsset(); 155 156 compileShader(); 157 158 mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath; 159 160 if (Platform::isFile(mScriptPath)) 161 Con::executeFile(mScriptPath, false, false); 162} 163 164void MaterialAsset::onAssetRefresh() 165{ 166 mScriptPath = getOwned() ? expandAssetFilePath(mScriptFile) : mScriptPath; 167 168 if (Platform::isFile(mScriptPath)) 169 Con::executeFile(mScriptPath, false, false); 170 171 if (mMatDefinitionName != StringTable->EmptyString()) 172 { 173 Material* matDef; 174 if (!Sim::findObject(mMatDefinitionName, matDef)) 175 { 176 Con::errorf("MaterialAsset: Unable to find the Material %s", mMatDefinitionName); 177 return; 178 } 179 180 matDef->reload(); 181 } 182} 183 184void MaterialAsset::setScriptFile(const char* pScriptFile) 185{ 186 // Sanity! 187 AssertFatal(pScriptFile != NULL, "Cannot use a NULL script file."); 188 189 // Fetch image file. 190 pScriptFile = StringTable->insert(pScriptFile); 191 192 // Update. 193 mScriptFile = getOwned() ? expandAssetFilePath(pScriptFile) : pScriptFile; 194 195 // Refresh the asset. 196 refreshAsset(); 197} 198 199//------------------------------------------------------------------------------ 200 201void MaterialAsset::compileShader() 202{ 203} 204 205void MaterialAsset::copyTo(SimObject* object) 206{ 207 // Call to parent. 208 Parent::copyTo(object); 209} 210 211DefineEngineMethod(MaterialAsset, compileShader, void, (), , "Compiles the material's generated shader, if any. Not yet implemented\n") 212{ 213 object->compileShader(); 214} 215 216//------------------------------------------------------------------------------ 217StringTableEntry MaterialAsset::getAssetIdByMaterialName(StringTableEntry matName) 218{ 219 StringTableEntry materialAssetId = StringTable->EmptyString(); 220 221 AssetQuery* query = new AssetQuery(); 222 U32 foundCount = AssetDatabase.findAssetType(query, "MaterialAsset"); 223 if (foundCount == 0) 224 { 225 //Didn't work, so have us fall back to a placeholder asset 226 materialAssetId = StringTable->insert("Core_Rendering:noMaterial"); 227 } 228 else 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 break; 237 } 238 AssetDatabase.releaseAsset(query->mAssetList[i]); //cleanup if that's not the one we needed 239 } 240 241 if (materialAssetId == StringTable->EmptyString()) 242 { 243 //Try auto-importing it if it exists already 244 BaseMaterialDefinition* baseMatDef; 245 if (!Sim::findObject(matName, baseMatDef)) 246 { 247 //Not even a real material, apparently? 248 //return back a blank 249 return StringTable->EmptyString(); 250 } 251 252 //Ok, a real mat def, we can work with this 253#if TORQUE_DEBUG 254 Con::warnf("MaterialAsset::getAssetIdByMaterialName - Attempted to in-place import a material(%s) that had no associated asset", matName); 255#endif 256 257 AssetImporter* autoAssetImporter; 258 if (!Sim::findObject("autoAssetImporter", autoAssetImporter)) 259 { 260 autoAssetImporter = new AssetImporter(); 261 autoAssetImporter->registerObject("autoAssetImporter"); 262 } 263 264 autoAssetImporter->resetImportSession(true); 265 266 String originalMaterialDefFile = Torque::Path(baseMatDef->getFilename()).getPath(); 267 268 autoAssetImporter->setTargetPath(originalMaterialDefFile); 269 270 autoAssetImporter->resetImportConfig(); 271 272 AssetImportObject* assetObj = autoAssetImporter->addImportingAsset("MaterialAsset", originalMaterialDefFile, nullptr, matName); 273 274 //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 275 ModuleDefinition* targetModuleDef = AssetImporter::getModuleFromPath(originalMaterialDefFile); 276 277 if (targetModuleDef == nullptr) 278 { 279 return StringTable->EmptyString(); 280 } 281 else 282 { 283 autoAssetImporter->setTargetModuleId(targetModuleDef->getModuleId()); 284 } 285 286 autoAssetImporter->processImportAssets(); 287 288 bool hasIssues = autoAssetImporter->validateAssets(); 289 290 if (hasIssues) 291 { 292 //log it 293 Con::errorf("Error! Import process of Material(%s) has failed due to issues discovered during validation!", matName); 294 return StringTable->EmptyString(); 295 } 296 else 297 { 298 autoAssetImporter->importAssets(); 299 } 300 301#if TORQUE_DEBUG 302 autoAssetImporter->dumpActivityLog(); 303#endif 304 305 if (hasIssues) 306 { 307 return StringTable->EmptyString(); 308 } 309 else 310 { 311 String assetId = autoAssetImporter->getTargetModuleId() + ":" + assetObj->assetName; 312 return StringTable->insert(assetId.c_str()); 313 } 314 } 315 } 316 317 return materialAssetId; 318} 319 320bool MaterialAsset::getAssetById(StringTableEntry assetId, AssetPtr<MaterialAsset>* materialAsset) 321{ 322 (*materialAsset) = assetId; 323 324 if (!materialAsset->isNull()) 325 return true; 326 327 //Didn't work, so have us fall back to a placeholder asset 328 StringTableEntry noImageId = StringTable->insert("Core_Rendering:noMaterial"); 329 materialAsset->setAssetId(noImageId); 330 331 if (!materialAsset->isNull()) 332 return true; 333 334 return false; 335} 336 337//----------------------------------------------------------------------------- 338// GuiInspectorTypeAssetId 339//----------------------------------------------------------------------------- 340 341IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetPtr); 342 343ConsoleDocClass(GuiInspectorTypeMaterialAssetPtr, 344 "@brief Inspector field type for Shapes\n\n" 345 "Editor use only.\n\n" 346 "@internal" 347); 348 349void GuiInspectorTypeMaterialAssetPtr::consoleInit() 350{ 351 Parent::consoleInit(); 352 353 ConsoleBaseType::getType(TypeMaterialAssetPtr)->setInspectorFieldType("GuiInspectorTypeMaterialAssetPtr"); 354} 355 356GuiControl* GuiInspectorTypeMaterialAssetPtr::constructEditControl() 357{ 358 // Create base filename edit controls 359 GuiControl* retCtrl = Parent::constructEditControl(); 360 if (retCtrl == NULL) 361 return retCtrl; 362 363 // Change filespec 364 char szBuffer[512]; 365 dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"MaterialAsset\", \"AssetBrowser.changeAsset\", %s, \"\");", 366 getIdString()); 367 mBrowseButton->setField("Command", szBuffer); 368 369 setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString()); 370 371 // Create "Open in Editor" button 372 mEditButton = new GuiBitmapButtonCtrl(); 373 374 dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.editAsset(%d.getText());", retCtrl->getId()); 375 mEditButton->setField("Command", szBuffer); 376 377 char bitmapName[512] = "tools/worldEditor/images/toolbar/material-editor"; 378 mEditButton->setBitmap(bitmapName); 379 380 mEditButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile"); 381 mEditButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile"); 382 mEditButton->setDataField(StringTable->insert("hovertime"), NULL, "1000"); 383 mEditButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Material Editor"); 384 385 mEditButton->registerObject(); 386 addObject(mEditButton); 387 388 return retCtrl; 389} 390 391bool GuiInspectorTypeMaterialAssetPtr::updateRects() 392{ 393 S32 dividerPos, dividerMargin; 394 mInspector->getDivider(dividerPos, dividerMargin); 395 Point2I fieldExtent = getExtent(); 396 Point2I fieldPos = getPosition(); 397 398 mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y); 399 mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y); 400 401 bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent); 402 if (mBrowseButton != NULL) 403 { 404 mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4); 405 resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent); 406 } 407 408 if (mEditButton != NULL) 409 { 410 RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4); 411 resized |= mEditButton->resize(shapeEdRect.point, shapeEdRect.extent); 412 } 413 414 return resized; 415} 416 417IMPLEMENT_CONOBJECT(GuiInspectorTypeMaterialAssetId); 418 419ConsoleDocClass(GuiInspectorTypeMaterialAssetId, 420 "@brief Inspector field type for Material Assets\n\n" 421 "Editor use only.\n\n" 422 "@internal" 423); 424 425void GuiInspectorTypeMaterialAssetId::consoleInit() 426{ 427 Parent::consoleInit(); 428 429 ConsoleBaseType::getType(TypeMaterialAssetId)->setInspectorFieldType("GuiInspectorTypeMaterialAssetId"); 430} 431