MP2 PATH support

This commit is contained in:
Jack Andersen 2020-04-08 19:19:50 -10:00
parent 5b0a4b5f60
commit df229671c4
12 changed files with 181 additions and 115 deletions

View File

@ -272,10 +272,10 @@ void AROTBuilder::Node::pathCountNodesAndLookups(size_t& nodeCount, size_t& look
}
}
void AROTBuilder::Node::pathWrite(DNAPATH::PATH& path, const zeus::CAABox& curAABB) {
template <uint32_t PathVer>
void AROTBuilder::Node::pathWrite(DNAPATH::PATH<PathVer>& path, const zeus::CAABox& curAABB) {
if (childNodes.empty()) {
path.octree.emplace_back();
DNAPATH::PATH::OctreeNode& n = path.octree.back();
auto& n = path.octree.emplace_back();
n.isLeaf = 1;
n.aabb[0] = curAABB.min;
n.aabb[1] = curAABB.max;
@ -294,8 +294,7 @@ void AROTBuilder::Node::pathWrite(DNAPATH::PATH& path, const zeus::CAABox& curAA
children[i] = path.octree.size() - 1;
}
path.octree.emplace_back();
DNAMP1::PATH::OctreeNode& n = path.octree.back();
auto& n = path.octree.emplace_back();
n.isLeaf = 0;
n.aabb[0] = curAABB.min;
n.aabb[1] = curAABB.max;
@ -307,6 +306,10 @@ void AROTBuilder::Node::pathWrite(DNAPATH::PATH& path, const zeus::CAABox& curAA
}
}
template void AROTBuilder::Node::pathWrite<4>(DNAPATH::PATH<4>& path, const zeus::CAABox& curAABB);
template void AROTBuilder::Node::pathWrite<6>(DNAPATH::PATH<6>& path, const zeus::CAABox& curAABB);
template void AROTBuilder::Node::pathWrite<7>(DNAPATH::PATH<7>& path, const zeus::CAABox& curAABB);
void AROTBuilder::build(std::vector<std::vector<uint8_t>>& secs, const zeus::CAABox& fullAabb,
const std::vector<zeus::CAABox>& meshAabbs, const std::vector<DNACMDL::Mesh>& meshes) {
/* Recursively split */
@ -377,8 +380,7 @@ std::pair<std::unique_ptr<uint8_t[]>, uint32_t> AROTBuilder::buildCol(const ColM
std::vector<zeus::CAABox> triBoxes;
triBoxes.reserve(mesh.trianges.size());
for (const ColMesh::Triangle& tri : mesh.trianges) {
triBoxes.emplace_back();
zeus::CAABox& aabb = triBoxes.back();
zeus::CAABox& aabb = triBoxes.emplace_back();
for (int e = 0; e < 3; ++e) {
const ColMesh::Edge& edge = mesh.edges[tri.edges[e]];
for (int v = 0; v < 2; ++v) {
@ -401,15 +403,14 @@ std::pair<std::unique_ptr<uint8_t[]>, uint32_t> AROTBuilder::buildCol(const ColM
return {std::move(ret), totalSize};
}
void AROTBuilder::buildPath(DNAPATH::PATH& path) {
template <uint32_t PathVer>
void AROTBuilder::buildPath(DNAPATH::PATH<PathVer>& path) {
/* Accumulate total AABB and gather region boxes */
std::vector<zeus::CAABox> regionBoxes;
regionBoxes.reserve(path.regions.size());
zeus::CAABox fullAABB;
for (const DNAPATH::PATH::Region& r : path.regions) {
regionBoxes.emplace_back(r.aabb[0], r.aabb[1]);
fullAABB.accumulateBounds(regionBoxes.back());
}
for (const auto& r : path.regions)
fullAABB.accumulateBounds(regionBoxes.emplace_back(r.aabb[0], r.aabb[1]));
/* Recursively split */
BspNodeType dontCare;
@ -426,4 +427,8 @@ void AROTBuilder::buildPath(DNAPATH::PATH& path) {
rootNode.pathWrite(path, fullAABB);
}
template void AROTBuilder::buildPath<4>(DNAPATH::PATH<4>& path);
template void AROTBuilder::buildPath<6>(DNAPATH::PATH<6>& path);
template void AROTBuilder::buildPath<7>(DNAPATH::PATH<7>& path);
} // namespace DataSpec

View File

@ -8,6 +8,7 @@
namespace DataSpec {
namespace DNAPATH {
template <atUint32 PathVer>
struct PATH;
}
@ -43,13 +44,15 @@ struct AROTBuilder {
void writeColNodes(uint8_t*& ptr, const zeus::CAABox& curAABB);
void pathCountNodesAndLookups(size_t& nodeCount, size_t& lookupCount);
void pathWrite(DNAPATH::PATH& path, const zeus::CAABox& curAABB);
template <uint32_t PathVer>
void pathWrite(DNAPATH::PATH<PathVer>& path, const zeus::CAABox& curAABB);
} rootNode;
void build(std::vector<std::vector<uint8_t>>& secs, const zeus::CAABox& fullAabb,
const std::vector<zeus::CAABox>& meshAabbs, const std::vector<DNACMDL::Mesh>& meshes);
std::pair<std::unique_ptr<uint8_t[]>, uint32_t> buildCol(const ColMesh& mesh, BspNodeType& rootOut);
void buildPath(DNAPATH::PATH& path);
template <uint32_t PathVer>
void buildPath(DNAPATH::PATH<PathVer>& path);
};
} // namespace DataSpec

View File

@ -13,71 +13,70 @@ static void OutputOctreeNode(hecl::blender::PyOutStream& os, int idx, const zeus
const zeus::CVector3f pos = aabb.center();
const zeus::CVector3f extent = aabb.extents();
os.format(
"obj = bpy.data.objects.new('Leaf_%d', None)\n"
"bpy.context.scene.collection.objects.link(obj)\n"
"obj.location = (%f,%f,%f)\n"
"obj.scale = (%f,%f,%f)\n"
"obj.empty_display_type = 'CUBE'\n"
"obj.layers[1] = True\n"
"obj.layers[0] = False\n", idx,
pos.x(), pos.y(), pos.z(), extent.x(), extent.y(), extent.z());
"obj = bpy.data.objects.new('Leaf_%d', None)\n"
"bpy.context.scene.collection.objects.link(obj)\n"
"obj.location = (%f,%f,%f)\n"
"obj.scale = (%f,%f,%f)\n"
"obj.empty_display_type = 'CUBE'\n"
"obj.layers[1] = True\n"
"obj.layers[0] = False\n",
idx, pos.x(), pos.y(), pos.z(), extent.x(), extent.y(), extent.z());
}
#endif
void PATH::sendToBlender(hecl::blender::Connection& conn, std::string_view entryName, const zeus::CMatrix4f* xf,
const std::string& areaPath) {
template <atUint32 Ver>
void PATH<Ver>::sendToBlender(hecl::blender::Connection& conn, std::string_view entryName, const zeus::CMatrix4f* xf,
const std::string& areaPath) {
/* Open Py Stream and read sections */
hecl::blender::PyOutStream os = conn.beginPythonOut(true);
os <<
"import bpy\n"
"import bmesh\n"
"from mathutils import Vector, Matrix\n"
"\n"
"bpy.types.Material.retro_path_idx_mask = bpy.props.IntProperty(name='Retro: Path Index Mask')\n"
"bpy.types.Material.retro_path_type_mask = bpy.props.IntProperty(name='Retro: Path Type Mask')\n"
"\n"
"material_dict = {}\n"
"material_index = []\n"
"def make_ground_material(idxMask):\n"
" mat = bpy.data.materials.new('Ground %X' % idxMask)\n"
" mat.diffuse_color = (0.8, 0.460, 0.194, 1.0)\n"
" return mat\n"
"def make_flyer_material(idxMask):\n"
" mat = bpy.data.materials.new('Flyer %X' % idxMask)\n"
" mat.diffuse_color = (0.016, 0.8, 0.8, 1.0)\n"
" return mat\n"
"def make_swimmer_material(idxMask):\n"
" mat = bpy.data.materials.new('Swimmer %X' % idxMask)\n"
" mat.diffuse_color = (0.074, 0.293, 0.8, 1.0)\n"
" return mat\n"
"def select_material(meshIdxMask, meshTypeMask):\n"
" key = (meshIdxMask, meshTypeMask)\n"
" if key in material_index:\n"
" return material_index.index(key)\n"
" elif key in material_dict:\n"
" material_index.append(key)\n"
" return len(material_index)-1\n"
" else:\n"
" if meshTypeMask == 0x2:\n"
" mat = make_flyer_material(meshIdxMask)\n"
" elif meshTypeMask == 0x4:\n"
" mat = make_swimmer_material(meshIdxMask)\n"
" else:\n"
" mat = make_ground_material(meshIdxMask)\n"
" mat.retro_path_idx_mask = meshIdxMask\n"
" mat.retro_path_type_mask = meshTypeMask\n"
" material_dict[key] = mat\n"
" material_index.append(key)\n"
" return len(material_index)-1\n"
"\n";
os << "import bpy\n"
"import bmesh\n"
"from mathutils import Vector, Matrix\n"
"\n"
"bpy.types.Material.retro_path_idx_mask = bpy.props.IntProperty(name='Retro: Path Index Mask')\n"
"bpy.types.Material.retro_path_type_mask = bpy.props.IntProperty(name='Retro: Path Type Mask')\n"
"\n"
"material_dict = {}\n"
"material_index = []\n"
"def make_ground_material(idxMask):\n"
" mat = bpy.data.materials.new('Ground %X' % idxMask)\n"
" mat.diffuse_color = (0.8, 0.460, 0.194, 1.0)\n"
" return mat\n"
"def make_flyer_material(idxMask):\n"
" mat = bpy.data.materials.new('Flyer %X' % idxMask)\n"
" mat.diffuse_color = (0.016, 0.8, 0.8, 1.0)\n"
" return mat\n"
"def make_swimmer_material(idxMask):\n"
" mat = bpy.data.materials.new('Swimmer %X' % idxMask)\n"
" mat.diffuse_color = (0.074, 0.293, 0.8, 1.0)\n"
" return mat\n"
"def select_material(meshIdxMask, meshTypeMask):\n"
" key = (meshIdxMask, meshTypeMask)\n"
" if key in material_index:\n"
" return material_index.index(key)\n"
" elif key in material_dict:\n"
" material_index.append(key)\n"
" return len(material_index)-1\n"
" else:\n"
" if meshTypeMask == 0x2:\n"
" mat = make_flyer_material(meshIdxMask)\n"
" elif meshTypeMask == 0x4:\n"
" mat = make_swimmer_material(meshIdxMask)\n"
" else:\n"
" mat = make_ground_material(meshIdxMask)\n"
" mat.retro_path_idx_mask = meshIdxMask\n"
" mat.retro_path_type_mask = meshTypeMask\n"
" material_dict[key] = mat\n"
" material_index.append(key)\n"
" return len(material_index)-1\n"
"\n";
os.format(fmt("bpy.context.scene.name = '{}'\n"), entryName);
os <<
"# Clear Scene\n"
"if len(bpy.data.collections):\n"
" bpy.data.collections.remove(bpy.data.collections[0])\n"
"\n"
"bm = bmesh.new()\n"
"height_lay = bm.faces.layers.float.new('Height')\n";
os << "# Clear Scene\n"
"if len(bpy.data.collections):\n"
" bpy.data.collections.remove(bpy.data.collections[0])\n"
"\n"
"bm = bmesh.new()\n"
"height_lay = bm.faces.layers.float.new('Height')\n";
for (const Node& n : nodes) {
zeus::simd_floats f(n.position.simd);
@ -91,16 +90,15 @@ void PATH::sendToBlender(hecl::blender::Connection& conn, std::string_view entry
for (atUint32 i = 0; i < r.nodeCount; ++i)
os.format(fmt("tri_verts.append(bm.verts[{}])\n"), r.nodeStart + i);
os.format(fmt(
"face = bm.faces.get(tri_verts)\n"
"if face is None:\n"
" face = bm.faces.new(tri_verts)\n"
" face.normal_flip()\n"
"face.material_index = select_material(0x{:04X}, 0x{:04X})\n"
"face.smooth = False\n"
"face[height_lay] = {}\n"
"\n"),
r.meshIndexMask, r.meshTypeMask, r.height);
os.format(fmt("face = bm.faces.get(tri_verts)\n"
"if face is None:\n"
" face = bm.faces.new(tri_verts)\n"
" face.normal_flip()\n"
"face.material_index = select_material(0x{:04X}, 0x{:04X})\n"
"face.smooth = False\n"
"face[height_lay] = {}\n"
"\n"),
r.meshIndexMask, r.meshTypeMask, r.height);
#if 0
const zeus::CVector3f center = xf->multiplyOneOverW(r.centroid);
@ -152,15 +150,14 @@ void PATH::sendToBlender(hecl::blender::Connection& conn, std::string_view entry
zeus::simd_floats xfMtxF[4];
for (int i = 0; i < 4; ++i)
w.m[i].mSimd.copy_to(xfMtxF[i]);
os.format(fmt(
"mtx = Matrix((({},{},{},{}),({},{},{},{}),({},{},{},{}),(0.0,0.0,0.0,1.0)))\n"
"mtxd = mtx.decompose()\n"
"path_mesh_obj.rotation_mode = 'QUATERNION'\n"
"path_mesh_obj.location = mtxd[0]\n"
"path_mesh_obj.rotation_quaternion = mtxd[1]\n"
"path_mesh_obj.scale = mtxd[2]\n"),
xfMtxF[0][0], xfMtxF[1][0], xfMtxF[2][0], xfMtxF[3][0], xfMtxF[0][1], xfMtxF[1][1], xfMtxF[2][1], xfMtxF[3][1],
xfMtxF[0][2], xfMtxF[1][2], xfMtxF[2][2], xfMtxF[3][2]);
os.format(fmt("mtx = Matrix((({},{},{},{}),({},{},{},{}),({},{},{},{}),(0.0,0.0,0.0,1.0)))\n"
"mtxd = mtx.decompose()\n"
"path_mesh_obj.rotation_mode = 'QUATERNION'\n"
"path_mesh_obj.location = mtxd[0]\n"
"path_mesh_obj.rotation_quaternion = mtxd[1]\n"
"path_mesh_obj.scale = mtxd[2]\n"),
xfMtxF[0][0], xfMtxF[1][0], xfMtxF[2][0], xfMtxF[3][0], xfMtxF[0][1], xfMtxF[1][1], xfMtxF[2][1],
xfMtxF[3][1], xfMtxF[0][2], xfMtxF[1][2], xfMtxF[2][2], xfMtxF[3][2]);
}
#if DUMP_OCTREE
@ -179,8 +176,9 @@ void PATH::sendToBlender(hecl::blender::Connection& conn, std::string_view entry
os.close();
}
bool PATH::Cook(const hecl::ProjectPath& outPath, const hecl::ProjectPath& inPath,
const PathMesh& mesh, hecl::blender::Token& btok) {
template <atUint32 Ver>
bool PATH<Ver>::Cook(const hecl::ProjectPath& outPath, const hecl::ProjectPath& inPath, const PathMesh& mesh,
hecl::blender::Token& btok) {
athena::io::MemoryReader r(mesh.data.data(), mesh.data.size());
PATH path;
path.read(r);
@ -219,4 +217,8 @@ bool PATH::Cook(const hecl::ProjectPath& outPath, const hecl::ProjectPath& inPat
return true;
}
} // namespace DataSpec::DNAMP1
template struct PATH<4>;
template struct PATH<6>;
template struct PATH<7>;
} // namespace DataSpec::DNAPATH

View File

@ -4,7 +4,32 @@
#include "DataSpec/DNACommon/PAK.hpp"
namespace DataSpec::DNAPATH {
struct PATH : BigDNA {
template <atUint32 Ver>
struct RegionPointers {};
template <>
struct RegionPointers<4> : BigDNA {
AT_DECL_DNA
Value<atUint32> regionIdxPtr;
};
template <>
struct RegionPointers<6> : BigDNA {
AT_DECL_DNA
Value<atUint32> unk0;
Value<atUint32> unk1;
Value<atUint32> unk2;
Value<atUint32> regionIdxPtr;
};
template <>
struct RegionPointers<7> : BigDNA {
AT_DECL_DNA
Value<atUint32> unk0;
Value<atUint32> unk1;
Value<atUint32> unk2;
Value<atUint32> regionIdxPtr;
};
template <atUint32 Ver>
struct AT_SPECIALIZE_PARMS(4, 6, 7) PATH : BigDNA {
using PathMesh = hecl::blender::PathMesh;
AT_DECL_DNA
@ -41,7 +66,7 @@ struct PATH : BigDNA {
Value<atUint32> regionIdx;
Value<atVec3f> centroid;
Value<atVec3f> aabb[2];
Value<atUint32> regionIdxPtr;
Value<RegionPointers<Ver>> pointers;
};
Value<atUint32> regionCount;
Vector<Region, AT_DNA_COUNT(regionCount)> regions;

View File

@ -3,7 +3,7 @@
#include "DataSpec/DNAMP1/DNAMP1.hpp"
namespace DataSpec::DNAMP1 {
struct PATH : DNAPATH::PATH {
struct PATH : DNAPATH::PATH<4> {
static bool Extract(const SpecBase& dataSpec, PAKEntryReadStream& rs, const hecl::ProjectPath& outPath,
PAKRouter<PAKBridge>& pakRouter, const PAK::Entry& entry, bool force, hecl::blender::Token& btok,
std::function<void(const hecl::SystemChar*)> fileChanged);

View File

@ -203,7 +203,6 @@ void PAKBridge::addMAPATransforms(PAKRouter<PAKBridge>& pakRouter,
fmt::format(fmt(_SYS_STR("!name_{}.yaml")), mlvl.worldNameId));
for (const MLVL::Area& area : mlvl.areas) {
#if 0
{
/* Get PATH transform */
const nod::Node* areaNode;
@ -214,7 +213,6 @@ void PAKBridge::addMAPATransforms(PAKRouter<PAKBridge>& pakRouter,
addTo[pathId] = zeus::CMatrix4f(area.transformMtx[0], area.transformMtx[1], area.transformMtx[2], BottomRow)
.transposed();
}
#endif
hecl::ProjectPath areaDirPath = pakRouter.getWorking(area.areaMREAId).getParentPath();
if (area.areaNameId.isValid())
pathOverrides[area.areaNameId] = hecl::ProjectPath(areaDirPath,
@ -272,8 +270,8 @@ ResExtractor<PAKBridge> PAKBridge::LookupExtractor(const nod::Node& pakNode, con
return {MAPA::Extract, {_SYS_STR(".blend")}, 4};
case SBIG('MAPU'):
return {MAPU::Extract, {_SYS_STR(".blend")}, 5};
// case SBIG('PATH'):
// return {PATH::Extract, {_SYS_STR(".blend")}, 5};
case SBIG('PATH'):
return {PATH::Extract, {_SYS_STR(".blend")}, 5};
case SBIG('FSM2'):
return {DNAFSM2::ExtractFSM2<UniqueID32>, {_SYS_STR(".yaml")}};
case SBIG('FONT'):

View File

@ -98,11 +98,11 @@ void MREA::StreamReader::seek(atInt64 diff, athena::SeekOrigin whence) {
if (newAccum > target)
break;
dAccum = newAccum;
++bIdx;
if (info.compSize)
cAccum += ROUND_UP_32(info.compSize);
else
cAccum += info.decompSize;
++bIdx;
}
/* Seek source if needed */
@ -116,6 +116,41 @@ void MREA::StreamReader::seek(atInt64 diff, athena::SeekOrigin whence) {
m_posInBlk = target - dAccum;
}
void MREA::StreamReader::seekToSection(atUint32 sec, const std::vector<atUint32>& secSizes) {
/* Determine which block contains section */
atUint32 sAccum = 0;
atUint32 dAccum = 0;
atUint32 cAccum = 0;
atUint32 bIdx = 0;
for (BlockInfo& info : m_blockInfos) {
atUint32 newSAccum = sAccum + info.secCount;
if (newSAccum > sec)
break;
sAccum = newSAccum;
dAccum += info.decompSize;
if (info.compSize)
cAccum += ROUND_UP_32(info.compSize);
else
cAccum += info.decompSize;
++bIdx;
}
/* Seek source if needed */
if (bIdx != m_nextBlk - 1) {
m_source.seek(m_blkBase + cAccum, athena::SeekOrigin::Begin);
m_nextBlk = bIdx;
nextBlock();
}
/* Seek within block */
atUint32 target = dAccum;
while (sAccum != sec)
target += secSizes[sAccum++];
m_pos = target;
m_posInBlk = target - dAccum;
}
atUint64 MREA::StreamReader::readUBytesToBuf(void* buf, atUint64 len) {
atUint8* bufCur = reinterpret_cast<atUint8*>(buf);
atUint64 rem = len;
@ -302,15 +337,12 @@ UniqueID32 MREA::GetPATHId(PAKEntryReadStream& rs) {
head.read(rs);
rs.seekAlign32();
/* MREA decompression stream */
StreamReader drs(rs, head.compressedBlockCount);
/* Skip to PATH */
atUint32 curSec = 0;
atUint64 secStart = rs.position();
while (curSec != head.pathSecIdx)
secStart += head.secSizes[curSec++];
if (!head.secSizes[curSec])
return {};
rs.seek(secStart, athena::SeekOrigin::Begin);
return {rs};
drs.seekToSection(head.pathSecIdx, head.secSizes);
return {drs};
}
} // namespace DNAMP2

View File

@ -43,6 +43,7 @@ struct MREA {
public:
StreamReader(athena::io::IStreamReader& source, atUint32 blkCount);
void seek(atInt64 diff, athena::SeekOrigin whence) override;
void seekToSection(atUint32 sec, const std::vector<atUint32>& secSizes);
atUint64 position() const override { return m_pos; }
atUint64 length() const override { return m_totalDecompLen; }
atUint64 readUBytesToBuf(void* buf, atUint64 len) override;

View File

@ -3,7 +3,7 @@
#include "DataSpec/DNAMP2/DNAMP2.hpp"
namespace DataSpec::DNAMP2 {
struct PATH : DNAPATH::PATH {
struct PATH : DNAPATH::PATH<6> {
static bool Extract(const SpecBase& dataSpec, PAKEntryReadStream& rs, const hecl::ProjectPath& outPath,
PAKRouter<PAKBridge>& pakRouter, const PAK::Entry& entry, bool force, hecl::blender::Token& btok,
std::function<void(const hecl::SystemChar*)> fileChanged);

View File

@ -661,7 +661,7 @@ struct SpecMP1 : SpecBase {
hecl::blender::Token& btok, FCookProgress progress) override {
PathMesh mesh = ds.compilePathMesh();
ds.close();
DNAPATH::PATH::Cook(out, in, mesh, btok);
DNAPATH::PATH<4>::Cook(out, in, mesh, btok);
}
void cookActor(const hecl::ProjectPath& out, const hecl::ProjectPath& in, BlendStream& ds, bool fast,

View File

@ -291,7 +291,7 @@ struct SpecMP2 : SpecBase {
hecl::blender::Token& btok, FCookProgress progress) override {
PathMesh mesh = ds.compilePathMesh();
ds.close();
DNAPATH::PATH::Cook(out, in, mesh, btok);
DNAPATH::PATH<6>::Cook(out, in, mesh, btok);
}
void cookActor(const hecl::ProjectPath& out, const hecl::ProjectPath& in, BlendStream& ds, bool fast,

2
hecl

@ -1 +1 @@
Subproject commit 138328b8f617071cbe9457f04e9cbb31cef1975e
Subproject commit 191ffdc8e06c20e8831e383be5431789d9d4b8fb