metaforce/DataSpec/DNAMP2/ANIM.cpp

627 lines
19 KiB
C++
Raw Normal View History

#include "ANIM.hpp"
2017-12-29 08:08:12 +00:00
#include "hecl/Blender/Connection.hpp"
2017-12-29 08:08:12 +00:00
namespace DataSpec::DNAMP2
{
2017-12-29 08:08:12 +00:00
using ANIMOutStream = hecl::blender::ANIMOutStream;
2017-12-29 08:08:12 +00:00
void ANIM::IANIM::sendANIMToBlender(hecl::blender::PyOutStream& os, const DNAANIM::RigInverter<CINF>& rig) const
{
2016-09-06 05:52:51 +00:00
os.format("act.hecl_fps = round(%f)\n"
"act.hecl_looping = %s\n",
(1.0f / mainInterval), looping ? "True" : "False");
auto kit = chanKeys.begin();
2016-04-09 07:10:09 +00:00
std::vector<zeus::CQuaternion> fixedRotKeys;
std::vector<zeus::CVector3f> fixedTransKeys;
2015-08-15 04:12:15 +00:00
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
2016-04-07 03:40:25 +00:00
const std::string* bName = rig.getCINF().getBoneNameFromId(bone.first);
if (!bName)
continue;
2016-04-07 03:40:25 +00:00
os.format("bone_string = '%s'\n", bName->c_str());
os << "action_group = act.groups.new(bone_string)\n"
2015-08-15 04:12:15 +00:00
"\n";
if (std::get<0>(bone.second))
os << "rotCurves = []\n"
"rotCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].rotation_quaternion', index=0, action_group=bone_string))\n"
"rotCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].rotation_quaternion', index=1, action_group=bone_string))\n"
"rotCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].rotation_quaternion', index=2, action_group=bone_string))\n"
"rotCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].rotation_quaternion', index=3, action_group=bone_string))\n"
"\n";
2015-08-15 04:12:15 +00:00
if (std::get<1>(bone.second))
2016-04-08 23:11:26 +00:00
os << "transCurves = []\n"
"transCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].location', index=0, action_group=bone_string))\n"
"transCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].location', index=1, action_group=bone_string))\n"
"transCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].location', index=2, action_group=bone_string))\n"
"\n";
2015-08-15 04:12:15 +00:00
if (std::get<2>(bone.second))
os << "scaleCurves = []\n"
"scaleCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].scale', index=0, action_group=bone_string))\n"
"scaleCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].scale', index=1, action_group=bone_string))\n"
"scaleCurves.append(act.fcurves.new('pose.bones[\"'+bone_string+'\"].scale', index=2, action_group=bone_string))\n"
"\n";
ANIMOutStream ao = os.beginANIMCurve();
2015-08-15 04:12:15 +00:00
if (std::get<0>(bone.second))
{
2015-08-15 04:12:15 +00:00
const std::vector<DNAANIM::Value>& rotKeys = *kit++;
2016-04-09 07:10:09 +00:00
fixedRotKeys.clear();
fixedRotKeys.resize(rotKeys.size());
for (int c=0 ; c<4 ; ++c)
{
size_t idx = 0;
for (const DNAANIM::Value& val : rotKeys)
fixedRotKeys[idx++][c] = val.v4.vec[c];
}
for (zeus::CQuaternion& rot : fixedRotKeys)
rot = rig.invertRotation(bone.first, rot);
for (int c=0 ; c<4 ; ++c)
2015-08-15 04:12:15 +00:00
{
auto frameit = frames.begin();
2015-11-21 01:16:07 +00:00
ao.changeCurve(ANIMOutStream::CurveType::Rotate, c, rotKeys.size());
2016-04-09 07:10:09 +00:00
for (const zeus::CQuaternion& val : fixedRotKeys)
ao.write(*frameit++, val[c]);
2015-08-15 04:12:15 +00:00
}
}
2015-08-15 04:12:15 +00:00
if (std::get<1>(bone.second))
{
const std::vector<DNAANIM::Value>& transKeys = *kit++;
2016-04-09 07:10:09 +00:00
fixedTransKeys.clear();
fixedTransKeys.resize(transKeys.size());
for (int c=0 ; c<3 ; ++c)
{
2016-04-09 07:10:09 +00:00
size_t idx = 0;
for (const DNAANIM::Value& val : transKeys)
2016-04-09 07:10:09 +00:00
fixedTransKeys[idx++][c] = val.v3.vec[c];
}
for (zeus::CVector3f& t : fixedTransKeys)
t = rig.invertPosition(bone.first, t, true);
for (int c=0 ; c<3 ; ++c)
{
auto frameit = frames.begin();
ao.changeCurve(ANIMOutStream::CurveType::Translate, c, fixedTransKeys.size());
for (const zeus::CVector3f& val : fixedTransKeys)
ao.write(*frameit++, val[c]);
2015-08-15 04:12:15 +00:00
}
}
if (std::get<2>(bone.second))
{
const std::vector<DNAANIM::Value>& scaleKeys = *kit++;
for (int c=0 ; c<3 ; ++c)
2015-08-15 04:12:15 +00:00
{
auto frameit = frames.begin();
2015-11-21 01:16:07 +00:00
ao.changeCurve(ANIMOutStream::CurveType::Scale, c, scaleKeys.size());
for (const DNAANIM::Value& val : scaleKeys)
ao.write(*frameit++, val.v3.vec[c]);
}
}
}
}
2018-02-22 07:24:51 +00:00
template <>
void ANIM::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
{
atUint32 version = reader.readUint32Big();
switch (version)
{
case 0:
m_anim.reset(new struct ANIM0);
m_anim->read(reader);
break;
case 2:
m_anim.reset(new struct ANIM2);
m_anim->read(reader);
break;
default:
Log.report(logvisor::Fatal, "unrecognized ANIM version");
break;
}
}
template <>
void ANIM::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
{
writer.writeUint32Big(m_anim->m_version);
m_anim->write(writer);
}
template <>
void ANIM::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
{
s += 4;
m_anim->binarySize(s);
}
const char* ANIM::ANIM0::DNAType() { return "ANIM0"; }
template <>
void ANIM::ANIM0::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
{
Header head;
head.read(reader);
mainInterval = head.interval;
frames.clear();
frames.reserve(head.keyCount);
for (size_t k=0 ; k<head.keyCount ; ++k)
frames.push_back(k);
std::map<atUint8, atUint32> boneMap;
for (size_t b=0 ; b<head.boneSlotCount ; ++b)
{
atUint8 idx = reader.readUByte();
if (idx == 0xff)
continue;
boneMap[idx] = b;
}
atUint32 boneCount = reader.readUint32Big();
bones.clear();
bones.reserve(boneCount);
for (size_t b=0 ; b<boneCount ; ++b)
{
2015-08-15 04:12:15 +00:00
bones.emplace_back(boneMap[b], std::make_tuple(false, false, false));
atUint8 idx = reader.readUByte();
if (idx != 0xff)
2015-08-15 04:12:15 +00:00
std::get<0>(bones.back().second) = true;
}
boneCount = reader.readUint32Big();
for (size_t b=0 ; b<boneCount ; ++b)
{
atUint8 idx = reader.readUByte();
if (idx != 0xff)
2015-09-27 03:05:43 +00:00
std::get<1>(bones[b].second) = true;
2015-08-15 04:12:15 +00:00
}
boneCount = reader.readUint32Big();
for (size_t b=0 ; b<boneCount ; ++b)
{
atUint8 idx = reader.readUByte();
if (idx != 0xff)
2015-09-27 03:05:43 +00:00
std::get<2>(bones[b].second) = true;
2015-08-15 04:12:15 +00:00
}
channels.clear();
chanKeys.clear();
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
if (std::get<0>(bone.second))
{
channels.emplace_back();
DNAANIM::Channel& chan = channels.back();
2015-11-21 01:16:07 +00:00
chan.type = DNAANIM::Channel::Type::Rotation;
2015-08-15 04:12:15 +00:00
chanKeys.emplace_back();
}
if (std::get<1>(bone.second))
{
channels.emplace_back();
DNAANIM::Channel& chan = channels.back();
2015-11-21 01:16:07 +00:00
chan.type = DNAANIM::Channel::Type::Translation;
2015-08-15 04:12:15 +00:00
chanKeys.emplace_back();
}
if (std::get<2>(bone.second))
{
channels.emplace_back();
DNAANIM::Channel& chan = channels.back();
2015-11-21 01:16:07 +00:00
chan.type = DNAANIM::Channel::Type::Scale;
2015-08-15 04:12:15 +00:00
chanKeys.emplace_back();
}
}
reader.readUint32Big();
2015-08-15 04:12:15 +00:00
auto kit = chanKeys.begin();
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
2015-08-15 04:12:15 +00:00
if (std::get<0>(bone.second))
++kit;
if (std::get<1>(bone.second))
++kit;
if (std::get<2>(bone.second))
{
std::vector<DNAANIM::Value>& keys = *kit++;
for (size_t k=0 ; k<head.keyCount ; ++k)
keys.emplace_back(reader.readVec3fBig());
}
}
2015-08-15 04:12:15 +00:00
reader.readUint32Big();
kit = chanKeys.begin();
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
if (std::get<0>(bone.second))
{
2015-09-01 21:46:40 +00:00
std::vector<DNAANIM::Value>& keys = *kit++;
2015-08-15 04:12:15 +00:00
for (size_t k=0 ; k<head.keyCount ; ++k)
keys.emplace_back(reader.readVec4fBig());
}
if (std::get<1>(bone.second))
++kit;
if (std::get<2>(bone.second))
++kit;
}
reader.readUint32Big();
2015-08-15 04:12:15 +00:00
kit = chanKeys.begin();
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
2015-08-15 04:12:15 +00:00
if (std::get<0>(bone.second))
++kit;
if (std::get<1>(bone.second))
{
std::vector<DNAANIM::Value>& keys = *kit++;
for (size_t k=0 ; k<head.keyCount ; ++k)
keys.emplace_back(reader.readVec3fBig());
}
2015-08-15 04:12:15 +00:00
if (std::get<2>(bone.second))
++kit;
}
}
2018-02-22 07:24:51 +00:00
template <>
void ANIM::ANIM0::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
{
Header head;
head.unk0 = 0;
head.unk1 = 0;
head.unk2 = 0;
head.keyCount = frames.size();
head.duration = head.keyCount * mainInterval;
head.interval = mainInterval;
atUint32 maxId = 0;
2015-08-15 04:12:15 +00:00
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
2015-09-26 03:12:08 +00:00
maxId = std::max(maxId, bone.first);
head.boneSlotCount = maxId + 1;
head.write(writer);
for (size_t s=0 ; s<head.boneSlotCount ; ++s)
{
size_t boneIdx = 0;
bool found = false;
2015-08-15 04:12:15 +00:00
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
if (s == bone.first)
{
writer.writeUByte(boneIdx);
found = true;
break;
}
++boneIdx;
}
if (!found)
writer.writeUByte(0xff);
}
writer.writeUint32Big(bones.size());
size_t boneIdx = 0;
2015-08-15 04:12:15 +00:00
size_t rotKeyCount = 0;
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
2015-08-15 04:12:15 +00:00
if (std::get<0>(bone.second))
{
writer.writeUByte(boneIdx);
2015-08-15 04:12:15 +00:00
++rotKeyCount;
}
else
writer.writeUByte(0xff);
++boneIdx;
}
2015-08-15 04:12:15 +00:00
writer.writeUint32Big(bones.size());
boneIdx = 0;
size_t transKeyCount = 0;
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
if (std::get<1>(bone.second))
{
writer.writeUByte(boneIdx);
++transKeyCount;
}
else
writer.writeUByte(0xff);
++boneIdx;
}
writer.writeUint32Big(bones.size());
boneIdx = 0;
size_t scaleKeyCount = 0;
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
2015-08-15 04:12:15 +00:00
if (std::get<2>(bone.second))
{
2015-08-15 04:12:15 +00:00
writer.writeUByte(boneIdx);
++scaleKeyCount;
}
else
writer.writeUByte(0xff);
++boneIdx;
}
writer.writeUint32Big(scaleKeyCount * head.keyCount);
auto cit = chanKeys.begin();
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
if (std::get<0>(bone.second))
++cit;
2015-08-15 04:12:15 +00:00
if (std::get<1>(bone.second))
++cit;
if (std::get<2>(bone.second))
{
const std::vector<DNAANIM::Value>& keys = *cit++;
auto kit = keys.begin();
for (size_t k=0 ; k<head.keyCount ; ++k)
writer.writeVec3fBig((*kit++).v3);
}
}
2015-08-15 04:12:15 +00:00
writer.writeUint32Big(rotKeyCount * head.keyCount);
cit = chanKeys.begin();
2015-08-15 04:12:15 +00:00
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
2015-08-15 04:12:15 +00:00
if (std::get<0>(bone.second))
{
const std::vector<DNAANIM::Value>& keys = *cit++;
auto kit = keys.begin();
for (size_t k=0 ; k<head.keyCount ; ++k)
2015-08-15 04:12:15 +00:00
writer.writeVec4fBig((*kit++).v4);
}
2015-08-15 04:12:15 +00:00
if (std::get<1>(bone.second))
++cit;
if (std::get<2>(bone.second))
++cit;
}
2015-08-15 04:12:15 +00:00
writer.writeUint32Big(transKeyCount * head.keyCount);
cit = chanKeys.begin();
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
if (std::get<0>(bone.second))
++cit;
if (std::get<1>(bone.second))
{
const std::vector<DNAANIM::Value>& keys = *cit++;
auto kit = keys.begin();
for (size_t k=0 ; k<head.keyCount ; ++k)
writer.writeVec3fBig((*kit++).v3);
}
if (std::get<2>(bone.second))
++cit;
}
}
2018-02-22 07:24:51 +00:00
template <>
void ANIM::ANIM0::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
{
Header head;
atUint32 maxId = 0;
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
maxId = std::max(maxId, bone.first);
2018-02-22 07:24:51 +00:00
head.binarySize(s);
s += maxId + 1;
s += bones.size() * 3 + 12;
2018-02-22 07:24:51 +00:00
s += 12;
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
if (std::get<0>(bone.second))
2018-02-22 07:24:51 +00:00
s += head.keyCount * 16;
if (std::get<1>(bone.second))
2018-02-22 07:24:51 +00:00
s += head.keyCount * 12;
if (std::get<2>(bone.second))
2018-02-22 07:24:51 +00:00
s += head.keyCount * 12;
}
}
2018-02-22 07:24:51 +00:00
const char* ANIM::ANIM2::DNAType() { return "ANIM2"; }
template <>
void ANIM::ANIM2::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
{
Header head;
head.read(reader);
mainInterval = head.interval;
2016-09-06 05:52:51 +00:00
looping = bool(head.looping);
WordBitmap keyBmp;
keyBmp.read(reader, head.keyBitmapBitCount);
frames.clear();
atUint32 frameAccum = 0;
for (bool bit : keyBmp)
{
if (bit)
frames.push_back(frameAccum);
++frameAccum;
}
2015-08-15 04:12:15 +00:00
reader.seek(4);
bones.clear();
bones.reserve(head.boneChannelCount);
channels.clear();
channels.reserve(head.boneChannelCount);
2015-09-26 03:12:08 +00:00
atUint16 keyframeCount = 0;
for (size_t b=0 ; b<head.boneChannelCount ; ++b)
{
ChannelDesc desc;
desc.read(reader);
2015-08-31 03:44:42 +00:00
bones.emplace_back(desc.id, std::make_tuple(desc.keyCount1 != 0, desc.keyCount2 != 0, desc.keyCount3 != 0));
if (desc.keyCount1)
{
channels.emplace_back();
DNAANIM::Channel& chan = channels.back();
2015-11-21 01:16:07 +00:00
chan.type = DNAANIM::Channel::Type::Rotation;
chan.i[0] = desc.initRX;
chan.q[0] = desc.qRX;
chan.i[1] = desc.initRY;
chan.q[1] = desc.qRY;
chan.i[2] = desc.initRZ;
chan.q[2] = desc.qRZ;
}
2015-09-26 03:12:08 +00:00
keyframeCount = std::max(keyframeCount, desc.keyCount1);
if (desc.keyCount2)
{
channels.emplace_back();
DNAANIM::Channel& chan = channels.back();
2015-11-21 01:16:07 +00:00
chan.type = DNAANIM::Channel::Type::Translation;
chan.i[0] = desc.initTX;
chan.q[0] = desc.qTX;
chan.i[1] = desc.initTY;
chan.q[1] = desc.qTY;
chan.i[2] = desc.initTZ;
chan.q[2] = desc.qTZ;
}
2015-09-26 03:12:08 +00:00
keyframeCount = std::max(keyframeCount, desc.keyCount2);
2015-08-15 04:12:15 +00:00
if (desc.keyCount3)
{
channels.emplace_back();
DNAANIM::Channel& chan = channels.back();
2015-11-21 01:16:07 +00:00
chan.type = DNAANIM::Channel::Type::Scale;
2015-08-15 04:12:15 +00:00
chan.i[0] = desc.initSX;
chan.q[0] = desc.qSX;
chan.i[1] = desc.initSY;
chan.q[1] = desc.qSY;
chan.i[2] = desc.initSZ;
chan.q[2] = desc.qSZ;
}
2015-09-26 03:12:08 +00:00
keyframeCount = std::max(keyframeCount, desc.keyCount3);
}
size_t bsSize = DNAANIM::ComputeBitstreamSize(keyframeCount, channels);
std::unique_ptr<atUint8[]> bsData = reader.readUBytes(bsSize);
DNAANIM::BitstreamReader bsReader;
chanKeys = bsReader.read(bsData.get(), keyframeCount, channels, head.rotDiv, head.translationMult, head.scaleMult);
}
2018-02-22 07:24:51 +00:00
template <>
void ANIM::ANIM2::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
{
/* TODO: conform to MP1 ANIM3 */
Header head;
2015-08-15 04:12:15 +00:00
head.unk1 = 1;
2016-09-06 05:52:51 +00:00
head.looping = looping;
head.interval = mainInterval;
head.rootBoneId = 0;
head.scaleMult = 0.f;
WordBitmap keyBmp;
size_t frameCount = 0;
for (atUint32 frame : frames)
{
while (keyBmp.getBit(frame))
++frame;
keyBmp.setBit(frame);
frameCount = frame + 1;
}
head.keyBitmapBitCount = keyBmp.getBitCount();
head.duration = frameCount * mainInterval;
head.boneChannelCount = bones.size();
size_t keyframeCount = frames.size();
std::vector<DNAANIM::Channel> qChannels = channels;
DNAANIM::BitstreamWriter bsWriter;
size_t bsSize;
std::unique_ptr<atUint8[]> bsData = bsWriter.write(chanKeys, keyframeCount, qChannels,
m_version == 3 ? 0x7fffff : 0x7fff,
head.rotDiv, head.translationMult, head.scaleMult, bsSize);
/* TODO: Figure out proper scratch size computation */
head.scratchSize = keyframeCount * channels.size() * 16;
head.write(writer);
keyBmp.write(writer);
writer.writeUint32Big(head.boneChannelCount);
auto cit = qChannels.begin();
2015-08-15 04:12:15 +00:00
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
ChannelDesc desc;
2015-08-15 04:12:15 +00:00
if (std::get<0>(bone.second))
{
DNAANIM::Channel& chan = *cit++;
desc.keyCount1 = keyframeCount;
desc.initRX = chan.i[0];
desc.qRX = chan.q[0];
desc.initRY = chan.i[1];
desc.qRY = chan.q[1];
desc.initRZ = chan.i[2];
desc.qRZ = chan.q[2];
}
if (std::get<1>(bone.second))
{
DNAANIM::Channel& chan = *cit++;
desc.keyCount2 = keyframeCount;
desc.initTX = chan.i[0];
desc.qTX = chan.q[0];
desc.initTY = chan.i[1];
desc.qTY = chan.q[1];
desc.initTZ = chan.i[2];
desc.qTZ = chan.q[2];
}
2015-08-15 04:12:15 +00:00
if (std::get<2>(bone.second))
{
DNAANIM::Channel& chan = *cit++;
desc.keyCount3 = keyframeCount;
desc.initSX = chan.i[0];
desc.qSX = chan.q[0];
desc.initSY = chan.i[1];
desc.qSY = chan.q[1];
desc.initSZ = chan.i[2];
desc.qSZ = chan.q[2];
}
}
writer.writeUBytes(bsData.get(), bsSize);
}
2018-02-22 07:24:51 +00:00
template <>
void ANIM::ANIM2::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
{
Header head;
WordBitmap keyBmp;
for (atUint32 frame : frames)
{
while (keyBmp.getBit(frame))
++frame;
keyBmp.setBit(frame);
}
2018-02-22 07:24:51 +00:00
head.binarySize(s);
keyBmp.binarySize(s);
s += 4;
for (const std::pair<atUint32, std::tuple<bool,bool,bool>>& bone : bones)
{
2018-02-22 07:24:51 +00:00
s += 7;
if (std::get<0>(bone.second))
2018-02-22 07:24:51 +00:00
s += 9;
if (std::get<1>(bone.second))
2018-02-22 07:24:51 +00:00
s += 9;
if (std::get<2>(bone.second))
2018-02-22 07:24:51 +00:00
s += 9;
}
2018-02-22 07:24:51 +00:00
s += DNAANIM::ComputeBitstreamSize(frames.size(), channels);
}
}