ShapeAsset.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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 _SHAPE_ASSET_H_
  23. #include "ShapeAsset.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 "core/resourceManager.h"
  38. // Debug Profiling.
  39. #include "platform/profiler.h"
  40. #include "T3D/assets/assetImporter.h"
  41. #ifdef TORQUE_TOOLS
  42. #include "ts/tsLastDetail.h"
  43. #endif
  44. #include "util/imposterCapture.h"
  45. #include "ts/tsShapeInstance.h"
  46. #include "gfx/bitmap/imageUtils.h"
  47. StringTableEntry ShapeAsset::smNoShapeAssetFallback = NULL;
  48. //-----------------------------------------------------------------------------
  49. IMPLEMENT_CONOBJECT(ShapeAsset);
  50. ConsoleType(assetIdString, TypeShapeAssetPtr, String, ASSET_ID_FIELD_PREFIX)
  51. ConsoleGetType(TypeShapeAssetPtr)
  52. {
  53. // Fetch asset Id.
  54. //return *((StringTableEntry*)dptr);
  55. return (*((AssetPtr<ShapeAsset>*)dptr)).getAssetId();
  56. }
  57. ConsoleSetType(TypeShapeAssetPtr)
  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 Id.
  65. StringTableEntry* assetId = (StringTableEntry*)(dptr);
  66. // Update asset value.
  67. *assetId = StringTable->insert(pFieldValue);
  68. return;
  69. }
  70. // Warn.
  71. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  72. }
  73. //-----------------------------------------------------------------------------
  74. ConsoleType(assetIdString, TypeShapeAssetId, const char*, ASSET_ID_FIELD_PREFIX)
  75. ConsoleGetType(TypeShapeAssetId)
  76. {
  77. // Fetch asset Id.
  78. return *((const char**)(dptr));
  79. }
  80. ConsoleSetType(TypeShapeAssetId)
  81. {
  82. // Was a single argument specified?
  83. if (argc == 1)
  84. {
  85. // Yes, so fetch field value.
  86. *((const char**)dptr) = StringTable->insert(argv[0]);
  87. return;
  88. }
  89. // Warn.
  90. Con::warnf("(TypeAssetId) - Cannot set multiple args to a single asset.");
  91. }
  92. //-----------------------------------------------------------------------------
  93. const String ShapeAsset::mShapeErrCodeStrings[] =
  94. {
  95. "TooManyVerts",
  96. "TooManyBones",
  97. "MissingAnimatons",
  98. "UnKnown"
  99. };
  100. //-----------------------------------------------------------------------------
  101. ShapeAsset::ShapeAsset()
  102. {
  103. mFileName = StringTable->EmptyString();
  104. mConstructorFileName = StringTable->EmptyString();
  105. mFilePath = StringTable->EmptyString();
  106. mConstructorFilePath = StringTable->EmptyString();
  107. mDiffuseImposterFileName = StringTable->EmptyString();
  108. mDiffuseImposterPath = StringTable->EmptyString();
  109. mNormalImposterFileName = StringTable->EmptyString();
  110. mNormalImposterPath = StringTable->EmptyString();
  111. mLoadedState = AssetErrCode::NotLoaded;
  112. }
  113. //-----------------------------------------------------------------------------
  114. ShapeAsset::~ShapeAsset()
  115. {
  116. }
  117. //-----------------------------------------------------------------------------
  118. void ShapeAsset::consoleInit()
  119. {
  120. Parent::consoleInit();
  121. Con::addVariable("$Core::NoShapeAssetFallback", TypeString, &smNoShapeAssetFallback,
  122. "The assetId of the shape to display when the requested shape asset is missing.\n"
  123. "@ingroup GFX\n");
  124. smNoShapeAssetFallback = StringTable->insert(Con::getVariable("$Core::NoShapeAssetFallback"));
  125. }
  126. //-----------------------------------------------------------------------------
  127. void ShapeAsset::initPersistFields()
  128. {
  129. // Call parent.
  130. Parent::initPersistFields();
  131. addProtectedField("fileName", TypeAssetLooseFilePath, Offset(mFileName, ShapeAsset),
  132. &setShapeFile, &getShapeFile, "Path to the shape file we want to render");
  133. addProtectedField("constuctorFileName", TypeAssetLooseFilePath, Offset(mConstructorFileName, ShapeAsset),
  134. &setShapeConstructorFile, &getShapeConstructorFile, "Path to the shape file we want to render");
  135. addProtectedField("diffuseImposterFileName", TypeAssetLooseFilePath, Offset(mDiffuseImposterFileName, ShapeAsset),
  136. &setDiffuseImposterFile, &getDiffuseImposterFile, "Path to the diffuse imposter file we want to render");
  137. addProtectedField("normalImposterFileName", TypeAssetLooseFilePath, Offset(mNormalImposterFileName, ShapeAsset),
  138. &setNormalImposterFile, &getNormalImposterFile, "Path to the normal imposter file we want to render");
  139. }
  140. void ShapeAsset::setDataField(StringTableEntry slotName, StringTableEntry array, StringTableEntry value)
  141. {
  142. Parent::setDataField(slotName, array, value);
  143. //Now, if it's a material slot of some fashion, set it up
  144. StringTableEntry matSlotName = StringTable->insert("materialAsset");
  145. if (String(slotName).startsWith(matSlotName))
  146. {
  147. StringTableEntry matId = StringTable->insert(value);
  148. mMaterialAssetIds.push_back(matId);
  149. }
  150. }
  151. void ShapeAsset::initializeAsset()
  152. {
  153. // Call parent.
  154. Parent::initializeAsset();
  155. if (mFileName == StringTable->EmptyString())
  156. return;
  157. ResourceManager::get().getChangedSignal().notify(this, &ShapeAsset::_onResourceChanged);
  158. //Ensure our path is expando'd if it isn't already
  159. mFilePath = getOwned() ? expandAssetFilePath(mFileName) : mFilePath;
  160. mConstructorFilePath = getOwned() ? expandAssetFilePath(mConstructorFileName) : mConstructorFilePath;
  161. if (!Torque::FS::IsFile(mConstructorFilePath))
  162. Con::errorf("ShapeAsset::initializeAsset (%s) could not find %s!", getAssetName(), mConstructorFilePath);
  163. mDiffuseImposterPath = getOwned() ? expandAssetFilePath(mDiffuseImposterFileName) : mDiffuseImposterFileName;
  164. if (mDiffuseImposterPath == StringTable->EmptyString())
  165. {
  166. String diffusePath = String(mFilePath) + "_imposter.dds";
  167. mDiffuseImposterPath = StringTable->insert(diffusePath.c_str());
  168. }
  169. mNormalImposterPath = getOwned() ? expandAssetFilePath(mNormalImposterFileName) : mNormalImposterFileName;
  170. if (mNormalImposterPath == StringTable->EmptyString())
  171. {
  172. String normalPath = String(mFilePath) + "_imposter_normals.dds";
  173. mNormalImposterPath = StringTable->insert(normalPath.c_str());
  174. }
  175. loadShape();
  176. }
  177. void ShapeAsset::setShapeFile(const char* pShapeFile)
  178. {
  179. // Sanity!
  180. AssertFatal(pShapeFile != NULL, "Cannot use a NULL shape file.");
  181. // Fetch image file.
  182. pShapeFile = StringTable->insert(pShapeFile, true);
  183. // Ignore no change,
  184. if (pShapeFile == mFileName)
  185. return;
  186. mFileName = getOwned() ? expandAssetFilePath(pShapeFile) : pShapeFile;
  187. // Refresh the asset.
  188. refreshAsset();
  189. }
  190. void ShapeAsset::setShapeConstructorFile(const char* pShapeConstructorFile)
  191. {
  192. // Sanity!
  193. AssertFatal(pShapeConstructorFile != NULL, "Cannot use a NULL shape constructor file.");
  194. // Fetch image file.
  195. pShapeConstructorFile = StringTable->insert(pShapeConstructorFile, true);
  196. // Ignore no change,
  197. if (pShapeConstructorFile == mConstructorFileName)
  198. return;
  199. mConstructorFileName = getOwned() ? expandAssetFilePath(pShapeConstructorFile) : pShapeConstructorFile;
  200. // Refresh the asset.
  201. refreshAsset();
  202. }
  203. void ShapeAsset::setDiffuseImposterFile(const char* pImageFile)
  204. {
  205. // Sanity!
  206. AssertFatal(pImageFile != NULL, "Cannot use a NULL image file.");
  207. // Fetch image file.
  208. pImageFile = StringTable->insert(pImageFile, true);
  209. // Ignore no change,
  210. if (pImageFile == mDiffuseImposterFileName)
  211. return;
  212. mDiffuseImposterFileName = getOwned() ? expandAssetFilePath(pImageFile) : pImageFile;
  213. // Refresh the asset.
  214. refreshAsset();
  215. }
  216. void ShapeAsset::setNormalImposterFile(const char* pImageFile)
  217. {
  218. // Sanity!
  219. AssertFatal(pImageFile != NULL, "Cannot use a NULL image file.");
  220. // Fetch image file.
  221. pImageFile = StringTable->insert(pImageFile, true);
  222. // Ignore no change,
  223. if (pImageFile == mNormalImposterFileName)
  224. return;
  225. mNormalImposterFileName = getOwned() ? expandAssetFilePath(pImageFile) : pImageFile;
  226. // Refresh the asset.
  227. refreshAsset();
  228. }
  229. void ShapeAsset::_onResourceChanged(const Torque::Path &path)
  230. {
  231. if (path != Torque::Path(mFilePath) )
  232. return;
  233. refreshAsset();
  234. loadShape();
  235. }
  236. bool ShapeAsset::loadShape()
  237. {
  238. mMaterialAssets.clear();
  239. mMaterialAssetIds.clear();
  240. //First, load any material, animation, etc assets we may be referencing in our asset
  241. // Find any asset dependencies.
  242. AssetManager::typeAssetDependsOnHash::Iterator assetDependenciesItr = mpOwningAssetManager->getDependedOnAssets()->find(mpAssetDefinition->mAssetId);
  243. // Does the asset have any dependencies?
  244. if (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end())
  245. {
  246. // Iterate all dependencies.
  247. while (assetDependenciesItr != mpOwningAssetManager->getDependedOnAssets()->end() && assetDependenciesItr->key == mpAssetDefinition->mAssetId)
  248. {
  249. StringTableEntry assetType = mpOwningAssetManager->getAssetType(assetDependenciesItr->value);
  250. if (assetType == StringTable->insert("MaterialAsset"))
  251. {
  252. mMaterialAssetIds.push_front(assetDependenciesItr->value);
  253. //Force the asset to become initialized if it hasn't been already
  254. AssetPtr<MaterialAsset> matAsset = assetDependenciesItr->value;
  255. mMaterialAssets.push_front(matAsset);
  256. }
  257. else if (assetType == StringTable->insert("ShapeAnimationAsset"))
  258. {
  259. mAnimationAssetIds.push_back(assetDependenciesItr->value);
  260. //Force the asset to become initialized if it hasn't been already
  261. AssetPtr<ShapeAnimationAsset> animAsset = assetDependenciesItr->value;
  262. mAnimationAssets.push_back(animAsset);
  263. }
  264. // Next dependency.
  265. assetDependenciesItr++;
  266. }
  267. }
  268. mShape = ResourceManager::get().load(mFilePath);
  269. if (!mShape)
  270. {
  271. Con::errorf("ShapeAsset::loadShape : failed to load shape file %s (%s)!", getAssetName(), mFilePath);
  272. mLoadedState = BadFileReference;
  273. return false; //if it failed to load, bail out
  274. }
  275. // Construct billboards if not done already
  276. if (GFXDevice::devicePresent())
  277. mShape->setupBillboardDetails(mFilePath, mDiffuseImposterPath, mNormalImposterPath);
  278. //If they exist, grab our imposters here and bind them to our shapeAsset
  279. bool hasBlends = false;
  280. //Now that we've successfully loaded our shape and have any materials and animations loaded
  281. //we need to set up the animations we're using on our shape
  282. for (S32 i = mAnimationAssets.size()-1; i >= 0; --i)
  283. {
  284. String srcName = mAnimationAssets[i]->getAnimationName();
  285. String srcPath(mAnimationAssets[i]->getAnimationFilename());
  286. //SplitSequencePathAndName(srcPath, srcName);
  287. if (!mShape->addSequence(srcPath, srcName, srcName,
  288. mAnimationAssets[i]->getStartFrame(), mAnimationAssets[i]->getEndFrame(), mAnimationAssets[i]->getPadRotation(), mAnimationAssets[i]->getPadTransforms()))
  289. {
  290. mLoadedState = MissingAnimatons;
  291. return false;
  292. }
  293. if (mAnimationAssets[i]->isBlend())
  294. hasBlends = true;
  295. }
  296. //if any of our animations are blends, set those up now
  297. if (hasBlends)
  298. {
  299. for (U32 i=0; i < mAnimationAssets.size(); ++i)
  300. {
  301. if (mAnimationAssets[i]->isBlend() && mAnimationAssets[i]->getBlendAnimationName() != StringTable->EmptyString())
  302. {
  303. //gotta do a bit of logic here.
  304. //First, we need to make sure the anim asset we depend on for our blend is loaded
  305. AssetPtr<ShapeAnimationAsset> blendAnimAsset = mAnimationAssets[i]->getBlendAnimationName();
  306. if (blendAnimAsset.isNull())
  307. {
  308. Con::errorf("ShapeAsset::initializeAsset - Unable to acquire reference animation asset %s for asset %s to blend!", mAnimationAssets[i]->getBlendAnimationName(), mAnimationAssets[i]->getAssetName());
  309. {
  310. mLoadedState = MissingAnimatons;
  311. return false;
  312. }
  313. }
  314. String refAnimName = blendAnimAsset->getAnimationName();
  315. if (!mShape->setSequenceBlend(mAnimationAssets[i]->getAnimationName(), true, blendAnimAsset->getAnimationName(), mAnimationAssets[i]->getBlendFrame()))
  316. {
  317. Con::errorf("ShapeAnimationAsset::initializeAsset - Unable to set animation clip %s for asset %s to blend!", mAnimationAssets[i]->getAnimationName(), mAnimationAssets[i]->getAssetName());
  318. {
  319. mLoadedState = MissingAnimatons;
  320. return false;
  321. }
  322. }
  323. }
  324. }
  325. }
  326. mChangeSignal.trigger();
  327. mLoadedState = Ok;
  328. return true;
  329. }
  330. //------------------------------------------------------------------------------
  331. //Utility function to 'fill out' bindings and resources with a matching asset if one exists
  332. U32 ShapeAsset::getAssetByFilename(StringTableEntry fileName, AssetPtr<ShapeAsset>* shapeAsset)
  333. {
  334. AssetQuery query;
  335. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  336. if (foundAssetcount == 0)
  337. {
  338. //Didn't work, so have us fall back to a placeholder asset
  339. shapeAsset->setAssetId(ShapeAsset::smNoShapeAssetFallback);
  340. if (shapeAsset->isNull())
  341. {
  342. //Well that's bad, loading the fallback failed.
  343. Con::warnf("ShapeAsset::getAssetByFilename - Finding of asset associated with file %s failed with no fallback asset", fileName);
  344. return AssetErrCode::Failed;
  345. }
  346. //handle noshape not being loaded itself
  347. if ((*shapeAsset)->mLoadedState == BadFileReference)
  348. {
  349. Con::warnf("ShapeAsset::getAssetByFilename - Finding of associated with file %s failed, and fallback asset reported error of Bad File Reference.", fileName);
  350. return AssetErrCode::BadFileReference;
  351. }
  352. Con::warnf("ShapeAsset::getAssetByFilename - Finding of associated with file %s failed, utilizing fallback asset", fileName);
  353. (*shapeAsset)->mLoadedState = AssetErrCode::UsingFallback;
  354. return AssetErrCode::UsingFallback;
  355. }
  356. else
  357. {
  358. //acquire and bind the asset, and return it out
  359. shapeAsset->setAssetId(query.mAssetList[0]);
  360. return (*shapeAsset)->mLoadedState;
  361. }
  362. }
  363. StringTableEntry ShapeAsset::getAssetIdByFilename(StringTableEntry fileName)
  364. {
  365. if (fileName == StringTable->EmptyString())
  366. return StringTable->EmptyString();
  367. StringTableEntry shapeAssetId = ShapeAsset::smNoShapeAssetFallback;
  368. AssetQuery query;
  369. S32 foundAssetcount = AssetDatabase.findAssetLooseFile(&query, fileName);
  370. if (foundAssetcount != 0)
  371. {
  372. //acquire and bind the asset, and return it out
  373. shapeAssetId = query.mAssetList[0];
  374. }
  375. else
  376. {
  377. AssetPtr<ShapeAsset> shapeAsset = shapeAssetId; //ensures the fallback is loaded
  378. }
  379. return shapeAssetId;
  380. }
  381. U32 ShapeAsset::getAssetById(StringTableEntry assetId, AssetPtr<ShapeAsset>* shapeAsset)
  382. {
  383. (*shapeAsset) = assetId;
  384. if (shapeAsset->notNull())
  385. {
  386. return (*shapeAsset)->mLoadedState;
  387. }
  388. else
  389. {
  390. //Didn't work, so have us fall back to a placeholder asset
  391. shapeAsset->setAssetId(ShapeAsset::smNoShapeAssetFallback);
  392. if (shapeAsset->isNull())
  393. {
  394. //Well that's bad, loading the fallback failed.
  395. Con::warnf("ShapeAsset::getAssetById - Finding of asset with id %s failed with no fallback asset", assetId);
  396. return AssetErrCode::Failed;
  397. }
  398. //handle noshape not being loaded itself
  399. if ((*shapeAsset)->mLoadedState == BadFileReference)
  400. {
  401. Con::warnf("ShapeAsset::getAssetById - Finding of asset with id %s failed, and fallback asset reported error of Bad File Reference.", assetId);
  402. return AssetErrCode::BadFileReference;
  403. }
  404. Con::warnf("ShapeAsset::getAssetById - Finding of asset with id %s failed, utilizing fallback asset", assetId);
  405. (*shapeAsset)->mLoadedState = AssetErrCode::UsingFallback;
  406. return AssetErrCode::UsingFallback;
  407. }
  408. }
  409. //------------------------------------------------------------------------------
  410. void ShapeAsset::copyTo(SimObject* object)
  411. {
  412. // Call to parent.
  413. Parent::copyTo(object);
  414. }
  415. void ShapeAsset::onAssetRefresh(void)
  416. {
  417. if (mFileName == StringTable->EmptyString())
  418. return;
  419. // Update.
  420. if(!Platform::isFullPath(mFileName))
  421. mFilePath = getOwned() ? expandAssetFilePath(mFileName) : mFilePath;
  422. loadShape();
  423. }
  424. void ShapeAsset::SplitSequencePathAndName(String& srcPath, String& srcName)
  425. {
  426. srcName = "";
  427. // Determine if there is a sequence name at the end of the source string, and
  428. // if so, split the filename from the sequence name
  429. S32 split = srcPath.find(' ', 0, String::Right);
  430. S32 split2 = srcPath.find('\t', 0, String::Right);
  431. if ((split == String::NPos) || (split2 > split))
  432. split = split2;
  433. if (split != String::NPos)
  434. {
  435. split2 = split + 1;
  436. while ((srcPath[split2] != '\0') && dIsspace(srcPath[split2]))
  437. split2++;
  438. // now 'split' is at the end of the path, and 'split2' is at the start of the sequence name
  439. srcName = srcPath.substr(split2);
  440. srcPath = srcPath.erase(split, srcPath.length() - split);
  441. }
  442. }
  443. ShapeAnimationAsset* ShapeAsset::getAnimation(S32 index)
  444. {
  445. if (index < mAnimationAssets.size())
  446. {
  447. return mAnimationAssets[index];
  448. }
  449. return nullptr;
  450. }
  451. #ifdef TORQUE_TOOLS
  452. const char* ShapeAsset::generateCachedPreviewImage(S32 resolution, String overrideMaterial)
  453. {
  454. if (!mShape)
  455. return "";
  456. // We're gonna render... make sure we can.
  457. bool sceneBegun = GFX->canCurrentlyRender();
  458. if (!sceneBegun)
  459. GFX->beginScene();
  460. // We need to create our own instance to render with.
  461. TSShapeInstance* shape = new TSShapeInstance(mShape, true);
  462. if (overrideMaterial.isNotEmpty())
  463. {
  464. Material *tMat = dynamic_cast<Material*>(Sim::findObject(overrideMaterial));
  465. if (tMat)
  466. shape->reSkin(tMat->mMapTo, mShape->materialList->getMaterialName(0));
  467. }
  468. // Animate the shape once.
  469. shape->animate(0);
  470. // So we don't have to change it everywhere.
  471. const GFXFormat format = GFXFormatR8G8B8A8;
  472. GBitmap* imposter = NULL;
  473. GBitmap* imposterNrml = NULL;
  474. ImposterCapture* imposterCap = new ImposterCapture();
  475. static const MatrixF topXfm(EulerF(-M_PI_F / 2.0f, 0, 0));
  476. static const MatrixF bottomXfm(EulerF(M_PI_F / 2.0f, 0, 0));
  477. MatrixF angMat;
  478. S32 mip = 0;
  479. PROFILE_START(ShapeAsset_generateCachedPreviewImage);
  480. //dMemset(destBmp.getWritableBits(mip), 0, destBmp.getWidth(mip) * destBmp.getHeight(mip) * GFXFormat_getByteSize(format));
  481. F32 rotX = -(mDegToRad(60.0) - 0.5f * M_PI_F);
  482. F32 rotZ = -(mDegToRad(45.0) - 0.5f * M_PI_F);
  483. // We capture the images in a particular order which must
  484. // match the order expected by the imposter renderer.
  485. imposterCap->begin(shape, 0, resolution, mShape->mRadius, mShape->center);
  486. angMat.mul(MatrixF(EulerF(rotX, 0, 0)),
  487. MatrixF(EulerF(0, 0, rotZ)));
  488. imposterCap->capture(angMat, &imposter, &imposterNrml);
  489. imposterCap->end();
  490. PROFILE_END(); // ShapeAsset_generateCachedPreviewImage
  491. delete imposterCap;
  492. delete shape;
  493. String dumpPath = String(mFilePath) + "_Preview.dds";
  494. char* returnBuffer = Con::getReturnBuffer(128);
  495. dSprintf(returnBuffer, 128, "%s", dumpPath.c_str());
  496. /*FileStream stream;
  497. if (stream.open(dumpPath, Torque::FS::File::Write))
  498. destBmp.writeBitmap("png", stream);
  499. stream.close();*/
  500. DDSFile* ddsDest = DDSFile::createDDSFileFromGBitmap(imposter);
  501. ImageUtil::ddsCompress(ddsDest, GFXFormatBC2);
  502. // Finally save the imposters to disk.
  503. FileStream fs;
  504. if (fs.open(returnBuffer, Torque::FS::File::Write))
  505. {
  506. ddsDest->write(fs);
  507. fs.close();
  508. }
  509. delete ddsDest;
  510. delete imposter;
  511. delete imposterNrml;
  512. // If we did a begin then end it now.
  513. if (!sceneBegun)
  514. GFX->endScene();
  515. return returnBuffer;
  516. }
  517. #endif
  518. DefineEngineMethod(ShapeAsset, getMaterialCount, S32, (), ,
  519. "Gets the number of materials for this shape asset.\n"
  520. "@return Material count.\n")
  521. {
  522. return object->getMaterialCount();
  523. }
  524. DefineEngineMethod(ShapeAsset, getAnimationCount, S32, (), ,
  525. "Gets the number of animations for this shape asset.\n"
  526. "@return Animation count.\n")
  527. {
  528. return object->getAnimationCount();
  529. }
  530. DefineEngineMethod(ShapeAsset, getAnimation, ShapeAnimationAsset*, (S32 index), (0),
  531. "Gets a particular shape animation asset for this shape.\n"
  532. "@param animation asset index.\n"
  533. "@return Shape Animation Asset.\n")
  534. {
  535. return object->getAnimation(index);
  536. }
  537. DefineEngineMethod(ShapeAsset, getShapePath, const char*, (), ,
  538. "Gets the shape's file path\n"
  539. "@return The filename of the shape file")
  540. {
  541. return object->getShapeFilePath();
  542. }
  543. DefineEngineMethod(ShapeAsset, getShapeConstructorFilePath, const char*, (), ,
  544. "Gets the shape's constructor file.\n"
  545. "@return The filename of the shape constructor file")
  546. {
  547. return object->getShapeConstructorFilePath();
  548. }
  549. DefineEngineMethod(ShapeAsset, getStatusString, String, (), , "get status string")\
  550. {
  551. return ShapeAsset::getAssetErrstrn(object->getStatus());
  552. }
  553. #ifdef TORQUE_TOOLS
  554. DefineEngineMethod(ShapeAsset, generateCachedPreviewImage, const char*, (S32 resolution, const char* overrideMaterialName), (256, ""),
  555. "Generates a baked preview image of the given shapeAsset. Only really used for generating Asset Browser icons.\n"
  556. "@param resolution Optional field for what resolution to bake the preview image at. Must be pow2\n"
  557. "@param overrideMaterialName Optional field for overriding the material used when rendering the shape for the bake.")
  558. {
  559. return object->generateCachedPreviewImage(resolution, overrideMaterialName);
  560. }
  561. DefineEngineStaticMethod(ShapeAsset, getAssetIdByFilename, const char*, (const char* filePath), (""),
  562. "Queries the Asset Database to see if any asset exists that is associated with the provided file path.\n"
  563. "@return The AssetId of the associated asset, if any.")
  564. {
  565. return ShapeAsset::getAssetIdByFilename(StringTable->insert(filePath));
  566. }
  567. #endif
  568. #ifdef TORQUE_TOOLS
  569. //-----------------------------------------------------------------------------
  570. // GuiInspectorTypeAssetId
  571. //-----------------------------------------------------------------------------
  572. IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetPtr);
  573. ConsoleDocClass(GuiInspectorTypeShapeAssetPtr,
  574. "@brief Inspector field type for Shapes\n\n"
  575. "Editor use only.\n\n"
  576. "@internal"
  577. );
  578. void GuiInspectorTypeShapeAssetPtr::consoleInit()
  579. {
  580. Parent::consoleInit();
  581. ConsoleBaseType::getType(TypeShapeAssetPtr)->setInspectorFieldType("GuiInspectorTypeShapeAssetPtr");
  582. }
  583. GuiControl* GuiInspectorTypeShapeAssetPtr::constructEditControl()
  584. {
  585. // Create base filename edit controls
  586. GuiControl *retCtrl = Parent::constructEditControl();
  587. if (retCtrl == NULL)
  588. return retCtrl;
  589. // Change filespec
  590. char szBuffer[512];
  591. if (mInspector->getInspectObject() != nullptr)
  592. {
  593. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ShapeAsset\", \"AssetBrowser.changeAsset\", %s, %s);",
  594. mInspector->getIdString(), mCaption);
  595. mBrowseButton->setField("Command", szBuffer);
  596. setDataField(StringTable->insert("targetObject"), NULL, mInspector->getInspectObject()->getIdString());
  597. }
  598. else
  599. {
  600. //if we don't have a target object, we'll be manipulating the desination value directly
  601. dSprintf(szBuffer, sizeof(szBuffer), "AssetBrowser.showDialog(\"ShapeAsset\", \"AssetBrowser.changeAsset\", %s, \"%s\");",
  602. mInspector->getIdString(), mVariableName);
  603. mBrowseButton->setField("Command", szBuffer);
  604. }
  605. // Create "Open in ShapeEditor" button
  606. mShapeEdButton = new GuiBitmapButtonCtrl();
  607. dSprintf(szBuffer, sizeof(szBuffer), "ShapeEditorPlugin.openShapeAssetId(%d.getText());", retCtrl->getId());
  608. mShapeEdButton->setField("Command", szBuffer);
  609. char bitmapName[512] = "ToolsModule:shape_editor_n_image";
  610. mShapeEdButton->setBitmap(StringTable->insert(bitmapName));
  611. mShapeEdButton->setDataField(StringTable->insert("Profile"), NULL, "GuiButtonProfile");
  612. mShapeEdButton->setDataField(StringTable->insert("tooltipprofile"), NULL, "GuiToolTipProfile");
  613. mShapeEdButton->setDataField(StringTable->insert("hovertime"), NULL, "1000");
  614. mShapeEdButton->setDataField(StringTable->insert("tooltip"), NULL, "Open this file in the Shape Editor");
  615. mShapeEdButton->registerObject();
  616. addObject(mShapeEdButton);
  617. return retCtrl;
  618. }
  619. bool GuiInspectorTypeShapeAssetPtr::updateRects()
  620. {
  621. S32 dividerPos, dividerMargin;
  622. mInspector->getDivider(dividerPos, dividerMargin);
  623. Point2I fieldExtent = getExtent();
  624. Point2I fieldPos = getPosition();
  625. mCaptionRect.set(0, 0, fieldExtent.x - dividerPos - dividerMargin, fieldExtent.y);
  626. mEditCtrlRect.set(fieldExtent.x - dividerPos + dividerMargin, 1, dividerPos - dividerMargin - 34, fieldExtent.y);
  627. bool resized = mEdit->resize(mEditCtrlRect.point, mEditCtrlRect.extent);
  628. if (mBrowseButton != NULL)
  629. {
  630. mBrowseRect.set(fieldExtent.x - 32, 2, 14, fieldExtent.y - 4);
  631. resized |= mBrowseButton->resize(mBrowseRect.point, mBrowseRect.extent);
  632. }
  633. if (mShapeEdButton != NULL)
  634. {
  635. RectI shapeEdRect(fieldExtent.x - 16, 2, 14, fieldExtent.y - 4);
  636. resized |= mShapeEdButton->resize(shapeEdRect.point, shapeEdRect.extent);
  637. }
  638. return resized;
  639. }
  640. IMPLEMENT_CONOBJECT(GuiInspectorTypeShapeAssetId);
  641. ConsoleDocClass(GuiInspectorTypeShapeAssetId,
  642. "@brief Inspector field type for Shapes\n\n"
  643. "Editor use only.\n\n"
  644. "@internal"
  645. );
  646. void GuiInspectorTypeShapeAssetId::consoleInit()
  647. {
  648. Parent::consoleInit();
  649. ConsoleBaseType::getType(TypeShapeAssetId)->setInspectorFieldType("GuiInspectorTypeShapeAssetId");
  650. }
  651. #endif