ShapeAsset.cpp 26 KB

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