OpenAssetImporter.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  23. // Please see LICENSE.md in repository root for license information
  24. // https://github.com/AtomicGameEngine/AtomicGameEngine
  25. #include <Atomic/Core/ProcessUtils.h>
  26. #include <Atomic/Core/Context.h>
  27. #include <Atomic/IO/Log.h>
  28. #include <Atomic/IO/File.h>
  29. #include <Atomic/IO/FileSystem.h>
  30. #include <Atomic/Resource/XMLFile.h>
  31. #include <Atomic/Atomic3D/AnimatedModel.h>
  32. #include <Atomic/Atomic3D/Animation.h>
  33. #include <Atomic/Graphics/Geometry.h>
  34. #include <Atomic/Graphics/IndexBuffer.h>
  35. #include <Atomic/Graphics/VertexBuffer.h>
  36. #include "OpenAssetImporter.h"
  37. namespace ToolCore
  38. {
  39. OpenAssetImporter::OpenAssetImporter(Context* context) : Object(context) ,
  40. scene_(0),
  41. rootNode_(0),
  42. useSubdirs_(true),
  43. localIDs_(false),
  44. saveBinary_(false),
  45. createZone_(true),
  46. noAnimations_(false),
  47. noHierarchy_(false),
  48. noMaterials_(false),
  49. noTextures_(false),
  50. noMaterialDiffuseColor_(false),
  51. noEmptyNodes_(false),
  52. saveMaterialList_(false),
  53. includeNonSkinningBones_(false),
  54. verboseLog_(false),
  55. emissiveAO_(false),
  56. noOverwriteMaterial_(true),
  57. noOverwriteTexture_(true),
  58. noOverwriteNewerTexture_(true),
  59. checkUniqueModel_(true),
  60. maxBones_(64),
  61. defaultTicksPerSecond_(4800.0f)
  62. {
  63. aiFlagsDefault_ =
  64. aiProcess_ConvertToLeftHanded |
  65. aiProcess_JoinIdenticalVertices |
  66. aiProcess_Triangulate |
  67. aiProcess_GenSmoothNormals |
  68. aiProcess_LimitBoneWeights |
  69. aiProcess_ImproveCacheLocality |
  70. aiProcess_RemoveRedundantMaterials |
  71. aiProcess_FixInfacingNormals |
  72. aiProcess_FindInvalidData |
  73. aiProcess_GenUVCoords |
  74. aiProcess_FindInstances |
  75. aiProcess_OptimizeMeshes;
  76. aiCurrentFlags_ = aiFlagsDefault_;
  77. }
  78. OpenAssetImporter::~OpenAssetImporter()
  79. {
  80. if (scene_)
  81. aiReleaseImport(scene_);
  82. }
  83. bool OpenAssetImporter::Load(const String &assetPath)
  84. {
  85. if (verboseLog_)
  86. Assimp::DefaultLogger::create("", Assimp::Logger::VERBOSE, aiDefaultLogStream_STDOUT);
  87. //PrintLine("Reading file " + assetPath);
  88. sourceAssetPath_ = GetPath(assetPath);
  89. scene_ = aiImportFile(GetNativePath(assetPath).CString(), aiCurrentFlags_);
  90. if (!scene_)
  91. ErrorExit("Could not open or parse input file " + assetPath + ": " + String(aiGetErrorString()));
  92. if (verboseLog_)
  93. Assimp::DefaultLogger::kill();
  94. rootNode_ = scene_->mRootNode;
  95. // DumpNodes(rootNode_, 0);
  96. return true;
  97. }
  98. void OpenAssetImporter::ExportModel(const String& outName, bool animationOnly)
  99. {
  100. if (outName.Empty())
  101. ErrorExit("No output file defined");
  102. OutModel model;
  103. model.rootNode_ = rootNode_;
  104. model.outName_ = outName;
  105. CollectMeshes(scene_, model, model.rootNode_);
  106. CollectBones(model, animationOnly);
  107. BuildBoneCollisionInfo(model);
  108. BuildAndSaveModel(model);
  109. if (!noAnimations_)
  110. {
  111. CollectAnimations(&model);
  112. BuildAndSaveAnimations(&model);
  113. // Save scene-global animations
  114. CollectAnimations();
  115. BuildAndSaveAnimations();
  116. }
  117. if (!noMaterials_)
  118. {
  119. HashSet<String> usedTextures;
  120. ExportMaterials(usedTextures);
  121. }
  122. }
  123. void OpenAssetImporter::BuildAndSaveModel(OutModel& model)
  124. {
  125. if (!model.rootNode_)
  126. ErrorExit("Null root node for model");
  127. String rootNodeName = FromAIString(model.rootNode_->mName);
  128. if (!model.meshes_.Size())
  129. ErrorExit("No geometries found starting from node " + rootNodeName);
  130. //PrintLine("Writing model " + rootNodeName);
  131. SharedPtr<Model> outModel(new Model(context_));
  132. Vector<PODVector<unsigned> > allBoneMappings;
  133. BoundingBox box;
  134. unsigned numValidGeometries = 0;
  135. bool combineBuffers = true;
  136. // Check if buffers can be combined (same vertex element mask, under 65535 vertices)
  137. unsigned elementMask = GetElementMask(model.meshes_[0]);
  138. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  139. {
  140. if (GetNumValidFaces(model.meshes_[i]))
  141. {
  142. ++numValidGeometries;
  143. if (i > 0 && GetElementMask(model.meshes_[i]) != elementMask)
  144. combineBuffers = false;
  145. }
  146. }
  147. // Check if keeping separate buffers allows to avoid 32-bit indices
  148. if (combineBuffers && model.totalVertices_ > 65535)
  149. {
  150. bool allUnder65k = true;
  151. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  152. {
  153. if (GetNumValidFaces(model.meshes_[i]))
  154. {
  155. if (model.meshes_[i]->mNumVertices > 65535)
  156. allUnder65k = false;
  157. }
  158. }
  159. if (allUnder65k == true)
  160. combineBuffers = false;
  161. }
  162. SharedPtr<IndexBuffer> ib;
  163. SharedPtr<VertexBuffer> vb;
  164. Vector<SharedPtr<VertexBuffer> > vbVector;
  165. Vector<SharedPtr<IndexBuffer> > ibVector;
  166. unsigned startVertexOffset = 0;
  167. unsigned startIndexOffset = 0;
  168. unsigned destGeomIndex = 0;
  169. outModel->SetNumGeometries(numValidGeometries);
  170. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  171. {
  172. aiMesh* mesh = model.meshes_[i];
  173. unsigned elementMask = GetElementMask(mesh);
  174. unsigned validFaces = GetNumValidFaces(mesh);
  175. if (!validFaces)
  176. continue;
  177. bool largeIndices;
  178. if (combineBuffers)
  179. largeIndices = model.totalIndices_ > 65535;
  180. else
  181. largeIndices = mesh->mNumVertices > 65535;
  182. // Create new buffers if necessary
  183. if (!combineBuffers || vbVector.Empty())
  184. {
  185. vb = new VertexBuffer(context_);
  186. ib = new IndexBuffer(context_);
  187. vb->SetShadowed(true);
  188. ib->SetShadowed(true);
  189. if (combineBuffers)
  190. {
  191. ib->SetSize(model.totalIndices_, largeIndices);
  192. vb->SetSize(model.totalVertices_, elementMask);
  193. }
  194. else
  195. {
  196. ib->SetSize(validFaces * 3, largeIndices);
  197. vb->SetSize(mesh->mNumVertices, elementMask);
  198. }
  199. vbVector.Push(vb);
  200. ibVector.Push(ib);
  201. startVertexOffset = 0;
  202. startIndexOffset = 0;
  203. }
  204. // Get the world transform of the mesh for baking into the vertices
  205. Matrix3x4 vertexTransform;
  206. Matrix3 normalTransform;
  207. Vector3 pos, scale;
  208. Quaternion rot;
  209. GetPosRotScale(GetMeshBakingTransform(model.meshNodes_[i], model.rootNode_), pos, rot, scale);
  210. vertexTransform = Matrix3x4(pos, rot, scale);
  211. normalTransform = rot.RotationMatrix();
  212. SharedPtr<Geometry> geom(new Geometry(context_));
  213. //PrintLine("Writing geometry " + String(i) + " with " + String(mesh->mNumVertices) + " vertices " +
  214. // String(validFaces * 3) + " indices");
  215. unsigned char* vertexData = vb->GetShadowData();
  216. unsigned char* indexData = ib->GetShadowData();
  217. assert(vertexData);
  218. assert(indexData);
  219. // Build the index data
  220. if (!largeIndices)
  221. {
  222. unsigned short* dest = (unsigned short*)indexData + startIndexOffset;
  223. for (unsigned j = 0; j < mesh->mNumFaces; ++j)
  224. WriteShortIndices(dest, mesh, j, startVertexOffset);
  225. }
  226. else
  227. {
  228. unsigned* dest = (unsigned*)indexData + startIndexOffset;
  229. for (unsigned j = 0; j < mesh->mNumFaces; ++j)
  230. WriteLargeIndices(dest, mesh, j, startVertexOffset);
  231. }
  232. // Build the vertex data
  233. // If there are bones, get blend data
  234. Vector<PODVector<unsigned char> > blendIndices;
  235. Vector<PODVector<float> > blendWeights;
  236. PODVector<unsigned> boneMappings;
  237. if (model.bones_.Size())
  238. GetBlendData(model, mesh, boneMappings, blendIndices, blendWeights, maxBones_);
  239. float* dest = (float*)((unsigned char*)vertexData + startVertexOffset * vb->GetVertexSize());
  240. for (unsigned j = 0; j < mesh->mNumVertices; ++j)
  241. WriteVertex(dest, mesh, j, elementMask, box, vertexTransform, normalTransform, blendIndices, blendWeights);
  242. // Calculate the geometry center
  243. Vector3 center = Vector3::ZERO;
  244. if (validFaces)
  245. {
  246. for (unsigned j = 0; j < mesh->mNumFaces; ++j)
  247. {
  248. if (mesh->mFaces[j].mNumIndices == 3)
  249. {
  250. center += vertexTransform * ToVector3(mesh->mVertices[mesh->mFaces[j].mIndices[0]]);
  251. center += vertexTransform * ToVector3(mesh->mVertices[mesh->mFaces[j].mIndices[1]]);
  252. center += vertexTransform * ToVector3(mesh->mVertices[mesh->mFaces[j].mIndices[2]]);
  253. }
  254. }
  255. center /= (float)validFaces * 3;
  256. }
  257. // Define the geometry
  258. geom->SetIndexBuffer(ib);
  259. geom->SetVertexBuffer(0, vb);
  260. geom->SetDrawRange(TRIANGLE_LIST, startIndexOffset, validFaces * 3, true);
  261. outModel->SetNumGeometryLodLevels(destGeomIndex, 1);
  262. outModel->SetGeometry(destGeomIndex, 0, geom);
  263. outModel->SetGeometryCenter(destGeomIndex, center);
  264. if (model.bones_.Size() > maxBones_)
  265. allBoneMappings.Push(boneMappings);
  266. startVertexOffset += mesh->mNumVertices;
  267. startIndexOffset += validFaces * 3;
  268. ++destGeomIndex;
  269. }
  270. // Define the model buffers and bounding box
  271. PODVector<unsigned> emptyMorphRange;
  272. outModel->SetVertexBuffers(vbVector, emptyMorphRange, emptyMorphRange);
  273. outModel->SetIndexBuffers(ibVector);
  274. outModel->SetBoundingBox(box);
  275. // Build skeleton if necessary
  276. if (model.bones_.Size() && model.rootBone_)
  277. {
  278. //PrintLine("Writing skeleton with " + String(model.bones_.Size()) + " bones, rootbone " +
  279. // FromAIString(model.rootBone_->mName));
  280. Skeleton skeleton;
  281. Vector<Bone>& bones = skeleton.GetModifiableBones();
  282. for (unsigned i = 0; i < model.bones_.Size(); ++i)
  283. {
  284. aiNode* boneNode = model.bones_[i];
  285. String boneName(FromAIString(boneNode->mName));
  286. Bone newBone;
  287. newBone.name_ = boneName;
  288. aiMatrix4x4 transform = boneNode->mTransformation;
  289. // Make the root bone transform relative to the model's root node, if it is not already
  290. if (boneNode == model.rootBone_)
  291. transform = GetDerivedTransform(boneNode, model.rootNode_);
  292. GetPosRotScale(transform, newBone.initialPosition_, newBone.initialRotation_, newBone.initialScale_);
  293. // Get offset information if exists
  294. newBone.offsetMatrix_ = GetOffsetMatrix(model, boneName);
  295. newBone.radius_ = model.boneRadii_[i];
  296. newBone.boundingBox_ = model.boneHitboxes_[i];
  297. newBone.collisionMask_ = BONECOLLISION_SPHERE | BONECOLLISION_BOX;
  298. newBone.parentIndex_ = i;
  299. bones.Push(newBone);
  300. }
  301. // Set the bone hierarchy
  302. for (unsigned i = 1; i < model.bones_.Size(); ++i)
  303. {
  304. String parentName = FromAIString(model.bones_[i]->mParent->mName);
  305. for (unsigned j = 0; j < bones.Size(); ++j)
  306. {
  307. if (bones[j].name_ == parentName)
  308. {
  309. bones[i].parentIndex_ = j;
  310. break;
  311. }
  312. }
  313. }
  314. outModel->SetSkeleton(skeleton);
  315. if (model.bones_.Size() > maxBones_)
  316. outModel->SetGeometryBoneMappings(allBoneMappings);
  317. }
  318. File outFile(context_);
  319. if (!outFile.Open(model.outName_, FILE_WRITE))
  320. ErrorExit("Could not open output file " + model.outName_);
  321. outModel->Save(outFile);
  322. // If exporting materials, also save material list for use by the editor
  323. if (!noMaterials_ && saveMaterialList_)
  324. {
  325. String materialListName = ReplaceExtension(model.outName_, ".txt");
  326. File listFile(context_);
  327. if (listFile.Open(materialListName, FILE_WRITE))
  328. {
  329. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  330. listFile.WriteLine(GetMeshMaterialName(model.meshes_[i]));
  331. }
  332. else
  333. {
  334. PrintLine("Warning: could not write material list file " + materialListName);
  335. }
  336. }
  337. }
  338. String OpenAssetImporter::GetMeshMaterialName(aiMesh* mesh)
  339. {
  340. aiMaterial* material = scene_->mMaterials[mesh->mMaterialIndex];
  341. aiString matNameStr;
  342. material->Get(AI_MATKEY_NAME, matNameStr);
  343. String matName = SanitateAssetName(FromAIString(matNameStr));
  344. if (matName.Trimmed().Empty())
  345. matName = GenerateMaterialName(material);
  346. return (useSubdirs_ ? "Materials/" : "") + matName + ".material";
  347. }
  348. String OpenAssetImporter::GenerateMaterialName(aiMaterial* material)
  349. {
  350. for (unsigned i = 0; i < scene_->mNumMaterials; ++i)
  351. {
  352. if (scene_->mMaterials[i] == material)
  353. return inputName_ + "_Material" + String(i);
  354. }
  355. // Should not go here
  356. return String::EMPTY;
  357. }
  358. String OpenAssetImporter::GetMaterialTextureName(const String& nameIn)
  359. {
  360. // Detect assimp embedded texture
  361. if (nameIn.Length() && nameIn[0] == '*')
  362. return GenerateTextureName(ToInt(nameIn.Substring(1)));
  363. else
  364. return (useSubdirs_ ? "Textures/" : "") + nameIn;
  365. }
  366. String OpenAssetImporter::GenerateTextureName(unsigned texIndex)
  367. {
  368. if (texIndex < scene_->mNumTextures)
  369. {
  370. // If embedded texture contains encoded data, use the format hint for file extension. Else save RGBA8 data as PNG
  371. aiTexture* tex = scene_->mTextures[texIndex];
  372. if (!tex->mHeight)
  373. return (useSubdirs_ ? "Textures/" : "") + inputName_ + "_Texture" + String(texIndex) + "." + tex->achFormatHint;
  374. else
  375. return (useSubdirs_ ? "Textures/" : "") + inputName_ + "_Texture" + String(texIndex) + ".png";
  376. }
  377. // Should not go here
  378. return String::EMPTY;
  379. }
  380. void OpenAssetImporter::CollectSceneModels(OutScene& scene, aiNode* node)
  381. {
  382. Vector<Pair<aiNode*, aiMesh*> > meshes;
  383. GetMeshesUnderNode(scene_, meshes, node);
  384. if (meshes.Size())
  385. {
  386. OutModel model;
  387. model.rootNode_ = node;
  388. model.outName_ = resourcePath_ + (useSubdirs_ ? "Models/" : "") + SanitateAssetName(FromAIString(node->mName)) + ".mdl";
  389. for (unsigned i = 0; i < meshes.Size(); ++i)
  390. {
  391. aiMesh* mesh = meshes[i].second_;
  392. unsigned meshIndex = GetMeshIndex(scene_, mesh);
  393. model.meshIndices_.Insert(meshIndex);
  394. model.meshes_.Push(mesh);
  395. model.meshNodes_.Push(meshes[i].first_);
  396. model.totalVertices_ += mesh->mNumVertices;
  397. model.totalIndices_ += GetNumValidFaces(mesh) * 3;
  398. }
  399. // Check if a model with identical mesh indices already exists. If yes, do not export twice
  400. bool unique = true;
  401. if (checkUniqueModel_)
  402. {
  403. for (unsigned i = 0; i < scene.models_.Size(); ++i)
  404. {
  405. if (scene.models_[i].meshIndices_ == model.meshIndices_)
  406. {
  407. //PrintLine("Added node " + FromAIString(node->mName));
  408. scene.nodes_.Push(node);
  409. scene.nodeModelIndices_.Push(i);
  410. unique = false;
  411. break;
  412. }
  413. }
  414. }
  415. if (unique)
  416. {
  417. // PrintLine("Added model " + model.outName_);
  418. // PrintLine("Added node " + FromAIString(node->mName));
  419. CollectBones(model);
  420. BuildBoneCollisionInfo(model);
  421. if (!noAnimations_)
  422. {
  423. CollectAnimations(&model);
  424. BuildAndSaveAnimations(&model);
  425. }
  426. scene.models_.Push(model);
  427. scene.nodes_.Push(node);
  428. scene.nodeModelIndices_.Push(scene.models_.Size() - 1);
  429. }
  430. }
  431. for (unsigned i = 0; i < node->mNumChildren; ++i)
  432. CollectSceneModels(scene, node->mChildren[i]);
  433. }
  434. void OpenAssetImporter::CollectBones(OutModel& model, bool animationOnly)
  435. {
  436. HashSet<aiNode*> necessary;
  437. HashSet<aiNode*> rootNodes;
  438. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  439. {
  440. aiMesh* mesh = model.meshes_[i];
  441. aiNode* meshNode = model.meshNodes_[i];
  442. aiNode* meshParentNode = meshNode->mParent;
  443. aiNode* rootNode = 0;
  444. for (unsigned j = 0; j < mesh->mNumBones; ++j)
  445. {
  446. aiBone* bone = mesh->mBones[j];
  447. String boneName(FromAIString(bone->mName));
  448. aiNode* boneNode = GetNode(boneName, scene_->mRootNode, true);
  449. if (!boneNode)
  450. ErrorExit("Could not find scene node for bone " + boneName);
  451. necessary.Insert(boneNode);
  452. rootNode = boneNode;
  453. for (;;)
  454. {
  455. boneNode = boneNode->mParent;
  456. if (!boneNode || ((boneNode == meshNode || boneNode == meshParentNode) && !animationOnly))
  457. break;
  458. rootNode = boneNode;
  459. necessary.Insert(boneNode);
  460. }
  461. if (rootNodes.Find(rootNode) == rootNodes.End())
  462. rootNodes.Insert(rootNode);
  463. }
  464. }
  465. // If we find multiple root nodes, try to remedy by using their parent instead
  466. if (rootNodes.Size() > 1)
  467. {
  468. aiNode* commonParent = (*rootNodes.Begin())->mParent;
  469. for (HashSet<aiNode*>::Iterator i = rootNodes.Begin(); i != rootNodes.End(); ++i)
  470. {
  471. if (*i != commonParent)
  472. {
  473. if (!commonParent || (*i)->mParent != commonParent)
  474. ErrorExit("Skeleton with multiple root nodes found, not supported");
  475. }
  476. }
  477. rootNodes.Clear();
  478. rootNodes.Insert(commonParent);
  479. necessary.Insert(commonParent);
  480. }
  481. if (rootNodes.Empty())
  482. return;
  483. model.rootBone_ = *rootNodes.Begin();
  484. CollectBonesFinal(model.bones_, necessary, model.rootBone_);
  485. // Initialize the bone collision info
  486. model.boneRadii_.Resize(model.bones_.Size());
  487. model.boneHitboxes_.Resize(model.bones_.Size());
  488. for (unsigned i = 0; i < model.bones_.Size(); ++i)
  489. {
  490. model.boneRadii_[i] = 0.0f;
  491. model.boneHitboxes_[i] = BoundingBox(0.0f, 0.0f);
  492. }
  493. }
  494. void OpenAssetImporter::CollectBonesFinal(PODVector<aiNode*>& dest, const HashSet<aiNode*>& necessary, aiNode* node)
  495. {
  496. bool includeBone = necessary.Find(node) != necessary.End();
  497. String boneName = FromAIString(node->mName);
  498. // Check include/exclude filters for non-skinned bones
  499. if (!includeBone && includeNonSkinningBones_)
  500. {
  501. // If no includes specified, include by default but check for excludes
  502. if (nonSkinningBoneIncludes_.Empty())
  503. includeBone = true;
  504. // Check against includes/excludes
  505. for (unsigned i = 0; i < nonSkinningBoneIncludes_.Size(); ++i)
  506. {
  507. if (boneName.Contains(nonSkinningBoneIncludes_[i], false))
  508. {
  509. includeBone = true;
  510. break;
  511. }
  512. }
  513. for (unsigned i = 0; i < nonSkinningBoneExcludes_.Size(); ++i)
  514. {
  515. if (boneName.Contains(nonSkinningBoneExcludes_[i], false))
  516. {
  517. includeBone = false;
  518. break;
  519. }
  520. }
  521. if (includeBone)
  522. {
  523. //PrintLine("Including non-skinning bone " + boneName);
  524. }
  525. }
  526. if (includeBone)
  527. dest.Push(node);
  528. for (unsigned i = 0; i < node->mNumChildren; ++i)
  529. CollectBonesFinal(dest, necessary, node->mChildren[i]);
  530. }
  531. void OpenAssetImporter::CollectAnimations(OutModel* model)
  532. {
  533. const aiScene* scene = scene_;
  534. for (unsigned i = 0; i < scene->mNumAnimations; ++i)
  535. {
  536. aiAnimation* anim = scene->mAnimations[i];
  537. if (allAnimations_.Contains(anim))
  538. continue;
  539. if (model)
  540. {
  541. bool modelBoneFound = false;
  542. for (unsigned j = 0; j < anim->mNumChannels; ++j)
  543. {
  544. aiNodeAnim* channel = anim->mChannels[j];
  545. String channelName = FromAIString(channel->mNodeName);
  546. if (GetBoneIndex(*model, channelName) != M_MAX_UNSIGNED)
  547. {
  548. modelBoneFound = true;
  549. break;
  550. }
  551. }
  552. if (modelBoneFound)
  553. {
  554. model->animations_.Push(anim);
  555. allAnimations_.Insert(anim);
  556. }
  557. }
  558. else
  559. {
  560. sceneAnimations_.Push(anim);
  561. allAnimations_.Insert(anim);
  562. }
  563. }
  564. /// \todo Vertex morphs are ignored for now
  565. }
  566. void OpenAssetImporter::BuildBoneCollisionInfo(OutModel& model)
  567. {
  568. for (unsigned i = 0; i < model.meshes_.Size(); ++i)
  569. {
  570. aiMesh* mesh = model.meshes_[i];
  571. for (unsigned j = 0; j < mesh->mNumBones; ++j)
  572. {
  573. aiBone* bone = mesh->mBones[j];
  574. String boneName = FromAIString(bone->mName);
  575. unsigned boneIndex = GetBoneIndex(model, boneName);
  576. if (boneIndex == M_MAX_UNSIGNED)
  577. continue;
  578. for (unsigned k = 0; k < bone->mNumWeights; ++k)
  579. {
  580. float weight = bone->mWeights[k].mWeight;
  581. // Require skinning weight to be sufficiently large before vertex contributes to bone hitbox
  582. if (weight > 0.33f)
  583. {
  584. aiVector3D vertexBoneSpace = bone->mOffsetMatrix * mesh->mVertices[bone->mWeights[k].mVertexId];
  585. Vector3 vertex = ToVector3(vertexBoneSpace);
  586. float radius = vertex.Length();
  587. if (radius > model.boneRadii_[boneIndex])
  588. model.boneRadii_[boneIndex] = radius;
  589. model.boneHitboxes_[boneIndex].Merge(vertex);
  590. }
  591. }
  592. }
  593. }
  594. }
  595. void OpenAssetImporter::BuildAndSaveAnimations(OutModel* model)
  596. {
  597. const PODVector<aiAnimation*>& animations = model ? model->animations_ : sceneAnimations_;
  598. for (unsigned i = 0; i < animations.Size(); ++i)
  599. {
  600. aiAnimation* anim = animations[i];
  601. float duration = (float)anim->mDuration;
  602. String animName = FromAIString(anim->mName);
  603. String animOutName;
  604. if (animName.Empty())
  605. animName = "Anim" + String(i + 1);
  606. if (model)
  607. animOutName = GetPath(model->outName_) + GetFileName(model->outName_) + "_" + SanitateAssetName(animName) + ".ani";
  608. else
  609. animOutName = outPath_ + SanitateAssetName(animName) + ".ani";
  610. float ticksPerSecond = (float)anim->mTicksPerSecond;
  611. // If ticks per second not specified, it's probably a .X file. In this case use the default tick rate
  612. if (ticksPerSecond < M_EPSILON)
  613. ticksPerSecond = defaultTicksPerSecond_;
  614. float tickConversion = 1.0f / ticksPerSecond;
  615. // Find out the start time of animation from each channel's first keyframe for adjusting the keyframe times
  616. // to start from zero
  617. float startTime = duration;
  618. for (unsigned j = 0; j < anim->mNumChannels; ++j)
  619. {
  620. aiNodeAnim* channel = anim->mChannels[j];
  621. if (channel->mNumPositionKeys > 0)
  622. startTime = Min(startTime, (float)channel->mPositionKeys[0].mTime);
  623. if (channel->mNumRotationKeys > 0)
  624. startTime = Min(startTime, (float)channel->mRotationKeys[0].mTime);
  625. if (channel->mScalingKeys > 0)
  626. startTime = Min(startTime, (float)channel->mScalingKeys[0].mTime);
  627. }
  628. duration -= startTime;
  629. SharedPtr<Animation> outAnim(new Animation(context_));
  630. outAnim->SetAnimationName(animName);
  631. outAnim->SetLength(duration * tickConversion);
  632. //PrintLine("Writing animation " + animName + " length " + String(outAnim->GetLength()));
  633. Vector<AnimationTrack> tracks;
  634. for (unsigned j = 0; j < anim->mNumChannels; ++j)
  635. {
  636. aiNodeAnim* channel = anim->mChannels[j];
  637. String channelName = FromAIString(channel->mNodeName);
  638. aiNode* boneNode = 0;
  639. bool isRootBone = false;
  640. if (model)
  641. {
  642. unsigned boneIndex = GetBoneIndex(*model, channelName);
  643. if (boneIndex == M_MAX_UNSIGNED)
  644. {
  645. PrintLine("Warning: skipping animation track " + channelName + " not found in model skeleton");
  646. continue;
  647. }
  648. boneNode = model->bones_[boneIndex];
  649. isRootBone = boneIndex == 0;
  650. }
  651. else
  652. {
  653. boneNode = GetNode(channelName, scene_->mRootNode);
  654. if (!boneNode)
  655. {
  656. PrintLine("Warning: skipping animation track " + channelName + " whose scene node was not found");
  657. continue;
  658. }
  659. }
  660. // To export single frame animation, check if first key frame is identical to bone transformation
  661. aiVector3D bonePos, boneScale;
  662. aiQuaternion boneRot;
  663. boneNode->mTransformation.Decompose(boneScale, boneRot, bonePos);
  664. bool posEqual = true;
  665. bool scaleEqual = true;
  666. bool rotEqual = true;
  667. if (channel->mNumPositionKeys > 0 && !ToVector3(bonePos).Equals(ToVector3(channel->mPositionKeys[0].mValue)))
  668. posEqual = false;
  669. if (channel->mNumScalingKeys > 0 && !ToVector3(boneScale).Equals(ToVector3(channel->mScalingKeys[0].mValue)))
  670. scaleEqual = false;
  671. if (channel->mNumRotationKeys > 0 && !ToQuaternion(boneRot).Equals(ToQuaternion(channel->mRotationKeys[0].mValue)))
  672. rotEqual = false;
  673. AnimationTrack track;
  674. track.name_ = channelName;
  675. track.nameHash_ = channelName;
  676. // Check which channels are used
  677. track.channelMask_ = 0;
  678. if (channel->mNumPositionKeys > 1 || !posEqual)
  679. track.channelMask_ |= CHANNEL_POSITION;
  680. if (channel->mNumRotationKeys > 1 || !rotEqual)
  681. track.channelMask_ |= CHANNEL_ROTATION;
  682. if (channel->mNumScalingKeys > 1 || !scaleEqual)
  683. track.channelMask_ |= CHANNEL_SCALE;
  684. // Check for redundant identity scale in all keyframes and remove in that case
  685. if (track.channelMask_ & CHANNEL_SCALE)
  686. {
  687. bool redundantScale = true;
  688. for (unsigned k = 0; k < channel->mNumScalingKeys; ++k)
  689. {
  690. float SCALE_EPSILON = 0.000001f;
  691. Vector3 scaleVec = ToVector3(channel->mScalingKeys[k].mValue);
  692. if (fabsf(scaleVec.x_ - 1.0f) >= SCALE_EPSILON || fabsf(scaleVec.y_ - 1.0f) >= SCALE_EPSILON ||
  693. fabsf(scaleVec.z_ - 1.0f) >= SCALE_EPSILON)
  694. {
  695. redundantScale = false;
  696. break;
  697. }
  698. }
  699. if (redundantScale)
  700. track.channelMask_ &= ~CHANNEL_SCALE;
  701. }
  702. if (!track.channelMask_)
  703. PrintLine("Warning: skipping animation track " + channelName + " with no keyframes");
  704. // Currently only same amount of keyframes is supported
  705. // Note: should also check the times of individual keyframes for match
  706. if ((channel->mNumPositionKeys > 1 && channel->mNumRotationKeys > 1 && channel->mNumPositionKeys != channel->mNumRotationKeys) ||
  707. (channel->mNumPositionKeys > 1 && channel->mNumScalingKeys > 1 && channel->mNumPositionKeys != channel->mNumScalingKeys) ||
  708. (channel->mNumRotationKeys > 1 && channel->mNumScalingKeys > 1 && channel->mNumRotationKeys != channel->mNumScalingKeys))
  709. {
  710. PrintLine("Warning: differing amounts of channel keyframes, skipping animation track " + channelName);
  711. continue;
  712. }
  713. unsigned keyFrames = channel->mNumPositionKeys;
  714. if (channel->mNumRotationKeys > keyFrames)
  715. keyFrames = channel->mNumRotationKeys;
  716. if (channel->mNumScalingKeys > keyFrames)
  717. keyFrames = channel->mNumScalingKeys;
  718. for (unsigned k = 0; k < keyFrames; ++k)
  719. {
  720. AnimationKeyFrame kf;
  721. kf.time_ = 0.0f;
  722. kf.position_ = Vector3::ZERO;
  723. kf.rotation_ = Quaternion::IDENTITY;
  724. kf.scale_ = Vector3::ONE;
  725. // Get time for the keyframe. Adjust with animation's start time
  726. if (track.channelMask_ & CHANNEL_POSITION && k < channel->mNumPositionKeys)
  727. kf.time_ = ((float)channel->mPositionKeys[k].mTime - startTime) * tickConversion;
  728. else if (track.channelMask_ & CHANNEL_ROTATION && k < channel->mNumRotationKeys)
  729. kf.time_ = ((float)channel->mRotationKeys[k].mTime - startTime) * tickConversion;
  730. else if (track.channelMask_ & CHANNEL_SCALE && k < channel->mNumScalingKeys)
  731. kf.time_ = ((float)channel->mScalingKeys[k].mTime - startTime) * tickConversion;
  732. // Make sure time stays positive
  733. kf.time_ = Max(kf.time_, 0.0f);
  734. // Start with the bone's base transform
  735. aiMatrix4x4 boneTransform = boneNode->mTransformation;
  736. aiVector3D pos, scale;
  737. aiQuaternion rot;
  738. boneTransform.Decompose(scale, rot, pos);
  739. // Then apply the active channels
  740. if (track.channelMask_ & CHANNEL_POSITION && k < channel->mNumPositionKeys)
  741. pos = channel->mPositionKeys[k].mValue;
  742. if (track.channelMask_ & CHANNEL_ROTATION && k < channel->mNumRotationKeys)
  743. rot = channel->mRotationKeys[k].mValue;
  744. if (track.channelMask_ & CHANNEL_SCALE && k < channel->mNumScalingKeys)
  745. scale = channel->mScalingKeys[k].mValue;
  746. // If root bone, transform with the model root node transform
  747. if (model && isRootBone)
  748. {
  749. aiMatrix4x4 transMat, scaleMat, rotMat;
  750. aiMatrix4x4::Translation(pos, transMat);
  751. aiMatrix4x4::Scaling(scale, scaleMat);
  752. rotMat = aiMatrix4x4(rot.GetMatrix());
  753. aiMatrix4x4 tform = transMat * rotMat * scaleMat;
  754. tform = GetDerivedTransform(tform, boneNode, model->rootNode_);
  755. tform.Decompose(scale, rot, pos);
  756. }
  757. if (track.channelMask_ & CHANNEL_POSITION)
  758. kf.position_ = ToVector3(pos);
  759. if (track.channelMask_ & CHANNEL_ROTATION)
  760. kf.rotation_ = ToQuaternion(rot);
  761. if (track.channelMask_ & CHANNEL_SCALE)
  762. kf.scale_ = ToVector3(scale);
  763. track.keyFrames_.Push(kf);
  764. }
  765. tracks.Push(track);
  766. }
  767. outAnim->SetTracks(tracks);
  768. File outFile(context_);
  769. if (!outFile.Open(animOutName, FILE_WRITE))
  770. ErrorExit("Could not open output file " + animOutName);
  771. outAnim->Save(outFile);
  772. }
  773. }
  774. // Materials
  775. void OpenAssetImporter::ExportMaterials(HashSet<String>& usedTextures)
  776. {
  777. if (useSubdirs_)
  778. {
  779. context_->GetSubsystem<FileSystem>()->CreateDir(sourceAssetPath_ + "Materials");
  780. }
  781. for (unsigned i = 0; i < scene_->mNumMaterials; ++i)
  782. BuildAndSaveMaterial(scene_->mMaterials[i], usedTextures);
  783. }
  784. void OpenAssetImporter::BuildAndSaveMaterial(aiMaterial* material, HashSet<String>& usedTextures)
  785. {
  786. aiString matNameStr;
  787. material->Get(AI_MATKEY_NAME, matNameStr);
  788. String matName = SanitateAssetName(FromAIString(matNameStr));
  789. if (matName.Trimmed().Empty())
  790. matName = GenerateMaterialName(material);
  791. // Do not actually create a material instance, but instead craft an xml file manually
  792. XMLFile outMaterial(context_);
  793. XMLElement materialElem = outMaterial.CreateRoot("material");
  794. String diffuseTexName;
  795. String normalTexName;
  796. String specularTexName;
  797. String lightmapTexName;
  798. String emissiveTexName;
  799. Color diffuseColor = Color::WHITE;
  800. Color specularColor;
  801. Color emissiveColor = Color::BLACK;
  802. bool hasAlpha = false;
  803. bool twoSided = false;
  804. float specPower = 1.0f;
  805. aiString stringVal;
  806. float floatVal;
  807. int intVal;
  808. aiColor3D colorVal;
  809. if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0), stringVal) == AI_SUCCESS)
  810. diffuseTexName = GetFileNameAndExtension(FromAIString(stringVal));
  811. if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0), stringVal) == AI_SUCCESS)
  812. normalTexName = GetFileNameAndExtension(FromAIString(stringVal));
  813. if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_SPECULAR, 0), stringVal) == AI_SUCCESS)
  814. specularTexName = GetFileNameAndExtension(FromAIString(stringVal));
  815. if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0), stringVal) == AI_SUCCESS)
  816. specularTexName = GetFileNameAndExtension(FromAIString(stringVal));
  817. if (material->Get(AI_MATKEY_TEXTURE(aiTextureType_EMISSIVE, 0), stringVal) == AI_SUCCESS)
  818. emissiveTexName = GetFileNameAndExtension(FromAIString(stringVal));
  819. if (!noMaterialDiffuseColor_)
  820. {
  821. if (material->Get(AI_MATKEY_COLOR_DIFFUSE, colorVal) == AI_SUCCESS)
  822. diffuseColor = Color(colorVal.r, colorVal.g, colorVal.b);
  823. }
  824. if (material->Get(AI_MATKEY_COLOR_SPECULAR, colorVal) == AI_SUCCESS)
  825. specularColor = Color(colorVal.r, colorVal.g, colorVal.b);
  826. if (!emissiveAO_)
  827. {
  828. if (material->Get(AI_MATKEY_COLOR_EMISSIVE, colorVal) == AI_SUCCESS)
  829. emissiveColor = Color(colorVal.r, colorVal.g, colorVal.b);
  830. }
  831. if (material->Get(AI_MATKEY_OPACITY, floatVal) == AI_SUCCESS)
  832. {
  833. if (floatVal < 1.0f)
  834. hasAlpha = true;
  835. diffuseColor.a_ = floatVal;
  836. }
  837. if (material->Get(AI_MATKEY_SHININESS, floatVal) == AI_SUCCESS)
  838. specPower = floatVal;
  839. if (material->Get(AI_MATKEY_TWOSIDED, intVal) == AI_SUCCESS)
  840. twoSided = (intVal != 0);
  841. String techniqueName = "Techniques/NoTexture";
  842. if (!diffuseTexName.Empty())
  843. {
  844. techniqueName = "Techniques/Diff";
  845. if (!normalTexName.Empty())
  846. techniqueName += "Normal";
  847. if (!specularTexName.Empty())
  848. techniqueName += "Spec";
  849. // For now lightmap does not coexist with normal & specular
  850. if (normalTexName.Empty() && specularTexName.Empty() && !lightmapTexName.Empty())
  851. techniqueName += "LightMap";
  852. if (lightmapTexName.Empty() && !emissiveTexName.Empty())
  853. techniqueName += emissiveAO_ ? "AO" : "Emissive";
  854. }
  855. if (hasAlpha)
  856. techniqueName += "Alpha";
  857. XMLElement techniqueElem = materialElem.CreateChild("technique");
  858. techniqueElem.SetString("name", techniqueName + ".xml");
  859. if (!diffuseTexName.Empty())
  860. {
  861. XMLElement diffuseElem = materialElem.CreateChild("texture");
  862. diffuseElem.SetString("unit", "diffuse");
  863. diffuseElem.SetString("name", GetMaterialTextureName(diffuseTexName));
  864. usedTextures.Insert(diffuseTexName);
  865. }
  866. if (!normalTexName.Empty())
  867. {
  868. XMLElement normalElem = materialElem.CreateChild("texture");
  869. normalElem.SetString("unit", "normal");
  870. normalElem.SetString("name", GetMaterialTextureName(normalTexName));
  871. usedTextures.Insert(normalTexName);
  872. }
  873. if (!specularTexName.Empty())
  874. {
  875. XMLElement specularElem = materialElem.CreateChild("texture");
  876. specularElem.SetString("unit", "specular");
  877. specularElem.SetString("name", GetMaterialTextureName(specularTexName));
  878. usedTextures.Insert(specularTexName);
  879. }
  880. if (!lightmapTexName.Empty())
  881. {
  882. XMLElement lightmapElem = materialElem.CreateChild("texture");
  883. lightmapElem.SetString("unit", "emissive");
  884. lightmapElem.SetString("name", GetMaterialTextureName(lightmapTexName));
  885. usedTextures.Insert(lightmapTexName);
  886. }
  887. if (!emissiveTexName.Empty())
  888. {
  889. XMLElement emissiveElem = materialElem.CreateChild("texture");
  890. emissiveElem.SetString("unit", "emissive");
  891. emissiveElem.SetString("name", GetMaterialTextureName(emissiveTexName));
  892. usedTextures.Insert(emissiveTexName);
  893. }
  894. XMLElement diffuseColorElem = materialElem.CreateChild("parameter");
  895. diffuseColorElem.SetString("name", "MatDiffColor");
  896. diffuseColorElem.SetColor("value", diffuseColor);
  897. XMLElement specularElem = materialElem.CreateChild("parameter");
  898. specularElem.SetString("name", "MatSpecColor");
  899. specularElem.SetVector4("value", Vector4(specularColor.r_, specularColor.g_, specularColor.b_, specPower));
  900. XMLElement emissiveColorElem = materialElem.CreateChild("parameter");
  901. emissiveColorElem.SetString("name", "MatEmissiveColor");
  902. emissiveColorElem.SetColor("value", emissiveColor);
  903. if (twoSided)
  904. {
  905. XMLElement cullElem = materialElem.CreateChild("cull");
  906. XMLElement shadowCullElem = materialElem.CreateChild("shadowcull");
  907. cullElem.SetString("value", "none");
  908. shadowCullElem.SetString("value", "none");
  909. }
  910. FileSystem* fileSystem = context_->GetSubsystem<FileSystem>();
  911. String outFileName = sourceAssetPath_ + (useSubdirs_ ? "Materials/" : "" ) + matName + ".material";
  912. if (noOverwriteMaterial_ && fileSystem->FileExists(outFileName))
  913. {
  914. PrintLine("Skipping save of existing material " + matName);
  915. return;
  916. }
  917. PrintLine("Writing material " + matName);
  918. File outFile(context_);
  919. if (!outFile.Open(outFileName, FILE_WRITE))
  920. ErrorExit("Could not open output file " + outFileName);
  921. outMaterial.Save(outFile);
  922. }
  923. void OpenAssetImporter::DumpNodes(aiNode* rootNode, unsigned level)
  924. {
  925. if (!rootNode)
  926. return;
  927. String indent(' ', level * 2);
  928. Vector3 pos, scale;
  929. Quaternion rot;
  930. aiMatrix4x4 transform = GetDerivedTransform(rootNode, rootNode_);
  931. GetPosRotScale(transform, pos, rot, scale);
  932. PrintLine(indent + "Node " + FromAIString(rootNode->mName) + " pos " + String(pos));
  933. if (rootNode->mNumMeshes == 1)
  934. PrintLine(indent + " " + String(rootNode->mNumMeshes) + " geometry");
  935. if (rootNode->mNumMeshes > 1)
  936. PrintLine(indent + " " + String(rootNode->mNumMeshes) + " geometries");
  937. for (unsigned i = 0; i < rootNode->mNumChildren; ++i)
  938. DumpNodes(rootNode->mChildren[i], level + 1);
  939. }
  940. }