metaforce/DataSpec/DNAMP1/MREA.cpp

219 lines
9.7 KiB
C++
Raw Normal View History

2015-09-09 00:49:20 +00:00
#include "MREA.hpp"
2015-09-12 07:13:40 +00:00
#include "DeafBabe.hpp"
2015-09-09 00:49:20 +00:00
namespace Retro
{
namespace DNAMP1
{
2015-09-12 07:13:40 +00:00
void MREA::ReadBabeDeadToBlender_1_2(HECL::BlenderConnection::PyOutStream& os,
Athena::io::IStreamReader& rs)
{
atUint32 bdMagic = rs.readUint32Big();
if (bdMagic != 0xBABEDEAD)
Log.report(LogVisor::FatalError, "invalid BABEDEAD magic");
2015-09-17 19:50:43 +00:00
os << "bpy.context.scene.render.engine = 'CYCLES'\n"
"bpy.context.scene.world.use_nodes = True\n"
"bpy.context.scene.render.engine = 'BLENDER_GAME'\n"
"bg_node = bpy.context.scene.world.node_tree.nodes['Background']\n";
2015-09-12 07:13:40 +00:00
for (atUint32 s=0 ; s<2 ; ++s)
{
atUint32 lightCount = rs.readUint32Big();
for (atUint32 l=0 ; l<lightCount ; ++l)
{
BabeDeadLight light;
light.read(rs);
switch (light.lightType)
{
case BabeDeadLight::LightLocalAmbient:
2015-09-17 19:50:43 +00:00
os.format("bg_node.inputs[0].default_value = (%f,%f,%f,1.0)\n"
"bg_node.inputs[1].default_value = %f\n",
light.color.vec[0], light.color.vec[1], light.color.vec[2],
light.q / 8.0);
2015-09-12 07:13:40 +00:00
continue;
case BabeDeadLight::LightDirectional:
os.format("lamp = bpy.data.lamps.new('LAMP_%01u_%03u', 'SUN')\n"
"lamp_obj = bpy.data.objects.new(lamp.name, lamp)\n"
"lamp_obj.rotation_mode = 'QUATERNION'\n"
"lamp_obj.rotation_quaternion = Vector((0,0,-1)).rotation_difference(Vector((%f,%f,%f)))\n"
"\n", s, l,
light.direction.vec[0], light.direction.vec[1], light.direction.vec[2]);
break;
case BabeDeadLight::LightCustom:
os.format("lamp = bpy.data.lamps.new('LAMP_%01u_%03u', 'POINT')\n"
"lamp_obj = bpy.data.objects.new(lamp.name, lamp)\n"
"\n", s, l);
break;
case BabeDeadLight::LightSpot:
os.format("lamp = bpy.data.lamps.new('LAMP_%01u_%03u', 'SPOT')\n"
"lamp.spot_size = %f\n"
"lamp_obj = bpy.data.objects.new(lamp.name, lamp)\n"
"lamp_obj.rotation_mode = 'QUATERNION'\n"
"lamp_obj.rotation_quaternion = Vector((0,0,-1)).rotation_difference(Vector((%f,%f,%f)))\n"
"\n", s, l,
light.spotCutoff * M_PI / 180.f,
light.direction.vec[0], light.direction.vec[1], light.direction.vec[2]);
break;
default: continue;
}
os.format("lamp.retro_layer = %u\n"
"lamp.use_nodes = True\n"
"falloff_node = lamp.node_tree.nodes.new('ShaderNodeLightFalloff')\n"
2015-09-17 19:50:43 +00:00
"lamp.energy = 0.0\n"
2015-09-12 07:13:40 +00:00
"falloff_node.inputs[0].default_value = %f\n"
2015-09-17 19:50:43 +00:00
"hue_sat_node = lamp.node_tree.nodes.new('ShaderNodeHueSaturation')\n"
"hue_sat_node.inputs[1].default_value = 1.25\n"
"hue_sat_node.inputs[4].default_value = (%f,%f,%f,1.0)\n"
"lamp.node_tree.links.new(hue_sat_node.outputs[0], lamp.node_tree.nodes['Emission'].inputs[0])\n"
2015-09-12 07:13:40 +00:00
"lamp_obj.location = (%f,%f,%f)\n"
"bpy.context.scene.objects.link(lamp_obj)\n"
2015-09-17 19:50:43 +00:00
"\n", s, light.q / 8.0,
2015-09-12 07:13:40 +00:00
light.color.vec[0], light.color.vec[1], light.color.vec[2],
light.position.vec[0], light.position.vec[1], light.position.vec[2]);
switch (light.falloff)
{
case BabeDeadLight::FalloffConstant:
2015-09-17 19:50:43 +00:00
os << "falloff_node.inputs[0].default_value *= 75.0\n"
"lamp.node_tree.links.new(falloff_node.outputs[2], lamp.node_tree.nodes['Emission'].inputs[1])\n";
break;
2015-09-12 07:13:40 +00:00
case BabeDeadLight::FalloffLinear:
os << "lamp.node_tree.links.new(falloff_node.outputs[1], lamp.node_tree.nodes['Emission'].inputs[1])\n";
2015-09-17 19:50:43 +00:00
break;
2015-09-12 07:13:40 +00:00
case BabeDeadLight::FalloffQuadratic:
os << "lamp.node_tree.links.new(falloff_node.outputs[0], lamp.node_tree.nodes['Emission'].inputs[1])\n";
2015-09-17 19:50:43 +00:00
break;
2015-09-12 07:13:40 +00:00
default: break;
}
}
}
}
2015-09-09 00:49:20 +00:00
bool MREA::Extract(const SpecBase& dataSpec,
PAKEntryReadStream& rs,
const HECL::ProjectPath& outPath,
PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
bool,
std::function<void(const HECL::SystemChar*)>)
{
using RigPair = std::pair<CSKR*, CINF*>;
RigPair dummy(nullptr, nullptr);
/* Do extract */
Header head;
head.read(rs);
rs.seekAlign32();
HECL::BlenderConnection& conn = HECL::BlenderConnection::SharedConnection();
2015-09-09 01:37:00 +00:00
if (!conn.createBlend(outPath.getAbsolutePath()))
2015-09-09 00:49:20 +00:00
return false;
/* Open Py Stream and read sections */
HECL::BlenderConnection::PyOutStream os = conn.beginPythonOut(true);
os.format("import bpy\n"
"import bmesh\n"
2015-09-12 07:13:40 +00:00
"from mathutils import Vector\n"
2015-09-09 00:49:20 +00:00
"\n"
"bpy.context.scene.name = '%s'\n"
"bpy.context.scene.hecl_type = 'AREA'\n",
pakRouter.getBestEntryName(entry).c_str());
DNACMDL::InitGeomBlenderContext(os, dataSpec.getMasterShaderPath());
MaterialSet::RegisterMaterialProps(os);
os << "# Clear Scene\n"
"for ob in bpy.data.objects:\n"
" bpy.context.scene.objects.unlink(ob)\n"
" bpy.data.objects.remove(ob)\n"
2015-09-12 07:13:40 +00:00
"bpy.types.Lamp.retro_layer = bpy.props.IntProperty(name='Retro: Light Layer')\n"
2015-09-09 00:49:20 +00:00
"bpy.types.Object.retro_disable_enviro_visor = bpy.props.BoolProperty(name='Retro: Disable in Combat/Scan Visor')\n"
"bpy.types.Object.retro_disable_thermal_visor = bpy.props.BoolProperty(name='Retro: Disable in Thermal Visor')\n"
"bpy.types.Object.retro_disable_xray_visor = bpy.props.BoolProperty(name='Retro: Disable in X-Ray Visor')\n"
"bpy.types.Object.retro_thermal_level = bpy.props.EnumProperty(items=[('COOL', 'Cool', 'Cool Temperature'),"
"('HOT', 'Hot', 'Hot Temperature'),"
"('WARM', 'Warm', 'Warm Temperature')],"
"name='Retro: Thermal Visor Level')\n"
"\n";
/* One shared material set for all meshes */
os << "# Materials\n"
"materials = []\n"
"\n";
MaterialSet matSet;
atUint64 secStart = rs.position();
matSet.read(rs);
2015-09-19 01:38:40 +00:00
matSet.readToBlender(os, pakRouter, entry, 0);
2015-09-09 00:49:20 +00:00
rs.seek(secStart + head.secSizes[0], Athena::Begin);
std::vector<DNACMDL::VertexAttributes> vertAttribs;
DNACMDL::GetVertexAttributes(matSet, vertAttribs);
/* Read meshes */
atUint32 curSec = 1;
for (int m=0 ; m<head.meshCount ; ++m)
{
MeshHeader mHeader;
secStart = rs.position();
mHeader.read(rs);
rs.seek(secStart + head.secSizes[curSec++], Athena::Begin);
curSec += DNACMDL::ReadGeomSectionsToBlender<PAKRouter<PAKBridge>, MaterialSet, RigPair>
2015-09-19 01:38:40 +00:00
(os, rs, pakRouter, entry, dummy, true,
2015-09-09 00:49:20 +00:00
true, vertAttribs, m, head.secCount, 0, &head.secSizes[curSec]);
os.format("obj.retro_disable_enviro_visor = %s\n"
"obj.retro_disable_thermal_visor = %s\n"
"obj.retro_disable_xray_visor = %s\n"
"obj.retro_thermal_level = '%s'\n",
mHeader.visorFlags.disableEnviro() ? "True" : "False",
mHeader.visorFlags.disableThermal() ? "True" : "False",
mHeader.visorFlags.disableXray() ? "True" : "False",
mHeader.visorFlags.thermalLevelStr());
}
2015-09-12 07:13:40 +00:00
/* Skip AROT */
rs.seek(head.secSizes[curSec++], Athena::Current);
/* Skip SCLY (for now) */
rs.seek(head.secSizes[curSec++], Athena::Current);
/* Read collision meshes */
DeafBabe collision;
secStart = rs.position();
collision.read(rs);
DeafBabe::BlenderInit(os);
collision.sendToBlender(os);
rs.seek(secStart + head.secSizes[curSec++], Athena::Begin);
/* Skip unknown section */
rs.seek(head.secSizes[curSec++], Athena::Current);
/* Read BABEDEAD Lights as Cycles emissives */
secStart = rs.position();
ReadBabeDeadToBlender_1_2(os, rs);
rs.seek(secStart + head.secSizes[curSec++], Athena::Begin);
2015-09-09 00:49:20 +00:00
/* Origins to center of mass */
2015-09-12 07:13:40 +00:00
os << "bpy.context.scene.layers[1] = True\n"
"bpy.ops.object.select_by_type(type='MESH')\n"
2015-09-09 00:49:20 +00:00
"bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_MASS')\n"
2015-09-12 07:13:40 +00:00
"bpy.ops.object.select_all(action='DESELECT')\n"
"bpy.context.scene.layers[1] = False\n";
2015-09-09 00:49:20 +00:00
/* Center view */
os << "bpy.context.user_preferences.view.smooth_view = 0\n"
"for window in bpy.context.window_manager.windows:\n"
" screen = window.screen\n"
" for area in screen.areas:\n"
" if area.type == 'VIEW_3D':\n"
" for region in area.regions:\n"
" if region.type == 'WINDOW':\n"
" override = {'scene': bpy.context.scene, 'window': window, 'screen': screen, 'area': area, 'region': region}\n"
" bpy.ops.view3d.view_all(override)\n"
" break\n";
os.close();
return conn.saveBlend();
}
}
}