mirror of
https://github.com/Laupetin/OpenAssetTools.git
synced 2026-06-26 19:08:07 +00:00
feat: xmodel preview in ModMan (#835)
* chore: upgrade webwindowed for dynamic assets * chore: make enums in ModMan lowercase * chore: add missing platform wiiu in ModMan * fix: register asset handler on all windows * chore: properly localize game and platform * chore: render example cube as xmodel preview * chore: allow origin * in debug * feat: show preview of xmodels with ModMan * feat: show images in xmodel preview * feat: auto load search paths in ModMan * chore: load objcontainer of loaded zones in ModMan * chore: add iw4x specific recognized zone dirs * chore: show when models are loading * fix: make sure webwindowed handles window and app destruction in correct order * chore: track and properly free threejs resources * chore: add skybox for 3d preview * chore: add small border radius to preview * fix: linting * fix: linux compilation * chore: update package lock
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#set DUMPER_HEADER "\"XModelDumper" + GAME + ".h\""
|
||||
#set COMMON_HEADER "\"Game/" + GAME + "/Common" + GAME + ".h\""
|
||||
#set JSON_HEADER "\"Game/" + GAME + "/XModel/JsonXModel" + GAME + ".h\""
|
||||
#set CONVERTER_HEADER "\"Game/" + GAME + "/XModel/XModelToCommonConverter" + GAME + ".h\""
|
||||
|
||||
#if GAME == "IW3"
|
||||
#define FEATURE_IW3
|
||||
@@ -31,6 +32,7 @@
|
||||
|
||||
#include COMMON_HEADER
|
||||
#include JSON_HEADER
|
||||
#include CONVERTER_HEADER
|
||||
|
||||
#include "ObjWriting.h"
|
||||
#include "Utils/DistinctMapper.h"
|
||||
@@ -48,251 +50,8 @@
|
||||
|
||||
using namespace GAME;
|
||||
|
||||
#set CLASS_NAME "Dumper" + GAME
|
||||
|
||||
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* GetImageFromTextureDef(const MaterialTextureDef& textureDef)
|
||||
{
|
||||
#ifdef FEATURE_T6
|
||||
return textureDef.image;
|
||||
#else
|
||||
return textureDef.u.image;
|
||||
#endif
|
||||
}
|
||||
|
||||
GfxImage* GetMaterialColorMap(const Material* material)
|
||||
{
|
||||
std::vector<MaterialTextureDef*> potentialTextureDefs;
|
||||
|
||||
for (auto textureIndex = 0u; textureIndex < material->textureCount; textureIndex++)
|
||||
{
|
||||
MaterialTextureDef* def = &material->textureTable[textureIndex];
|
||||
|
||||
#if defined(FEATURE_IW3) || defined(FEATURE_IW4) || defined(FEATURE_IW5)
|
||||
if (def->semantic == TS_COLOR_MAP)
|
||||
potentialTextureDefs.push_back(def);
|
||||
#else
|
||||
if (def->semantic == TS_COLOR_MAP || def->semantic >= TS_COLOR0_MAP && def->semantic <= TS_COLOR15_MAP)
|
||||
potentialTextureDefs.push_back(def);
|
||||
#endif
|
||||
}
|
||||
|
||||
if (potentialTextureDefs.empty())
|
||||
return nullptr;
|
||||
if (potentialTextureDefs.size() == 1)
|
||||
return GetImageFromTextureDef(*potentialTextureDefs[0]);
|
||||
|
||||
for (const auto* def : potentialTextureDefs)
|
||||
{
|
||||
if (tolower(def->nameStart) == 'c' && tolower(def->nameEnd) == 'p')
|
||||
return GetImageFromTextureDef(*def);
|
||||
}
|
||||
|
||||
for (const auto* def : potentialTextureDefs)
|
||||
{
|
||||
if (tolower(def->nameStart) == 'r' && tolower(def->nameEnd) == 'k')
|
||||
return GetImageFromTextureDef(*def);
|
||||
}
|
||||
|
||||
for (const auto* def : potentialTextureDefs)
|
||||
{
|
||||
if (tolower(def->nameStart) == 'd' && tolower(def->nameEnd) == 'p')
|
||||
return GetImageFromTextureDef(*def);
|
||||
}
|
||||
|
||||
return GetImageFromTextureDef(*potentialTextureDefs[0]);
|
||||
}
|
||||
|
||||
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 GetImageFromTextureDef(*potentialTextureDefs[0]);
|
||||
|
||||
for (const auto* def : potentialTextureDefs)
|
||||
{
|
||||
if (def->nameStart == 'n' && def->nameEnd == 'p')
|
||||
return GetImageFromTextureDef(*def);
|
||||
}
|
||||
|
||||
return GetImageFromTextureDef(*potentialTextureDefs[0]);
|
||||
}
|
||||
|
||||
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 GetImageFromTextureDef(*potentialTextureDefs[0]);
|
||||
|
||||
for (const auto* def : potentialTextureDefs)
|
||||
{
|
||||
if (def->nameStart == 's' && def->nameEnd == 'p')
|
||||
return GetImageFromTextureDef(*def);
|
||||
}
|
||||
|
||||
return GetImageFromTextureDef(*potentialTextureDefs[0]);
|
||||
}
|
||||
|
||||
bool GetSurfaces(const XModel& model, const unsigned lod, XSurface*& surfs, unsigned& surfCount)
|
||||
{
|
||||
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
|
||||
if (!model.lodInfo[lod].modelSurfs || !model.lodInfo[lod].modelSurfs->surfs)
|
||||
return false;
|
||||
|
||||
surfs = model.lodInfo[lod].modelSurfs->surfs;
|
||||
surfCount = model.lodInfo[lod].modelSurfs->numsurfs;
|
||||
#else
|
||||
if (!model.surfs)
|
||||
return false;
|
||||
|
||||
surfs = &model.surfs[model.lodInfo[lod].surfIndex];
|
||||
surfCount = model.lodInfo[lod].numsurfs;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HasDefaultArmatureForLod(const XModel& model, const unsigned lod)
|
||||
{
|
||||
if (model.numRootBones != 1 || model.numBones != 1)
|
||||
return false;
|
||||
|
||||
XSurface* surfs;
|
||||
unsigned surfCount;
|
||||
if (!GetSurfaces(model, lod, surfs, surfCount))
|
||||
return true;
|
||||
|
||||
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
|
||||
{
|
||||
const auto& surface = surfs[surfIndex];
|
||||
|
||||
if (surface.vertListCount != 1 || surface.vertInfo.vertsBlend)
|
||||
return false;
|
||||
|
||||
const auto& vertList = surface.vertList[0];
|
||||
#ifdef FEATURE_IW3
|
||||
// IW3 has some models that are missing 1 (a single) tri in its first lod.
|
||||
// It is not contained in any vert list or blend
|
||||
// I think this is a bug (?), so omit anyway.
|
||||
// The "one tri missing" is not supported by the exporter anyway.
|
||||
if (vertList.boneOffset != 0 || vertList.triOffset != 0 || vertList.vertCount != surface.vertCount)
|
||||
#else
|
||||
if (vertList.boneOffset != 0 || vertList.triOffset != 0 || vertList.triCount != surface.triCount || vertList.vertCount != surface.vertCount)
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HasDefaultArmatureForAllLods(const XModel& model)
|
||||
{
|
||||
for (auto lod = 0u; lod < model.numLods; lod++)
|
||||
{
|
||||
if (!HasDefaultArmatureForLod(model, lod))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void OmitDefaultArmature(XModelCommon& common)
|
||||
{
|
||||
common.m_bones.clear();
|
||||
common.m_bone_weight_data.weights.clear();
|
||||
common.m_vertex_bone_weights.resize(common.m_vertices.size());
|
||||
for (auto& vertexWeights : common.m_vertex_bone_weights)
|
||||
{
|
||||
vertexWeights.weightOffset = 0u;
|
||||
vertexWeights.weightCount = 0u;
|
||||
}
|
||||
}
|
||||
|
||||
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 = static_cast<int>(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;
|
||||
|
||||
const auto& baseMat = model.baseMat[boneNum];
|
||||
bone.globalOffset[0] = baseMat.trans.x;
|
||||
bone.globalOffset[1] = baseMat.trans.y;
|
||||
bone.globalOffset[2] = baseMat.trans.z;
|
||||
bone.globalRotation = {
|
||||
.x = baseMat.quat.x,
|
||||
.y = baseMat.quat.y,
|
||||
.z = baseMat.quat.z,
|
||||
.w = baseMat.quat.w,
|
||||
};
|
||||
|
||||
if (boneNum < model.numRootBones)
|
||||
{
|
||||
bone.localOffset[0] = 0;
|
||||
bone.localOffset[1] = 0;
|
||||
bone.localOffset[2] = 0;
|
||||
bone.localRotation = {.x = 0, .y = 0, .z = 0, .w = 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 = {
|
||||
.x = QuatInt16::ToFloat(quat.v[0]),
|
||||
.y = QuatInt16::ToFloat(quat.v[1]),
|
||||
.z = QuatInt16::ToFloat(quat.v[2]),
|
||||
.w = QuatInt16::ToFloat(quat.v[3]),
|
||||
};
|
||||
}
|
||||
|
||||
out.m_bones.emplace_back(std::move(bone));
|
||||
}
|
||||
}
|
||||
|
||||
const char* AssetName(const char* input)
|
||||
{
|
||||
if (input && input[0] == ',')
|
||||
@@ -300,297 +59,10 @@ namespace
|
||||
|
||||
return input;
|
||||
}
|
||||
|
||||
void AddXModelMaterials(XModelCommon& out, DistinctMapper<Material*>& materialMapper, const XModel& model)
|
||||
|
||||
std::string GetFileNameForLod(const std::string& modelName, const unsigned lod, const std::string& extension)
|
||||
{
|
||||
for (auto surfaceMaterialNum = 0u; 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)
|
||||
{
|
||||
XSurface* surfs;
|
||||
unsigned surfCount;
|
||||
if (!GetSurfaces(model, lod, surfs, surfCount))
|
||||
return;
|
||||
|
||||
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.x;
|
||||
vertex.coordinates[1] = v.xyz.y;
|
||||
vertex.coordinates[2] = v.xyz.z;
|
||||
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)
|
||||
{
|
||||
XSurface* surfs;
|
||||
unsigned surfCount;
|
||||
if (!GetSurfaces(model, lod, surfs, surfCount))
|
||||
return;
|
||||
|
||||
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)
|
||||
{
|
||||
XSurface* surfs;
|
||||
unsigned surfCount;
|
||||
if (!GetSurfaces(model, lod, surfs, surfCount))
|
||||
return;
|
||||
|
||||
auto& weightCollection = out.m_bone_weight_data;
|
||||
auto weightOffset = 0u;
|
||||
|
||||
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
|
||||
{
|
||||
const auto& surface = surfs[surfIndex];
|
||||
auto handledVertices = 0u;
|
||||
|
||||
if (surface.vertList)
|
||||
{
|
||||
#if defined(FEATURE_IW3) || defined(FEATURE_IW4)
|
||||
assert(!surface.deformed);
|
||||
#else
|
||||
assert((surface.flags & XSURFACE_FLAG_DEFORMED) == 0);
|
||||
#endif
|
||||
|
||||
for (auto vertListIndex = 0u; vertListIndex < surface.vertListCount; vertListIndex++)
|
||||
{
|
||||
const auto& vertList = surface.vertList[vertListIndex];
|
||||
const auto boneWeightOffset = weightOffset;
|
||||
|
||||
weightCollection.weights[weightOffset++] =
|
||||
XModelBoneWeight{.boneIndex = static_cast<unsigned>(vertList.boneOffset / sizeof(DObjSkelMat)), .weight = 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)
|
||||
{
|
||||
#if defined(FEATURE_IW3) || defined(FEATURE_IW4)
|
||||
assert(surface.deformed);
|
||||
#else
|
||||
assert((surface.flags & XSURFACE_FLAG_DEFORMED) > 0);
|
||||
#endif
|
||||
|
||||
// 1 bone weight
|
||||
for (auto vertIndex = 0; vertIndex < surface.vertInfo.vertCount[0]; vertIndex++)
|
||||
{
|
||||
const auto boneWeightOffset = weightOffset;
|
||||
const unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
|
||||
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex0, .weight = 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 unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
|
||||
const unsigned 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{.boneIndex = boneIndex0, .weight = boneWeight0};
|
||||
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex1, .weight = 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 unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
|
||||
const unsigned boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
|
||||
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
|
||||
const unsigned 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{.boneIndex = boneIndex0, .weight = boneWeight0};
|
||||
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex1, .weight = boneWeight1};
|
||||
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex2, .weight = 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 unsigned boneIndex0 = surface.vertInfo.vertsBlend[vertsBlendOffset + 0] / sizeof(DObjSkelMat);
|
||||
const unsigned boneIndex1 = surface.vertInfo.vertsBlend[vertsBlendOffset + 1] / sizeof(DObjSkelMat);
|
||||
const auto boneWeight1 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 2]);
|
||||
const unsigned boneIndex2 = surface.vertInfo.vertsBlend[vertsBlendOffset + 3] / sizeof(DObjSkelMat);
|
||||
const auto boneWeight2 = BoneWeight16(surface.vertInfo.vertsBlend[vertsBlendOffset + 4]);
|
||||
const unsigned 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{.boneIndex = boneIndex0, .weight = boneWeight0};
|
||||
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex1, .weight = boneWeight1};
|
||||
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex2, .weight = boneWeight2};
|
||||
weightCollection.weights[weightOffset++] = XModelBoneWeight{.boneIndex = boneIndex3, .weight = 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)
|
||||
{
|
||||
XSurface* surfs;
|
||||
unsigned surfCount;
|
||||
if (!GetSurfaces(model, lod, surfs, surfCount))
|
||||
return;
|
||||
|
||||
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.i[0] + surface.baseVertIndex;
|
||||
face.vertexIndex[1] = tri.i[1] + surface.baseVertIndex;
|
||||
face.vertexIndex[2] = tri.i[2] + surface.baseVertIndex;
|
||||
object.m_faces.emplace_back(face);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool CanOmitDefaultArmature()
|
||||
{
|
||||
return ObjWriting::Configuration.ModelOutputFormat != ModelOutputFormat_e::XMODEL_EXPORT
|
||||
&& ObjWriting::Configuration.ModelOutputFormat != ModelOutputFormat_e::XMODEL_BIN;
|
||||
}
|
||||
|
||||
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);
|
||||
AddXModelMaterials(out, materialMapper, model);
|
||||
AddXModelObjects(out, model, lod, materialMapper);
|
||||
AddXModelVertices(out, model, lod);
|
||||
AddXModelFaces(out, model, lod);
|
||||
|
||||
// Keep armature handling consistent across all LODs so dumped GLTF/GLB round-trips
|
||||
// preserve the same bone layout when re-imported.
|
||||
if (!CanOmitDefaultArmature() || !HasDefaultArmatureForAllLods(model))
|
||||
{
|
||||
AddXModelBones(out, context, model);
|
||||
AddXModelVertexBoneWeights(out, model, lod);
|
||||
}
|
||||
else
|
||||
{
|
||||
OmitDefaultArmature(out);
|
||||
}
|
||||
return std::format("model_export/{}_lod{}{}", modelName, lod, extension);
|
||||
}
|
||||
|
||||
void DumpObjMtl(const XModelCommon& common, const AssetDumpingContext& context, const XAssetInfo<XModel>& asset)
|
||||
@@ -667,37 +139,43 @@ namespace
|
||||
writer->Write(common);
|
||||
}
|
||||
|
||||
#set CONVERTER_NAME "ToCommonConverter" + GAME
|
||||
|
||||
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, model);
|
||||
const auto maybeCommon = xmodel::CONVERTER_NAME().Convert(asset, currentLod);
|
||||
if (!maybeCommon)
|
||||
{
|
||||
con::warn("Failed to convert to convert xmodel \"{}\" (lod: {})", model.name, currentLod);
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (ObjWriting::Configuration.ModelOutputFormat)
|
||||
{
|
||||
case ModelOutputFormat_e::OBJ:
|
||||
DumpObjLod(common, context, asset, currentLod);
|
||||
DumpObjLod(*maybeCommon, context, asset, currentLod);
|
||||
if (currentLod == 0u)
|
||||
DumpObjMtl(common, context, asset);
|
||||
DumpObjMtl(*maybeCommon, context, asset);
|
||||
break;
|
||||
|
||||
case ModelOutputFormat_e::XMODEL_EXPORT:
|
||||
DumpXModelExportLod(common, context, asset, currentLod);
|
||||
DumpXModelExportLod(*maybeCommon, context, asset, currentLod);
|
||||
break;
|
||||
|
||||
case ModelOutputFormat_e::XMODEL_BIN:
|
||||
DumpXModelBinLod(common, context, asset, currentLod);
|
||||
DumpXModelBinLod(*maybeCommon, context, asset, currentLod);
|
||||
break;
|
||||
|
||||
case ModelOutputFormat_e::GLTF:
|
||||
DumpGltfLod<gltf::TextOutput>(common, context, asset, currentLod, ".gltf");
|
||||
DumpGltfLod<gltf::TextOutput>(*maybeCommon, context, asset, currentLod, ".gltf");
|
||||
break;
|
||||
|
||||
case ModelOutputFormat_e::GLB:
|
||||
DumpGltfLod<gltf::BinOutput>(common, context, asset, currentLod, ".glb");
|
||||
DumpGltfLod<gltf::BinOutput>(*maybeCommon, context, asset, currentLod, ".glb");
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -815,6 +293,75 @@ namespace
|
||||
if (rootBoneName != xmodel::DEFAULT_XMODEL_ROOT_BONE_NAME)
|
||||
jXModel.rootBoneName = rootBoneName;
|
||||
}
|
||||
|
||||
bool GetSurfaces(const XModel& model, const unsigned lod, XSurface*& surfs, unsigned& surfCount)
|
||||
{
|
||||
#if defined(FEATURE_IW4) || defined(FEATURE_IW5)
|
||||
if (!model.lodInfo[lod].modelSurfs || !model.lodInfo[lod].modelSurfs->surfs)
|
||||
return false;
|
||||
|
||||
surfs = model.lodInfo[lod].modelSurfs->surfs;
|
||||
surfCount = model.lodInfo[lod].modelSurfs->numsurfs;
|
||||
#else
|
||||
if (!model.surfs)
|
||||
return false;
|
||||
|
||||
surfs = &model.surfs[model.lodInfo[lod].surfIndex];
|
||||
surfCount = model.lodInfo[lod].numsurfs;
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HasDefaultArmatureForLod(const XModel& model, const unsigned lod)
|
||||
{
|
||||
if (model.numRootBones != 1 || model.numBones != 1)
|
||||
return false;
|
||||
|
||||
XSurface* surfs;
|
||||
unsigned surfCount;
|
||||
if (!GetSurfaces(model, lod, surfs, surfCount))
|
||||
return true;
|
||||
|
||||
for (auto surfIndex = 0u; surfIndex < surfCount; surfIndex++)
|
||||
{
|
||||
const auto& surface = surfs[surfIndex];
|
||||
|
||||
if (surface.vertListCount != 1 || surface.vertInfo.vertsBlend)
|
||||
return false;
|
||||
|
||||
const auto& vertList = surface.vertList[0];
|
||||
#ifdef FEATURE_IW3
|
||||
// IW3 has some models that are missing 1 (a single) tri in its first lod.
|
||||
// It is not contained in any vert list or blend
|
||||
// I think this is a bug (?), so omit anyway.
|
||||
// The "one tri missing" is not supported by the exporter anyway.
|
||||
if (vertList.boneOffset != 0 || vertList.triOffset != 0 || vertList.vertCount != surface.vertCount)
|
||||
#else
|
||||
if (vertList.boneOffset != 0 || vertList.triOffset != 0 || vertList.triCount != surface.triCount || vertList.vertCount != surface.vertCount)
|
||||
#endif
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HasDefaultArmatureForAllLods(const XModel& model)
|
||||
{
|
||||
for (auto lod = 0u; lod < model.numLods; lod++)
|
||||
{
|
||||
if (!HasDefaultArmatureForLod(model, lod))
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CanOmitDefaultArmature()
|
||||
{
|
||||
return ObjWriting::Configuration.ModelOutputFormat != ModelOutputFormat_e::XMODEL_EXPORT
|
||||
&& ObjWriting::Configuration.ModelOutputFormat != ModelOutputFormat_e::XMODEL_BIN;
|
||||
}
|
||||
|
||||
void CreateJsonXModel(AssetDumpingContext& context, JsonXModel& jXModel, const XModel& model)
|
||||
{
|
||||
@@ -884,6 +431,8 @@ namespace
|
||||
}
|
||||
} // namespace
|
||||
|
||||
#set CLASS_NAME "Dumper" + GAME
|
||||
|
||||
namespace xmodel
|
||||
{
|
||||
void CLASS_NAME::DumpAsset(AssetDumpingContext& context, const XAssetInfo<AssetXModel::Type>& asset)
|
||||
|
||||
Reference in New Issue
Block a user