LumixEngine/src/renderer/editor/fbx_importer.h

191 lines
4.8 KiB
C
Raw Normal View History

2018-08-05 18:00:30 +02:00
#pragma once
#include "engine/array.h"
#include "engine/geometry.h"
2019-06-13 17:26:52 +02:00
#include "engine/math.h"
2019-06-11 22:39:39 +02:00
#include "engine/os.h"
#include "engine/stream.h"
2018-08-05 18:00:30 +02:00
#include "engine/string.h"
#include "ofbx.h"
namespace Lumix
{
struct FBXImporter
{
struct ImportConfig
{
2018-10-14 17:46:42 +02:00
enum class Origin : int
{
SOURCE,
CENTER,
BOTTOM
};
enum class Physics {
NONE,
CONVEX,
TRIMESH
};
2018-08-05 18:00:30 +02:00
float mesh_scale;
2018-10-14 17:46:42 +02:00
Origin origin = Origin::SOURCE;
2019-10-05 14:26:26 +02:00
bool create_impostor = false;
Physics physics = Physics::NONE;
2019-10-05 14:26:26 +02:00
float lods_distances[4] = {-10, -100, -1000, -10000};
2019-11-22 21:07:27 +01:00
float position_error = 0.02f;
float rotation_error = 0.001f;
2018-08-05 18:00:30 +02:00
};
enum class Orientation
{
Y_UP,
Z_UP,
Z_MINUS_UP,
X_MINUS_UP,
X_UP
};
struct Key
2018-08-05 18:00:30 +02:00
{
Vec3 pos;
Quat rot;
2019-09-11 18:23:26 +02:00
i64 time;
u8 flags = 0;
2018-08-05 18:00:30 +02:00
};
struct Skin
{
float weights[4];
i16 joints[4];
int count = 0;
};
struct ImportAnimation
{
const ofbx::AnimationStack* fbx = nullptr;
const ofbx::IScene* scene = nullptr;
StaticString<MAX_PATH_LENGTH> name;
2018-08-05 18:00:30 +02:00
bool import = true;
int root_motion_bone_idx = -1;
};
struct ImportTexture
{
enum Type
{
DIFFUSE,
NORMAL,
2019-06-26 21:21:48 +02:00
SPECULAR,
2018-08-05 18:00:30 +02:00
COUNT
};
const ofbx::Texture* fbx = nullptr;
bool import = true;
bool to_dds = true;
bool is_valid = false;
StaticString<MAX_PATH_LENGTH> path;
StaticString<MAX_PATH_LENGTH> src;
};
struct ImportMaterial
{
const ofbx::Material* fbx = nullptr;
bool import = true;
bool alpha_cutout = false;
ImportTexture textures[ImportTexture::COUNT];
char shader[20];
};
struct ImportMesh
{
ImportMesh(IAllocator& allocator)
: vertex_data(allocator)
, indices(allocator)
{
}
const ofbx::Mesh* fbx = nullptr;
const ofbx::Material* fbx_mat = nullptr;
2019-07-21 22:20:59 +02:00
bool is_skinned = false;
int bone_idx = -1;
2018-08-05 18:00:30 +02:00
bool import = true;
2019-09-10 18:12:05 +02:00
u32 lod = 0;
2019-06-03 00:36:17 +02:00
int submesh = -1;
2019-06-11 22:39:39 +02:00
OutputMemoryStream vertex_data;
2018-08-05 18:00:30 +02:00
Array<int> indices;
AABB aabb;
float radius_squared;
2018-10-15 00:39:57 +02:00
Matrix transform_matrix = Matrix::IDENTITY;
2018-08-05 18:00:30 +02:00
};
2020-02-21 22:09:11 +01:00
FBXImporter(struct StudioApp& app);
2018-08-05 18:00:30 +02:00
~FBXImporter();
bool setSource(const char* filename, bool ignore_geometry);
2018-08-05 18:00:30 +02:00
void writeMaterials(const char* src, const ImportConfig& cfg);
void writeAnimations(const char* src, const ImportConfig& cfg);
void writeSubmodels(const char* src, const ImportConfig& cfg);
2018-10-15 00:39:57 +02:00
void writePrefab(const char* src, const ImportConfig& cfg);
void writeModel(const char* src, const ImportConfig& cfg);
void writePhysics(const char* src, const ImportConfig& cfg);
2020-02-21 22:09:11 +01:00
bool createImpostorTextures(struct Model* model, Ref<Array<u32>> gb0_rgba, Ref<Array<u32>> gb1_rgba, Ref<IVec2> size);
2018-08-05 18:00:30 +02:00
const Array<ImportMesh>& getMeshes() const { return m_meshes; }
const Array<ImportAnimation>& getAnimations() const { return m_animations; }
2019-06-03 00:36:17 +02:00
static void getImportMeshName(const ImportMesh& mesh, char (&name)[256]);
2018-11-08 22:11:02 +01:00
ofbx::IScene* getOFBXScene() { return scene; }
2018-10-14 17:46:42 +02:00
2018-08-05 18:00:30 +02:00
private:
2019-07-21 22:20:59 +02:00
const ImportMesh* getAnyMeshFromBone(const ofbx::Object* node, int bone_idx) const;
2019-08-29 19:12:13 +02:00
void gatherMaterials(const char* src_dir);
2018-08-05 18:00:30 +02:00
void sortBones();
void gatherBones(const ofbx::IScene& scene);
void gatherAnimations(const ofbx::IScene& scene);
2019-06-11 22:39:39 +02:00
void writePackedVec3(const ofbx::Vec3& vec, const Matrix& mtx, OutputMemoryStream* blob) const;
void postprocessMeshes(const ImportConfig& cfg, const char* path);
2018-08-05 18:00:30 +02:00
void gatherMeshes(ofbx::IScene* scene);
2019-07-21 22:20:59 +02:00
void insertHierarchy(Array<const ofbx::Object*>& bones, const ofbx::Object* node);
2018-08-05 18:00:30 +02:00
template <typename T> void write(const T& obj) { out_file.write(&obj, sizeof(obj)); }
void write(const void* ptr, size_t size) { out_file.write(ptr, size); }
void writeString(const char* str);
2019-07-21 22:20:59 +02:00
int getVertexSize(const ImportMesh& mesh) const;
void fillSkinInfo(Array<Skin>& skinning, const ImportMesh& mesh) const;
2018-08-05 18:00:30 +02:00
Vec3 fixOrientation(const Vec3& v) const;
Quat fixOrientation(const Quat& v) const;
2019-10-05 14:26:26 +02:00
void writeImpostorVertices(const AABB& aabb);
void writeGeometry(const ImportConfig& cfg);
2018-10-14 17:46:42 +02:00
void writeGeometry(int mesh_idx);
2019-10-05 14:26:26 +02:00
void writeImpostorMesh(const char* dir, const char* model_name);
void writeMeshes(const char* src, int mesh_idx, const ImportConfig& cfg);
2018-08-05 18:00:30 +02:00
void writeSkeleton(const ImportConfig& cfg);
2019-10-05 14:26:26 +02:00
void writeLODs(const ImportConfig& cfg);
2019-07-21 22:20:59 +02:00
int getAttributeCount(const ImportMesh& mesh) const;
2018-08-05 18:00:30 +02:00
bool areIndices16Bit(const ImportMesh& mesh) const;
void writeModelHeader();
void writePhysicsTriMesh(OutputMemoryStream& file);
2018-08-05 18:00:30 +02:00
IAllocator& m_allocator;
struct FileSystem& m_filesystem;
StudioApp& m_app;
struct AssetCompiler& m_compiler;
Array<ImportMaterial> m_materials;
Array<ImportMesh> m_meshes;
Array<ImportAnimation> m_animations;
Array<const ofbx::Object*> m_bones;
2018-08-05 18:00:30 +02:00
ofbx::IScene* scene;
2019-06-23 14:01:08 +02:00
OutputMemoryStream out_file;
float m_time_scale = 1.0f;
2018-08-05 18:00:30 +02:00
bool cancel_mesh_transforms = false;
bool m_import_vertex_colors = true;
float m_fbx_scale = 1.f;
Orientation m_orientation = Orientation::Y_UP;
2018-08-05 18:00:30 +02:00
};
} // namespace Lumix