assimpShapeLoader.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 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. /*
  23. Resource stream -> Buffer
  24. Buffer -> Collada DOM
  25. Collada DOM -> TSShapeLoader
  26. TSShapeLoader installed into TSShape
  27. */
  28. //-----------------------------------------------------------------------------
  29. #include "platform/platform.h"
  30. #include "ts/assimp/assimpShapeLoader.h"
  31. #include "ts/assimp/assimpAppNode.h"
  32. #include "ts/assimp/assimpAppMesh.h"
  33. #include "ts/assimp/assimpAppMaterial.h"
  34. #include "ts/assimp/assimpAppSequence.h"
  35. #include "core/util/tVector.h"
  36. #include "core/strings/findMatch.h"
  37. #include "core/strings/stringUnit.h"
  38. #include "core/stream/fileStream.h"
  39. #include "core/fileObject.h"
  40. #include "ts/tsShape.h"
  41. #include "ts/tsShapeInstance.h"
  42. #include "materials/materialManager.h"
  43. #include "console/persistenceManager.h"
  44. #include "ts/tsShapeConstruct.h"
  45. #include "core/util/zip/zipVolume.h"
  46. #include "gfx/bitmap/gBitmap.h"
  47. #include "gui/controls/guiTreeViewCtrl.h"
  48. // assimp include files.
  49. #include <assimp/cimport.h>
  50. #include <assimp/scene.h>
  51. #include <assimp/postprocess.h>
  52. #include <assimp/types.h>
  53. #include <assimp/config.h>
  54. #include <exception>
  55. #include <assimp/Importer.hpp>
  56. MODULE_BEGIN( AssimpShapeLoader )
  57. MODULE_INIT_AFTER( ShapeLoader )
  58. MODULE_INIT
  59. {
  60. TSShapeLoader::addFormat("DirectX X", "x");
  61. TSShapeLoader::addFormat("Autodesk FBX", "fbx");
  62. TSShapeLoader::addFormat("Blender 3D", "blend" );
  63. TSShapeLoader::addFormat("3ds Max 3DS", "3ds");
  64. TSShapeLoader::addFormat("3ds Max ASE", "ase");
  65. TSShapeLoader::addFormat("Wavefront Object", "obj");
  66. TSShapeLoader::addFormat("Industry Foundation Classes (IFC/Step)", "ifc");
  67. TSShapeLoader::addFormat("Stanford Polygon Library", "ply");
  68. TSShapeLoader::addFormat("AutoCAD DXF", "dxf");
  69. TSShapeLoader::addFormat("LightWave", "lwo");
  70. TSShapeLoader::addFormat("LightWave Scene", "lws");
  71. TSShapeLoader::addFormat("Modo", "lxo");
  72. TSShapeLoader::addFormat("Stereolithography", "stl");
  73. TSShapeLoader::addFormat("AC3D", "ac");
  74. TSShapeLoader::addFormat("Milkshape 3D", "ms3d");
  75. TSShapeLoader::addFormat("TrueSpace COB", "cob");
  76. TSShapeLoader::addFormat("TrueSpace SCN", "scn");
  77. TSShapeLoader::addFormat("Ogre XML", "xml");
  78. TSShapeLoader::addFormat("Irrlicht Mesh", "irrmesh");
  79. TSShapeLoader::addFormat("Irrlicht Scene", "irr");
  80. TSShapeLoader::addFormat("Quake I", "mdl" );
  81. TSShapeLoader::addFormat("Quake II", "md2" );
  82. TSShapeLoader::addFormat("Quake III Mesh", "md3");
  83. TSShapeLoader::addFormat("Quake III Map/BSP", "pk3");
  84. TSShapeLoader::addFormat("Return to Castle Wolfenstein", "mdc");
  85. TSShapeLoader::addFormat("Doom 3", "md5" );
  86. TSShapeLoader::addFormat("Valve SMD", "smd");
  87. TSShapeLoader::addFormat("Valve VTA", "vta");
  88. TSShapeLoader::addFormat("Starcraft II M3", "m3");
  89. TSShapeLoader::addFormat("Unreal", "3d");
  90. TSShapeLoader::addFormat("BlitzBasic 3D", "b3d" );
  91. TSShapeLoader::addFormat("Quick3D Q3D", "q3d");
  92. TSShapeLoader::addFormat("Quick3D Q3S", "q3s");
  93. TSShapeLoader::addFormat("Neutral File Format", "nff");
  94. TSShapeLoader::addFormat("Object File Format", "off");
  95. TSShapeLoader::addFormat("PovRAY Raw", "raw");
  96. TSShapeLoader::addFormat("Terragen Terrain", "ter");
  97. TSShapeLoader::addFormat("3D GameStudio (3DGS)", "mdl");
  98. TSShapeLoader::addFormat("3D GameStudio (3DGS) Terrain", "hmp");
  99. TSShapeLoader::addFormat("Izware Nendo", "ndo");
  100. TSShapeLoader::addFormat("gltf", "gltf");
  101. TSShapeLoader::addFormat("gltf binary", "glb");
  102. }
  103. MODULE_END;
  104. //-----------------------------------------------------------------------------
  105. AssimpShapeLoader::AssimpShapeLoader()
  106. {
  107. mScene = NULL;
  108. }
  109. AssimpShapeLoader::~AssimpShapeLoader()
  110. {
  111. }
  112. void AssimpShapeLoader::releaseImport()
  113. {
  114. aiReleaseImport(mScene);
  115. }
  116. void AssimpShapeLoader::enumerateScene()
  117. {
  118. TSShapeLoader::updateProgress(TSShapeLoader::Load_ReadFile, "Reading File");
  119. Con::printf("[ASSIMP] Attempting to load file: %s", shapePath.getFullPath().c_str());
  120. // Post-Processing
  121. unsigned int ppsteps =
  122. (ColladaUtils::getOptions().convertLeftHanded ? aiProcess_MakeLeftHanded : 0) |
  123. (ColladaUtils::getOptions().reverseWindingOrder ? aiProcess_FlipWindingOrder : 0) |
  124. (ColladaUtils::getOptions().calcTangentSpace ? aiProcess_CalcTangentSpace : 0) |
  125. (ColladaUtils::getOptions().joinIdenticalVerts ? aiProcess_JoinIdenticalVertices : 0) |
  126. (ColladaUtils::getOptions().removeRedundantMats ? aiProcess_RemoveRedundantMaterials : 0) |
  127. (ColladaUtils::getOptions().genUVCoords ? aiProcess_GenUVCoords : 0) |
  128. (ColladaUtils::getOptions().transformUVCoords ? aiProcess_TransformUVCoords : 0) |
  129. (ColladaUtils::getOptions().flipUVCoords ? aiProcess_FlipUVs : 0) |
  130. (ColladaUtils::getOptions().findInstances ? aiProcess_FindInstances : 0) |
  131. (ColladaUtils::getOptions().limitBoneWeights ? aiProcess_LimitBoneWeights : 0);
  132. if (Con::getBoolVariable("$Assimp::OptimizeMeshes", false))
  133. ppsteps |= aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph;
  134. if (Con::getBoolVariable("$Assimp::SplitLargeMeshes", false))
  135. ppsteps |= aiProcess_SplitLargeMeshes;
  136. // Mandatory options
  137. //ppsteps |= aiProcess_ValidateDataStructure | aiProcess_Triangulate | aiProcess_ImproveCacheLocality;
  138. ppsteps |= aiProcess_Triangulate;
  139. //aiProcess_SortByPType | // make 'clean' meshes which consist of a single typ of primitives
  140. aiPropertyStore* props = aiCreatePropertyStore();
  141. struct aiLogStream shapeLog = aiGetPredefinedLogStream(aiDefaultLogStream_STDOUT, NULL);
  142. shapeLog.callback = assimpLogCallback;
  143. shapeLog.user = 0;
  144. aiAttachLogStream(&shapeLog);
  145. #ifdef TORQUE_DEBUG
  146. aiEnableVerboseLogging(true);
  147. #endif
  148. mScene = (aiScene*)aiImportFileExWithProperties(shapePath.getFullPath().c_str(), ppsteps, NULL, props);
  149. aiReleasePropertyStore(props);
  150. if ( mScene )
  151. {
  152. Con::printf("[ASSIMP] Mesh Count: %d", mScene->mNumMeshes);
  153. Con::printf("[ASSIMP] Material Count: %d", mScene->mNumMaterials);
  154. // Setup default units for shape format
  155. String importFormat;
  156. String fileExt = String::ToLower(shapePath.getExtension());
  157. const aiImporterDesc* importerDescription = aiGetImporterDesc(fileExt.c_str());
  158. if (importerDescription && StringTable->insert(importerDescription->mName) == StringTable->insert("Autodesk FBX Importer"))
  159. {
  160. ColladaUtils::getOptions().formatScaleFactor = 0.01f;
  161. }
  162. // Set import options (if they are not set to override)
  163. if (ColladaUtils::getOptions().unit <= 0.0f)
  164. {
  165. F64 unit;
  166. if (!getMetaDouble("UnitScaleFactor", unit))
  167. {
  168. F32 floatVal;
  169. S32 intVal;
  170. if (getMetaFloat("UnitScaleFactor", floatVal))
  171. unit = (F64)floatVal;
  172. else if (getMetaInt("UnitScaleFactor", intVal))
  173. unit = (F64)intVal;
  174. else
  175. unit = 1.0;
  176. }
  177. ColladaUtils::getOptions().unit = (F32)unit;
  178. }
  179. if (ColladaUtils::getOptions().upAxis == UPAXISTYPE_COUNT)
  180. {
  181. S32 upAxis;
  182. if (!getMetaInt("UpAxis", upAxis))
  183. upAxis = UPAXISTYPE_Z_UP;
  184. ColladaUtils::getOptions().upAxis = (domUpAxisType) upAxis;
  185. }
  186. // Extract embedded textures
  187. for (U32 i = 0; i < mScene->mNumTextures; ++i)
  188. extractTexture(i, mScene->mTextures[i]);
  189. // Load all the materials.
  190. AssimpAppMaterial::sDefaultMatNumber = 0;
  191. for ( U32 i = 0; i < mScene->mNumMaterials; i++ )
  192. AppMesh::appMaterials.push_back(new AssimpAppMaterial(mScene->mMaterials[i]));
  193. // Setup LOD checks
  194. detectDetails();
  195. // Define the root node, and process down the chain.
  196. AssimpAppNode* node = new AssimpAppNode(mScene, mScene->mRootNode, 0);
  197. if (!processNode(node))
  198. delete node;
  199. // add bounds node.
  200. if (!boundsNode)
  201. {
  202. aiNode* req[1];
  203. req[0] = new aiNode("bounds");
  204. mScene->mRootNode->addChildren(1, req);
  205. AssimpAppNode* appBounds = new AssimpAppNode(mScene, req[0]);
  206. if (!processNode(appBounds))
  207. delete appBounds;
  208. }
  209. // Check for animations and process those.
  210. processAnimations();
  211. }
  212. else
  213. {
  214. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Import failed");
  215. Con::printf("[ASSIMP] Import Error: %s", aiGetErrorString());
  216. }
  217. aiDetachLogStream(&shapeLog);
  218. }
  219. void AssimpShapeLoader::processAnimations()
  220. {
  221. // add all animations into 1 ambient animation.
  222. aiAnimation* ambientSeq = new aiAnimation();
  223. ambientSeq->mName = "ambient";
  224. Vector<aiNodeAnim*> ambientChannels;
  225. F32 duration = 0.0f;
  226. if (mScene->mNumAnimations > 0)
  227. {
  228. for (U32 i = 0; i < mScene->mNumAnimations; ++i)
  229. {
  230. aiAnimation* anim = mScene->mAnimations[i];
  231. for (U32 j = 0; j < anim->mNumChannels; j++)
  232. {
  233. aiNodeAnim* nodeAnim = anim->mChannels[j];
  234. // Determine the maximum keyframe time for this animation
  235. F32 maxKeyTime = 0.0f;
  236. for (U32 k = 0; k < nodeAnim->mNumPositionKeys; k++) {
  237. maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mPositionKeys[k].mTime);
  238. }
  239. for (U32 k = 0; k < nodeAnim->mNumRotationKeys; k++) {
  240. maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mRotationKeys[k].mTime);
  241. }
  242. for (U32 k = 0; k < nodeAnim->mNumScalingKeys; k++) {
  243. maxKeyTime = getMax(maxKeyTime, (F32)nodeAnim->mScalingKeys[k].mTime);
  244. }
  245. ambientChannels.push_back(nodeAnim);
  246. duration = getMax(duration, maxKeyTime);
  247. }
  248. }
  249. ambientSeq->mNumChannels = ambientChannels.size();
  250. ambientSeq->mChannels = ambientChannels.address();
  251. ambientSeq->mDuration = duration;
  252. ambientSeq->mTicksPerSecond = 24.0;
  253. AssimpAppSequence* defaultAssimpSeq = new AssimpAppSequence(ambientSeq);
  254. appSequences.push_back(defaultAssimpSeq);
  255. }
  256. }
  257. void AssimpShapeLoader::computeBounds(Box3F& bounds)
  258. {
  259. TSShapeLoader::computeBounds(bounds);
  260. // Check if the model origin needs adjusting
  261. bool adjustCenter = ColladaUtils::getOptions().adjustCenter;
  262. bool adjustFloor = ColladaUtils::getOptions().adjustFloor;
  263. if (bounds.isValidBox() && (adjustCenter || adjustFloor))
  264. {
  265. // Compute shape offset
  266. Point3F shapeOffset = Point3F::Zero;
  267. if (adjustCenter)
  268. {
  269. bounds.getCenter(&shapeOffset);
  270. shapeOffset = -shapeOffset;
  271. }
  272. if (adjustFloor)
  273. shapeOffset.z = -bounds.minExtents.z;
  274. // Adjust bounds
  275. bounds.minExtents += shapeOffset;
  276. bounds.maxExtents += shapeOffset;
  277. // Now adjust all positions for root level nodes (nodes with no parent)
  278. for (S32 iNode = 0; iNode < shape->nodes.size(); iNode++)
  279. {
  280. if (!appNodes[iNode]->isParentRoot())
  281. continue;
  282. // Adjust default translation
  283. shape->defaultTranslations[iNode] += shapeOffset;
  284. // Adjust animated translations
  285. for (S32 iSeq = 0; iSeq < shape->sequences.size(); iSeq++)
  286. {
  287. const TSShape::Sequence& seq = shape->sequences[iSeq];
  288. if (seq.translationMatters.test(iNode))
  289. {
  290. for (S32 iFrame = 0; iFrame < seq.numKeyframes; iFrame++)
  291. {
  292. S32 index = seq.baseTranslation + seq.translationMatters.count(iNode)*seq.numKeyframes + iFrame;
  293. shape->nodeTranslations[index] += shapeOffset;
  294. }
  295. }
  296. }
  297. }
  298. }
  299. }
  300. bool AssimpShapeLoader::fillGuiTreeView(const char* sourceShapePath, GuiTreeViewCtrl* tree)
  301. {
  302. Assimp::Importer importer;
  303. Torque::Path path(sourceShapePath);
  304. String cleanFile = AppMaterial::cleanString(path.getFileName());
  305. // Attempt to import with Assimp.
  306. const aiScene* shapeScene = importer.ReadFile(path.getFullPath().c_str(), (aiProcessPreset_TargetRealtime_Quality | aiProcess_CalcTangentSpace)
  307. & ~aiProcess_RemoveRedundantMaterials & ~aiProcess_GenSmoothNormals);
  308. if (!shapeScene)
  309. {
  310. Con::printf("AssimpShapeLoader::fillGuiTreeView - Assimp Error: %s", importer.GetErrorString());
  311. return false;
  312. }
  313. mScene = shapeScene;
  314. // Initialize tree
  315. tree->removeItem(0);
  316. S32 meshItem = tree->insertItem(0, "Meshes", String::ToString("%i", shapeScene->mNumMeshes));
  317. S32 matItem = tree->insertItem(0, "Materials", String::ToString("%i", shapeScene->mNumMaterials));
  318. S32 animItem = tree->insertItem(0, "Animations", String::ToString("%i", shapeScene->mNumAnimations));
  319. //S32 lightsItem = tree->insertItem(0, "Lights", String::ToString("%i", shapeScene->mNumLights));
  320. //S32 texturesItem = tree->insertItem(0, "Textures", String::ToString("%i", shapeScene->mNumTextures));
  321. //Details!
  322. U32 numPolys = 0;
  323. U32 numVerts = 0;
  324. for (U32 i = 0; i < shapeScene->mNumMeshes; i++)
  325. {
  326. tree->insertItem(meshItem, String::ToString("%s", shapeScene->mMeshes[i]->mName.C_Str()));
  327. numPolys += shapeScene->mMeshes[i]->mNumFaces;
  328. numVerts += shapeScene->mMeshes[i]->mNumVertices;
  329. }
  330. U32 defaultMatNumber = 0;
  331. for (U32 i = 0; i < shapeScene->mNumMaterials; i++)
  332. {
  333. aiMaterial* aiMat = shapeScene->mMaterials[i];
  334. aiString matName;
  335. aiMat->Get(AI_MATKEY_NAME, matName);
  336. String name = matName.C_Str();
  337. if (name.isEmpty())
  338. {
  339. name = AppMaterial::cleanString(path.getFileName());
  340. name += "_defMat";
  341. name += String::ToString("%d", defaultMatNumber);
  342. defaultMatNumber++;
  343. }
  344. aiString texPath;
  345. aiMat->GetTexture(aiTextureType::aiTextureType_DIFFUSE, 0, &texPath);
  346. String texName = texPath.C_Str();
  347. if (texName.isEmpty())
  348. {
  349. aiColor3D read_color(1.f, 1.f, 1.f);
  350. if (AI_SUCCESS == aiMat->Get(AI_MATKEY_COLOR_DIFFUSE, read_color))
  351. texName = String::ToString("Color: %0.3f %0.3f %0.3f", (F32)read_color.r, (F32)read_color.g, (F32)read_color.b); //formatted as words for easy parsing
  352. else
  353. texName = "No Texture";
  354. }
  355. else
  356. texName = AssimpAppMaterial::cleanTextureName(texName, cleanFile, sourceShapePath, true);
  357. tree->insertItem(matItem, String::ToString("%s", name.c_str()), String::ToString("%s", texName.c_str()));
  358. }
  359. if (shapeScene->mNumAnimations == 0)
  360. {
  361. tree->insertItem(animItem, "ambient", "animation", "", 0, 0);
  362. }
  363. else
  364. {
  365. for (U32 i = 0; i < shapeScene->mNumAnimations; i++)
  366. {
  367. tree->insertItem(animItem, shapeScene->mAnimations[i]->mName.C_Str(), "animation", "", 0, 0);
  368. }
  369. }
  370. U32 numNodes = 0;
  371. if (shapeScene->mRootNode)
  372. {
  373. S32 nodesItem = tree->insertItem(0, "Nodes", "");
  374. addNodeToTree(nodesItem, shapeScene->mRootNode, tree, numNodes);
  375. tree->setItemValue(nodesItem, String::ToString("%i", numNodes));
  376. }
  377. U32 numMetaTags = shapeScene->mMetaData ? shapeScene->mMetaData->mNumProperties : 0;
  378. if (numMetaTags)
  379. addMetaDataToTree(shapeScene->mMetaData, tree);
  380. F64 unit;
  381. if (!getMetaDouble("UnitScaleFactor", unit))
  382. unit = 1.0f;
  383. S32 upAxis;
  384. if (!getMetaInt("UpAxis", upAxis))
  385. upAxis = UPAXISTYPE_Z_UP;
  386. /*for (U32 i = 0; i < shapeScene->mNumLights; i++)
  387. {
  388. treeObj->insertItem(lightsItem, String::ToString("%s", shapeScene->mLights[i]->mType));
  389. }*/
  390. // Store shape information in the tree control
  391. tree->setDataField(StringTable->insert("_nodeCount"), 0, avar("%d", numNodes));
  392. tree->setDataField(StringTable->insert("_meshCount"), 0, avar("%d", shapeScene->mNumMeshes));
  393. tree->setDataField(StringTable->insert("_polygonCount"), 0, avar("%d", numPolys));
  394. tree->setDataField(StringTable->insert("_materialCount"), 0, avar("%d", shapeScene->mNumMaterials));
  395. tree->setDataField(StringTable->insert("_lightCount"), 0, avar("%d", shapeScene->mNumLights));
  396. tree->setDataField(StringTable->insert("_animCount"), 0, avar("%d", shapeScene->mNumAnimations));
  397. tree->setDataField(StringTable->insert("_textureCount"), 0, avar("%d", shapeScene->mNumTextures));
  398. tree->setDataField(StringTable->insert("_vertCount"), 0, avar("%d", numVerts));
  399. tree->setDataField(StringTable->insert("_metaTagCount"), 0, avar("%d", numMetaTags));
  400. tree->setDataField(StringTable->insert("_unit"), 0, avar("%g", (F32)unit));
  401. if (upAxis == UPAXISTYPE_X_UP)
  402. tree->setDataField(StringTable->insert("_upAxis"), 0, "X_AXIS");
  403. else if (upAxis == UPAXISTYPE_Y_UP)
  404. tree->setDataField(StringTable->insert("_upAxis"), 0, "Y_AXIS");
  405. else
  406. tree->setDataField(StringTable->insert("_upAxis"), 0, "Z_AXIS");
  407. return true;
  408. }
  409. void AssimpShapeLoader::updateMaterialsScript(const Torque::Path &path)
  410. {
  411. return;
  412. /*
  413. Torque::Path scriptPath(path);
  414. scriptPath.setFileName("materials");
  415. scriptPath.setExtension(TORQUE_SCRIPT_EXTENSION);
  416. // First see what materials we need to update
  417. PersistenceManager persistMgr;
  418. for ( U32 iMat = 0; iMat < AppMesh::appMaterials.size(); iMat++ )
  419. {
  420. AssimpAppMaterial *mat = dynamic_cast<AssimpAppMaterial*>( AppMesh::appMaterials[iMat] );
  421. if ( mat )
  422. {
  423. Material *mappedMat;
  424. if ( Sim::findObject( MATMGR->getMapEntry( mat->getName() ), mappedMat ) )
  425. {
  426. // Only update existing materials if forced to
  427. if (ColladaUtils::getOptions().forceUpdateMaterials)
  428. {
  429. mat->initMaterial(scriptPath, mappedMat);
  430. persistMgr.setDirty(mappedMat);
  431. }
  432. }
  433. else
  434. {
  435. // Create a new material definition
  436. persistMgr.setDirty( mat->createMaterial( scriptPath ), scriptPath.getFullPath() );
  437. }
  438. }
  439. }
  440. if ( persistMgr.getDirtyList().empty() )
  441. return;
  442. persistMgr.saveDirty();
  443. */
  444. }
  445. /// Check if an up-to-date cached DTS is available for this DAE file
  446. bool AssimpShapeLoader::canLoadCachedDTS(const Torque::Path& path)
  447. {
  448. // Generate the cached filename
  449. Torque::Path cachedPath(path);
  450. cachedPath.setExtension("cached.dts");
  451. // Check if a cached DTS newer than this file is available
  452. FileTime cachedModifyTime;
  453. if (Platform::getFileTimes(cachedPath.getFullPath(), NULL, &cachedModifyTime))
  454. {
  455. bool forceLoad = Con::getBoolVariable("$assimp::forceLoad", false);
  456. FileTime daeModifyTime;
  457. if (!Platform::getFileTimes(path.getFullPath(), NULL, &daeModifyTime) ||
  458. (!forceLoad && (Platform::compareFileTimes(cachedModifyTime, daeModifyTime) >= 0) ))
  459. {
  460. // Original file not found, or cached DTS is newer
  461. return true;
  462. }
  463. }
  464. return false;
  465. }
  466. void AssimpShapeLoader::assimpLogCallback(const char* message, char* user)
  467. {
  468. Con::printf("[Assimp log message] %s", StringUnit::getUnit(message, 0, "\n"));
  469. }
  470. bool AssimpShapeLoader::ignoreNode(const String& name)
  471. {
  472. // Do not add AssimpFbx dummy nodes to the TSShape. See: Assimp::FBX::ImportSettings::preservePivots
  473. // https://github.com/assimp/assimp/blob/master/code/FBXImportSettings.h#L116-L135
  474. if (name.find("_$AssimpFbx$_") != String::NPos)
  475. return true;
  476. if (FindMatch::isMatchMultipleExprs(ColladaUtils::getOptions().alwaysImport, name, false))
  477. return false;
  478. return FindMatch::isMatchMultipleExprs(ColladaUtils::getOptions().neverImport, name, false);
  479. }
  480. bool AssimpShapeLoader::ignoreMesh(const String& name)
  481. {
  482. if (FindMatch::isMatchMultipleExprs(ColladaUtils::getOptions().alwaysImportMesh, name, false))
  483. return false;
  484. else
  485. return FindMatch::isMatchMultipleExprs(ColladaUtils::getOptions().neverImportMesh, name, false);
  486. }
  487. void AssimpShapeLoader::detectDetails()
  488. {
  489. // Set LOD option
  490. bool singleDetail = true;
  491. switch (ColladaUtils::getOptions().lodType)
  492. {
  493. case ColladaUtils::ImportOptions::DetectDTS:
  494. // Check for a baseXX->startXX hierarchy at the top-level, if we find
  495. // one, use trailing numbers for LOD, otherwise use a single size
  496. for (S32 iNode = 0; singleDetail && (iNode < mScene->mRootNode->mNumChildren); iNode++) {
  497. aiNode* node = mScene->mRootNode->mChildren[iNode];
  498. if (node && dStrStartsWith(node->mName.C_Str(), "base")) {
  499. for (S32 iChild = 0; iChild < node->mNumChildren; iChild++) {
  500. aiNode* child = node->mChildren[iChild];
  501. if (child && dStrStartsWith(child->mName.C_Str(), "start")) {
  502. singleDetail = false;
  503. break;
  504. }
  505. }
  506. }
  507. }
  508. break;
  509. case ColladaUtils::ImportOptions::SingleSize:
  510. singleDetail = true;
  511. break;
  512. case ColladaUtils::ImportOptions::TrailingNumber:
  513. singleDetail = false;
  514. break;
  515. default:
  516. break;
  517. }
  518. AssimpAppMesh::fixDetailSize(singleDetail, ColladaUtils::getOptions().singleDetailSize);
  519. }
  520. void AssimpShapeLoader::extractTexture(U32 index, aiTexture* pTex)
  521. { // Cache an embedded texture to disk
  522. updateProgress(Load_EnumerateScene, "Extracting Textures...", mScene->mNumTextures, index);
  523. Con::printf("[Assimp] Extracting Texture %s, W: %d, H: %d, %d of %d, format hint: (%s)", pTex->mFilename.C_Str(),
  524. pTex->mWidth, pTex->mHeight, index, mScene->mNumTextures, pTex->achFormatHint);
  525. // Create the texture filename
  526. String cleanFile = AppMaterial::cleanString(TSShapeLoader::getShapePath().getFileName());
  527. String texName = String::ToString("%s_cachedTex%d", cleanFile.c_str(), index);
  528. Torque::Path texPath = shapePath;
  529. texPath.setFileName(texName);
  530. if (pTex->mHeight == 0)
  531. { // Compressed format, write the data directly to disc
  532. texPath.setExtension(pTex->achFormatHint);
  533. FileStream *outputStream;
  534. if ((outputStream = FileStream::createAndOpen(texPath.getFullPath(), Torque::FS::File::Write)) != NULL)
  535. {
  536. outputStream->setPosition(0);
  537. outputStream->write(pTex->mWidth, pTex->pcData);
  538. outputStream->close();
  539. delete outputStream;
  540. }
  541. }
  542. else
  543. { // Embedded pixel data, fill a bitmap and save it.
  544. GFXTexHandle shapeTex;
  545. shapeTex.set(pTex->mWidth, pTex->mHeight, GFXFormatR8G8B8A8_SRGB, &GFXDynamicTextureSRGBProfile,
  546. String::ToString("AssimpShapeLoader (%s:%i)", __FILE__, __LINE__), 1, 0);
  547. GFXLockedRect *rect = shapeTex.lock();
  548. for (U32 y = 0; y < pTex->mHeight; ++y)
  549. {
  550. for (U32 x = 0; x < pTex->mWidth; ++x)
  551. {
  552. U32 targetIndex = (y * rect->pitch) + (x * 4);
  553. U32 sourceIndex = ((y * pTex->mWidth) + x) * 4;
  554. rect->bits[targetIndex] = pTex->pcData[sourceIndex].r;
  555. rect->bits[targetIndex + 1] = pTex->pcData[sourceIndex].g;
  556. rect->bits[targetIndex + 2] = pTex->pcData[sourceIndex].b;
  557. rect->bits[targetIndex + 3] = pTex->pcData[sourceIndex].a;
  558. }
  559. }
  560. shapeTex.unlock();
  561. texPath.setExtension("png");
  562. shapeTex->dumpToDisk("PNG", texPath.getFullPath());
  563. }
  564. }
  565. void AssimpShapeLoader::addNodeToTree(S32 parentItem, aiNode* node, GuiTreeViewCtrl* tree, U32& nodeCount)
  566. {
  567. // Add this node
  568. S32 nodeItem = parentItem;
  569. String nodeName = node->mName.C_Str();
  570. if (!ignoreNode(nodeName))
  571. {
  572. if (nodeName.isEmpty())
  573. nodeName = "null";
  574. nodeItem = tree->insertItem(parentItem, nodeName.c_str(), String::ToString("%i", node->mNumChildren));
  575. nodeCount++;
  576. }
  577. // Add any child nodes
  578. for (U32 n = 0; n < node->mNumChildren; ++n)
  579. addNodeToTree(nodeItem, node->mChildren[n], tree, nodeCount);
  580. }
  581. void AssimpShapeLoader::addMetaDataToTree(const aiMetadata* metaData, GuiTreeViewCtrl* tree)
  582. {
  583. S32 metaItem = tree->insertItem(0, "MetaData", String::ToString("%i", metaData->mNumProperties));
  584. aiString valString;
  585. aiVector3D valVec;
  586. for (U32 n = 0; n < metaData->mNumProperties; ++n)
  587. {
  588. String keyStr = metaData->mKeys[n].C_Str();
  589. keyStr += ": ";
  590. switch (metaData->mValues[n].mType)
  591. {
  592. case AI_BOOL:
  593. keyStr += ((bool)metaData->mValues[n].mData) ? "true" : "false";
  594. break;
  595. case AI_INT32:
  596. keyStr += String::ToString(*((S32*)(metaData->mValues[n].mData)));
  597. break;
  598. case AI_UINT64:
  599. keyStr += String::ToString("%I64u", *((U64*)metaData->mValues[n].mData));
  600. break;
  601. case AI_FLOAT:
  602. keyStr += String::ToString(*((F32*)metaData->mValues[n].mData));
  603. break;
  604. case AI_DOUBLE:
  605. keyStr += String::ToString(*((F64*)metaData->mValues[n].mData));
  606. break;
  607. case AI_AISTRING:
  608. metaData->Get<aiString>(metaData->mKeys[n], valString);
  609. keyStr += valString.C_Str();
  610. break;
  611. case AI_AIVECTOR3D:
  612. metaData->Get<aiVector3D>(metaData->mKeys[n], valVec);
  613. keyStr += String::ToString("%f, %f, %f", valVec.x, valVec.y, valVec.z);
  614. break;
  615. default:
  616. break;
  617. }
  618. tree->insertItem(metaItem, keyStr.c_str(), String::ToString("%i", n));
  619. }
  620. }
  621. bool AssimpShapeLoader::getMetabool(const char* key, bool& boolVal)
  622. {
  623. if (!mScene || !mScene->mMetaData)
  624. return false;
  625. String keyStr = key;
  626. for (U32 n = 0; n < mScene->mMetaData->mNumProperties; ++n)
  627. {
  628. if (keyStr.equal(mScene->mMetaData->mKeys[n].C_Str(), String::NoCase))
  629. {
  630. if (mScene->mMetaData->mValues[n].mType == AI_BOOL)
  631. {
  632. boolVal = (bool)mScene->mMetaData->mValues[n].mData;
  633. return true;
  634. }
  635. }
  636. }
  637. return false;
  638. }
  639. bool AssimpShapeLoader::getMetaInt(const char* key, S32& intVal)
  640. {
  641. if (!mScene || !mScene->mMetaData)
  642. return false;
  643. String keyStr = key;
  644. for (U32 n = 0; n < mScene->mMetaData->mNumProperties; ++n)
  645. {
  646. if (keyStr.equal(mScene->mMetaData->mKeys[n].C_Str(), String::NoCase))
  647. {
  648. if (mScene->mMetaData->mValues[n].mType == AI_INT32)
  649. {
  650. intVal = *((S32*)(mScene->mMetaData->mValues[n].mData));
  651. return true;
  652. }
  653. }
  654. }
  655. return false;
  656. }
  657. bool AssimpShapeLoader::getMetaFloat(const char* key, F32& floatVal)
  658. {
  659. if (!mScene || !mScene->mMetaData)
  660. return false;
  661. String keyStr = key;
  662. for (U32 n = 0; n < mScene->mMetaData->mNumProperties; ++n)
  663. {
  664. if (keyStr.equal(mScene->mMetaData->mKeys[n].C_Str(), String::NoCase))
  665. {
  666. if (mScene->mMetaData->mValues[n].mType == AI_FLOAT)
  667. {
  668. floatVal = *((F32*)mScene->mMetaData->mValues[n].mData);
  669. return true;
  670. }
  671. }
  672. }
  673. return false;
  674. }
  675. bool AssimpShapeLoader::getMetaDouble(const char* key, F64& doubleVal)
  676. {
  677. if (!mScene || !mScene->mMetaData)
  678. return false;
  679. String keyStr = key;
  680. for (U32 n = 0; n < mScene->mMetaData->mNumProperties; ++n)
  681. {
  682. if (keyStr.equal(mScene->mMetaData->mKeys[n].C_Str(), String::NoCase))
  683. {
  684. if (mScene->mMetaData->mValues[n].mType == AI_DOUBLE)
  685. {
  686. doubleVal = *((F64*)mScene->mMetaData->mValues[n].mData);
  687. return true;
  688. }
  689. }
  690. }
  691. return false;
  692. }
  693. bool AssimpShapeLoader::getMetaString(const char* key, String& stringVal)
  694. {
  695. if (!mScene || !mScene->mMetaData)
  696. return false;
  697. String keyStr = key;
  698. for (U32 n = 0; n < mScene->mMetaData->mNumProperties; ++n)
  699. {
  700. if (keyStr.equal(mScene->mMetaData->mKeys[n].C_Str(), String::NoCase))
  701. {
  702. if (mScene->mMetaData->mValues[n].mType == AI_AISTRING)
  703. {
  704. aiString valString;
  705. mScene->mMetaData->Get<aiString>(mScene->mMetaData->mKeys[n], valString);
  706. stringVal = valString.C_Str();
  707. return true;
  708. }
  709. }
  710. }
  711. return false;
  712. }
  713. //-----------------------------------------------------------------------------
  714. /// This function is invoked by the resource manager based on file extension.
  715. TSShape* assimpLoadShape(const Torque::Path &path)
  716. {
  717. // TODO: add .cached.dts generation.
  718. // Generate the cached filename
  719. Torque::Path cachedPath(path);
  720. cachedPath.setExtension("cached.dts");
  721. // Check if an up-to-date cached DTS version of this file exists, and
  722. // if so, use that instead.
  723. if (AssimpShapeLoader::canLoadCachedDTS(path))
  724. {
  725. FileStream cachedStream;
  726. cachedStream.open(cachedPath.getFullPath(), Torque::FS::File::Read);
  727. if (cachedStream.getStatus() == Stream::Ok)
  728. {
  729. TSShape *shape = new TSShape;
  730. bool readSuccess = shape->read(&cachedStream);
  731. cachedStream.close();
  732. if (readSuccess)
  733. {
  734. #ifdef TORQUE_DEBUG
  735. Con::printf("Loaded cached shape from %s", cachedPath.getFullPath().c_str());
  736. #endif
  737. return shape;
  738. }
  739. else
  740. delete shape;
  741. }
  742. Con::warnf("Failed to load cached shape from %s", cachedPath.getFullPath().c_str());
  743. }
  744. if (!Torque::FS::IsFile(path))
  745. {
  746. // File does not exist, bail.
  747. return NULL;
  748. }
  749. // Allow TSShapeConstructor object to override properties
  750. ColladaUtils::getOptions().reset();
  751. TSShapeConstructor* tscon = TSShapeConstructor::findShapeConstructorByFilename(path.getFullPath());
  752. if (tscon)
  753. {
  754. ColladaUtils::getOptions() = tscon->mOptions;
  755. }
  756. AssimpShapeLoader loader;
  757. TSShape* tss = loader.generateShape(path);
  758. if (tss)
  759. {
  760. TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Import complete");
  761. Con::printf("[ASSIMP] Shape created successfully.");
  762. // Cache the model to a DTS file for faster loading next time.
  763. FileStream dtsStream;
  764. if (dtsStream.open(cachedPath.getFullPath(), Torque::FS::File::Write))
  765. {
  766. Con::printf("Writing cached shape to %s", cachedPath.getFullPath().c_str());
  767. tss->write(&dtsStream);
  768. }
  769. loader.updateMaterialsScript(path);
  770. }
  771. loader.releaseImport();
  772. return tss;
  773. }
  774. DefineEngineFunction(GetShapeInfo, bool, (const char* shapePath, const char* ctrl, bool loadCachedDts), ("", "", true),
  775. "(string shapePath, GuiTreeViewCtrl ctrl) Collect scene information from "
  776. "a shape file and store it in a GuiTreeView control. This function is "
  777. "used by the assimp import gui to show a preview of the scene contents "
  778. "prior to import, and is probably not much use for anything else.\n"
  779. "@param shapePath shape filename\n"
  780. "@param ctrl GuiTreeView control to add elements to\n"
  781. "@return true if successful, false otherwise\n"
  782. "@ingroup Editors\n"
  783. "@internal")
  784. {
  785. GuiTreeViewCtrl* tree;
  786. if (!Sim::findObject(ctrl, tree))
  787. {
  788. Con::errorf("enumColladaScene::Could not find GuiTreeViewCtrl '%s'", ctrl);
  789. return false;
  790. }
  791. // Check if a cached DTS is available => no need to import the source file
  792. // if we can load the DTS instead
  793. Torque::Path path(shapePath);
  794. if (loadCachedDts && AssimpShapeLoader::canLoadCachedDTS(path))
  795. return false;
  796. AssimpShapeLoader loader;
  797. return loader.fillGuiTreeView(shapePath, tree);
  798. }