Skip to content

Commit

Permalink
fixed fbx -> prefab
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Dec 14, 2024
1 parent c511bcd commit a0813cb
Show file tree
Hide file tree
Showing 15 changed files with 730 additions and 446 deletions.
36 changes: 22 additions & 14 deletions external/openfbx/ofbx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1182,7 +1182,10 @@ struct GeometryDataImpl : GeometryData {
Vec4Attributes getColors() const override { return patchAttributes<Vec4Attributes>(colors); }
Vec3Attributes getTangents() const override { return patchAttributes<Vec3Attributes>(tangents); }
int getPartitionCount() const override { return (int)partitions.size(); }


const int getMaterialMapSize() const override { return (int)materials.size(); }
const int* getMaterialMap() const override { return materials.empty() ? nullptr : &materials[0]; }

GeometryPartition getPartition(int index) const override {
if (index >= partitions.size()) return {nullptr, 0, 0, 0};
return {
Expand Down Expand Up @@ -3123,26 +3126,28 @@ static OptionalError<Object*> parseAnimationCurve(const Scene& scene, const Elem
return curve;
}

static OptionalError<Object*> parseGeometry(const Element& element, GeometryImpl& geom, std::vector<ParseDataJob> &jobs, Allocator& allocator) {
static OptionalError<Object*> parseGeometry(const Element& element, GeometryImpl& geom, std::vector<ParseDataJob> &jobs, bool ignore_geometry, Allocator& allocator) {
assert(element.first_property);

if (!parseGeometryMaterials(geom, element, jobs)) return Error("Invalid materials");

const Element* vertices_element = findChild(element, "Vertices");
if (!vertices_element || !vertices_element->first_property)
{
if (!vertices_element || !vertices_element->first_property) {
return &geom;
}

const Element* polys_element = findChild(element, "PolygonVertexIndex");
if (!polys_element || !polys_element->first_property) return Error("Indices missing");

if (!pushJob(jobs, *vertices_element->first_property, geom.positions.values)) return Error("Invalid vertices");
if (!pushJob(jobs, *polys_element->first_property, geom.positions.indices)) return Error("Invalid vertices");
if (!ignore_geometry) {
if (!pushJob(jobs, *vertices_element->first_property, geom.positions.values)) return Error("Invalid vertices");
if (!pushJob(jobs, *polys_element->first_property, geom.positions.indices)) return Error("Invalid vertices");

if (!parseGeometryMaterials(geom, element, jobs)) return Error("Invalid materials");
if (!parseGeometryUVs(geom, element, jobs)) return Error("Invalid vertex attributes");
if (!parseGeometryTangents(geom, element, jobs)) return Error("Invalid vertex attributes");
if (!parseGeometryColors(geom, element, jobs)) return Error("Invalid vertex attributes");
if (!parseGeometryNormals(geom, element, jobs)) return Error("Invalid vertex attributes");
if (!parseGeometryUVs(geom, element, jobs)) return Error("Invalid vertex attributes");
if (!parseGeometryTangents(geom, element, jobs)) return Error("Invalid vertex attributes");
if (!parseGeometryColors(geom, element, jobs)) return Error("Invalid vertex attributes");
if (!parseGeometryNormals(geom, element, jobs)) return Error("Invalid vertex attributes");
}

return &geom;
}
Expand Down Expand Up @@ -3406,6 +3411,7 @@ static bool parseObjects(const Element& root, Scene& scene, u16 flags, Allocator
const bool ignore_limbs = (flags & (u16)LoadFlags::IGNORE_LIMBS) != 0;
const bool ignore_meshes = (flags & (u16)LoadFlags::IGNORE_MESHES) != 0;
const bool ignore_models = (flags & (u16)LoadFlags::IGNORE_MODELS) != 0;
const bool keep_matertial_map = (flags & (u16)LoadFlags::KEEP_MATERIAL_MAP) != 0;

const Element* objs = findChild(root, "Objects");
if (!objs) return true;
Expand Down Expand Up @@ -3438,18 +3444,20 @@ static bool parseObjects(const Element& root, Scene& scene, u16 flags, Allocator

if (iter.second.object == scene.m_root) continue;

if (iter.second.element->id == "Geometry" && !ignore_geometry)
if (iter.second.element->id == "Geometry")
{
Property* last_prop = iter.second.element->first_property;
while (last_prop->next) last_prop = last_prop->next;
if (last_prop && last_prop->value == "Mesh")
{
GeometryImpl* geom = allocator.allocate<GeometryImpl>(scene, *iter.second.element);
parseGeometry(*iter.second.element, *geom, jobs, allocator);
if (!ignore_geometry || keep_matertial_map) {
parseGeometry(*iter.second.element, *geom, jobs, ignore_geometry, allocator);
}
obj = geom;
scene.m_geometries.push_back(geom);
}
else if (last_prop && last_prop->value == "Shape")
else if (last_prop && last_prop->value == "Shape" && !ignore_geometry)
{
obj = allocator.allocate<ShapeImpl>(scene, *iter.second.element);
}
Expand Down
4 changes: 3 additions & 1 deletion external/openfbx/ofbx.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ using JobProcessor = void (*)(JobFunction, void*, void*, u32, u32);
enum class LoadFlags : u16
{
NONE = 0,
UNUSED = 1 << 0, // can be reused
KEEP_MATERIAL_MAP = 1 << 0, // keep material map even if IGNORE_GEOMETRY is used
IGNORE_GEOMETRY = 1 << 1,
IGNORE_BLEND_SHAPES = 1 << 2,
IGNORE_CAMERAS = 1 << 3,
Expand Down Expand Up @@ -544,6 +544,8 @@ struct GeometryData {
virtual Vec2Attributes getUVs(int index = 0) const = 0;
virtual Vec4Attributes getColors() const = 0;
virtual Vec3Attributes getTangents() const = 0;
virtual const int getMaterialMapSize() const = 0;
virtual const int* getMaterialMap() const = 0;
virtual int getPartitionCount() const = 0;
virtual GeometryPartition getPartition(int partition_index) const = 0;
};
Expand Down
44 changes: 27 additions & 17 deletions src/core/job_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ struct WorkerTask : Thread {

Signal* m_signal_to_check = nullptr;
WaitingFiber* m_waiting_fiber_to_push = nullptr;
i32 m_deferred_push_to_worker = -1;

Fiber::Handle m_primary_fiber;
System& m_system;
Expand Down Expand Up @@ -412,6 +413,17 @@ LUMIX_FORCE_INLINE static bool popWork(Work& work, WorkerTask* worker) {
static void afterSwitch() {
WorkerTask* worker = getWorker();

if (worker->m_deferred_push_to_worker >= 0) {
if (worker->m_deferred_push_to_worker != ANY_WORKER) {
WorkerTask* dst_worker = g_system->m_workers[worker->m_deferred_push_to_worker % g_system->m_workers.size()];
dst_worker->m_work_queue.pushAndWake(worker->m_waiting_fiber_to_push->fiber, dst_worker);
}
else {
g_system->m_global_queue.pushAndWake(worker->m_waiting_fiber_to_push->fiber, nullptr);
}
worker->m_deferred_push_to_worker = -1;
}

if (!worker->m_signal_to_check) return;

Signal* signal = worker->m_signal_to_check;
Expand Down Expand Up @@ -737,31 +749,29 @@ void exit(Mutex* mutex) {

// TODO race condition in both moveJobToWorker and yield, we could be poppped while we are still inside
void moveJobToWorker(u8 worker_index) {
FiberJobPair* this_fiber = getWorker()->m_current_fiber;
WorkerTask* worker = g_system->m_workers[worker_index % g_system->m_workers.size()];
worker->m_work_queue.pushAndWake(this_fiber, worker);

WorkerTask* worker = getWorker();
if (worker->m_worker_index == worker_index) return;

FiberJobPair* this_fiber = worker->m_current_fiber;
WaitingFiber waiting_fiber;
waiting_fiber.fiber = worker->m_current_fiber;
waiting_fiber.next = nullptr;
worker->m_waiting_fiber_to_push = &waiting_fiber;
worker->m_deferred_push_to_worker = worker_index;

FiberJobPair* new_fiber = popFreeFiber();
getWorker()->m_current_fiber = new_fiber;
worker->m_current_fiber = new_fiber;
this_fiber->current_job.worker_index = worker_index;

Fiber::switchTo(&this_fiber->fiber, new_fiber->fiber);
afterSwitch();
getWorker()->m_current_fiber = this_fiber;
ASSERT(getWorker()->m_worker_index == worker_index);
worker = getWorker();
worker->m_current_fiber = this_fiber;
ASSERT(worker->m_worker_index == worker_index || worker_index == ANY_WORKER);
}

void yield() {
FiberJobPair* this_fiber = getWorker()->m_current_fiber;
WorkerTask* worker = getWorker();
g_system->m_global_queue.pushAndWake(this_fiber, nullptr);

FiberJobPair* new_fiber = popFreeFiber();
this_fiber->current_job.worker_index = ANY_WORKER;
getWorker()->m_current_fiber = new_fiber;
Fiber::switchTo(&this_fiber->fiber, new_fiber->fiber);
afterSwitch();
getWorker()->m_current_fiber = this_fiber;
moveJobToWorker(ANY_WORKER);
}

void run(void* data, void(*task)(void*), Counter* on_finished, u8 worker_index)
Expand Down
16 changes: 16 additions & 0 deletions src/core/math.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,22 @@ void Matrix::setPerspective(float fov, float ratio, float near_plane, float far_
}
}

// assumes ortho matrix
void Matrix::decompose(Vec3& position, Quat& rotation, Vec3& scale) const {
position = getTranslation();
Vec3 x = getXVector();
Vec3 y = getYVector();
Vec3 z = getZVector();
scale.x = length(x);
scale.y = length(y);
scale.z = length(z);
Matrix mtx = Matrix::IDENTITY;
mtx.setXVector(x / scale.x);
mtx.setYVector(y / scale.y);
mtx.setZVector(z / scale.z);
rotation = mtx.getRotation();
}

void Matrix::decompose(Vec3& position, Quat& rotation, float& scale) const {
position = getTranslation();
scale = length(getXVector());
Expand Down
1 change: 1 addition & 0 deletions src/core/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ struct alignas(16) LUMIX_CORE_API Matrix {
Matrix(const Vec3& pos, const Quat& rot);

void decompose(Vec3& position, Quat& rotation, float& scale) const;
void decompose(Vec3& position, Quat& rotation, Vec3& scale) const;

float operator[](int index) const { return (&columns[0].x)[index]; }
float& operator[](int index) { return (&columns[0].x)[index]; }
Expand Down
5 changes: 5 additions & 0 deletions src/core/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,4 +809,9 @@ bool startsWithInsensitive(StringView str, StringView prefix) {
return equalIStrings(str, prefix);
}

bool StringView::operator==(const StringView& rhs) const {
return equalStrings(*this, rhs);
}


} // namespace Lumix
1 change: 1 addition & 0 deletions src/core/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct LUMIX_CORE_API StringView {
void removeSuffix(u32 count) { ASSERT(count <= size()); end -= count; }
void removePrefix(u32 count) { ASSERT(count <= size()); begin += count; }
bool empty() const { return begin == end || !begin[0]; }
bool operator==(const StringView& rhs) const;

const char* begin = nullptr;
const char* end = nullptr;
Expand Down
4 changes: 4 additions & 0 deletions src/core/win/simple_win.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,10 @@ __inline DWORD GetCurrentDirectory(DWORD nBufferLength, LPSTR lpBuffer)
{
return GetCurrentDirectoryA(nBufferLength, lpBuffer);
}

WINBASEAPI DWORD WINAPI GetFinalPathNameByHandleA(HANDLE hFile, LPSTR lpszFilePath, DWORD cchFilePath, DWORD dwFlags);
WINBASEAPI DWORD WINAPI GetFullPathNameA(LPCSTR lpFileName, DWORD nBufferLength, LPSTR lpBuffer, LPSTR* lpFilePart);

WINBASEAPI HANDLE WINAPI FindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData);
WINUSERAPI BOOL WINAPI
PeekMessageA(LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg);
Expand Down
1 change: 1 addition & 0 deletions src/editor/file_system_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct LUMIX_EDITOR_API FileSystemWatcher
virtual ~FileSystemWatcher() {}

static UniquePtr<FileSystemWatcher> create(const char* path, struct IAllocator& allocator);
// TODO on windows, this always returns lower case path
virtual Delegate<void (const char*)>& getCallback() = 0;
};

Expand Down
1 change: 1 addition & 0 deletions src/engine/file_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ struct FileSystemImpl : FileSystem {
}

bool getContentSync(const Path& path, OutputMemoryStream& content) override {
PROFILE_FUNCTION();
os::InputFile file;
const Path full_path(m_base_path, path);

Expand Down
Loading

0 comments on commit a0813cb

Please sign in to comment.