more cleanup & some fucking around with xanimparts

This commit is contained in:
Roxanne 2024-01-26 14:10:33 +01:00
parent 1edf6ce398
commit f1ec16983a

View File

@ -235,615 +235,148 @@ namespace Components
} }
} }
#endif #endif
if (type == Game::XAssetType::ASSET_TYPE_XANIMPARTS)
{
auto anim = asset.parts;
if (anim->name == "unarmed_cowerstand_idle"s || anim->name == "civilian_walk_cool"s || anim->name == "civilian_run_upright"s)
{
auto str = Game::SL_FindString("j_spinelower");
int interestingPart = 0;
for (size_t i = 0; i < anim->boneCount[Game::XAnimPartType::PART_TYPE_ALL]; i++)
{
if (anim->names[i] == str)
{
interestingPart = i;
//anim->names[i] = Game::SL_FindString("torso_stabilizer");
}
}
// Something interesting to do there
//for (size_t frameIndex = 0; frameIndex < anim->numframes; frameIndex++)
//{
// const auto delta = &anim->deltaPart[frameIndex];
// const auto frameCount = anim->numframes;
// if (delta->trans->size)
// {
// const auto test = delta->trans->u.frames;
// if (frameCount > 0xFF)
// {
// delta->trans->u.frames.indices._1
// buffer->saveArray(delta->trans->u.frames.indices._2, delta->trans->size + 1);
// }
// else
// {
// buffer->saveArray(delta->trans->u.frames.indices._1, delta->trans->size + 1);
// }
// if (delta->trans->u.frames.frames._1)
// {
// if (delta->trans->smallTrans)
// {
// buffer->save(delta->trans->u.frames.frames._1, 3, delta->trans->size + 1);
// }
// else
// {
// buffer->align(Utils::Stream::ALIGN_4);
// buffer->save(delta->trans->u.frames.frames._1, 6, delta->trans->size + 1);
// }
// }
// }
// else
// {
// buffer->save(delta->trans->u.frame0, 12);
// }
//}
}
}
if (type == Game::XAssetType::ASSET_TYPE_XMODEL) if (type == Game::XAssetType::ASSET_TYPE_XMODEL)
//if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s) //if (name == "body_urban_civ_female_a"s || name == "mp_body_opforce_arab_assault_a"s)
{ {
const auto model = asset.model;
if (!model->quats || !model->trans) if (name == "body_urban_civ_male_aa"s)
{ {
return; printf("");
} }
// Update vertex weights const auto model = asset.model;
//for (int i = 0; i < model->numLods; i++)
//{
// const auto lod = &model->lodInfo[i];
// for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++) int total =
// { model->noScalePartBits[0] |
// auto vertsBlendOffset = 0u; model->noScalePartBits[1] |
model->noScalePartBits[2] |
model->noScalePartBits[3] |
model->noScalePartBits[4] |
model->noScalePartBits[5];
// const auto surface = &lod->modelSurfs->surfs[surfIndex]; if (total != 0)
// surface->partBits[0] = 0x00000000;
// surface->partBits[1] = 0x00000000;
// surface->partBits[2] = 0x00000000;
// surface->partBits[3] = 0x00000000;
// surface->partBits[4] = 0x00000000;
// surface->partBits[5] = 0x00000000;
// }
//}
//return;
class QuatInt16
{ {
public: printf("");
static uint16_t ToInt16(const float quat) }
{
return static_cast<uint16_t>(quat * INT16_MAX);
}
static float ToFloat(const uint16_t quat) if (!model->quats || !model->trans || model->numBones < 10)
{ {
return static_cast<float>(quat) / static_cast<float>(INT16_MAX); // Unlikely candidate
} return;
}; }
// //
static std::vector<std::string> names{}; static std::vector<std::string> names{};
names.clear(); names.clear();
static Utils::Memory::Allocator allocator{};
for (auto i = 0; i < model->numBones; i++) for (auto i = 0; i < model->numBones; i++)
{ {
const auto bone = model->boneNames[i]; const auto bone = model->boneNames[i];
const auto name = Game::SL_ConvertToString(bone); const auto boneName = Game::SL_ConvertToString(bone);
const auto str = std::format("{} => q({} {} {} {}) t({} {} {})",
boneName,
model->baseMat[i - 1].quat[0],
model->baseMat[i - 1].quat[1],
model->baseMat[i - 1].quat[2],
model->baseMat[i - 1].quat[3],
model->baseMat[i - 1].trans[0],
model->baseMat[i - 1].trans[1],
model->baseMat[i - 1].trans[2]
);
const auto duplicated = allocator.duplicateString(str);
names.push_back( names.push_back(
std::format("{} => q({} {} {} {}) t({} {} {})", duplicated
name,
model->baseMat[i-1].quat[0],
model->baseMat[i-1].quat[1],
model->baseMat[i-1].quat[2],
model->baseMat[i-1].quat[3],
model->baseMat[i-1].trans[0],
model->baseMat[i-1].trans[1],
model->baseMat[i-1].trans[2]
)
); );
} }
printf("");
// //
const auto getIndexOfBone = [&](std::string name) Assets::IXModel::ConvertPlayerModelFromSingleplayerToMultiplayer(model, allocator);
{
for (uint8_t i = 0; i < model->numBones; i++)
{
const auto bone = model->boneNames[i];
const auto boneName = Game::SL_ConvertToString(bone);
if (name == boneName)
{
return i;
}
}
return static_cast<uint8_t>(UCHAR_MAX);
};
const auto getParentIndexOfBone = [&](uint8_t index)
{
const auto parentIndex = index - model->parentList[index - model->numRootBones];
return parentIndex;
};
const auto setParentIndexOfBone = [&](uint8_t boneIndex, uint8_t parentIndex)
{
if (boneIndex == SCHAR_MAX)
{
return;
}
model->parentList[boneIndex - model->numRootBones] = boneIndex - parentIndex;
};
const auto getParentOfBone = [&](int index)
{
const auto parentIndex = getParentIndexOfBone(index);
const auto boneName = Game::SL_ConvertToString(model->boneNames[parentIndex]);
return boneName;
};
const auto insertBoneInPartbits = [&](int8_t atPosition, int* partBits, bool hasAnyWeight = false)
{
constexpr auto LENGTH = 6;
// Bit masks and bit shifting are a pain honestly
std::vector<bool> flags{};
int check[LENGTH]{};
std::memcpy(check, partBits, LENGTH * sizeof(int));
for (auto i = 0; i < LENGTH; i++)
{
for (signed int bitPosition = 32 - 1; bitPosition >= 0; bitPosition--)
{
flags.emplace_back((partBits[i] >> bitPosition) & 0x1);
}
}
// copy parent flag
const auto partBitIndex = atPosition / 32;
const auto bitPosition = atPosition % 32; // The vector is already reversed so this should be correct
flags.insert(flags.begin() + (partBitIndex * 32 + bitPosition), hasAnyWeight);
flags.pop_back();
// clear it
for (auto i = 0; i < LENGTH; i++)
{
partBits[i] = 0;
}
// Write it
for (auto i = 0; i < LENGTH; i++)
{
partBits[i] = 0;
for (int bitPosition = 0; bitPosition < 32; bitPosition++)
{
const auto value = flags[i * 32 + 31 - bitPosition];
partBits[i] |= (value << bitPosition);
}
}
flags.clear();
};
const auto insertBone = [&](const std::string& boneName, const std::string& parentName)
{
assert(getIndexOfBone(boneName) == UCHAR_MAX);
// Start with backing up parent links that we will have to restore
// We'll restore them at the end
std::map<std::string, std::string> parentsToRestore{};
for (int i = model->numRootBones; i < model->numBones; i++)
{
parentsToRestore[Game::SL_ConvertToString(model->boneNames[i])] = getParentOfBone(i);
}
uint8_t newBoneCount = model->numBones + 1;
uint8_t newBoneCountMinusRoot = newBoneCount - model->numRootBones;
const auto parentIndex = getIndexOfBone(parentName);
assert(parentIndex != UCHAR_MAX);
int atPosition = parentIndex + 1;
const auto newBoneIndex = atPosition;
const auto newBoneIndexMinusRoot = atPosition - model->numRootBones;
// Reallocate
const auto newBoneNames = (uint16_t*)Game::Z_Malloc(sizeof(uint16_t) * newBoneCount);
const auto newMats = (Game::DObjAnimMat*)Game::Z_Malloc(sizeof(Game::DObjAnimMat) * newBoneCount);
const auto newBoneInfo = (Game::XBoneInfo*)Game::Z_Malloc(sizeof(Game::XBoneInfo) * newBoneCount);
const auto newQuats = (int16_t*)Game::Z_Malloc(sizeof(uint16_t) * 4 * newBoneCountMinusRoot);
const auto newTrans = (float*)Game::Z_Malloc(sizeof(float) * 3 * newBoneCountMinusRoot);
const auto newParentList = reinterpret_cast<unsigned char*>(Game::Z_Malloc(sizeof(uint8_t) * newBoneCountMinusRoot));
int lengthOfFirstPart = atPosition;
int lengthOfSecondPart = model->numBones - atPosition;
int lengthOfFirstPartM1 = atPosition - model->numRootBones;
int lengthOfSecondPartM1 = model->numBones - model->numRootBones - (atPosition - model->numRootBones);
int atPositionM1 = atPosition - model->numRootBones;
// should be equal to model->numBones
int total = lengthOfFirstPart + lengthOfSecondPart;
assert(total = model->numBones);
// should be equal to model->numBones - model->numRootBones
int totalM1 = lengthOfFirstPartM1 + lengthOfSecondPartM1;
assert(totalM1 == model->numBones - model->numRootBones);
// Copy before
if (lengthOfFirstPart > 0)
{
std::memcpy(newBoneNames, model->boneNames, sizeof(uint16_t) * lengthOfFirstPart);
std::memcpy(newMats, model->baseMat, sizeof(Game::DObjAnimMat) * lengthOfFirstPart);
std::memcpy(newBoneInfo, model->boneInfo, sizeof(Game::XBoneInfo) * lengthOfFirstPart);
std::memcpy(newQuats, model->quats, sizeof(uint16_t) * 4 * lengthOfFirstPartM1);
std::memcpy(newTrans, model->trans, sizeof(float) * 3 * lengthOfFirstPartM1);
}
// Insert new bone
{
unsigned int name = Game::SL_GetString(boneName.data(), 0);
Game::XBoneInfo boneInfo{};
Game::DObjAnimMat mat{};
// It's ABSOLUTE!
mat = model->baseMat[parentIndex];
boneInfo = model->boneInfo[parentIndex];
// It's RELATIVE !
uint16_t quat[4]{};
quat[3] = 32767; // 0 0 0 32767
float trans[3]{};
mat.transWeight = 1.9999f; // Should be 1.9999 like everybody?
newMats[newBoneIndex] = mat;
newBoneInfo[newBoneIndex] = boneInfo;
newBoneNames[newBoneIndex] = name;
std::memcpy(&newQuats[newBoneIndexMinusRoot * 4], quat, ARRAYSIZE(quat) * sizeof(uint16_t));
std::memcpy(&newTrans[newBoneIndexMinusRoot * 3], trans, ARRAYSIZE(trans) * sizeof(float));
}
// Copy after
if (lengthOfSecondPart > 0)
{
std::memcpy(&newBoneNames[atPosition + 1], &model->boneNames[atPosition], sizeof(uint16_t) * lengthOfSecondPart);
std::memcpy(&newMats[atPosition + 1], &model->baseMat[atPosition], sizeof(Game::DObjAnimMat) * lengthOfSecondPart);
std::memcpy(&newBoneInfo[atPosition + 1], &model->boneInfo[atPosition], sizeof(Game::XBoneInfo) * lengthOfSecondPart);
std::memcpy(&newQuats[(atPositionM1 + 1) * 4], &model->quats[atPositionM1 * 4], sizeof(uint16_t) * 4 * lengthOfSecondPartM1);
std::memcpy(&newTrans[(atPositionM1 + 1) * 3], &model->trans[atPositionM1 * 3], sizeof(float) * 3 * lengthOfSecondPartM1);
}
// Assign reallocated
model->baseMat = newMats;
model->boneInfo = newBoneInfo;
model->boneNames = newBoneNames;
model->quats = newQuats;
model->trans = newTrans;
model->parentList = newParentList;
model->numBones++;
// Update vertex weights
for (int i = 0; i < model->numLods; i++)
{
const auto lod = &model->lodInfo[i];
insertBoneInPartbits(atPosition, lod->partBits, false);
insertBoneInPartbits(atPosition, lod->modelSurfs->partBits, false);
for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++)
{
auto vertsBlendOffset = 0u;
const auto surface = &lod->modelSurfs->surfs[surfIndex];
//surface->partBits[0] = 0b00000000000000000000000000000000;
//surface->partBits[1] = 0b01010101010101010101010101010101;
//surface->partBits[2] = 0b00110011001100110011001100110011;
//surface->partBits[3] = 0b00011100011100011100011100011100;
//surface->partBits[4] = 0b00001111000011110000111100001111;
//surface->partBits[5] = 0b00000111110000011111000001111100;
insertBoneInPartbits(atPosition, surface->partBits, false);
{
const auto fixVertexBlendIndex = [&](unsigned int offset) {
int index = static_cast<int>(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat));
if (index >= atPosition)
{
if (index < 0 || index >= model->numBones - 1)
{
//assert(false);
}
index++;
surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat);
}
};
// Fix bone offsets
if (surface->vertList)
{
for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++)
{
const auto vertList = &surface->vertList[vertListIndex];
auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat);
if (index < 0 || index >= model->numBones - 1)
{
//assert(false);
}
if (index >= atPosition)
{
index++;
vertList->boneOffset = index * sizeof(Game::DObjSkelMat);
}
}
}
// 1 bone weight
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++)
{
fixVertexBlendIndex(vertsBlendOffset + 0);
vertsBlendOffset += 1;
}
// 2 bone weights
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++)
{
fixVertexBlendIndex(vertsBlendOffset + 0);
fixVertexBlendIndex(vertsBlendOffset + 1);
vertsBlendOffset += 3;
}
// 3 bone weights
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++)
{
fixVertexBlendIndex(vertsBlendOffset + 0);
fixVertexBlendIndex(vertsBlendOffset + 1);
fixVertexBlendIndex(vertsBlendOffset + 3);
vertsBlendOffset += 5;
}
// 4 bone weights
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++)
{
fixVertexBlendIndex(vertsBlendOffset + 0);
fixVertexBlendIndex(vertsBlendOffset + 1);
fixVertexBlendIndex(vertsBlendOffset + 3);
fixVertexBlendIndex(vertsBlendOffset + 5);
vertsBlendOffset += 7;
}
}
}
}
// TODO free memory of original lists
printf("");
setParentIndexOfBone(atPosition, parentIndex);
// Restore parents
for (const auto& kv : parentsToRestore)
{
// Fix parents
const auto key = kv.first;
const auto beforeVal = kv.second;
const auto parentIndex = getIndexOfBone(beforeVal);
const auto index = getIndexOfBone(key);
setParentIndexOfBone(index, parentIndex);
}
// check
for (const auto& kv : parentsToRestore)
{
const auto key = kv.first;
const auto beforeVal = kv.second;
const auto index = getIndexOfBone(key);
const auto afterVal = getParentOfBone(index);
if (beforeVal != afterVal)
{
printf("");
}
}
//
return atPosition; // Bone index of added bone
};
const auto transferWeights = [&](const uint8_t origin, const uint8_t destination)
{
return;
const auto originalWeights = model->baseMat[origin].transWeight;
model->baseMat[origin].transWeight = model->baseMat[destination].transWeight;
model->baseMat[destination].transWeight = originalWeights;
for (int i = 0; i < model->numLods; i++)
{
const auto lod = &model->lodInfo[i];
for (int surfIndex = 0; surfIndex < lod->modelSurfs->numsurfs; surfIndex++)
{
auto vertsBlendOffset = 0u;
const auto surface = &lod->modelSurfs->surfs[surfIndex];
{
const auto transferVertexBlendIndex = [&](unsigned int offset) {
int index = static_cast<int>(surface->vertInfo.vertsBlend[offset] / sizeof(Game::DObjSkelMat));
if (index == origin)
{
if (index < 0 || index >= model->numBones - 1)
{
assert(false);
}
index = destination;
surface->vertInfo.vertsBlend[offset] = index * sizeof(Game::DObjSkelMat);
}
};
// Fix bone offsets
if (surface->vertList)
{
for (auto vertListIndex = 0u; vertListIndex < surface->vertListCount; vertListIndex++)
{
const auto vertList = &surface->vertList[vertListIndex];
auto index = vertList->boneOffset / sizeof(Game::DObjSkelMat);
if (index < 0 || index >= model->numBones - 1)
{
assert(false);
}
if (index == origin)
{
index = destination;
vertList->boneOffset = index * sizeof(Game::DObjSkelMat);
}
}
}
// 1 bone weight
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[0]; vertIndex++)
{
transferVertexBlendIndex(vertsBlendOffset + 0);
vertsBlendOffset += 1;
}
// 2 bone weights
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[1]; vertIndex++)
{
transferVertexBlendIndex(vertsBlendOffset + 0);
transferVertexBlendIndex(vertsBlendOffset + 1);
vertsBlendOffset += 3;
}
// 3 bone weights
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[2]; vertIndex++)
{
transferVertexBlendIndex(vertsBlendOffset + 0);
transferVertexBlendIndex(vertsBlendOffset + 1);
transferVertexBlendIndex(vertsBlendOffset + 3);
vertsBlendOffset += 5;
}
// 4 bone weights
for (auto vertIndex = 0; vertIndex < surface->vertInfo.vertCount[3]; vertIndex++)
{
transferVertexBlendIndex(vertsBlendOffset + 0);
transferVertexBlendIndex(vertsBlendOffset + 1);
transferVertexBlendIndex(vertsBlendOffset + 3);
transferVertexBlendIndex(vertsBlendOffset + 5);
vertsBlendOffset += 7;
}
}
}
}
};
auto indexOfSpine = getIndexOfBone("j_spinelower");
if (indexOfSpine < UCHAR_MAX)
{
const auto nameOfParent = getParentOfBone(indexOfSpine);
if (getIndexOfBone("torso_stabilizer") == UCHAR_MAX)
{
// No stabilizer - let's do surgery
// We're trying to get there:
// tag_origin
// j_main_root
// pelvis
// j_hip_le
// j_hip_ri
// tag_stowed_hip_rear
// torso_stabilizer
// j_spinelower
// back_low
// j_spineupper
// back_mid
// j_spine4
const auto root = getIndexOfBone("j_mainroot");
if (root < UCHAR_MAX) {
#if true
// Add pelvis
const uint8_t indexOfPelvis = insertBone("pelvis", "j_mainroot");
transferWeights(root, indexOfPelvis);
setParentIndexOfBone(getIndexOfBone("j_hip_le"), indexOfPelvis);
setParentIndexOfBone(getIndexOfBone("j_hip_ri"), indexOfPelvis);
setParentIndexOfBone(getIndexOfBone("tag_stowed_hip_rear"), indexOfPelvis);
const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "pelvis");
setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer);
const uint8_t backLow = insertBone("back_low", "j_spinelower");
transferWeights(getIndexOfBone("j_spinelower"), backLow);
setParentIndexOfBone(getIndexOfBone("j_spineupper"), backLow);
const uint8_t backMid = insertBone("back_mid", "j_spineupper");
transferWeights(getIndexOfBone("j_spineupper"), backMid);
setParentIndexOfBone(getIndexOfBone("j_spine4"), backMid);
assert(root == getIndexOfBone("j_mainroot"));
assert(indexOfPelvis == getIndexOfBone("pelvis"));
assert(backLow == getIndexOfBone("back_low"));
assert(backMid == getIndexOfBone("back_mid"));
// Fix up torso stabilizer
model->baseMat[torsoStabilizer].quat[0] = 0.F;
model->baseMat[torsoStabilizer].quat[1] = 0.F;
model->baseMat[torsoStabilizer].quat[2] = 0.F;
model->baseMat[torsoStabilizer].quat[3] = 1.F;
const auto spineLowerM1 = getIndexOfBone("j_spinelower")-1;
//model->trans[(torsoStabilizer-model->numRootBones) * 3 + 0] = 2.0f;
//model->trans[(torsoStabilizer-model->numRootBones) * 3 + 1] = -5.0f;
//model->trans[(torsoStabilizer-model->numRootBones) * 3 + 2] = 2.0F;
model->trans[spineLowerM1 * 3 + 0] = 0.069828756;
model->trans[spineLowerM1 * 3 + 1] = -0.0f;
model->trans[spineLowerM1 * 3 + 2] = 5.2035017F;
//// Euler -180.000 88.572 -90.000
model->quats[(torsoStabilizer-model->numRootBones) * 4 + 0] = 16179; // 0.4952
model->quats[(torsoStabilizer-model->numRootBones) * 4 + 1] = 16586; // 0.5077
model->quats[(torsoStabilizer-model->numRootBones) * 4 + 2] = 16586; // 0.5077
model->quats[(torsoStabilizer-model->numRootBones) * 4 + 3] = 16178; // 0.4952
#else
const uint8_t torsoStabilizer = insertBone("torso_stabilizer", "j_mainroot");
transferWeights(getIndexOfBone("j_mainroot"), getIndexOfBone("torso_stabilizer"));
setParentIndexOfBone(getIndexOfBone("j_spinelower"), torsoStabilizer);
#endif
#if DEBUG
const auto newRoot = getIndexOfBone("j_mainroot");
assert(root == newRoot);
#endif
printf("");
}
}
printf("");
}
printf(""); printf("");
// //
names.clear(); names.clear();
for (auto i = 1; i < model->numBones; i++) for (auto i = 1; i < model->numBones; i++)
{ {
const auto bone = model->boneNames[i]; const auto bone = model->boneNames[i];
const auto name = Game::SL_ConvertToString(bone); const auto boneName = Game::SL_ConvertToString(bone);
const auto m1 = i-1; const auto m1 = i - 1;
const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})", const auto fmt = std::format("{} => q({} {} {} {}) t({} {} {})",
name, boneName,
model->quats[m1 * 4 + 0], model->quats[m1 * 4 + 0],
model->quats[m1* 4 + 1], model->quats[m1 * 4 + 1],
model->quats[m1 * 4 + 2], model->quats[m1 * 4 + 2],
model->quats[m1 * 4 + 3], model->quats[m1 * 4 + 3],
model->trans[m1 * 3 + 0], model->trans[m1 * 3 + 0],
model->trans[m1* 3 +1], model->trans[m1 * 3 + 1],
model->trans[m1 * 3 +2] model->trans[m1 * 3 + 2]
); );
names.push_back( names.push_back(
fmt fmt
); );
printf(""); printf("");
} }
@ -911,6 +444,7 @@ namespace Components
bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset) bool AssetHandler::IsAssetEligible(Game::XAssetType type, Game::XAssetHeader* asset)
{ {
const char* name = Game::DB_GetXAssetNameHandlers[type](asset); const char* name = Game::DB_GetXAssetNameHandlers[type](asset);
if (!name) return false; if (!name) return false;
for (auto i = AssetHandler::EmptyAssets.begin(); i != AssetHandler::EmptyAssets.end();) for (auto i = AssetHandler::EmptyAssets.begin(); i != AssetHandler::EmptyAssets.end();)