Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions editor/includes/converters/assimpconverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ class AssimpConverter : public AssetConverter {

Actor *createActor(const AssetConverterSettings *settings, const TString &guid) const override;

Actor *importObject(const aiScene *scene, const aiNode *element, Actor *parent, AssimpImportSettings *fbxSettings);
Actor *importObject(const aiScene *scene, const aiNode *element, AssimpImportSettings *fbxSettings, Actor *parent);

static Mesh *importMesh(const aiScene *scene, const aiNode *element, Actor *actor, AssimpImportSettings *fbxSettings);
static Mesh *importMesh(const aiScene *scene, const aiNode *element, AssimpImportSettings *fbxSettings, Actor *actor, int32_t lod);

static void importAnimation(const aiScene *scene, AssimpImportSettings *fbxSettings);

Expand Down
4 changes: 2 additions & 2 deletions editor/includes/editor/assetconverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ class EDITOR_EXPORT AssetConverterSettings : public Object {
std::list<std::pair<TString, TString>> &changedUuids();
void clearChangedUuids();

TString fixUuid(const TString &uuid, const TString &type, int lod, bool solve = true);

protected:
virtual TString propertyAllias(const TString &name) const;

static TString defaultIconPath(const TString &type);

void setVersion(uint32_t version);

TString fixUuid(const TString &uuid, const TString &type, int lod, bool solve = true);

static QImage renderDocumentIcon(const TString &path, const TString &color = TString("#0277bd"));

protected:
Expand Down
2 changes: 0 additions & 2 deletions editor/includes/editor/assetmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ protected slots:

BaseAssetProvider *m_assetProvider;

ResourceSystem::Dictionary &m_indices;

QTimer *m_timer;

bool m_force;
Expand Down
70 changes: 43 additions & 27 deletions editor/src/converters/assimpconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <log.h>
#include <url.h>
#include <file.h>
#include <uuid.h>

#include "components/actor.h"
#include "components/armature.h"
Expand Down Expand Up @@ -258,7 +259,7 @@ AssetConverter::ReturnCode AssimpConverter::convertFile(AssetConverterSettings *
}

/// \todo We need to reuse actors if possible
Actor *root = importObject(scene, scene->mRootNode, nullptr, fbxSettings);
Actor *root = importObject(scene, scene->mRootNode, fbxSettings, nullptr);

prefab->setActor(root);

Expand Down Expand Up @@ -291,15 +292,33 @@ AssetConverter::ReturnCode AssimpConverter::convertFile(AssetConverterSettings *
}

Actor *importObjectHelper(const aiScene *scene, const aiNode *element, const aiMatrix4x4 &p, Actor *parent, AssimpImportSettings *fbxSettings) {
std::string name = element->mName.C_Str();
TString name = element->mName.C_Str();

if(name.find("$") != std::string::npos) {
if(name.contains("$")) {
for(uint32_t c = 0; c < element->mNumChildren; c++) {
importObjectHelper(scene, element->mChildren[c], p * element->mTransformation, parent, fbxSettings);
}
} else {
Actor *actor = Engine::objectCreate<Actor>(name, parent);
Transform *transform = static_cast<Transform *>(actor->addComponent("Transform"));
uint32_t lod = 0;
StringList pair = name.split("_lod");
if(pair.size() == 2) {
name = pair.front();
lod = pair.back().toInt();
}

Actor *actor = nullptr;
Transform *transform = nullptr;

auto it = fbxSettings->m_actors.find(name);
if(it != fbxSettings->m_actors.end()) {
actor = it->second;
transform = actor->transform();
} else {
actor = Engine::objectCreate<Actor>(name, parent);
transform = static_cast<Transform *>(actor->addComponent("Transform"));

fbxSettings->m_actors[name] = actor;
}

if(fbxSettings->m_rootActor == nullptr) {
fbxSettings->m_rootActor = actor;
Expand All @@ -314,8 +333,6 @@ Actor *importObjectHelper(const aiScene *scene, const aiNode *element, const aiM
}
}

fbxSettings->m_actors[actor->name()] = actor;

aiMatrix4x4 t = p * element->mTransformation;

aiVector3D scale, euler, position;
Expand All @@ -330,19 +347,16 @@ Actor *importObjectHelper(const aiScene *scene, const aiNode *element, const aiM
transform->setRotation(Vector3(euler.x, euler.y, euler.z) * RAD2DEG);
transform->setScale(Vector3(scale.x, scale.y, scale.z));

Mesh *result = AssimpConverter::importMesh(scene, element, actor, fbxSettings);
if(result) {
Mesh *result = AssimpConverter::importMesh(scene, element, fbxSettings, actor, lod);
if(result && lod == 0) {
MeshRender *render = nullptr;
if(!result->weights().empty()) {
SkinnedMeshRender *render = static_cast<SkinnedMeshRender *>(actor->addComponent("SkinnedMeshRender"));

render->setMesh(result);
fbxSettings->m_renders.push_back(render);
render = static_cast<SkinnedMeshRender *>(actor->addComponent("SkinnedMeshRender"));
} else {
MeshRender *render = static_cast<MeshRender *>(actor->addComponent("MeshRender"));

render->setMesh(result);
fbxSettings->m_renders.push_back(render);
render = static_cast<MeshRender *>(actor->addComponent("MeshRender"));
}
render->setMesh(result);
fbxSettings->m_renders.push_back(render);
}

for(uint32_t c = 0; c < element->mNumChildren; c++) {
Expand All @@ -355,27 +369,21 @@ Actor *importObjectHelper(const aiScene *scene, const aiNode *element, const aiM
return nullptr;
}

Actor *AssimpConverter::importObject(const aiScene *scene, const aiNode *element, Actor *parent, AssimpImportSettings *fbxSettings) {
Actor *AssimpConverter::importObject(const aiScene *scene, const aiNode *element, AssimpImportSettings *fbxSettings, Actor *parent) {
aiMatrix4x4 m;
return importObjectHelper(scene, element, m, parent, fbxSettings);
}

Mesh *AssimpConverter::importMesh(const aiScene *scene, const aiNode *element, Actor *actor, AssimpImportSettings *fbxSettings) {
Mesh *AssimpConverter::importMesh(const aiScene *scene, const aiNode *element, AssimpImportSettings *fbxSettings, Actor *actor, int32_t lod) {
if(element->mNumMeshes) {
uint32_t hash = 16;
for(uint32_t index = 0; index < element->mNumMeshes; index++) {
Mathf::hashCombine(hash, element->mMeshes[index]);
}

Mesh *mesh = nullptr;

auto it = fbxSettings->m_meshes.find(hash);
if(it != fbxSettings->m_meshes.end()) {
mesh = it->second;
}

if(mesh) {
return mesh;
return it->second;
}

size_t total_v = 0;
Expand All @@ -385,7 +393,11 @@ Mesh *AssimpConverter::importMesh(const aiScene *scene, const aiNode *element, A
size_t count_i = 0;

ResourceSystem::ResourceInfo info = fbxSettings->subItem(actor->name(), MetaType::name<Mesh>());
if(lod > 0) {
info.uuid = fbxSettings->fixUuid(info.uuid, info.type, lod, false);
}

Mesh *mesh = nullptr;
for(uint32_t index = 0; index < element->mNumMeshes; index++) {
const aiMesh *item = scene->mMeshes[element->mMeshes[index]];

Expand Down Expand Up @@ -621,7 +633,11 @@ Mesh *AssimpConverter::importMesh(const aiScene *scene, const aiNode *element, A
AssetConverter::ReturnCode result = fbxSettings->saveBinary(Engine::toVariant(mesh), dst.absoluteDir() + "/" + info.uuid);
if(result == AssetConverter::Success) {
info.id = mesh->uuid();
fbxSettings->setSubItem(actor->name(), info);
TString name(actor->name());
if(lod > 0) {
name += TString("_lod%1").arg(TString::number(lod));
}
fbxSettings->setSubItem(name, info, lod);
}

fbxSettings->m_meshes[hash] = mesh;
Expand Down
12 changes: 8 additions & 4 deletions editor/src/editor/assetconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,11 @@ TString AssetConverterSettings::fixUuid(const TString &uuid, const TString &type
if(!result.isNull()) {
int index = Engine::resourceSystem()->indexOf(type) + 1;
ByteArray array = result.toByteArray();
array[0] = index;
array[1] = (array[1] & 0x0F) | ((lod & 0x0F) << 4);
result.fromByteArray(array);
if(array[0] != index) {
array[0] = index;
array[1] = (array[1] & 0x0F) | ((lod & 0x0F) << 4);
result.fromByteArray(array);
}
}

TString str(result.toString());
Expand Down Expand Up @@ -373,6 +375,8 @@ ResourceSystem::ResourceInfo AssetConverterSettings::subItem(const TString &key,
ResourceSystem::ResourceInfo info;
info.type = type;
info.uuid = fixUuid(Uuid::createUuid().toString(), info.type, 0, false);
info.md5 = m_info.md5;
info.bundle = m_info.bundle;
return info;
}
return ResourceSystem::ResourceInfo();
Expand All @@ -383,7 +387,7 @@ ResourceSystem::ResourceInfo AssetConverterSettings::subItem(const TString &key,
void AssetConverterSettings::setSubItem(const TString &name, const ResourceSystem::ResourceInfo &info, int lod) {
if(!name.isEmpty() && !info.uuid.isEmpty()) {
ResourceSystem::ResourceInfo fixedInfo(info);
fixedInfo.uuid = fixUuid(fixedInfo.uuid, fixedInfo.type, 0);
fixedInfo.uuid = fixUuid(fixedInfo.uuid, fixedInfo.type, lod);

m_subItems[name] = {fixedInfo, QImage(), false};
}
Expand Down
49 changes: 26 additions & 23 deletions editor/src/editor/assetmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ namespace {

AssetManager::AssetManager() :
m_assetProvider(new BaseAssetProvider),
m_indices(Engine::resourceSystem()->indices()),
m_timer(new QTimer(this)),
m_force(false) {

Expand Down Expand Up @@ -410,13 +409,14 @@ TString AssetManager::uuidToPath(const TString &uuid) const {
}

TString AssetManager::pathToUuid(const TString &path) const {
auto it = m_indices.find(path);
if(it != m_indices.end()) {
return it->second.uuid;
ResourceSystem::Aliases &aliases = Engine::resourceSystem()->aliases();
auto it = aliases.find(path);
if(it != aliases.end()) {
return it->second;
}
it = m_indices.find(pathToLocal(path));
if(it != m_indices.end()) {
return it->second.uuid;
it = aliases.find(pathToLocal(path));
if(it != aliases.end()) {
return it->second;
}

return TString();
Expand Down Expand Up @@ -452,9 +452,9 @@ void AssetManager::dumpBundle() {
VariantMap root;

VariantMap paths;
for(auto &it : m_indices) {
for(auto &it : Engine::resourceSystem()->indices()) {
VariantList item;
item.push_back(it.first);
item.push_back(pathToLocal(uuidToPath(it.first)));
item.push_back(it.second.type);
item.push_back(it.second.md5);
item.push_back(static_cast<int>(it.second.id));
Expand Down Expand Up @@ -522,10 +522,11 @@ void AssetManager::onPerform() {
}
}

auto tmp = m_indices;
ResourceSystem::Dictionary &indices = Engine::resourceSystem()->indices();
auto tmp = indices;
for(auto &index : tmp) {
if(index.second.uuid.isEmpty() || (!File::exists(ProjectSettings::instance()->importPath() + "/" + index.second.uuid))) {
m_indices.erase(m_indices.find(index.first));
indices.erase(index.second.uuid);
}
}

Expand Down Expand Up @@ -615,26 +616,28 @@ void AssetManager::registerAsset(const TString &source, const ResourceSystem::Re
if(File::exists(ProjectSettings::instance()->importPath() + "/" + info.uuid)) {
TString path = pathToLocal(source);

m_indices[path] = info;
ResourceSystem::Dictionary &indices = Engine::resourceSystem()->indices();
indices[info.uuid] = info;
ResourceSystem::Aliases &aliases = Engine::resourceSystem()->aliases();
aliases[path] = info.uuid;

m_paths[info.uuid] = source;

m_labels.insert(info.type);
}
}

TString AssetManager::unregisterAsset(const TString &source) {
TString path = pathToLocal(source);
auto it = m_indices.find(path);
if(it != m_indices.end()) {
auto pathIt = m_paths.find(it->second.uuid);
if(pathIt != m_paths.end() && !pathIt->second.isEmpty()) {
TString uuid(it->second.uuid);
TString uuid(pathToUuid(source));
if(!uuid.isEmpty()) {
ResourceSystem::Dictionary &indices = Engine::resourceSystem()->indices();
indices.erase(uuid);
ResourceSystem::Aliases &aliases = Engine::resourceSystem()->aliases();
aliases.erase(pathToLocal(source));
m_paths.erase(uuid);

m_indices.erase(it);
m_paths.erase(pathIt);

return uuid;
}
return uuid;
}

return TString();
}
2 changes: 2 additions & 0 deletions editor/src/viewport/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ void Viewport::onDraw() {
}

Engine::instance().processEvents();
Engine::resourceSystem()->setActiveWorld(m_world);
Engine::resourceSystem()->processEvents();
Engine::renderSystem()->setActiveWorld(m_world);
Engine::renderSystem()->processEvents();
}
Expand Down
11 changes: 9 additions & 2 deletions engine/includes/components/meshrender.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <mesh.h>

#include <array>

class Material;
class MaterialInstance;

Expand Down Expand Up @@ -39,12 +41,17 @@ class ENGINE_EXPORT MeshRender : public Renderable {

void composeComponent() override;

Mesh *ensureMeshInstance();
bool ensureMeshInstance();

void updateBlendShapes();

protected:
std::vector<float> m_blendShapeWeights;
std::array<std::pair<Mesh *, bool>, 3> m_lods;

TString m_meshRef;

Mesh *m_mesh;
Mesh *m_baseMesh;
Mesh *m_meshInstance;

};
Expand Down
5 changes: 2 additions & 3 deletions engine/includes/components/renderable.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@

#include <amath.h>

class CommandBuffer;
class PipelineContext;

class Mesh;
class Material;
class MaterialInstance;
Expand Down Expand Up @@ -58,6 +55,8 @@ class ENGINE_EXPORT Renderable : public NativeBehaviour {

virtual AABBox localBound();

virtual void setLod(uint32_t lod);

virtual void setMaterialsList(const std::list<Material *> &materials);

void applyBlendShapeWeights(Mesh &mesh, Mesh &instance, const std::vector<float> &weights);
Expand Down
Loading
Loading