metaforce/DataSpec/DNAMP3/CMDLMaterials.cpp

385 lines
15 KiB
C++
Raw Normal View History

#include "CMDLMaterials.hpp"
2017-12-29 08:08:12 +00:00
#include "hecl/Blender/Connection.hpp"
2017-12-29 08:08:12 +00:00
using Stream = hecl::blender::PyOutStream;
2017-12-29 08:08:12 +00:00
namespace DataSpec::DNAMP3
{
2015-09-19 01:38:40 +00:00
using Material = MaterialSet::Material;
2018-02-22 07:24:51 +00:00
template <>
void MaterialSet::Material::SectionFactory::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
{
DNAFourCC type;
type.read(reader);
switch (ISection::Type(type.toUint32()))
{
case ISection::Type::PASS:
section.reset(new struct SectionPASS);
section->read(reader);
break;
case ISection::Type::CLR:
section.reset(new struct SectionCLR);
section->read(reader);
break;
case ISection::Type::INT:
section.reset(new struct SectionINT);
section->read(reader);
break;
default:
section.reset(nullptr);
break;
}
}
template <>
void MaterialSet::Material::SectionFactory::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
{
if (!section)
return;
writer.writeUBytes((atUint8*)&section->m_type, 4);
section->write(writer);
}
template <>
void MaterialSet::Material::SectionFactory::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
{
s += 4;
section->binarySize(s);
}
template <>
void MaterialSet::Material::Enumerate<BigDNA::Read>(typename Read::StreamT& reader)
{
header.read(reader);
sections.clear();
do {
sections.emplace_back();
sections.back().read(reader);
} while (sections.back().section);
sections.pop_back();
}
template <>
void MaterialSet::Material::Enumerate<BigDNA::Write>(typename Write::StreamT& writer)
{
header.write(writer);
for (const SectionFactory& section : sections)
section.write(writer);
writer.writeUBytes((atUint8*)"END ", 4);
}
template <>
void MaterialSet::Material::Enumerate<BigDNA::BinarySize>(typename BinarySize::StreamT& s)
{
header.binarySize(s);
for (const SectionFactory& section : sections)
section.binarySize(s);
s += 4;
}
2015-09-19 01:38:40 +00:00
void MaterialSet::RegisterMaterialProps(Stream& out)
{
out << "bpy.types.Material.retro_punchthrough_alpha = bpy.props.BoolProperty(name='Retro: Punchthrough Alpha')\n"
"bpy.types.Material.retro_shadow_occluder = bpy.props.BoolProperty(name='Retro: Shadow Occluder')\n"
"bpy.types.Material.retro_lightmapped = bpy.props.BoolProperty(name='Retro: Lightmapped')\n"
"bpy.types.Material.retro_opac = bpy.props.IntProperty(name='Retro: OPAC')\n"
"bpy.types.Material.retro_blod = bpy.props.IntProperty(name='Retro: BLOD')\n"
"bpy.types.Material.retro_bloi = bpy.props.IntProperty(name='Retro: BLOI')\n"
"bpy.types.Material.retro_bnif = bpy.props.IntProperty(name='Retro: BNIF')\n"
"bpy.types.Material.retro_xrbr = bpy.props.IntProperty(name='Retro: XRBR')\n"
"\n";
}
void MaterialSet::ConstructMaterial(Stream& out,
2015-09-19 01:38:40 +00:00
const PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
const Material& material,
unsigned groupIdx,
unsigned matIdx)
{
2015-09-19 01:38:40 +00:00
unsigned i;
out.format("new_material = bpy.data.materials.new('MAT_%u_%u')\n"
"new_material.use_shadows = True\n"
"new_material.use_transparent_shadows = True\n"
"new_material.diffuse_color = (1.0,1.0,1.0)\n"
"new_material.diffuse_intensity = 1.0\n"
"new_material.specular_intensity = 0.0\n"
"new_material.use_nodes = True\n"
"new_nodetree = new_material.node_tree\n"
"material_node = new_nodetree.nodes['Material']\n"
"final_node = new_nodetree.nodes['Output']\n"
"\n"
"gridder = hecl.Nodegrid(new_nodetree)\n"
"gridder.place_node(final_node, 3)\n"
"gridder.place_node(material_node, 0)\n"
"material_node.material = new_material\n"
"\n"
"texture_nodes = []\n"
"kcolor_nodes = []\n"
"color_combiner_nodes = []\n"
"alpha_combiner_nodes = []\n"
"tex_links = []\n"
"tev_reg_sockets = [None]*4\n"
"\n", groupIdx, matIdx);
/* Material Flags */
out.format("new_material.retro_punchthrough_alpha = %s\n"
"new_material.retro_shadow_occluder = %s\n"
"new_material.game_settings.invisible = %s\n",
material.header.flags.punchthroughAlpha() ? "True" : "False",
material.header.flags.shadowOccluderMesh() ? "True" : "False",
material.header.flags.shadowOccluderMesh() ? "True" : "False");
/* Blend factors */
2016-01-18 00:00:11 +00:00
if (material.header.flags.alphaBlending())
out << "new_material.game_settings.alpha_blend = 'ALPHA'\n"
2015-09-19 01:38:40 +00:00
"new_material.use_transparency = True\n"
"new_material.transparency_method = 'RAYTRACE'\n"
"new_material.alpha = 1.0\n";
2016-01-18 00:00:11 +00:00
else if (material.header.flags.additiveBlending())
out << "new_material.game_settings.alpha_blend = 'ADD'\n"
2015-09-19 01:38:40 +00:00
"new_material.use_transparency = True\n"
"new_material.transparency_method = 'RAYTRACE'\n"
"new_material.alpha = 1.0\n";
/* Texmap list */
out << "tex_maps = []\n"
2015-09-19 07:08:01 +00:00
"pnode = None\n"
2016-01-18 00:00:11 +00:00
"anode = None\n"
2015-09-19 07:08:01 +00:00
"rflv_tex_node = None\n";
2015-09-19 01:38:40 +00:00
/* Add PASSes */
i=0;
unsigned texMapIdx = 0;
unsigned texMtxIdx = 0;
unsigned kColorIdx = 0;
Material::ISection* prevSection = nullptr;
for (const Material::SectionFactory& factory : material.sections)
{
factory.section->constructNode(out, pakRouter, entry, prevSection, i++, texMapIdx, texMtxIdx, kColorIdx);
2015-09-19 20:36:46 +00:00
Material::SectionPASS* pass = dynamic_cast<Material::SectionPASS*>(factory.section.get());
2015-11-21 01:16:07 +00:00
if (!pass || (pass && Material::SectionPASS::Subtype(pass->subtype.toUint32()) != Material::SectionPASS::Subtype::RFLV))
2015-09-19 20:36:46 +00:00
prevSection = factory.section.get();
2015-09-19 01:38:40 +00:00
}
/* Connect final PASS */
out << "if pnode:\n"
" new_nodetree.links.new(pnode.outputs['Next Color'], final_node.inputs['Color'])\n"
"else:\n"
" new_nodetree.links.new(kcolor_nodes[-1][0].outputs[0], final_node.inputs['Color'])\n"
2016-01-18 00:00:11 +00:00
"if anode:\n"
" new_nodetree.links.new(anode.outputs['Value'], final_node.inputs['Alpha'])\n"
"elif pnode:\n"
" new_nodetree.links.new(pnode.outputs['Next Alpha'], final_node.inputs['Alpha'])\n"
"else:\n"
2015-09-19 01:38:40 +00:00
" new_nodetree.links.new(kcolor_nodes[-1][1].outputs[0], final_node.inputs['Alpha'])\n";
}
2017-12-29 08:08:12 +00:00
void Material::SectionPASS::constructNode(hecl::blender::PyOutStream& out,
2015-09-19 01:38:40 +00:00
const PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
const Material::ISection* prevSection,
unsigned idx,
unsigned& texMapIdx,
unsigned& texMtxIdx,
unsigned& kColorIdx) const
{
/* Add Texture nodes */
if (txtrId)
{
std::string texName = pakRouter.getBestEntryName(txtrId);
2016-03-04 23:04:53 +00:00
const nod::Node* node;
2015-09-19 01:38:40 +00:00
const PAK::Entry* texEntry = pakRouter.lookupEntry(txtrId, &node);
2016-03-04 23:04:53 +00:00
hecl::ProjectPath txtrPath = pakRouter.getWorking(texEntry);
2016-09-18 23:47:48 +00:00
if (txtrPath.isNone())
2015-09-19 01:38:40 +00:00
{
txtrPath.makeDirChain(false);
2015-09-19 01:38:40 +00:00
PAKEntryReadStream rs = texEntry->beginReadStream(*node);
TXTR::Extract(rs, txtrPath);
}
2016-03-04 23:04:53 +00:00
hecl::SystemString resPath = pakRouter.getResourceRelativePath(entry, txtrId);
2017-11-13 06:19:18 +00:00
hecl::SystemUTF8Conv resPathView(resPath);
2015-09-19 01:38:40 +00:00
out.format("if '%s' in bpy.data.textures:\n"
" image = bpy.data.images['%s']\n"
" texture = bpy.data.textures[image.name]\n"
"else:\n"
" image = bpy.data.images.load('''//%s''')\n"
" image.name = '%s'\n"
" texture = bpy.data.textures.new(image.name, 'IMAGE')\n"
" texture.image = image\n"
"tex_maps.append(texture)\n"
"\n", texName.c_str(), texName.c_str(),
2017-11-13 06:19:18 +00:00
resPathView.c_str(), texName.c_str());
2015-09-19 01:38:40 +00:00
if (uvAnim.size())
{
const UVAnimation& uva = uvAnim[0];
2015-09-19 07:08:01 +00:00
DNAMP1::MaterialSet::Material::AddTexture(out, GX::TexGenSrc(uva.unk1 + (uva.unk1 < 2 ? 0 : 2)), texMtxIdx, texMapIdx++);
2015-09-19 01:38:40 +00:00
DNAMP1::MaterialSet::Material::AddTextureAnim(out, uva.anim.mode, texMtxIdx++, uva.anim.vals);
}
else
2015-09-19 05:24:32 +00:00
DNAMP1::MaterialSet::Material::AddTexture(out, GX::TexGenSrc(uvSrc + 4), -1, texMapIdx++);
2015-09-19 01:38:40 +00:00
}
2015-09-19 07:08:01 +00:00
/* Special case for RFLV (environment UV mask) */
2015-11-21 01:16:07 +00:00
if (Subtype(subtype.toUint32()) == Subtype::RFLV)
2015-09-19 07:08:01 +00:00
{
if (txtrId)
out << "rflv_tex_node = texture_nodes[-1]\n";
return;
}
2015-09-19 01:38:40 +00:00
/* Add PASS node */
2015-09-19 07:08:01 +00:00
bool linkRAS = false;
2015-09-19 01:38:40 +00:00
out << "prev_pnode = pnode\n"
"pnode = new_nodetree.nodes.new('ShaderNodeGroup')\n";
2015-11-21 01:16:07 +00:00
switch (Subtype(subtype.toUint32()))
2015-09-19 01:38:40 +00:00
{
2015-11-21 01:16:07 +00:00
case Subtype::DIFF:
2016-02-26 07:57:22 +00:00
{
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassDIFF']\n";
2016-02-26 07:57:22 +00:00
if (txtrId)
{
out << "new_material.hecl_lightmap = texture.name\n"
<< "texture.image.use_fake_user = True\n";
}
2015-09-19 20:36:46 +00:00
linkRAS = true;
2015-09-19 01:38:40 +00:00
break;
2016-02-26 07:57:22 +00:00
}
2015-11-21 01:16:07 +00:00
case Subtype::RIML:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassRIML']\n";
2015-09-19 20:36:46 +00:00
if (idx == 0)
linkRAS = true;
2015-09-19 01:38:40 +00:00
break;
2015-11-21 01:16:07 +00:00
case Subtype::BLOL:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassBLOL']\n";
2015-09-19 20:36:46 +00:00
if (idx == 0)
linkRAS = true;
2015-09-19 01:38:40 +00:00
break;
2015-11-21 01:16:07 +00:00
case Subtype::BLOD:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassBLOD']\n";
2015-09-19 20:36:46 +00:00
if (idx == 0)
linkRAS = true;
2015-09-19 01:38:40 +00:00
break;
2015-11-21 01:16:07 +00:00
case Subtype::CLR:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassCLR']\n";
2015-09-19 20:36:46 +00:00
if (idx == 0)
linkRAS = true;
2015-09-19 01:38:40 +00:00
break;
2015-11-21 01:16:07 +00:00
case Subtype::TRAN:
2015-09-19 20:36:46 +00:00
if (flags.TRANInvert())
out << "pnode.node_tree = bpy.data.node_groups['RetroPassTRANInv']\n";
else
out << "pnode.node_tree = bpy.data.node_groups['RetroPassTRAN']\n";
2015-09-19 01:38:40 +00:00
break;
2015-11-21 01:16:07 +00:00
case Subtype::INCA:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassINCA']\n";
break;
2015-11-21 01:16:07 +00:00
case Subtype::RFLV:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassRFLV']\n";
break;
2015-11-21 01:16:07 +00:00
case Subtype::RFLD:
2015-09-19 07:08:01 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassRFLD']\n"
"if rflv_tex_node:\n"
" new_nodetree.links.new(rflv_tex_node.outputs['Color'], pnode.inputs['Mask Color'])\n"
" new_nodetree.links.new(rflv_tex_node.outputs['Value'], pnode.inputs['Mask Alpha'])\n";
2015-09-19 01:38:40 +00:00
break;
2015-11-21 01:16:07 +00:00
case Subtype::LRLD:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassLRLD']\n";
break;
2015-11-21 01:16:07 +00:00
case Subtype::LURD:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassLURD']\n";
break;
2015-11-21 01:16:07 +00:00
case Subtype::BLOI:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassBLOI']\n";
break;
2015-11-21 01:16:07 +00:00
case Subtype::XRAY:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassXRAY']\n";
break;
2015-11-21 01:16:07 +00:00
case Subtype::TOON:
2015-09-19 01:38:40 +00:00
out << "pnode.node_tree = bpy.data.node_groups['RetroPassTOON']\n";
break;
default: break;
}
out << "gridder.place_node(pnode, 2)\n";
2015-09-19 01:38:40 +00:00
if (txtrId)
{
out << "new_nodetree.links.new(texture_nodes[-1].outputs['Color'], pnode.inputs['Tex Color'])\n"
"new_nodetree.links.new(texture_nodes[-1].outputs['Value'], pnode.inputs['Tex Alpha'])\n";
}
2015-09-19 07:08:01 +00:00
if (linkRAS)
out << "new_nodetree.links.new(material_node.outputs['Color'], pnode.inputs['Prev Color'])\n"
"new_nodetree.links.new(material_node.outputs['Alpha'], pnode.inputs['Prev Alpha'])\n";
else if (prevSection)
2015-09-19 01:38:40 +00:00
{
2015-11-21 01:16:07 +00:00
if (prevSection->m_type == ISection::Type::PASS &&
Subtype(static_cast<const SectionPASS*>(prevSection)->subtype.toUint32()) != Subtype::RFLV)
2015-09-19 01:38:40 +00:00
out << "new_nodetree.links.new(prev_pnode.outputs['Next Color'], pnode.inputs['Prev Color'])\n"
"new_nodetree.links.new(prev_pnode.outputs['Next Alpha'], pnode.inputs['Prev Alpha'])\n";
2015-11-21 01:16:07 +00:00
else if (prevSection->m_type == ISection::Type::CLR)
2015-09-19 01:38:40 +00:00
out << "new_nodetree.links.new(kcolor_nodes[-1][0].outputs[0], pnode.inputs['Prev Color'])\n"
"new_nodetree.links.new(kcolor_nodes[-1][1].outputs[0], pnode.inputs['Prev Alpha'])\n";
}
/* Row Break in gridder */
out << "gridder.row_break(2)\n";
}
2017-12-29 08:08:12 +00:00
void Material::SectionCLR::constructNode(hecl::blender::PyOutStream& out,
2015-09-19 01:38:40 +00:00
const PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
const Material::ISection* prevSection,
unsigned idx,
unsigned& texMapIdx,
unsigned& texMtxIdx,
unsigned& kColorIdx) const
{
DNAMP1::MaterialSet::Material::AddKcolor(out, color, kColorIdx++);
2015-11-21 01:16:07 +00:00
switch (Subtype(subtype.toUint32()))
2015-09-19 01:38:40 +00:00
{
2015-11-21 01:16:07 +00:00
case Subtype::DIFB:
2015-09-19 01:38:40 +00:00
out << "kc_node.label += ' DIFB'\n"
"ka_node.label += ' DIFB'\n";
break;
default: break;
}
}
2017-12-29 08:08:12 +00:00
void Material::SectionINT::constructNode(hecl::blender::PyOutStream& out,
2015-09-19 01:38:40 +00:00
const PAKRouter<PAKBridge>& pakRouter,
const PAK::Entry& entry,
const Material::ISection* prevSection,
unsigned idx,
unsigned& texMapIdx,
unsigned& texMtxIdx,
unsigned& kColorIdx) const
{
2015-11-21 01:16:07 +00:00
switch (Subtype(subtype.toUint32()))
2015-09-19 01:38:40 +00:00
{
2015-11-21 01:16:07 +00:00
case Subtype::OPAC:
2016-01-18 00:00:11 +00:00
{
GX::Color clr(value);
out.format("anode = new_nodetree.nodes.new('ShaderNodeValue')\n"
"anode.outputs['Value'].default_value = %f\n",
float(clr[3]) / float(0xff));
out << "gridder.place_node(anode, 1)\n";
}
2015-09-19 01:38:40 +00:00
break;
2015-11-21 01:16:07 +00:00
case Subtype::BLOD:
2015-09-19 01:38:40 +00:00
out.format("new_material.retro_blod = %d\n", value);
break;
2015-11-21 01:16:07 +00:00
case Subtype::BLOI:
2015-09-19 01:38:40 +00:00
out.format("new_material.retro_bloi = %d\n", value);
break;
2015-11-21 01:16:07 +00:00
case Subtype::BNIF:
2015-09-19 01:38:40 +00:00
out.format("new_material.retro_bnif = %d\n", value);
break;
2015-11-21 01:16:07 +00:00
case Subtype::XRBR:
2015-09-19 01:38:40 +00:00
out.format("new_material.retro_xrbr = %d\n", value);
break;
default: break;
}
}
}