metaforce/Runtime/Camera/CCinematicCamera.cpp

458 lines
16 KiB
C++
Raw Normal View History

#include "Runtime/Camera/CCinematicCamera.hpp"
#include "Runtime/CStateManager.hpp"
#include "Runtime/GameGlobalObjects.hpp"
#include "Runtime/Character/CAnimTreeNode.hpp"
#include "Runtime/World/CPlayer.hpp"
#include "Runtime/World/CScriptActor.hpp"
#include "Runtime/World/CScriptCameraWaypoint.hpp"
#include "TCastTo.hpp" // Generated file, do not modify include path
2016-04-24 04:03:30 +00:00
2021-04-10 08:42:06 +00:00
namespace metaforce {
2016-04-24 04:03:30 +00:00
2017-11-13 06:19:18 +00:00
CCinematicCamera::CCinematicCamera(TUniqueId uid, std::string_view name, const CEntityInfo& info,
2018-12-08 05:30:43 +00:00
const zeus::CTransform& xf, bool active, float shotDuration, float fovy, float znear,
float zfar, float aspect, u32 flags)
: CGameCamera(uid, active, name, info, xf, fovy, znear, zfar, aspect, kInvalidUniqueId, (flags & 0x20) != 0, 0)
2018-12-08 05:30:43 +00:00
, x1e8_duration(shotDuration)
, x1f0_origFovy(fovy)
, x1fc_origOrientation(zeus::CQuaternion(xf.basis))
, x21c_flags(flags) {
x220_24_ = false;
2016-04-24 04:03:30 +00:00
}
2018-12-08 05:30:43 +00:00
void CCinematicCamera::Accept(IVisitor& visitor) { visitor.Visit(this); }
2017-01-15 03:07:01 +00:00
2018-12-08 05:30:43 +00:00
void CCinematicCamera::ProcessInput(const CFinalInput&, CStateManager& mgr) {
// Empty
}
2018-12-08 05:30:43 +00:00
void CCinematicCamera::Reset(const zeus::CTransform&, CStateManager& mgr) {
// Empty
}
2017-02-18 02:19:50 +00:00
2018-12-08 05:30:43 +00:00
void CCinematicCamera::WasDeactivated(CStateManager& mgr) {
mgr.GetCameraManager()->RemoveCinemaCamera(GetUniqueId(), mgr);
mgr.GetPlayer().GetMorphBall()->LoadMorphBallModel(mgr);
if ((x21c_flags & 0x100) != 0) {
2018-12-08 05:30:43 +00:00
mgr.SetCinematicPause(false);
}
2018-12-08 05:30:43 +00:00
x188_viewPoints.clear();
x198_viewOrientations.clear();
x1a8_viewPointArrivals.clear();
x1b8_targets.clear();
x1c8_targetArrivals.clear();
x1d8_viewHFovs.clear();
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CVector3f CCinematicCamera::GetInterpolatedSplinePoint(const std::vector<zeus::CVector3f>& points, int& idxOut,
float tin) const {
if (points.empty()) {
return {};
2018-12-08 05:30:43 +00:00
}
2017-11-24 08:23:28 +00:00
const float cycleT = std::fmod(tin, x1e8_duration);
const float durPerPoint = x1e8_duration / float(points.size() - 1);
idxOut = int(cycleT / durPerPoint);
const float t = (cycleT - float(idxOut) * durPerPoint) / durPerPoint;
if (points.size() == 1) {
return points.front();
}
if (points.size() == 2) {
return (points[1] - points[0]) * t + points[0];
}
zeus::CVector3f ptA;
if (idxOut > 0) {
ptA = points[idxOut - 1];
} else {
ptA = points[0] - (points[1] - points[0]);
}
const zeus::CVector3f ptB = points[idxOut];
zeus::CVector3f ptC;
if (size_t(idxOut + 1) >= points.size()) {
const zeus::CVector3f& tmpA = points[points.size() - 1];
const zeus::CVector3f& tmpB = points[points.size() - 2];
ptC = tmpA - (tmpB - tmpA);
} else {
ptC = points[idxOut + 1];
}
zeus::CVector3f ptD;
if (size_t(idxOut + 2) >= points.size()) {
const zeus::CVector3f& tmpA = points[points.size() - 1];
const zeus::CVector3f& tmpB = points[points.size() - 2];
ptD = tmpA - (tmpB - tmpA);
} else {
ptD = points[idxOut + 2];
}
return zeus::getCatmullRomSplinePoint(ptA, ptB, ptC, ptD, t);
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
zeus::CQuaternion CCinematicCamera::GetInterpolatedOrientation(const std::vector<zeus::CQuaternion>& rotations,
float tin) const {
if (rotations.empty()) {
2018-12-08 05:30:43 +00:00
return x1fc_origOrientation;
}
if (rotations.size() == 1) {
2018-12-08 05:30:43 +00:00
return rotations.front();
}
2018-12-08 05:30:43 +00:00
const float cycleT = std::fmod(tin, x1e8_duration);
const float durPerPoint = x1e8_duration / float(rotations.size() - 1);
const int idx = int(cycleT / durPerPoint);
const float t = (cycleT - float(idx) * durPerPoint) / durPerPoint;
2018-12-08 05:30:43 +00:00
return zeus::CQuaternion::slerp(rotations[idx], rotations[idx + 1], t);
}
2017-11-24 08:23:28 +00:00
2018-12-08 05:30:43 +00:00
float CCinematicCamera::GetInterpolatedHFov(const std::vector<float>& fovs, float tin) const {
if (fovs.empty()) {
2018-12-08 05:30:43 +00:00
return x1f0_origFovy;
}
if (fovs.size() == 1) {
2018-12-08 05:30:43 +00:00
return fovs.front();
}
2018-12-08 05:30:43 +00:00
const float cycleT = std::fmod(tin, x1e8_duration);
const float durPerPoint = x1e8_duration / float(fovs.size() - 1);
const int idx = int(cycleT / durPerPoint);
const float t = (cycleT - float(idx) * durPerPoint) / durPerPoint;
2018-12-08 05:30:43 +00:00
return (fovs[idx + 1] - fovs[idx]) * t + fovs[idx];
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
float CCinematicCamera::GetMoveOutofIntoAlpha() const {
const float startDist = 0.25f + x160_znear;
const float endDist = 1.f * startDist;
const float deltaMag = (GetTranslation() - x210_moveIntoEyePos).magnitude();
if (deltaMag >= startDist && deltaMag <= endDist) {
2018-12-08 05:30:43 +00:00
return (deltaMag - startDist) / (endDist - startDist);
}
if (deltaMag > endDist) {
2018-12-08 05:30:43 +00:00
return 1.f;
}
2018-12-08 05:30:43 +00:00
return 0.f;
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
void CCinematicCamera::DeactivateSelf(CStateManager& mgr) {
SetActive(false);
SendScriptMsgs(EScriptObjectState::Inactive, mgr, EScriptObjectMessage::None);
WasDeactivated(mgr);
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
void CCinematicCamera::Think(float dt, CStateManager& mgr) {
if (GetActive()) {
zeus::CVector3f viewPoint = GetTranslation();
if (!x188_viewPoints.empty()) {
2018-12-08 05:30:43 +00:00
int idx = 0;
viewPoint = GetInterpolatedSplinePoint(x188_viewPoints, idx, x1ec_t);
if (idx > x1f4_passedViewPoint) {
x1f4_passedViewPoint = idx;
SendArrivedMsg(x1a8_viewPointArrivals[x1f4_passedViewPoint], mgr);
}
}
2017-11-24 08:23:28 +00:00
const zeus::CQuaternion orientation = GetInterpolatedOrientation(x198_viewOrientations, x1ec_t);
2018-12-08 05:30:43 +00:00
if ((x21c_flags & 0x1) == 0) {
if (!x1b8_targets.empty()) {
2018-12-08 05:30:43 +00:00
int idx = 0;
zeus::CVector3f target = GetInterpolatedSplinePoint(x1b8_targets, idx, x1ec_t);
if (x1b8_targets.size() == 1) {
if (const TCastToConstPtr<CActor> act = mgr.GetObjectById(x1c8_targetArrivals.front())) {
2018-12-08 05:30:43 +00:00
target = act->GetTranslation();
} else {
2018-12-08 05:30:43 +00:00
x1ec_t = x1e8_duration;
}
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
if (idx > x1f8_passedTarget) {
x1f8_passedTarget = idx;
SendArrivedMsg(x1c8_targetArrivals[x1f8_passedTarget], mgr);
2017-11-24 08:23:28 +00:00
}
const zeus::CVector3f upVec = orientation.transform(zeus::skUp);
if ((target - viewPoint).toVec2f().magnitude() < 0.0011920929f) {
2018-12-08 05:30:43 +00:00
SetTranslation(target);
} else {
2018-12-08 05:30:43 +00:00
SetTransform(zeus::lookAt(viewPoint, target, upVec));
}
2018-12-08 05:30:43 +00:00
} else {
SetTransform(zeus::CTransform(orientation, viewPoint));
}
} else {
zeus::CVector3f target = mgr.GetPlayer().GetTranslation();
if (mgr.GetPlayer().GetMorphballTransitionState() == CPlayer::EPlayerMorphBallState::Morphed) {
2018-12-08 05:30:43 +00:00
target.z() += mgr.GetPlayer().GetMorphBall()->GetBallRadius();
} else {
2018-12-08 05:30:43 +00:00
target.z() += mgr.GetPlayer().GetEyeHeight();
}
2018-12-08 05:30:43 +00:00
const zeus::CVector3f upVec = orientation.transform(zeus::skUp);
if ((target - viewPoint).toVec2f().magnitude() < 0.0011920929f) {
2018-12-08 05:30:43 +00:00
SetTranslation(target);
} else {
2018-12-08 05:30:43 +00:00
SetTransform(zeus::lookAt(viewPoint, target, upVec));
}
2018-12-08 05:30:43 +00:00
}
2017-11-24 08:23:28 +00:00
2018-12-08 05:30:43 +00:00
x15c_currentFov = GetInterpolatedHFov(x1d8_viewHFovs, x1ec_t) / x168_aspect;
x170_24_perspDirty = true;
if (x20c_lookAtId != kInvalidUniqueId) {
if (const TCastToPtr<CScriptActor> act = mgr.ObjectById(x20c_lookAtId)) {
if (act->IsPlayerActor()) {
2018-12-08 05:30:43 +00:00
act->SetDrawFlags({5, 0, 3, zeus::CColor(1.f, GetMoveOutofIntoAlpha())});
}
}
}
2018-12-08 05:30:43 +00:00
x1ec_t += dt;
if (x1ec_t > x1e8_duration) {
for (auto i = static_cast<size_t>(x1f4_passedViewPoint) + 1; i < x1a8_viewPointArrivals.size(); ++i) {
2018-12-08 05:30:43 +00:00
SendArrivedMsg(x1a8_viewPointArrivals[i], mgr);
}
for (auto i = static_cast<size_t>(x1f8_passedTarget) + 1; i < x1c8_targetArrivals.size(); ++i) {
2018-12-08 05:30:43 +00:00
SendArrivedMsg(x1c8_targetArrivals[i], mgr);
}
2018-12-08 05:30:43 +00:00
DeactivateSelf(mgr);
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
}
}
2018-12-08 05:30:43 +00:00
void CCinematicCamera::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId uid, CStateManager& mgr) {
CGameCamera::AcceptScriptMsg(msg, uid, mgr);
switch (msg) {
case EScriptObjectMessage::InitializedInArea:
if ((x21c_flags & 0x4) != 0 || (x21c_flags & 0x2) != 0) {
2018-12-08 05:30:43 +00:00
for (const SConnection& conn : x20_conns) {
const TUniqueId id = mgr.GetIdForScript(conn.x8_objId);
if (const TCastToConstPtr<CScriptActor> act = mgr.ObjectById(id)) {
2018-12-08 05:30:43 +00:00
if (act->IsPlayerActor()) {
x20c_lookAtId = id;
if (conn.x4_msg != EScriptObjectMessage::Deactivate && conn.x4_msg != EScriptObjectMessage::Reset) {
2018-12-08 05:30:43 +00:00
break;
}
2018-12-08 05:30:43 +00:00
}
}
2018-12-08 05:30:43 +00:00
}
}
2018-12-08 05:30:43 +00:00
break;
case EScriptObjectMessage::Activate:
CalculateWaypoints(mgr);
if ((x21c_flags & 1) == 0 && x220_24_ && x1b8_targets.empty()) {
2018-12-08 05:30:43 +00:00
break;
}
2018-12-08 05:30:43 +00:00
x1ec_t = 0.f;
Think(0.f, mgr);
mgr.GetCameraManager()->AddCinemaCamera(GetUniqueId(), mgr);
x1f4_passedViewPoint = 0;
if (!x1a8_viewPointArrivals.empty()) {
2018-12-08 05:30:43 +00:00
SendArrivedMsg(x1a8_viewPointArrivals[x1f4_passedViewPoint], mgr);
}
2018-12-08 05:30:43 +00:00
x1f8_passedTarget = 0;
if (!x1c8_targetArrivals.empty()) {
2018-12-08 05:30:43 +00:00
SendArrivedMsg(x1c8_targetArrivals[x1f8_passedTarget], mgr);
}
if ((x21c_flags & 0x100) != 0) {
2018-12-08 05:30:43 +00:00
mgr.SetCinematicPause(true);
}
2018-12-08 05:30:43 +00:00
break;
case EScriptObjectMessage::Deactivate:
WasDeactivated(mgr);
break;
default:
break;
}
}
2017-02-18 02:19:50 +00:00
2018-12-08 05:30:43 +00:00
void CCinematicCamera::CalculateMoveOutofIntoEyePosition(bool outOfEye, CStateManager& mgr) {
zeus::CQuaternion q(mgr.GetPlayer().GetTransform().basis);
zeus::CVector3f eyePos = mgr.GetPlayer().GetEyePosition();
if (x20c_lookAtId != kInvalidUniqueId) {
if (const TCastToConstPtr<CScriptActor> act = mgr.GetObjectById(x20c_lookAtId)) {
2018-12-08 05:30:43 +00:00
if (act->IsPlayerActor()) {
if (const CModelData* mData = act->GetModelData()) {
if (const CAnimData* aData = mData->GetAnimationData()) {
if (const CAnimTreeNode* root = aData->GetRootAnimationTree().get()) {
const CSegId lEye = aData->GetLocatorSegId("L_eye"sv);
const CSegId rEye = aData->GetLocatorSegId("R_eye"sv);
if (lEye.IsValid() && rEye.IsValid()) {
const CCharAnimTime time =
outOfEye ? CCharAnimTime(0.f) : root->VGetSteadyStateAnimInfo().GetDuration();
const CCharAnimTime* pTime = outOfEye ? nullptr : &time;
2018-12-08 05:30:43 +00:00
eyePos = ((act->GetTransform() * mData->GetScaledLocatorTransformDynamic("L_eye"sv, pTime)).origin +
(act->GetTransform() * mData->GetScaledLocatorTransformDynamic("R_eye"sv, pTime)).origin) *
0.5f;
q = zeus::CQuaternion(act->GetTransform().basis);
}
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
}
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
}
zeus::CVector3f behindPos = eyePos;
zeus::CVector3f behindDelta = q.transform({0.f, -g_tweakPlayerRes->xf0_cinematicMoveOutofIntoPlayerDistance, 0.f});
if (!outOfEye) {
behindPos += behindDelta;
behindDelta = -behindDelta;
}
for (size_t i = 0; i < 2; ++i) {
2018-12-08 05:30:43 +00:00
x188_viewPoints[outOfEye ? i : x188_viewPoints.size() - (2 - i)] = behindPos;
x198_viewOrientations[outOfEye ? i : x198_viewOrientations.size() - (2 - i)] = q;
x1b8_targets[outOfEye ? i : x1b8_targets.size() - (2 - i)] = eyePos;
behindPos += behindDelta;
}
x210_moveIntoEyePos = eyePos;
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
void CCinematicCamera::GenerateMoveOutofIntoPoints(bool outOfEye, CStateManager& mgr) {
const zeus::CQuaternion q(mgr.GetPlayer().GetTransform().basis);
const zeus::CVector3f eyePos = mgr.GetPlayer().GetEyePosition();
2018-12-08 05:30:43 +00:00
zeus::CVector3f behindDelta = q.transform({0.f, -g_tweakPlayerRes->xf0_cinematicMoveOutofIntoPlayerDistance, 0.f});
zeus::CVector3f behindPos = eyePos;
if (!outOfEye) {
behindPos += behindDelta;
behindDelta = -behindDelta;
}
for (int i = 0; i < 2; ++i) {
x188_viewPoints.emplace_back(behindPos);
x198_viewOrientations.emplace_back(q);
x1a8_viewPointArrivals.emplace_back(mgr.GetPlayer().GetUniqueId());
x1b8_targets.emplace_back(eyePos);
x1c8_targetArrivals.emplace_back(kInvalidUniqueId);
2018-12-08 05:30:43 +00:00
behindPos += behindDelta;
}
CalculateMoveOutofIntoEyePosition(outOfEye, mgr);
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
bool CCinematicCamera::PickRandomActiveConnection(const std::vector<SConnection>& conns, SConnection& randConn,
CStateManager& mgr) {
int count = 0;
for (const SConnection& conn : conns) {
if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) {
if (const TCastToConstPtr<CActor> act = mgr.GetObjectById(mgr.GetIdForScript(conn.x8_objId))) {
if (act->GetActive()) {
2018-12-08 05:30:43 +00:00
++count;
}
}
}
}
2018-12-08 05:30:43 +00:00
if (count == 0) {
2018-12-08 05:30:43 +00:00
return false;
}
2018-12-08 05:30:43 +00:00
const int randIdx = mgr.GetActiveRandom()->Next() % count;
2018-12-08 05:30:43 +00:00
int idx = 0;
for (const SConnection& conn : conns) {
if (conn.x0_state == EScriptObjectState::Arrived && conn.x4_msg == EScriptObjectMessage::Next) {
if (const TCastToConstPtr<CActor> act = mgr.GetObjectById(mgr.GetIdForScript(conn.x8_objId))) {
2018-12-08 05:30:43 +00:00
if (act->GetActive()) {
if (randIdx == idx) {
randConn = conn;
break;
}
++idx;
}
}
}
}
2017-11-24 08:23:28 +00:00
2018-12-08 05:30:43 +00:00
return true;
}
2017-11-24 08:23:28 +00:00
2018-12-08 05:30:43 +00:00
void CCinematicCamera::CalculateWaypoints(CStateManager& mgr) {
const SConnection* firstVP = nullptr;
const SConnection* firstTarget = nullptr;
for (const SConnection& conn : x20_conns) {
if (conn.x0_state == EScriptObjectState::CameraPath && conn.x4_msg == EScriptObjectMessage::Activate) {
2018-12-08 05:30:43 +00:00
firstVP = &conn;
} else if (conn.x0_state == EScriptObjectState::CameraTarget && conn.x4_msg == EScriptObjectMessage::Activate) {
2018-12-08 05:30:43 +00:00
firstTarget = &conn;
}
2018-12-08 05:30:43 +00:00
}
x188_viewPoints.clear();
x188_viewPoints.reserve(3);
x198_viewOrientations.clear();
x198_viewOrientations.reserve(3);
x1a8_viewPointArrivals.clear();
x1a8_viewPointArrivals.reserve(3);
x1b8_targets.clear();
x1b8_targets.reserve(3);
x1c8_targetArrivals.clear();
x1c8_targetArrivals.reserve(3);
x1d8_viewHFovs.clear();
x1d8_viewHFovs.reserve(3);
x220_24_ = false;
if ((x21c_flags & 0x2) != 0 && (x21c_flags & 0x200) == 0) {
2018-12-08 05:30:43 +00:00
GenerateMoveOutofIntoPoints(true, mgr);
}
2018-12-08 05:30:43 +00:00
if (firstVP) {
TCastToConstPtr<CActor> wp = mgr.GetObjectById(mgr.GetIdForScript(firstVP->x8_objId));
while (wp) {
x188_viewPoints.push_back(wp->GetTranslation());
x198_viewOrientations.emplace_back(wp->GetTransform().basis);
if (const TCastToConstPtr<CScriptCameraWaypoint> cwp = wp.GetPtr()) {
2018-12-08 05:30:43 +00:00
x1d8_viewHFovs.push_back(cwp->GetHFov());
}
const auto search = std::find_if(x1a8_viewPointArrivals.cbegin(), x1a8_viewPointArrivals.cend(),
[&wp](TUniqueId id) { return id == wp->GetUniqueId(); });
if (search == x1a8_viewPointArrivals.cend()) {
2018-12-08 05:30:43 +00:00
x1a8_viewPointArrivals.push_back(wp->GetUniqueId());
SConnection randConn;
if (PickRandomActiveConnection(wp->GetConnectionList(), randConn, mgr)) {
2018-12-08 05:30:43 +00:00
wp = mgr.GetObjectById(mgr.GetIdForScript(randConn.x8_objId));
} else {
2018-12-08 05:30:43 +00:00
break;
}
} else {
2018-12-08 05:30:43 +00:00
break;
}
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
}
if (firstTarget) {
TCastToConstPtr<CActor> tgt = mgr.GetObjectById(mgr.GetIdForScript(firstTarget->x8_objId));
while (tgt) {
x1b8_targets.push_back(tgt->GetTranslation());
const auto search = std::find_if(x1c8_targetArrivals.cbegin(), x1c8_targetArrivals.cend(),
[&tgt](TUniqueId id) { return id == tgt->GetUniqueId(); });
if (search == x1c8_targetArrivals.cend()) {
2018-12-08 05:30:43 +00:00
x1c8_targetArrivals.push_back(tgt->GetUniqueId());
SConnection randConn;
if (PickRandomActiveConnection(tgt->GetConnectionList(), randConn, mgr)) {
2018-12-08 05:30:43 +00:00
tgt = mgr.GetObjectById(mgr.GetIdForScript(randConn.x8_objId));
} else {
2018-12-08 05:30:43 +00:00
break;
}
} else {
2018-12-08 05:30:43 +00:00
break;
}
2017-11-24 08:23:28 +00:00
}
2018-12-08 05:30:43 +00:00
}
if ((x21c_flags & 0x4) != 0 && (x21c_flags & 0x200) == 0) {
2018-12-08 05:30:43 +00:00
GenerateMoveOutofIntoPoints(false, mgr);
}
}
2018-12-08 05:30:43 +00:00
void CCinematicCamera::SendArrivedMsg(TUniqueId reciever, CStateManager& mgr) {
mgr.SendScriptMsgAlways(reciever, GetUniqueId(), EScriptObjectMessage::Arrived);
2016-04-24 04:03:30 +00:00
}
2021-04-10 08:42:06 +00:00
} // namespace metaforce