metaforce/DataSpec/DNACommon/BabeDead.cpp

167 lines
7.3 KiB
C++
Raw Permalink Normal View History

#include "DataSpec/DNACommon/BabeDead.hpp"
#include "DataSpec/DNAMP1/MREA.hpp"
#include "DataSpec/DNAMP3/MREA.hpp"
#include <cfloat>
#include <hecl/Blender/Connection.hpp>
#include <zeus/CTransform.hpp>
#include <zeus/Math.hpp>
#include <fmt/format.h>
2018-12-08 05:30:43 +00:00
namespace DataSpec {
2018-12-08 05:30:43 +00:00
template <class BabeDeadLight>
void ReadBabeDeadLightToBlender(hecl::blender::PyOutStream& os, const BabeDeadLight& light, unsigned s, unsigned l) {
switch (light.lightType) {
case BabeDeadLight::LightType::LocalAmbient:
case BabeDeadLight::LightType::LocalAmbient2:
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING(
2019-07-20 04:27:21 +00:00
"bg_node.inputs[0].default_value = ({},{},{},1.0)\n"
"bg_node.inputs[1].default_value = {}\n"),
2018-12-08 05:30:43 +00:00
light.color.simd[0], light.color.simd[1], light.color.simd[2], light.q / 8.f);
return;
case BabeDeadLight::LightType::Directional:
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING(
2019-07-20 04:27:21 +00:00
"lamp = bpy.data.lights.new('LAMP_{:01d}_{:03d}', 'SUN')\n"
"lamp.color = ({},{},{})\n"
2018-12-08 05:30:43 +00:00
"lamp_obj = bpy.data.objects.new(lamp.name, lamp)\n"
"lamp_obj.rotation_mode = 'QUATERNION'\n"
2019-07-20 04:27:21 +00:00
"lamp_obj.rotation_quaternion = Vector((0,0,-1)).rotation_difference(Vector(({},{},{})))\n"
"lamp.use_shadow = {}\n"
"\n"),
2018-12-08 05:30:43 +00:00
s, l, light.color.simd[0], light.color.simd[1], light.color.simd[2], light.direction.simd[0],
light.direction.simd[1], light.direction.simd[2], light.castShadows ? "True" : "False");
2018-12-08 05:30:43 +00:00
return;
case BabeDeadLight::LightType::Custom:
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING(
2019-07-20 04:27:21 +00:00
"lamp = bpy.data.lights.new('LAMP_{:01d}_{:03d}', 'POINT')\n"
"lamp.color = ({},{},{})\n"
2018-12-08 05:30:43 +00:00
"lamp_obj = bpy.data.objects.new(lamp.name, lamp)\n"
"lamp.shadow_soft_size = 1.0\n"
2019-07-20 04:27:21 +00:00
"lamp.use_shadow = {}\n"
"\n"),
2018-12-08 05:30:43 +00:00
s, l, light.color.simd[0], light.color.simd[1], light.color.simd[2],
light.castShadows ? "True" : "False");
2018-12-08 05:30:43 +00:00
break;
case BabeDeadLight::LightType::Spot:
case BabeDeadLight::LightType::Spot2:
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING(
2019-07-20 04:27:21 +00:00
"lamp = bpy.data.lights.new('LAMP_{:01d}_{:03d}', 'SPOT')\n"
"lamp.color = ({},{},{})\n"
"lamp.spot_size = {:.6g}\n"
2018-12-08 05:30:43 +00:00
"lamp_obj = bpy.data.objects.new(lamp.name, lamp)\n"
"lamp_obj.rotation_mode = 'QUATERNION'\n"
2019-07-20 04:27:21 +00:00
"lamp_obj.rotation_quaternion = Vector((0,0,-1)).rotation_difference(Vector(({},{},{})))\n"
2018-12-08 05:30:43 +00:00
"lamp.shadow_soft_size = 0.5\n"
2019-07-20 04:27:21 +00:00
"lamp.use_shadow = {}\n"
"\n"),
2018-12-08 05:30:43 +00:00
s, l, light.color.simd[0], light.color.simd[1], light.color.simd[2], zeus::degToRad(light.spotCutoff),
light.direction.simd[0], light.direction.simd[1], light.direction.simd[2],
light.castShadows ? "True" : "False");
2018-12-08 05:30:43 +00:00
break;
default:
return;
}
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING(
2019-07-20 04:27:21 +00:00
"lamp.retro_layer = {}\n"
"lamp.retro_origtype = {}\n"
2018-12-08 05:30:43 +00:00
"lamp.falloff_type = 'INVERSE_COEFFICIENTS'\n"
"lamp.constant_coefficient = 0\n"
"lamp.use_nodes = True\n"
"falloff_node = lamp.node_tree.nodes.new('ShaderNodeLightFalloff')\n"
"lamp.energy = 0.0\n"
2019-07-20 04:27:21 +00:00
"falloff_node.inputs[0].default_value = {}\n"
2018-12-08 05:30:43 +00:00
"hue_sat_node = lamp.node_tree.nodes.new('ShaderNodeHueSaturation')\n"
"hue_sat_node.inputs[1].default_value = 1.25\n"
2019-07-20 04:27:21 +00:00
"hue_sat_node.inputs[4].default_value = ({},{},{},1.0)\n"
2018-12-08 05:30:43 +00:00
"lamp.node_tree.links.new(hue_sat_node.outputs[0], lamp.node_tree.nodes['Emission'].inputs[0])\n"
2019-07-20 04:27:21 +00:00
"lamp_obj.location = ({},{},{})\n"
"bpy.context.scene.collection.objects.link(lamp_obj)\n"
2019-07-20 04:27:21 +00:00
"\n"),
s, unsigned(light.lightType), light.q / 8.f, light.color.simd[0], light.color.simd[1], light.color.simd[2],
2018-12-08 05:30:43 +00:00
light.position.simd[0], light.position.simd[1], light.position.simd[2]);
2018-12-08 05:30:43 +00:00
switch (light.falloff) {
case BabeDeadLight::Falloff::Constant:
os << "falloff_node.inputs[0].default_value *= 150.0\n"
"lamp.node_tree.links.new(falloff_node.outputs[2], lamp.node_tree.nodes['Emission'].inputs[1])\n";
if (light.q > FLT_EPSILON)
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING("lamp.constant_coefficient = 2.0 / {}\n"), light.q);
2018-12-08 05:30:43 +00:00
break;
case BabeDeadLight::Falloff::Linear:
os << "lamp.node_tree.links.new(falloff_node.outputs[1], lamp.node_tree.nodes['Emission'].inputs[1])\n";
if (light.q > FLT_EPSILON)
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING("lamp.linear_coefficient = 250 / {}\n"), light.q);
2018-12-08 05:30:43 +00:00
break;
case BabeDeadLight::Falloff::Quadratic:
os << "lamp.node_tree.links.new(falloff_node.outputs[0], lamp.node_tree.nodes['Emission'].inputs[1])\n";
if (light.q > FLT_EPSILON)
2020-04-11 22:51:39 +00:00
os.format(FMT_STRING("lamp.quadratic_coefficient = 25000 / {}\n"), light.q);
2018-12-08 05:30:43 +00:00
break;
default:
break;
}
}
2018-12-08 05:30:43 +00:00
template void ReadBabeDeadLightToBlender<DNAMP1::MREA::BabeDeadLight>(hecl::blender::PyOutStream& os,
const DNAMP1::MREA::BabeDeadLight& light,
unsigned s, unsigned l);
template void ReadBabeDeadLightToBlender<DNAMP3::MREA::BabeDeadLight>(hecl::blender::PyOutStream& os,
const DNAMP3::MREA::BabeDeadLight& light,
unsigned s, unsigned l);
2018-12-08 05:30:43 +00:00
template <class BabeDeadLight>
void WriteBabeDeadLightFromBlender(BabeDeadLight& lightOut, const hecl::blender::Light& lightIn) {
using InterType = hecl::blender::Light::Type;
switch (lightIn.type) {
case InterType::Ambient:
lightOut.lightType = BabeDeadLight::LightType::LocalAmbient;
break;
case InterType::Directional:
lightOut.lightType = BabeDeadLight::LightType::Directional;
break;
case InterType::Custom:
default:
lightOut.lightType = BabeDeadLight::LightType::Custom;
break;
case InterType::Spot:
lightOut.lightType = BabeDeadLight::LightType::Spot;
break;
}
2018-12-08 05:30:43 +00:00
if (lightIn.type == InterType::Ambient) {
lightOut.falloff = BabeDeadLight::Falloff::Constant;
lightOut.q = lightIn.energy * 8.f;
} else if (lightIn.linear > lightIn.constant && lightIn.linear > lightIn.quadratic) {
lightOut.falloff = BabeDeadLight::Falloff::Linear;
lightOut.q = 250.f / lightIn.linear;
} else if (lightIn.quadratic > lightIn.constant && lightIn.quadratic > lightIn.linear) {
lightOut.falloff = BabeDeadLight::Falloff::Quadratic;
lightOut.q = 25000.f / lightIn.quadratic;
} else {
lightOut.falloff = BabeDeadLight::Falloff::Constant;
lightOut.q = 2.f / lightIn.constant;
}
2018-12-08 05:30:43 +00:00
lightOut.color = lightIn.color;
lightOut.spotCutoff = zeus::radToDeg(lightIn.spotCutoff);
lightOut.castShadows = lightIn.shadow;
lightOut.position.simd[0] = lightIn.sceneXf[0].simd[3];
lightOut.position.simd[1] = lightIn.sceneXf[1].simd[3];
lightOut.position.simd[2] = lightIn.sceneXf[2].simd[3];
2018-12-08 05:30:43 +00:00
zeus::CTransform lightXf(&lightIn.sceneXf[0]);
lightOut.direction = (lightXf.basis.transposed() * zeus::CVector3f(0.f, 0.f, -1.f)).normalized();
}
2018-12-08 05:30:43 +00:00
template void WriteBabeDeadLightFromBlender<DNAMP1::MREA::BabeDeadLight>(DNAMP1::MREA::BabeDeadLight& lightOut,
const hecl::blender::Light& lightIn);
template void WriteBabeDeadLightFromBlender<DNAMP3::MREA::BabeDeadLight>(DNAMP3::MREA::BabeDeadLight& lightOut,
const hecl::blender::Light& lightIn);
2018-12-08 05:30:43 +00:00
} // namespace DataSpec