75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <vector>
|
|
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/quaternion.hpp>
|
|
|
|
#include "asset_base.hpp"
|
|
|
|
struct GameObjectAsset;
|
|
|
|
struct TransformAsset : public AssetBase
|
|
{
|
|
glm::quat Rotation;
|
|
glm::vec3 Position;
|
|
glm::vec3 Scale;
|
|
|
|
glm::mat4 GlobalMatrix;
|
|
|
|
GameObjectAsset* GameObject{ nullptr };
|
|
std::vector<TransformAsset*> Children;
|
|
TransformAsset* Parent{ nullptr };
|
|
};
|
|
|
|
inline glm::vec3 ParseVector3(const ryml::ConstNodeRef& node)
|
|
{
|
|
glm::vec3 vector;
|
|
node["x"] >> vector.x;
|
|
node["y"] >> vector.y;
|
|
node["z"] >> vector.z;
|
|
return vector;
|
|
}
|
|
|
|
inline glm::quat ParseQuaternion(const ryml::ConstNodeRef& node)
|
|
{
|
|
glm::quat quaternion;
|
|
node["x"] >> quaternion.x;
|
|
node["y"] >> quaternion.y;
|
|
node["z"] >> quaternion.z;
|
|
node["w"] >> quaternion.w;
|
|
return quaternion;
|
|
}
|
|
|
|
inline TransformAsset ParseTransform(const ryml::ConstNodeRef& node)
|
|
{
|
|
TransformAsset transform;
|
|
transform.Rotation = ParseQuaternion(node["m_LocalRotation"]);
|
|
transform.Position = ParseVector3(node["m_LocalPosition"]);
|
|
transform.Scale = ParseVector3(node["m_LocalScale"]);
|
|
|
|
transform.Parent = ParseAssetRef<TransformAsset>(node["m_Father"]);
|
|
transform.GameObject = ParseAssetRef<GameObjectAsset>(node["m_GameObject"]);
|
|
|
|
auto children = node["m_Children"];
|
|
for (const auto& child : children)
|
|
{
|
|
transform.Children.push_back(ParseAssetRef<TransformAsset>(child));
|
|
}
|
|
|
|
return transform;
|
|
}
|
|
|
|
inline void LinkTransform(const std::unordered_map<int64_t, AssetBase*>& assetsMap, TransformAsset& transform)
|
|
{
|
|
LinkAssetRef<TransformAsset>(assetsMap, transform.Parent);
|
|
for (int i = static_cast<int>(transform.Children.size()) - 1; i >= 0; i--)
|
|
{
|
|
if (!LinkAssetRef<TransformAsset>(assetsMap, transform.Children[i]))
|
|
{
|
|
std::swap(transform.Children[i], transform.Children.back());
|
|
transform.Children.pop_back();
|
|
}
|
|
}
|
|
LinkAssetRef<GameObjectAsset>(assetsMap, transform.GameObject);
|
|
} |