metaforce/DataSpec/DNACommon/MAPU.cpp

149 lines
6.3 KiB
C++
Raw Permalink Normal View History

#include "DataSpec/DNACommon/MAPU.hpp"
#include "DataSpec/DNAMP1/DNAMP1.hpp"
#include "DataSpec/DNAMP2/DNAMP2.hpp"
#include "DataSpec/DNAMP3/DNAMP3.hpp"
#include <hecl/Blender/Connection.hpp>
#include <zeus/Global.hpp>
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
namespace DataSpec::DNAMAPU {
2017-03-20 05:09:53 +00:00
template <typename PAKRouter>
2018-12-08 05:30:43 +00:00
bool ReadMAPUToBlender(hecl::blender::Connection& conn, const MAPU& mapu, const hecl::ProjectPath& outPath,
PAKRouter& pakRouter, const typename PAKRouter::EntryType& entry, bool force) {
if (!force && outPath.isFile())
return true;
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
if (!conn.createBlend(outPath, hecl::blender::BlendType::MapUniverse))
return false;
hecl::blender::PyOutStream os = conn.beginPythonOut(true);
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
os << "import bpy\n"
"from mathutils import Matrix\n"
"\n"
"# Clear Scene\n"
"if len(bpy.data.collections):\n"
" bpy.data.collections.remove(bpy.data.collections[0])\n"
2018-12-08 05:30:43 +00:00
"\n"
"bpy.types.Object.retro_mapworld_color = bpy.props.FloatVectorProperty(name='Retro: MapWorld Color',"
" description='Sets map world color', subtype='COLOR', size=4, min=0.0, max=1.0)\n"
"bpy.types.Object.retro_mapworld_path = bpy.props.StringProperty(name='Retro: MapWorld Path',"
" description='Sets path to World root')\n"
"\n";
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
hecl::ProjectPath hexPath = pakRouter.getWorking(mapu.hexMapa);
2019-10-01 07:38:03 +00:00
os.linkMesh(hexPath.getAbsolutePathUTF8(), "MAP");
2018-12-08 05:30:43 +00:00
os << "hexMesh = bpy.data.objects['MAP'].data\n";
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
for (const MAPU::World& wld : mapu.worlds) {
hecl::ProjectPath path = UniqueIDBridge::TranslatePakIdToPath(wld.mlvl);
const MAPU::Transform& wldXf = wld.transform;
zeus::simd_floats wldXfF[3];
for (int i = 0; i < 3; ++i)
wldXf.xf[i].simd.copy_to(wldXfF[i]);
zeus::simd_floats hexColorF(wld.hexColor.mSimd);
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING(
2019-07-20 04:27:21 +00:00
"wldObj = bpy.data.objects.new('{}', None)\n"
"mtx = Matrix((({},{},{},{}),({},{},{},{}),({},{},{},{}),(0.0,0.0,0.0,1.0)))\n"
2018-12-08 05:30:43 +00:00
"mtxd = mtx.decompose()\n"
"wldObj.rotation_mode = 'QUATERNION'\n"
"wldObj.location = mtxd[0]\n"
"wldObj.rotation_quaternion = mtxd[1]\n"
"wldObj.scale = mtxd[2]\n"
2019-07-20 04:27:21 +00:00
"wldObj.retro_mapworld_color = ({}, {}, {}, {})\n"
"wldObj.retro_mapworld_path = '''{}'''\n"
"bpy.context.scene.collection.objects.link(wldObj)\n"),
wld.name, wldXfF[0][0], wldXfF[0][1], wldXfF[0][2], wldXfF[0][3], wldXfF[1][0], wldXfF[1][1],
2018-12-08 05:30:43 +00:00
wldXfF[1][2], wldXfF[1][3], wldXfF[2][0], wldXfF[2][1], wldXfF[2][2], wldXfF[2][3], hexColorF[0], hexColorF[1],
2019-07-20 04:27:21 +00:00
hexColorF[2], hexColorF[3], path.getParentPath().getRelativePathUTF8());
2018-12-08 05:30:43 +00:00
int idx = 0;
for (const MAPU::Transform& hexXf : wld.hexTransforms) {
zeus::simd_floats hexXfF[3];
for (int i = 0; i < 3; ++i)
hexXf.xf[i].simd.copy_to(hexXfF[i]);
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING(
2019-07-20 04:27:21 +00:00
"obj = bpy.data.objects.new('{}_{}', hexMesh)\n"
"mtx = Matrix((({},{},{},{}),({},{},{},{}),({},{},{},{}),(0.0,0.0,0.0,1.0)))\n"
2018-12-08 05:30:43 +00:00
"mtxd = mtx.decompose()\n"
"obj.rotation_mode = 'QUATERNION'\n"
"obj.location = mtxd[0]\n"
"obj.rotation_quaternion = mtxd[1]\n"
"obj.scale = mtxd[2]\n"
"bpy.context.scene.collection.objects.link(obj)\n"
2019-07-20 04:27:21 +00:00
"obj.parent = wldObj\n"),
wld.name, idx++, hexXfF[0][0], hexXfF[0][1], hexXfF[0][2], hexXfF[0][3], hexXfF[1][0], hexXfF[1][1],
2018-12-08 05:30:43 +00:00
hexXfF[1][2], hexXfF[1][3], hexXfF[2][0], hexXfF[2][1], hexXfF[2][2], hexXfF[2][3]);
2017-03-20 05:09:53 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
os << "for screen in bpy.data.screens:\n"
" for area in screen.areas:\n"
" for space in area.spaces:\n"
" if space.type == 'VIEW_3D':\n"
" space.clip_end = 8000.0\n";
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
os.centerView();
os.close();
conn.saveBlend();
return true;
2017-03-20 05:09:53 +00:00
}
2018-12-08 05:30:43 +00:00
template bool ReadMAPUToBlender<PAKRouter<DNAMP1::PAKBridge>>(hecl::blender::Connection& conn, const MAPU& mapu,
const hecl::ProjectPath& outPath,
PAKRouter<DNAMP1::PAKBridge>& pakRouter,
const PAKRouter<DNAMP1::PAKBridge>::EntryType& entry,
bool force);
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
template bool ReadMAPUToBlender<PAKRouter<DNAMP2::PAKBridge>>(hecl::blender::Connection& conn, const MAPU& mapu,
const hecl::ProjectPath& outPath,
PAKRouter<DNAMP2::PAKBridge>& pakRouter,
const PAKRouter<DNAMP2::PAKBridge>::EntryType& entry,
bool force);
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
bool MAPU::Cook(const hecl::blender::MapUniverse& mapuIn, const hecl::ProjectPath& out) {
MAPU mapu;
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
mapu.magic = 0xABCDEF01;
mapu.version = 1;
mapu.hexMapa = mapuIn.hexagonPath;
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
mapu.worldCount = mapuIn.worlds.size();
mapu.worlds.reserve(mapuIn.worlds.size());
for (const hecl::blender::MapUniverse::World& wld : mapuIn.worlds) {
mapu.worlds.emplace_back();
MAPU::World& wldOut = mapu.worlds.back();
wldOut.name = wld.name;
2019-10-01 07:38:03 +00:00
for (const auto& ent : wld.worldPath.enumerateDir()) {
if (hecl::StringUtils::BeginsWith(ent.m_name, _SYS_STR("!world")) &&
2019-10-01 07:38:03 +00:00
hecl::StringUtils::EndsWith(ent.m_name, _SYS_STR(".blend"))) {
wldOut.mlvl = hecl::ProjectPath(wld.worldPath, ent.m_name);
break;
}
}
2018-12-08 05:30:43 +00:00
wldOut.transform.xf[0] = wld.xf.val[0];
wldOut.transform.xf[1] = wld.xf.val[1];
wldOut.transform.xf[2] = wld.xf.val[2];
wldOut.hexCount = wld.hexagons.size();
wldOut.hexTransforms.reserve(wld.hexagons.size());
for (const hecl::blender::Matrix4f& mtx : wld.hexagons) {
wldOut.hexTransforms.emplace_back();
MAPU::Transform& xf = wldOut.hexTransforms.back();
xf.xf[0] = mtx.val[0];
xf.xf[1] = mtx.val[1];
xf.xf[2] = mtx.val[2];
2017-03-20 05:09:53 +00:00
}
2018-12-08 05:30:43 +00:00
wldOut.hexColor = zeus::CColor(wld.color.val);
}
2017-03-20 05:09:53 +00:00
2018-12-08 05:30:43 +00:00
athena::io::FileWriter f(out.getAbsolutePath());
mapu.write(f);
int64_t rem = f.position() % 32;
if (rem)
for (int64_t i = 0; i < 32 - rem; ++i)
f.writeBytes((atInt8*)"\xff", 1);
return true;
2017-03-20 05:09:53 +00:00
}
2018-12-08 05:30:43 +00:00
} // namespace DataSpec::DNAMAPU