2019-12-22 20:04:07 +00:00
|
|
|
#include "Runtime/Particle/CDecal.hpp"
|
|
|
|
|
|
|
|
#include "Runtime/Graphics/CModel.hpp"
|
|
|
|
#include "Runtime/Graphics/Shaders/CDecalShaders.hpp"
|
|
|
|
#include "Runtime/Particle/CParticleGlobals.hpp"
|
2016-08-04 22:24:28 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
namespace metaforce {
|
2018-11-10 02:47:07 +00:00
|
|
|
CRandom16 CDecal::sDecalRandom;
|
2018-03-17 03:41:01 +00:00
|
|
|
bool CDecal::sMoveRedToAlphaBuffer = false;
|
2016-08-04 22:24:28 +00:00
|
|
|
|
|
|
|
CDecal::CDecal(const TToken<CDecalDescription>& desc, const zeus::CTransform& xf)
|
2020-04-20 04:57:50 +00:00
|
|
|
: x0_description(desc), xc_transform(xf) {
|
2018-12-08 05:30:43 +00:00
|
|
|
CGlobalRandom gr(sDecalRandom);
|
2016-08-04 22:24:28 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CDecalDescription& desco = *x0_description;
|
|
|
|
x5c_31_quad1Invalid = InitQuad(x3c_decalQuads[0], desco.x0_Quads[0]);
|
|
|
|
x5c_30_quad2Invalid = InitQuad(x3c_decalQuads[1], desco.x0_Quads[1]);
|
2017-01-22 17:36:25 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
CDecalDescription* d = x0_description.GetObj();
|
|
|
|
if (d->x38_DMDL) {
|
|
|
|
if (d->x48_DLFT)
|
|
|
|
d->x48_DLFT->GetValue(0, x54_modelLifetime);
|
|
|
|
else
|
|
|
|
x54_modelLifetime = 0x7FFFFF;
|
|
|
|
|
|
|
|
if (d->x50_DMRT)
|
|
|
|
d->x50_DMRT->GetValue(0, x60_rotation);
|
2020-03-21 05:51:28 +00:00
|
|
|
} else {
|
2018-12-08 05:30:43 +00:00
|
|
|
x5c_29_modelInvalid = true;
|
2020-03-21 05:51:28 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2022-02-01 00:06:54 +00:00
|
|
|
// CGraphics::CommitResources([this](boo::IGraphicsDataFactory::Context& ctx) {
|
|
|
|
// for (auto& decal : x3c_decalQuads) {
|
|
|
|
// if (decal.m_desc->x14_TEX) {
|
|
|
|
// decal.m_instBuf = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SParticleInstanceTex), 1);
|
|
|
|
// } else {
|
|
|
|
// decal.m_instBuf = ctx.newDynamicBuffer(boo::BufferUse::Vertex, sizeof(SParticleInstanceNoTex), 1);
|
|
|
|
// }
|
|
|
|
// decal.m_uniformBuf = ctx.newDynamicBuffer(boo::BufferUse::Uniform, sizeof(SParticleUniforms), 1);
|
|
|
|
// CDecalShaders::BuildShaderDataBinding(ctx, decal);
|
|
|
|
// }
|
|
|
|
// return true;
|
|
|
|
// } BooTrace);
|
2017-01-22 17:36:25 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
bool CDecal::InitQuad(CQuadDecal& quad, const SQuadDescr& desc) {
|
|
|
|
quad.m_desc = &desc;
|
|
|
|
if (desc.x14_TEX) {
|
|
|
|
if (desc.x0_LFT)
|
|
|
|
desc.x0_LFT->GetValue(0, quad.x4_lifetime);
|
2018-03-17 03:41:01 +00:00
|
|
|
else
|
2018-12-08 05:30:43 +00:00
|
|
|
quad.x4_lifetime = 0x7FFFFF;
|
|
|
|
if (desc.x8_ROT) {
|
|
|
|
desc.x8_ROT->GetValue(0, quad.x8_rotation);
|
|
|
|
quad.x0_24_invalid = desc.x8_ROT->IsConstant();
|
2018-03-17 03:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (desc.x4_SZE) {
|
|
|
|
quad.x0_24_invalid = desc.x4_SZE->IsConstant();
|
|
|
|
float size = 1.f;
|
|
|
|
desc.x4_SZE->GetValue(0, size);
|
|
|
|
quad.x0_24_invalid = size <= 1.f;
|
2018-03-17 03:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
if (desc.xc_OFF)
|
|
|
|
quad.x0_24_invalid = desc.xc_OFF->IsFastConstant();
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-17 03:41:01 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
quad.x0_24_invalid = false;
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-17 03:41:01 +00:00
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CDecal::SetGlobalSeed(u16 seed) { sDecalRandom.SetSeed(seed); }
|
|
|
|
|
|
|
|
void CDecal::SetMoveRedToAlphaBuffer(bool move) { sMoveRedToAlphaBuffer = move; }
|
|
|
|
|
|
|
|
void CDecal::RenderQuad(CQuadDecal& decal, const SQuadDescr& desc) const {
|
2019-02-24 07:15:54 +00:00
|
|
|
zeus::CColor color = zeus::skWhite;
|
2018-12-08 05:30:43 +00:00
|
|
|
float size = 1.f;
|
|
|
|
zeus::CVector3f offset;
|
|
|
|
if (CColorElement* clr = desc.x10_CLR.get())
|
|
|
|
clr->GetValue(x58_frameIdx, color);
|
|
|
|
if (CRealElement* sze = desc.x4_SZE.get()) {
|
|
|
|
sze->GetValue(x58_frameIdx, size);
|
|
|
|
size *= 0.5f;
|
|
|
|
}
|
|
|
|
if (CRealElement* rot = desc.x8_ROT.get())
|
|
|
|
rot->GetValue(x58_frameIdx, decal.x8_rotation);
|
|
|
|
if (CVectorElement* off = desc.xc_OFF.get()) {
|
|
|
|
off->GetValue(x58_frameIdx, offset);
|
|
|
|
offset.y() = 0.f;
|
|
|
|
}
|
|
|
|
zeus::CTransform modXf = xc_transform;
|
|
|
|
modXf.origin += offset;
|
|
|
|
CGraphics::SetModelMatrix(modXf);
|
|
|
|
|
|
|
|
SParticleUniforms uniformData = {
|
|
|
|
CGraphics::GetPerspectiveProjectionMatrix(true) * CGraphics::g_GXModelView.toMatrix4f(), {1.f, 1.f, 1.f, 1.f}};
|
2022-02-01 00:06:54 +00:00
|
|
|
// decal.m_uniformBuf->load(&uniformData, sizeof(SParticleUniforms));
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
bool redToAlpha = sMoveRedToAlphaBuffer && desc.x18_ADD && desc.x14_TEX;
|
|
|
|
|
|
|
|
SUVElementSet uvSet = {0.f, 1.f, 0.f, 1.f};
|
|
|
|
if (CUVElement* tex = desc.x14_TEX.get()) {
|
|
|
|
TLockedToken<CTexture> texObj = tex->GetValueTexture(x58_frameIdx);
|
|
|
|
if (!texObj.IsLoaded())
|
|
|
|
return;
|
|
|
|
tex->GetValueUV(x58_frameIdx, uvSet);
|
2022-02-01 00:06:54 +00:00
|
|
|
// if (redToAlpha)
|
|
|
|
// CGraphics::SetShaderDataBinding(decal.m_redToAlphaDataBind);
|
|
|
|
// else
|
|
|
|
// CGraphics::SetShaderDataBinding(decal.m_normalDataBind);
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
g_instTexData.clear();
|
|
|
|
g_instTexData.reserve(1);
|
|
|
|
|
2020-03-13 20:32:24 +00:00
|
|
|
SParticleInstanceTex& inst = g_instTexData.emplace_back();
|
2018-12-08 05:30:43 +00:00
|
|
|
if (decal.x8_rotation == 0.f) {
|
|
|
|
inst.pos[0] = zeus::CVector3f(-size, 0.001f, size);
|
|
|
|
inst.pos[1] = zeus::CVector3f(size, 0.001f, size);
|
|
|
|
inst.pos[2] = zeus::CVector3f(-size, 0.001f, -size);
|
|
|
|
inst.pos[3] = zeus::CVector3f(size, 0.001f, -size);
|
|
|
|
} else {
|
|
|
|
float ang = zeus::degToRad(decal.x8_rotation);
|
|
|
|
float sinSize = std::sin(ang) * size;
|
|
|
|
float cosSize = std::cos(ang) * size;
|
|
|
|
inst.pos[0] = zeus::CVector3f(sinSize - cosSize, 0.001f, cosSize + sinSize);
|
|
|
|
inst.pos[1] = zeus::CVector3f(cosSize + sinSize, 0.001f, cosSize - sinSize);
|
|
|
|
inst.pos[2] = zeus::CVector3f(-(cosSize + sinSize), 0.001f, -(cosSize - sinSize));
|
|
|
|
inst.pos[3] = zeus::CVector3f(-sinSize + cosSize, 0.001f, -cosSize - sinSize);
|
2018-03-17 03:41:01 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
inst.color = color;
|
|
|
|
inst.uvs[0] = zeus::CVector2f(uvSet.xMin, uvSet.yMin);
|
|
|
|
inst.uvs[1] = zeus::CVector2f(uvSet.xMax, uvSet.yMin);
|
|
|
|
inst.uvs[2] = zeus::CVector2f(uvSet.xMin, uvSet.yMax);
|
|
|
|
inst.uvs[3] = zeus::CVector2f(uvSet.xMax, uvSet.yMax);
|
|
|
|
|
2022-02-01 00:06:54 +00:00
|
|
|
// decal.m_instBuf->load(g_instTexData.data(), g_instTexData.size() * sizeof(SParticleInstanceTex));
|
|
|
|
// CGraphics::DrawInstances(0, 4, g_instTexData.size());
|
2018-12-08 05:30:43 +00:00
|
|
|
} else {
|
|
|
|
g_instNoTexData.clear();
|
|
|
|
g_instNoTexData.reserve(1);
|
|
|
|
|
2020-03-13 20:32:24 +00:00
|
|
|
SParticleInstanceNoTex& inst = g_instNoTexData.emplace_back();
|
2018-12-08 05:30:43 +00:00
|
|
|
if (decal.x8_rotation == 0.f) {
|
|
|
|
inst.pos[0] = zeus::CVector3f(-size, 0.001f, size);
|
|
|
|
inst.pos[1] = zeus::CVector3f(size, 0.001f, size);
|
|
|
|
inst.pos[2] = zeus::CVector3f(-size, 0.001f, -size);
|
|
|
|
inst.pos[3] = zeus::CVector3f(size, 0.001f, -size);
|
|
|
|
} else {
|
|
|
|
float ang = zeus::degToRad(decal.x8_rotation);
|
|
|
|
float sinSize = std::sin(ang) * size;
|
|
|
|
float cosSize = std::cos(ang) * size;
|
|
|
|
inst.pos[0] = zeus::CVector3f(sinSize - cosSize, 0.001f, cosSize + sinSize);
|
|
|
|
inst.pos[1] = zeus::CVector3f(cosSize + sinSize, 0.001f, cosSize - sinSize);
|
|
|
|
inst.pos[2] = zeus::CVector3f(-(cosSize + sinSize), 0.001f, -(cosSize - sinSize));
|
|
|
|
inst.pos[3] = zeus::CVector3f(-sinSize + cosSize, 0.001f, -cosSize - sinSize);
|
2018-03-17 03:41:01 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
inst.color = color;
|
2018-03-17 03:41:01 +00:00
|
|
|
|
2022-02-01 00:06:54 +00:00
|
|
|
// decal.m_instBuf->load(g_instNoTexData.data(), g_instNoTexData.size() * sizeof(SParticleInstanceNoTex));
|
|
|
|
// CGraphics::DrawInstances(0, 4, g_instNoTexData.size());
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-01-22 17:36:25 +00:00
|
|
|
}
|
2017-03-04 04:31:08 +00:00
|
|
|
|
2020-04-09 17:28:20 +00:00
|
|
|
void CDecal::RenderMdl() {
|
2018-12-08 05:30:43 +00:00
|
|
|
const CDecalDescription& desc = *x0_description;
|
2019-02-24 07:15:54 +00:00
|
|
|
zeus::CColor color = zeus::skWhite;
|
2018-12-08 05:30:43 +00:00
|
|
|
zeus::CVector3f dmop;
|
|
|
|
zeus::CTransform rotXf;
|
|
|
|
|
|
|
|
if (!desc.x5c_25_DMOO)
|
|
|
|
rotXf = xc_transform.getRotation();
|
|
|
|
|
|
|
|
bool dmrtIsConst = false;
|
|
|
|
if (CVectorElement* dmrt = desc.x50_DMRT.get())
|
|
|
|
dmrtIsConst = dmrt->IsFastConstant();
|
|
|
|
|
|
|
|
zeus::CTransform dmrtXf;
|
|
|
|
if (dmrtIsConst) {
|
2020-04-09 17:28:20 +00:00
|
|
|
desc.x50_DMRT->GetValue(x58_frameIdx, x60_rotation);
|
2018-12-08 05:30:43 +00:00
|
|
|
dmrtXf = zeus::CTransform::RotateZ(zeus::degToRad(x60_rotation.z()));
|
|
|
|
dmrtXf.rotateLocalY(zeus::degToRad(x60_rotation.y()));
|
|
|
|
dmrtXf.rotateLocalX(zeus::degToRad(x60_rotation.x()));
|
|
|
|
}
|
|
|
|
|
|
|
|
dmrtXf = rotXf * dmrtXf;
|
|
|
|
|
|
|
|
if (CVectorElement* dmopo = desc.x4c_DMOP.get())
|
|
|
|
dmopo->GetValue(x58_frameIdx, dmop);
|
|
|
|
|
|
|
|
zeus::CTransform worldXf = zeus::CTransform::Translate(rotXf * dmop + xc_transform.origin);
|
|
|
|
|
|
|
|
if (dmrtIsConst) {
|
|
|
|
worldXf = worldXf * dmrtXf;
|
|
|
|
} else {
|
|
|
|
if (CVectorElement* dmrt = desc.x50_DMRT.get()) {
|
|
|
|
zeus::CVector3f dmrtVec;
|
|
|
|
dmrt->GetValue(x58_frameIdx, dmrtVec);
|
|
|
|
dmrtXf = zeus::CTransform::RotateZ(zeus::degToRad(dmrtVec.z()));
|
|
|
|
dmrtXf.rotateLocalY(zeus::degToRad(dmrtVec.y()));
|
|
|
|
dmrtXf.rotateLocalX(zeus::degToRad(dmrtVec.x()));
|
|
|
|
worldXf = worldXf * rotXf * dmrtXf;
|
|
|
|
} else {
|
|
|
|
worldXf = worldXf * dmrtXf;
|
2018-03-17 03:41:01 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CVectorElement* dmsc = desc.x54_DMSC.get()) {
|
|
|
|
zeus::CVector3f dmscVec;
|
|
|
|
dmsc->GetValue(x58_frameIdx, dmscVec);
|
|
|
|
worldXf = worldXf * zeus::CTransform::Scale(dmscVec);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CColorElement* dmcl = desc.x58_DMCL.get())
|
|
|
|
dmcl->GetValue(x58_frameIdx, color);
|
|
|
|
|
|
|
|
CGraphics::SetModelMatrix(worldXf);
|
|
|
|
|
|
|
|
if (desc.x5c_24_DMAB) {
|
2020-04-11 20:53:59 +00:00
|
|
|
const CModelFlags flags(7, 0, 1, color);
|
2018-12-08 05:30:43 +00:00
|
|
|
desc.x38_DMDL.m_token->Draw(flags);
|
|
|
|
} else {
|
|
|
|
if (color.a() == 1.f) {
|
2020-04-11 20:53:59 +00:00
|
|
|
constexpr CModelFlags flags(0, 0, 3, zeus::skWhite);
|
2018-12-08 05:30:43 +00:00
|
|
|
desc.x38_DMDL.m_token->Draw(flags);
|
|
|
|
} else {
|
2020-04-11 20:53:59 +00:00
|
|
|
const CModelFlags flags(5, 0, 1, color);
|
2018-12-08 05:30:43 +00:00
|
|
|
desc.x38_DMDL.m_token->Draw(flags);
|
2018-03-17 03:41:01 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
2017-03-04 04:31:08 +00:00
|
|
|
}
|
2018-03-17 03:41:01 +00:00
|
|
|
|
2020-04-09 17:28:20 +00:00
|
|
|
void CDecal::Render() {
|
2019-07-21 08:42:52 +00:00
|
|
|
SCOPED_GRAPHICS_DEBUG_GROUP("CDecal::Render", zeus::skYellow);
|
2018-12-08 05:30:43 +00:00
|
|
|
CGlobalRandom gr(sDecalRandom);
|
|
|
|
if (x5c_29_modelInvalid && x5c_30_quad2Invalid && x5c_31_quad1Invalid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
CGraphics::DisableAllLights();
|
2019-12-03 08:47:05 +00:00
|
|
|
CParticleGlobals::instance()->SetEmitterTime(x58_frameIdx);
|
2018-12-08 05:30:43 +00:00
|
|
|
|
|
|
|
const CDecalDescription& desc = *x0_description;
|
|
|
|
if (desc.x0_Quads[0].x14_TEX && !x5c_31_quad1Invalid) {
|
2019-12-03 08:47:05 +00:00
|
|
|
CParticleGlobals::instance()->SetParticleLifetime(x3c_decalQuads[0].x4_lifetime);
|
|
|
|
CParticleGlobals::instance()->UpdateParticleLifetimeTweenValues(x58_frameIdx);
|
2020-04-09 17:28:20 +00:00
|
|
|
RenderQuad(x3c_decalQuads[0], desc.x0_Quads[0]);
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
if (desc.x0_Quads[1].x14_TEX && !x5c_30_quad2Invalid) {
|
2019-12-03 08:47:05 +00:00
|
|
|
CParticleGlobals::instance()->SetParticleLifetime(x3c_decalQuads[1].x4_lifetime);
|
|
|
|
CParticleGlobals::instance()->UpdateParticleLifetimeTweenValues(x58_frameIdx);
|
2020-04-09 17:28:20 +00:00
|
|
|
RenderQuad(x3c_decalQuads[1], desc.x0_Quads[1]);
|
2018-12-08 05:30:43 +00:00
|
|
|
}
|
|
|
|
if (desc.x38_DMDL && !x5c_29_modelInvalid) {
|
2019-12-03 08:47:05 +00:00
|
|
|
CParticleGlobals::instance()->SetParticleLifetime(x54_modelLifetime);
|
|
|
|
CParticleGlobals::instance()->UpdateParticleLifetimeTweenValues(x58_frameIdx);
|
2018-12-08 05:30:43 +00:00
|
|
|
RenderMdl();
|
|
|
|
}
|
2018-03-17 03:41:01 +00:00
|
|
|
}
|
|
|
|
|
2018-12-08 05:30:43 +00:00
|
|
|
void CDecal::Update(float dt) {
|
|
|
|
if (x58_frameIdx >= x3c_decalQuads[0].x4_lifetime)
|
|
|
|
x5c_31_quad1Invalid = true;
|
|
|
|
if (x58_frameIdx >= x3c_decalQuads[1].x4_lifetime)
|
|
|
|
x5c_30_quad2Invalid = true;
|
|
|
|
if (x58_frameIdx >= x54_modelLifetime)
|
|
|
|
x5c_29_modelInvalid = true;
|
|
|
|
++x58_frameIdx;
|
2016-08-04 22:24:28 +00:00
|
|
|
}
|
2018-12-08 05:30:43 +00:00
|
|
|
|
2021-04-10 08:42:06 +00:00
|
|
|
} // namespace metaforce
|