| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504 |
- //
- // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Atomic/Core/ProcessUtils.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Scene/Node.h>
- #include <Atomic/Graphics/AnimatedModel.h>
- #include <Atomic/Graphics/Animation.h>
- #include <Atomic/Graphics/AnimationController.h>
- #include <Atomic/Graphics/StaticModel.h>
- #include <Atomic/Graphics/Model.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include <Atomic/Resource/XMLFile.h>
- #include "../Import/OpenAssetImporter.h"
- #include "Asset.h"
- #include "AssetDatabase.h"
- #include "ModelImporter.h"
- namespace ToolCore
- {
- /// Node + Model (static or animated)
- ModelImporter::ModelImporter(Context* context, Asset *asset) : AssetImporter(context, asset)
- {
- requiresCacheFile_ = true;
- SetDefaults();
- }
- ModelImporter::~ModelImporter()
- {
- }
- void ModelImporter::SetDefaults()
- {
- AssetImporter::SetDefaults();
- SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
- scale_ = 1.0;
- importAnimations_ = false;
- importMaterials_ = importer->GetImportMaterialsDefault();
- includeNonSkinningBones_ = importer->GetIncludeNonSkinningBones();
- animationInfo_.Clear();
- genLightmapUV_ = false;
- }
- bool ModelImporter::ImportModel()
- {
- ATOMIC_LOGDEBUGF("Importing Model: %s", asset_->GetPath().CString());
- SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
- importer->SetVerboseLog(true);
- importer->SetScale(scale_);
- importer->SetExportAnimations(false);
- importer->SetImportNode(importNode_);
- importer->SetImportMaterials(importMaterials_);
- importer->SetIncludeNonSkinningBones(includeNonSkinningBones_);
- importer->SetGenerateLightmapUV(genLightmapUV_);
- if (importer->Load(asset_->GetPath()))
- {
- importer->ExportModel(asset_->GetCachePath());
- return true;
- }
- else
- {
- asset_->PostImportError(importer->GetErrorMessage());
- }
- return false;
- }
- /*void ModelImporter::SetImportMaterials(bool importMat)
- {
- LOGDEBUGF("Importing Materials of: %s", asset_->GetPath().CString());
- SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
- importer->SetImportMaterials(importMat);
- }*/
- bool ModelImporter::ImportAnimation(const String& filename, const String& name, float startTime, float endTime)
- {
- SharedPtr<OpenAssetImporter> importer(new OpenAssetImporter(context_));
- //importer->SetVerboseLog(true);
- importer->SetScale(scale_);
- importer->SetExportAnimations(true);
- importer->SetStartTime(startTime);
- importer->SetEndTime(endTime);
- if (importer->Load(filename))
- {
- importer->ExportModel(asset_->GetCachePath(), name, true);
- const Vector<OpenAssetImporter::AnimationInfo>& infos = importer->GetAnimationInfos();
- for (unsigned i = 0; i < infos.Size(); i++)
- {
- const OpenAssetImporter::AnimationInfo& info = infos.At(i);
- String pathName, fileName, extension;
- SplitPath(info.cacheFilename_, pathName, fileName, extension);
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- AnimatedModel* animatedModel = importNode_->GetComponent<AnimatedModel>();
- AnimationController* controller = importNode_->GetComponent<AnimationController>();
- if (!controller)
- {
- importNode_->CreateComponent<AnimationController>();
- controller = importNode_->GetComponent<AnimationController>();
- }
- SharedPtr<Animation> animation = cache->GetTempResource<Animation>(fileName + extension);
- if (animation)
- controller->AddAnimationResource(animation);
- ATOMIC_LOGINFOF("Import Info: %s : %s", info.name_.CString(), fileName.CString());
- }
- return true;
- }
- return false;
- }
- bool ModelImporter::ImportAnimations()
- {
- if (!animationInfo_.Size())
- {
- if (!ImportAnimation(asset_->GetPath(), "RootAnim"))
- return false;
- }
- // embedded animations
- for (unsigned i = 0; i < animationInfo_.Size(); i++)
- {
- const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
- if (!ImportAnimation(asset_->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
- return false;
- }
- // add @ animations
- FileSystem* fs = GetSubsystem<FileSystem>();
- String pathName, fileName, ext;
- SplitPath(asset_->GetPath(), pathName, fileName, ext);
- Vector<String> results;
- fs->ScanDir(results, pathName, ext, SCAN_FILES, false);
- for (unsigned i = 0; i < results.Size(); i++)
- {
- const String& result = results[i];
- if (result.Contains("@"))
- {
- Vector<String> components = GetFileName(result).Split('@');
- if (components.Size() == 2 && components[1].Length() && components[0] == fileName)
- {
- String animationName = components[1];
- AssetDatabase* db = GetSubsystem<AssetDatabase>();
- Asset* asset = db->GetAssetByPath(pathName + result);
- assert(asset);
- assert(asset->GetImporter()->GetType() == ModelImporter::GetTypeStatic());
- ModelImporter* importer = (ModelImporter*) asset->GetImporter();
- if (!importer->animationInfo_.Size())
- {
- if (!ImportAnimation(asset->GetPath(), animationName))
- return false;
- }
- else
- {
- // embedded animations
- for (unsigned i = 0; i < importer->animationInfo_.Size(); i++)
- {
- const SharedPtr<AnimationImportInfo>& info = importer->animationInfo_[i];
- if (!ImportAnimation(asset->GetPath(), info->GetName(), info->GetStartTime(), info->GetEndTime()))
- return false;
- }
- }
- }
- }
- }
- return true;
- }
- bool ModelImporter::Import()
- {
- String ext = asset_->GetExtension();
- String modelAssetFilename = asset_->GetPath();
- importNode_ = new Node(context_);
- if (ext == ".mdl")
- {
- FileSystem* fs = GetSubsystem<FileSystem>();
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- // mdl files are native file format that doesn't need to be converted
- // doesn't allow scale, animations
- String cacheFilename = asset_->GetCachePath() + ".mdl";
- if (!fs->Copy(asset_->GetPath(), cacheFilename))
- {
- importNode_= 0;
- return false;
- }
- Model* mdl = cache->GetResource<Model>(cacheFilename);
- if (!mdl)
- {
- importNode_= 0;
- return false;
- }
- if (genLightmapUV_)
- {
- if (!OpenAssetImporter::GenerateLightmapUV(mdl))
- {
- ATOMIC_LOGERRORF("Failed to generate lightmap UV %s", asset_->GetPath().CString());
- importNode_= 0;
- return false;
- }
- File outFile(context_);
- if (!outFile.Open(cacheFilename, FILE_WRITE))
- {
- ATOMIC_LOGERRORF("Could not open output file %s", asset_->GetPath().CString());
- importNode_= 0;
- return false;
- }
- mdl->Save(outFile);
- outFile.Close();
- // Force a reload, though file watchers will catch this delayed and load again
- cache->ReloadResource(mdl);
- }
- StaticModel* staticModel = importNode_->CreateComponent<StaticModel>();
- staticModel->SetModel(mdl);
- staticModel->SetLightmap(genLightmapUV_);
- }
- else
- {
- // skip external animations, they will be brought in when importing their
- // corresponding model
- if (!modelAssetFilename.Contains("@"))
- {
- ImportModel();
- if (importAnimations_)
- {
- ImportAnimations();
- }
- SetImportMaterials(importMaterials_);
- }
- }
- File outFile(context_);
- if (!outFile.Open(asset_->GetCachePath(), FILE_WRITE))
- ErrorExit("Could not open output file " + asset_->GetCachePath());
- importNode_->SaveXML(outFile);
- importNode_ = 0;
- return true;
- }
- unsigned ModelImporter::GetAnimationCount()
- {
- return animationInfo_.Size();
- }
- void ModelImporter::SetAnimationCount(unsigned count)
- {
- if (animationInfo_.Size() >= count)
- {
- animationInfo_.Resize(count);
- }
- else
- {
- for (unsigned i = animationInfo_.Size(); i < count; i++)
- {
- SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
- animationInfo_.Push(info);
- }
- }
- }
- void ModelImporter::GetAnimations(PODVector<Animation*>& animations)
- {
- animations.Clear();
- SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
- SharedPtr<XMLFile> xml(new XMLFile(context_));
- if (!xml->Load(*file))
- return;
- SharedPtr<Node> node(new Node(context_));
- node->LoadXML(xml->GetRoot());
- AnimationController* controller = node->GetComponent<AnimationController>();
- if (!controller)
- return;
- const Vector<SharedPtr<Animation>>& animresources = controller->GetAnimationResources();
- for (unsigned i = 0; i < animresources.Size(); i++)
- {
- if (animresources[i].NotNull())
- {
- animations.Push(animresources[i]);
- }
- }
- }
- void ModelImporter::GetAssetCacheMap(HashMap<String, String>& assetMap)
- {
- if (asset_.Null())
- return;
- String assetPath = asset_->GetRelativePath().ToLower();
- String cachePath = asset_->GetGUID().ToLower();
- // Default is load node xml
- assetMap["Node;" + assetPath] = cachePath;
- assetMap["Model;" + assetPath] = cachePath + ".mdl";
- PODVector<Animation*> animations;
- GetAnimations(animations);
- for (unsigned i = 0; i < animations.Size(); i++)
- {
- Animation* anim = animations[i];
- assetMap["Animation;" + anim->GetAnimationName().ToLower() + "@" + assetPath] = cachePath + "_" + anim->GetAnimationName() + ".ani";
- }
- }
- bool ModelImporter::LoadSettingsInternal(JSONValue& jsonRoot)
- {
- if (!AssetImporter::LoadSettingsInternal(jsonRoot))
- return false;
- JSONValue import = jsonRoot.Get("ModelImporter");
- SetDefaults();
- if (import.Get("scale").IsNumber())
- scale_ = import.Get("scale").GetDouble();
- if (import.Get("genLightmapUV").IsBool())
- genLightmapUV_ = import.Get("genLightmapUV").GetBool();
- if (import.Get("importAnimations").IsBool())
- importAnimations_ = import.Get("importAnimations").GetBool();
- if (import.Get("importMaterials").IsBool())
- {
- importMaterials_ = import.Get("importMaterials").GetBool();
- }
- else
- {
- }
- if (import.Get("animInfo").IsArray())
- {
- JSONArray animInfo = import.Get("animInfo").GetArray();
- for (unsigned i = 0; i < animInfo.Size(); i++)
- {
- JSONValue anim = animInfo[i];
- SharedPtr<AnimationImportInfo> info(new AnimationImportInfo(context_));
- info->name_ = anim.Get("name").GetString();
- info->SetStartTime(anim.Get("startTime").GetFloat());
- info->SetEndTime(anim.Get("endTime").GetFloat());
- animationInfo_.Push(info);
- }
- }
- return true;
- }
- bool ModelImporter::SaveSettingsInternal(JSONValue& jsonRoot)
- {
- if (!AssetImporter::SaveSettingsInternal(jsonRoot))
- return false;
- JSONValue save;
- save.Set("scale", scale_);
- save.Set("importAnimations", importAnimations_);
- save.Set("importMaterials", importMaterials_);
- save.Set("genLightmapUV", genLightmapUV_);
- JSONArray animInfo;
- for (unsigned i = 0; i < animationInfo_.Size(); i++)
- {
- const SharedPtr<AnimationImportInfo>& info = animationInfo_[i];
- JSONValue jinfo;
- jinfo.Set("name", info->GetName());
- jinfo.Set("startTime", info->GetStartTime());
- jinfo.Set("endTime", info->GetEndTime());
- animInfo.Push(jinfo);
- }
- save.Set("animInfo", animInfo);
- jsonRoot.Set("ModelImporter", save);
- return true;
- }
- Resource* ModelImporter::GetResource(const String& typeName)
- {
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- Model* model = cache->GetResource<Model>(asset_->GetCachePath() + ".mdl");
- return model;
- }
- Node* ModelImporter::InstantiateNode(Node* parent, const String& name)
- {
- SharedPtr<File> file(new File(context_, asset_->GetCachePath()));
- SharedPtr<XMLFile> xml(new XMLFile(context_));
- if (!xml->Load(*file))
- return 0;
- Node* node = parent->CreateChild(name);
- node->LoadXML(xml->GetRoot());
- node->SetName(asset_->GetName());
- return node;
- }
- }
|