chore: use generic xmodel loader and dumper code for t5

This commit is contained in:
Jan 2024-09-13 21:51:12 +02:00
parent 28ecee3a1d
commit a2735b4f23
No known key found for this signature in database
GPG Key ID: 44B581F78FF5C57C
13 changed files with 243 additions and 541 deletions

View File

@ -208,6 +208,8 @@ namespace T5
typedef char cbrushedge_t; typedef char cbrushedge_t;
typedef tdef_align(128) unsigned int raw_uint128; typedef tdef_align(128) unsigned int raw_uint128;
typedef uint16_t ScriptString;
struct PhysPreset struct PhysPreset
{ {
const char* name; const char* name;
@ -459,8 +461,8 @@ namespace T5
struct DObjAnimMat struct DObjAnimMat
{ {
float quat[4]; vec4_t quat;
float trans[3]; vec3_t trans;
float transWeight; float transWeight;
}; };
@ -490,7 +492,7 @@ namespace T5
struct type_align(16) GfxPackedVertex struct type_align(16) GfxPackedVertex
{ {
float xyz[3]; vec3_t xyz;
float binormalSign; float binormalSign;
GfxColor color; GfxColor color;
PackedTexCoords texCoord; PackedTexCoords texCoord;
@ -535,7 +537,12 @@ namespace T5
XSurfaceCollisionTree* collisionTree; XSurfaceCollisionTree* collisionTree;
}; };
typedef tdef_align(16) uint16_t r_index16_t; struct XSurfaceTri
{
uint16_t i[3];
};
typedef tdef_align(16) XSurfaceTri XSurfaceTri16;
struct XSurface struct XSurface
{ {
@ -546,7 +553,7 @@ namespace T5
uint16_t triCount; uint16_t triCount;
uint16_t baseTriIndex; uint16_t baseTriIndex;
uint16_t baseVertIndex; uint16_t baseVertIndex;
r_index16_t (*triIndices)[3]; XSurfaceTri16* triIndices;
XSurfaceVertexInfo vertInfo; XSurfaceVertexInfo vertInfo;
GfxPackedVertex* verts0; GfxPackedVertex* verts0;
void /*IDirect3DVertexBuffer9*/* vb0; void /*IDirect3DVertexBuffer9*/* vb0;
@ -587,8 +594,8 @@ namespace T5
struct XBoneInfo struct XBoneInfo
{ {
float bounds[2][3]; vec3_t bounds[2];
float offset[3]; vec3_t offset;
float radiusSquared; float radiusSquared;
char collmap; char collmap;
}; };
@ -657,6 +664,14 @@ namespace T5
PhysGeomList* geomList; PhysGeomList* geomList;
}; };
enum XModelLodRampType : unsigned char
{
XMODEL_LOD_RAMP_RIGID = 0x0,
XMODEL_LOD_RAMP_SKINNED = 0x1,
XMODEL_LOD_RAMP_COUNT
};
struct XModelQuat struct XModelQuat
{ {
int16_t v[4]; int16_t v[4];
@ -668,12 +683,12 @@ namespace T5
unsigned char numBones; unsigned char numBones;
unsigned char numRootBones; unsigned char numRootBones;
unsigned char numsurfs; unsigned char numsurfs;
char lodRampType; XModelLodRampType lodRampType;
uint16_t* boneNames; ScriptString* boneNames;
char* parentList; unsigned char* parentList;
XModelQuat* quats; XModelQuat* quats;
float* trans; float* trans;
char* partClassification; unsigned char* partClassification;
DObjAnimMat* baseMat; DObjAnimMat* baseMat;
XSurface* surfs; XSurface* surfs;
Material** materialHandles; Material** materialHandles;
@ -684,13 +699,13 @@ namespace T5
int contents; int contents;
XBoneInfo* boneInfo; XBoneInfo* boneInfo;
float radius; float radius;
float mins[3]; vec3_t mins;
float maxs[3]; vec3_t maxs;
uint16_t numLods; uint16_t numLods;
uint16_t collLod; int16_t collLod;
XModelStreamInfo streamInfo; XModelStreamInfo streamInfo;
int memUsage; int memUsage;
int flags; unsigned int flags;
bool bad; bool bad;
PhysPreset* physPreset; PhysPreset* physPreset;
unsigned char numCollmaps; unsigned char numCollmaps;
@ -775,7 +790,7 @@ namespace T5
char nameStart; char nameStart;
char nameEnd; char nameEnd;
char samplerState; char samplerState;
char semantic; unsigned char semantic; // TextureSemantic
char isMatureContent; char isMatureContent;
char pad[3]; char pad[3];
MaterialTextureDefInfo u; MaterialTextureDefInfo u;

View File

@ -0,0 +1,31 @@
#pragma once
#include "Json/JsonCommon.h"
#include <memory>
#include <optional>
#include <string>
#include <vector>
namespace T5
{
class JsonXModelLod
{
public:
std::string file;
float distance;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonXModelLod, file, distance);
class JsonXModel
{
public:
std::vector<JsonXModelLod> lods;
std::optional<int> collLod;
std::optional<std::string> physPreset;
std::optional<std::string> physConstraints;
unsigned flags;
};
NLOHMANN_DEFINE_TYPE_EXTENSION(JsonXModel, lods, collLod, physPreset, physConstraints, flags);
} // namespace T5

View File

@ -0,0 +1,42 @@
#include "AssetLoaderXModel.h"
#include "Game/T5/T5.h"
#include "Game/T5/XModel/XModelLoaderT5.h"
#include "Pool/GlobalAssetPool.h"
#include <cstring>
#include <format>
#include <iostream>
using namespace T5;
void* AssetLoaderXModel::CreateEmptyAsset(const std::string& assetName, MemoryManager* memory)
{
auto* asset = memory->Alloc<AssetXModel::Type>();
asset->name = memory->Dup(assetName.c_str());
return asset;
}
bool AssetLoaderXModel::CanLoadFromRaw() const
{
return true;
}
bool AssetLoaderXModel::LoadFromRaw(
const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const
{
const auto file = searchPath->Open(std::format("xmodel/{}.json", assetName));
if (!file.IsOpen())
return false;
auto* xmodel = memory->Alloc<XModel>();
xmodel->name = memory->Dup(assetName.c_str());
std::vector<XAssetInfoGeneric*> dependencies;
if (LoadXModel(*file.m_stream, *xmodel, memory, manager, dependencies))
manager->AddAsset<AssetXModel>(assetName, xmodel, std::move(dependencies));
else
std::cerr << std::format("Failed to load xmodel \"{}\"\n", assetName);
return true;
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "AssetLoading/BasicAssetLoader.h"
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/T5/T5.h"
#include "SearchPath/ISearchPath.h"
namespace T5
{
class AssetLoaderXModel final : public BasicAssetLoader<AssetXModel>
{
static std::string GetFileNameForAsset(const std::string& assetName);
public:
_NODISCARD void* CreateEmptyAsset(const std::string& assetName, MemoryManager* memory) override;
_NODISCARD bool CanLoadFromRaw() const override;
bool
LoadFromRaw(const std::string& assetName, ISearchPath* searchPath, MemoryManager* memory, IAssetLoadingManager* manager, Zone* zone) const override;
};
} // namespace T5

View File

@ -3,6 +3,7 @@
#include "AssetLoaders/AssetLoaderLocalizeEntry.h" #include "AssetLoaders/AssetLoaderLocalizeEntry.h"
#include "AssetLoaders/AssetLoaderRawFile.h" #include "AssetLoaders/AssetLoaderRawFile.h"
#include "AssetLoaders/AssetLoaderStringTable.h" #include "AssetLoaders/AssetLoaderStringTable.h"
#include "AssetLoaders/AssetLoaderXModel.h"
#include "AssetLoading/AssetLoadingManager.h" #include "AssetLoading/AssetLoadingManager.h"
#include "Game/T5/GameAssetPoolT5.h" #include "Game/T5/GameAssetPoolT5.h"
#include "Game/T5/GameT5.h" #include "Game/T5/GameT5.h"
@ -27,7 +28,7 @@ ObjLoader::ObjLoader()
REGISTER_ASSET_LOADER(BasicAssetLoader<AssetPhysConstraints>) REGISTER_ASSET_LOADER(BasicAssetLoader<AssetPhysConstraints>)
REGISTER_ASSET_LOADER(BasicAssetLoader<AssetDestructibleDef>) REGISTER_ASSET_LOADER(BasicAssetLoader<AssetDestructibleDef>)
REGISTER_ASSET_LOADER(BasicAssetLoader<AssetXAnim>) REGISTER_ASSET_LOADER(BasicAssetLoader<AssetXAnim>)
REGISTER_ASSET_LOADER(BasicAssetLoader<AssetXModel>) REGISTER_ASSET_LOADER(AssetLoaderXModel)
REGISTER_ASSET_LOADER(BasicAssetLoader<AssetMaterial>) REGISTER_ASSET_LOADER(BasicAssetLoader<AssetMaterial>)
REGISTER_ASSET_LOADER(BasicAssetLoader<AssetTechniqueSet>) REGISTER_ASSET_LOADER(BasicAssetLoader<AssetTechniqueSet>)
REGISTER_ASSET_LOADER(BasicAssetLoader<AssetImage>) REGISTER_ASSET_LOADER(BasicAssetLoader<AssetImage>)

View File

@ -0,0 +1,49 @@
#include "XModelLoaderT5.h"
#include "Game/T5/CommonT5.h"
#include "Game/T5/XModel/JsonXModel.h"
#define GAME_NAMESPACE T5
namespace T5
{
const char* HITLOC_NAMES[]{
// clang-format off
"none",
"helmet",
"head",
"neck",
"torso_upper",
"torso_lower",
"right_arm_upper",
"left_arm_upper",
"right_arm_lower",
"left_arm_lower",
"right_hand",
"left_hand",
"right_leg_upper",
"left_leg_upper",
"right_leg_lower",
"left_leg_lower",
"right_foot",
"left_foot",
"gun",
// clang-format on
};
static_assert(std::extent_v<decltype(HITLOC_NAMES)> == HITLOC_COUNT);
} // namespace T5
#include "XModel/GenericXModelLoader.inc.h"
namespace T5
{
bool LoadXModel(std::istream& stream, XModel& xmodel, MemoryManager* memory, IAssetLoadingManager* manager, std::vector<XAssetInfoGeneric*>& dependencies)
{
std::set<XAssetInfoGeneric*> dependenciesSet;
XModelLoader loader(stream, *memory, *manager, dependenciesSet);
dependencies.assign(dependenciesSet.cbegin(), dependenciesSet.cend());
return loader.Load(xmodel);
}
} // namespace T5

View File

@ -0,0 +1,13 @@
#pragma once
#include "AssetLoading/IAssetLoadingManager.h"
#include "Game/T5/T5.h"
#include "Utils/MemoryManager.h"
#include <istream>
#include <vector>
namespace T5
{
bool LoadXModel(std::istream& stream, XModel& xmodel, MemoryManager* memory, IAssetLoadingManager* manager, std::vector<XAssetInfoGeneric*>& dependencies);
}

View File

@ -773,10 +773,13 @@ namespace GAME_NAMESPACE
} }
xmodel.flags = jXModel.flags; xmodel.flags = jXModel.flags;
#ifdef FEATURE_T6
xmodel.lightingOriginOffset.x = jXModel.lightingOriginOffset.x; xmodel.lightingOriginOffset.x = jXModel.lightingOriginOffset.x;
xmodel.lightingOriginOffset.y = jXModel.lightingOriginOffset.y; xmodel.lightingOriginOffset.y = jXModel.lightingOriginOffset.y;
xmodel.lightingOriginOffset.z = jXModel.lightingOriginOffset.z; xmodel.lightingOriginOffset.z = jXModel.lightingOriginOffset.z;
xmodel.lightingOriginRange = jXModel.lightingOriginRange; xmodel.lightingOriginRange = jXModel.lightingOriginRange;
#endif
return true; return true;
} }

View File

@ -1,519 +1,9 @@
#include "AssetDumperXModel.h" #include "AssetDumperXModel.h"
#include "Game/T5/CommonT5.h" #include "Game/T5/XModel/XModelDumperT5.h"
#include "ObjWriting.h"
#include "Utils/DistinctMapper.h"
#include "Utils/QuatInt16.h"
#include "XModel/Export/XModelExportWriter.h"
#include "XModel/Gltf/GltfBinOutput.h"
#include "XModel/Gltf/GltfTextOutput.h"
#include "XModel/Gltf/GltfWriter.h"
#include "XModel/Obj/ObjWriter.h"
#include "XModel/XModelWriter.h"
#include <cassert>
#include <format>
using namespace T5; using namespace T5;
namespace
{
std::string GetFileNameForLod(const std::string& modelName, const unsigned lod, const std::string& extension)
{
return std::format("model_export/{}_lod{}{}", modelName, lod, extension);
}
GfxImage* GetMaterialColorMap(const Material* material)
{
std::vector<MaterialTextureDef*> potentialTextureDefs;
for (auto textureIndex = 0u; textureIndex < material->textureCount; textureIndex++)
{
MaterialTextureDef* def = &material->textureTable[textureIndex];
if (def->semantic == TS_COLOR_MAP || def->semantic >= TS_COLOR0_MAP && def->semantic <= TS_COLOR15_MAP)
potentialTextureDefs.push_back(def);
}
if (potentialTextureDefs.empty())
return nullptr;
if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->u.image;
for (const auto* def : potentialTextureDefs)
{
if (def->nameStart == 'c' && def->nameEnd == 'p')
return def->u.image;
}
return potentialTextureDefs[0]->u.image;
}
GfxImage* GetMaterialNormalMap(const Material* material)
{
std::vector<MaterialTextureDef*> potentialTextureDefs;
for (auto textureIndex = 0u; textureIndex < material->textureCount; textureIndex++)
{
MaterialTextureDef* def = &material->textureTable[textureIndex];
if (def->semantic == TS_NORMAL_MAP)
potentialTextureDefs.push_back(def);
}
if (potentialTextureDefs.empty())
return nullptr;
if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->u.image;
for (const auto* def : potentialTextureDefs)
{
if (def->nameStart == 'n' && def->nameEnd == 'p')
return def->u.image;
}
return potentialTextureDefs[0]->u.image;
}
GfxImage* GetMaterialSpecularMap(const Material* material)
{
std::vector<MaterialTextureDef*> potentialTextureDefs;
for (auto textureIndex = 0u; textureIndex < material->textureCount; textureIndex++)
{
MaterialTextureDef* def = &material->textureTable[textureIndex];
if (def->semantic == TS_SPECULAR_MAP)
potentialTextureDefs.push_back(def);
}
if (potentialTextureDefs.empty())
return nullptr;
if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->u.image;
for (const auto* def : potentialTextureDefs)
{
if (def->nameStart == 's' && def->nameEnd == 'p')
return def->u.image;
}
return potentialTextureDefs[0]->u.image;
}
void AddXModelBones(XModelCommon& out, const AssetDumpingContext& context, const XModel* model)
{
for (auto boneNum = 0u; boneNum < model->numBones; boneNum++)
{
XModelBone bone;
if (model->boneNames[boneNum] < context.m_zone->m_script_strings.Count())
bone.name = context.m_zone->m_script_strings[model->boneNames[boneNum]];
else
bone.name = "INVALID_BONE_NAME";
if (boneNum >= model->numRootBones)
bone.parentIndex = boneNum - static_cast<unsigned int>(model->parentList[boneNum - model->numRootBones]);
else
bone.parentIndex = std::nullopt;
bone.scale[0] = 1.0f;
bone.scale[1] = 1.0f;
bone.scale[2] = 1.0f;
bone.globalOffset[0] = model->baseMat[boneNum].trans[0];
bone.globalOffset[1] = model->baseMat[boneNum].trans[1];
bone.globalOffset[2] = model->baseMat[boneNum].trans[2];
bone.globalRotation = {
model->baseMat[boneNum].quat[0],
model->baseMat[boneNum].quat[1],
model->baseMat[boneNum].quat[2],
model->baseMat[boneNum].quat[3],
};
if (boneNum < model->numRootBones)
{
bone.localOffset[0] = 0;
bone.localOffset[1] = 0;
bone.localOffset[2] = 0;
bone.localRotation = {0, 0, 0, 1};
}
else
{
const auto* trans = &model->trans[(boneNum - model->numRootBones) * 3];
bone.localOffset[0] = trans[0];
bone.localOffset[1] = trans[1];
bone.localOffset[2] = trans[2];
const auto& quat = model->quats[boneNum - model->numRootBones];
bone.localRotation = {
QuatInt16::ToFloat(quat.v[0]),
QuatInt16::ToFloat(quat.v[1]),
QuatInt16::ToFloat(quat.v[2]),
QuatInt16::ToFloat(quat.v[3]),
};
}
out.m_bones.emplace_back(std::move(bone));
}
}
const char* AssetName(const char* input)
{
if (input && input[0] == ',')
return &input[1];
return input;
}
void AddXModelMaterials(XModelCommon& out, DistinctMapper<Material*>& materialMapper, const XModel* model)
{
for (auto surfaceMaterialNum = 0; surfaceMaterialNum < model->numsurfs; surfaceMaterialNum++)
{
Material* material = model->materialHandles[surfaceMaterialNum];
if (materialMapper.Add(material))
{
XModelMaterial xMaterial;
xMaterial.ApplyDefaults();
xMaterial.name = AssetName(material->info.name);
const auto* colorMap = GetMaterialColorMap(material);
if (colorMap)
xMaterial.colorMapName = AssetName(colorMap->name);
const auto* normalMap = GetMaterialNormalMap(material);
if (normalMap)
xMaterial.normalMapName = AssetName(normalMap->name);
const auto* specularMap = GetMaterialSpecularMap(material);
if (specularMap)
xMaterial.specularMapName = AssetName(specularMap->name);
out.m_materials.emplace_back(std::move(xMaterial));
}
}
}
void AddXModelObjects(XModelCommon& out, const XModel* model, const unsigned lod, const DistinctMapper<Material*>& materialMapper)
{
const auto surfCount = model->lodInfo[lod].numsurfs;
const auto baseSurfaceIndex = model->lodInfo[lod].surfIndex;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
{
XModelObject object;
object.name = std::format("surf{}", surfIndex);
object.materialIndex = static_cast<int>(materialMapper.GetDistinctPositionByInputPosition(surfIndex + baseSurfaceIndex));
out.m_objects.emplace_back(std::move(object));
}
}
void AddXModelVertices(XModelCommon& out, const XModel* model, const unsigned lod)
{
const auto* surfs = &model->surfs[model->lodInfo[lod].surfIndex];
const auto surfCount = model->lodInfo[lod].numsurfs;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
{
const auto& surface = surfs[surfIndex];
for (auto vertexIndex = 0u; vertexIndex < surface.vertCount; vertexIndex++)
{
const auto& v = surface.verts0[vertexIndex];
XModelVertex vertex{};
vertex.coordinates[0] = v.xyz[0];
vertex.coordinates[1] = v.xyz[1];
vertex.coordinates[2] = v.xyz[2];
Common::Vec3UnpackUnitVec(v.normal, vertex.normal);
Common::Vec4UnpackGfxColor(v.color, vertex.color);
Common::Vec2UnpackTexCoords(v.texCoord, vertex.uv);
out.m_vertices.emplace_back(vertex);
}
}
}
void AllocateXModelBoneWeights(const XModel* model, const unsigned lod, XModelVertexBoneWeightCollection& weightCollection)
{
const auto* surfs = &model->surfs[model->lodInfo[lod].surfIndex];
const auto surfCount = model->lodInfo[lod].numsurfs;
auto totalWeightCount = 0u;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
{
const auto& surface = surfs[surfIndex];
if (surface.vertList)
{
totalWeightCount += surface.vertListCount;
}
if (surface.vertInfo.vertsBlend)
{
totalWeightCount += surface.vertInfo.vertCount[0] * 1;
totalWeightCount += surface.vertInfo.vertCount[1] * 2;
totalWeightCount += surface.vertInfo.vertCount[2] * 3;
totalWeightCount += surface.vertInfo.vertCount[3] * 4;
}
}
weightCollection.weights.resize(totalWeightCount);
}
float BoneWeight16(const uint16_t value)
{
return static_cast<float>(value) / static_cast<float>(std::numeric_limits<uint16_t>::max());
}
void AddXModelVertexBoneWeights(XModelCommon& out, const XModel* model, const unsigned lod)
{
const auto* surfs = &model->surfs[model->lodInfo[lod].surfIndex];
const auto surfCount = model->lodInfo[lod].numsurfs;
auto& weightCollection = out.m_bone_weight_data;
size_t weightOffset = 0u;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
{
const auto& surface = surfs[surfIndex];
auto handledVertices = 0u;
if (surface.vertList)
{
for (auto vertListIndex = 0u; vertListIndex < surface.vertListCount; vertListIndex++)
{
const auto& vertList = surface.vertList[vertListIndex];
const auto boneWeightOffset = weightOffset;
weightCollection.weights[weightOffset++] = XModelBoneWeight{vertList.boneOffset / sizeof(DObjSkelMat), 1.0f};
for (auto vertListVertexOffset = 0u; vertListVertexOffset < vertList.vertCount; vertListVertexOffset++)
{
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 1);
}
handledVertices += vertList.vertCount;
}
}
auto vertsBlendOffset = 0u;
if (surface.vertInfo.vertsBlend)
{
// 1 bone weight
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[0]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const auto boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex0, 1.0f};
vertsBlendOffset += 1;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 1);
}
// 2 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[1]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const auto boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
const auto boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const auto boneWeight0 = 1.0f - boneWeight1;
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex0, boneWeight0};
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex1, boneWeight1};
vertsBlendOffset += 3;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 2);
}
// 3 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[2]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const auto boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
const auto boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const auto boneIndex2 = surface.vertInfo.vertsBlend[vertsBlendOffset + 3] / sizeof(DObjSkelMat);
const auto boneWeight2 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 4]);
const auto boneWeight0 = 1.0f - boneWeight1 - boneWeight2;
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex0, boneWeight0};
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex1, boneWeight1};
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex2, boneWeight2};
vertsBlendOffset += 5;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 3);
}
// 4 bone weights
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[3]; vertIndex++)
{
const auto boneWeightOffset = weightOffset;
const auto boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
const auto boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
const auto boneIndex2 = surface.vertInfo.vertsBlend[vertsBlendOffset + 3] / sizeof(DObjSkelMat);
const auto boneWeight2 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 4]);
const auto boneIndex3 = surface.vertInfo.vertsBlend[vertsBlendOffset + 5] / sizeof(DObjSkelMat);
const auto boneWeight3 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 6]);
const auto boneWeight0 = 1.0f - boneWeight1 - boneWeight2 - boneWeight3;
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex0, boneWeight0};
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex1, boneWeight1};
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex2, boneWeight2};
weightCollection.weights[weightOffset++] = XModelBoneWeight{boneIndex3, boneWeight3};
vertsBlendOffset += 7;
out.m_vertex_bone_weights.emplace_back(boneWeightOffset, 4);
}
handledVertices +=
surface.vertInfo.vertCount[0] + surface.vertInfo.vertCount[1] + surface.vertInfo.vertCount[2] + surface.vertInfo.vertCount[3];
}
for (; handledVertices < surface.vertCount; handledVertices++)
{
out.m_vertex_bone_weights.emplace_back(0, 0);
}
}
}
void AddXModelFaces(XModelCommon& out, const XModel* model, const unsigned lod)
{
const auto* surfs = &model->surfs[model->lodInfo[lod].surfIndex];
const auto surfCount = model->lodInfo[lod].numsurfs;
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
{
const auto& surface = surfs[surfIndex];
auto& object = out.m_objects[surfIndex];
object.m_faces.reserve(surface.triCount);
for (auto triIndex = 0u; triIndex < surface.triCount; triIndex++)
{
const auto& tri = surface.triIndices[triIndex];
XModelFace face{};
face.vertexIndex[0] = tri[0] + surface.baseVertIndex;
face.vertexIndex[1] = tri[1] + surface.baseVertIndex;
face.vertexIndex[2] = tri[2] + surface.baseVertIndex;
object.m_faces.emplace_back(face);
}
}
}
void PopulateXModelWriter(XModelCommon& out, const AssetDumpingContext& context, const unsigned lod, const XModel* model)
{
DistinctMapper<Material*> materialMapper(model->numsurfs);
AllocateXModelBoneWeights(model, lod, out.m_bone_weight_data);
out.m_name = std::format("{}_lod{}", model->name, lod);
AddXModelBones(out, context, model);
AddXModelMaterials(out, materialMapper, model);
AddXModelObjects(out, model, lod, materialMapper);
AddXModelVertices(out, model, lod);
AddXModelVertexBoneWeights(out, model, lod);
AddXModelFaces(out, model, lod);
}
void DumpObjMtl(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>* asset)
{
const auto* model = asset->Asset();
const auto mtlFile = context.OpenAssetFile(std::format("model_export/{}.mtl", model->name));
if (!mtlFile)
return;
const auto writer = obj::CreateMtlWriter(*mtlFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name);
DistinctMapper<Material*> materialMapper(model->numsurfs);
writer->Write(common);
}
void DumpObjLod(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>* asset, const unsigned lod)
{
const auto* model = asset->Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model->name, lod, ".obj"));
if (!assetFile)
return;
const auto writer =
obj::CreateObjWriter(*assetFile, std::format("{}.mtl", model->name), context.m_zone->m_game->GetShortName(), context.m_zone->m_name);
DistinctMapper<Material*> materialMapper(model->numsurfs);
writer->Write(common);
}
void DumpXModelExportLod(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>* asset, const unsigned lod)
{
const auto* model = asset->Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model->name, lod, ".XMODEL_EXPORT"));
if (!assetFile)
return;
const auto writer = xmodel_export::CreateWriterForVersion6(*assetFile, context.m_zone->m_game->GetShortName(), context.m_zone->m_name);
writer->Write(common);
}
template<typename T>
void DumpGltfLod(
const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>* asset, const unsigned lod, const std::string& extension)
{
const auto* model = asset->Asset();
const auto assetFile = context.OpenAssetFile(GetFileNameForLod(model->name, lod, extension));
if (!assetFile)
return;
const auto output = std::make_unique<T>(*assetFile);
const auto writer = gltf::Writer::CreateWriter(output.get(), context.m_zone->m_game->GetShortName(), context.m_zone->m_name);
writer->Write(common);
}
void DumpXModelSurfs(const AssetDumpingContext& context, const XAssetInfo<XModel>* asset)
{
const auto* model = asset->Asset();
for (auto currentLod = 0u; currentLod < model->numLods; currentLod++)
{
XModelCommon common;
PopulateXModelWriter(common, context, currentLod, asset->Asset());
switch (ObjWriting::Configuration.ModelOutputFormat)
{
case ObjWriting::Configuration_t::ModelOutputFormat_e::OBJ:
DumpObjLod(common, context, asset, currentLod);
if (currentLod == 0u)
DumpObjMtl(common, context, asset);
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::XMODEL_EXPORT:
DumpXModelExportLod(common, context, asset, currentLod);
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::GLTF:
DumpGltfLod<gltf::TextOutput>(common, context, asset, currentLod, ".gltf");
break;
case ObjWriting::Configuration_t::ModelOutputFormat_e::GLB:
DumpGltfLod<gltf::BinOutput>(common, context, asset, currentLod, ".glb");
break;
default:
assert(false);
break;
}
}
}
} // namespace
bool AssetDumperXModel::ShouldDump(XAssetInfo<XModel>* asset) bool AssetDumperXModel::ShouldDump(XAssetInfo<XModel>* asset)
{ {
return !asset->m_name.empty() && asset->m_name[0] != ','; return !asset->m_name.empty() && asset->m_name[0] != ',';
@ -521,5 +11,5 @@ bool AssetDumperXModel::ShouldDump(XAssetInfo<XModel>* asset)
void AssetDumperXModel::DumpAsset(AssetDumpingContext& context, XAssetInfo<XModel>* asset) void AssetDumperXModel::DumpAsset(AssetDumpingContext& context, XAssetInfo<XModel>* asset)
{ {
DumpXModelSurfs(context, asset); DumpXModel(context, asset);
} }

View File

@ -0,0 +1,17 @@
#include "XModelDumperT5.h"
#include "Game/T5/CommonT5.h"
#include "Game/T5/XModel/JsonXModel.h"
#define GAME_NAMESPACE T5
#include "XModel/GenericXModelDumper.inc.h"
namespace T5
{
void DumpXModel(AssetDumpingContext& context, XAssetInfo<XModel>* asset)
{
DumpXModelJson(context, asset);
DumpXModelSurfs(context, asset);
}
} // namespace T5

View File

@ -0,0 +1,9 @@
#pragma once
#include "Dumping/AssetDumpingContext.h"
#include "Game/T5/T5.h"
namespace T5
{
void DumpXModel(AssetDumpingContext& context, XAssetInfo<XModel>* asset);
}

View File

@ -1,9 +1,10 @@
#include "XModelDumperT6.h" #include "XModelDumperT6.h"
#include "Game/T6/CommonT6.h" #include "Game/T6/CommonT6.h"
#include "Game/T6/Json/JsonXModel.h" #include "Game/T6/XModel/JsonXModel.h"
#define GAME_NAMESPACE T6 #define GAME_NAMESPACE T6
#define FEATURE_T6
#include "XModel/GenericXModelDumper.inc.h" #include "XModel/GenericXModelDumper.inc.h"

View File

@ -25,6 +25,15 @@ namespace GAME_NAMESPACE
return std::format("model_export/{}_lod{}{}", modelName, lod, extension); return std::format("model_export/{}_lod{}{}", modelName, lod, extension);
} }
inline GfxImage* GetImageFromTextureDef(const MaterialTextureDef& textureDef)
{
#ifdef FEATURE_T6
return textureDef.image;
#else
return textureDef.u.image;
#endif
}
inline GfxImage* GetMaterialColorMap(const Material* material) inline GfxImage* GetMaterialColorMap(const Material* material)
{ {
std::vector<MaterialTextureDef*> potentialTextureDefs; std::vector<MaterialTextureDef*> potentialTextureDefs;
@ -40,27 +49,27 @@ namespace GAME_NAMESPACE
if (potentialTextureDefs.empty()) if (potentialTextureDefs.empty())
return nullptr; return nullptr;
if (potentialTextureDefs.size() == 1) if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->image; return GetImageFromTextureDef(*potentialTextureDefs[0]);
for (const auto* def : potentialTextureDefs) for (const auto* def : potentialTextureDefs)
{ {
if (tolower(def->nameStart) == 'c' && tolower(def->nameEnd) == 'p') if (tolower(def->nameStart) == 'c' && tolower(def->nameEnd) == 'p')
return def->image; return GetImageFromTextureDef(*def);
} }
for (const auto* def : potentialTextureDefs) for (const auto* def : potentialTextureDefs)
{ {
if (tolower(def->nameStart) == 'r' && tolower(def->nameEnd) == 'k') if (tolower(def->nameStart) == 'r' && tolower(def->nameEnd) == 'k')
return def->image; return GetImageFromTextureDef(*def);
} }
for (const auto* def : potentialTextureDefs) for (const auto* def : potentialTextureDefs)
{ {
if (tolower(def->nameStart) == 'd' && tolower(def->nameEnd) == 'p') if (tolower(def->nameStart) == 'd' && tolower(def->nameEnd) == 'p')
return def->image; return GetImageFromTextureDef(*def);
} }
return potentialTextureDefs[0]->image; return GetImageFromTextureDef(*potentialTextureDefs[0]);
} }
inline GfxImage* GetMaterialNormalMap(const Material* material) inline GfxImage* GetMaterialNormalMap(const Material* material)
@ -78,15 +87,15 @@ namespace GAME_NAMESPACE
if (potentialTextureDefs.empty()) if (potentialTextureDefs.empty())
return nullptr; return nullptr;
if (potentialTextureDefs.size() == 1) if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->image; return GetImageFromTextureDef(*potentialTextureDefs[0]);
for (const auto* def : potentialTextureDefs) for (const auto* def : potentialTextureDefs)
{ {
if (def->nameStart == 'n' && def->nameEnd == 'p') if (def->nameStart == 'n' && def->nameEnd == 'p')
return def->image; return GetImageFromTextureDef(*def);
} }
return potentialTextureDefs[0]->image; return GetImageFromTextureDef(*potentialTextureDefs[0]);
} }
inline GfxImage* GetMaterialSpecularMap(const Material* material) inline GfxImage* GetMaterialSpecularMap(const Material* material)
@ -104,15 +113,15 @@ namespace GAME_NAMESPACE
if (potentialTextureDefs.empty()) if (potentialTextureDefs.empty())
return nullptr; return nullptr;
if (potentialTextureDefs.size() == 1) if (potentialTextureDefs.size() == 1)
return potentialTextureDefs[0]->image; return GetImageFromTextureDef(*potentialTextureDefs[0]);
for (const auto* def : potentialTextureDefs) for (const auto* def : potentialTextureDefs)
{ {
if (def->nameStart == 's' && def->nameEnd == 'p') if (def->nameStart == 's' && def->nameEnd == 'p')
return def->image; return GetImageFromTextureDef(*def);
} }
return potentialTextureDefs[0]->image; return GetImageFromTextureDef(*potentialTextureDefs[0]);
} }
inline bool HasDefaultArmature(const XModel* model, const unsigned lod) inline bool HasDefaultArmature(const XModel* model, const unsigned lod)
@ -654,10 +663,13 @@ namespace GAME_NAMESPACE
jXModel.physConstraints = AssetName(xmodel.physConstraints->name); jXModel.physConstraints = AssetName(xmodel.physConstraints->name);
jXModel.flags = xmodel.flags; jXModel.flags = xmodel.flags;
#ifdef FEATURE_T6
jXModel.lightingOriginOffset.x = xmodel.lightingOriginOffset.x; jXModel.lightingOriginOffset.x = xmodel.lightingOriginOffset.x;
jXModel.lightingOriginOffset.y = xmodel.lightingOriginOffset.y; jXModel.lightingOriginOffset.y = xmodel.lightingOriginOffset.y;
jXModel.lightingOriginOffset.z = xmodel.lightingOriginOffset.z; jXModel.lightingOriginOffset.z = xmodel.lightingOriginOffset.z;
jXModel.lightingOriginRange = xmodel.lightingOriginRange; jXModel.lightingOriginRange = xmodel.lightingOriginRange;
#endif
} }
std::ostream& m_stream; std::ostream& m_stream;