Skip to content

Commit

Permalink
Port some more stuff from SiraUtil to here for the time being, still …
Browse files Browse the repository at this point in the history
…no luck on saber coloring
  • Loading branch information
Fernthedev committed Feb 28, 2021
1 parent 55dac0b commit b9221e9
Showing 1 changed file with 100 additions and 27 deletions.
127 changes: 100 additions & 27 deletions src/colorizer/SaberColorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@
#include "GlobalNamespace/SetSaberGlowColor_PropertyTintColorPair.hpp"
#include "GlobalNamespace/Parametric3SliceSpriteController.hpp"
#include "GlobalNamespace/SaberManager.hpp"
#include "GlobalNamespace/SaberModelController.hpp"
#include "GlobalNamespace/SaberBurnMarkSparkles.hpp"
#include "UnityEngine/LineRenderer.hpp"
#include "UnityEngine/ParticleSystem.hpp"
#include "UnityEngine/ParticleSystem_MainModule.hpp"
#include "UnityEngine/Mathf.hpp"
#include "UnityEngine/ParticleSystem_MinMaxGradient.hpp"
#include "hooks/SaberManager.hpp"

#include <unordered_map>
Expand Down Expand Up @@ -106,49 +113,115 @@ void OverrideColor(SetSaberFakeGlowColor* ssfgc, UnityEngine::Color color) {
}



void ChangeColorCoroutine2(Saber *instance, UnityEngine::Color color) {
getLogger().debug("Coloring saber model %d", (int) instance->get_saberType());

auto *modelController = instance->get_gameObject()->GetComponentInChildren<SaberModelController *>(true);


// if (modelController is IColorable colorable)
// {
// colorable.SetColor(color);
// }

auto tintColor = modelController->initData->trailTintColor;
Array<SetSaberGlowColor *> *setSaberGlowColors = modelController->setSaberGlowColors;
Array<SetSaberFakeGlowColor *> *setSaberFakeGlowColors = modelController->setSaberFakeGlowColors;

modelController->saberTrail->color = (color * tintColor).get_linear();

for (int i = 0; i != setSaberGlowColors->Length(); i++) {
OverrideColor(setSaberGlowColors->values[i], color);
}
for (int i = 0; i < setSaberFakeGlowColors->Length(); i++) {
OverrideColor(setSaberFakeGlowColors->values[i], color);
}
getLogger().debug("Model controller klass %s", modelController->klass->name);

TubeBloomPrePassLight *saberLight = modelController->saberLight;


if (saberLight) {
saberLight->color = color;
} else {
getLogger().debug("Saber light is null, should be normal right?");
}
}

float linearToGamma(float c) {
return 255.0f * std::pow(c/255.0f, 1/2.2f);
}

UnityEngine::Color getGamma(UnityEngine::Color color) {
return UnityEngine::Color(linearToGamma(color.r), linearToGamma(color.g), linearToGamma(color.b), color.a);
}

UnityEngine::Color getSaberColor(Saber* saber) {
auto* modelController = saber->get_gameObject()->GetComponentInChildren<SaberModelController*>(true);
// if (modelController is IColorable)
// {
// var colorable = modelController as IColorable;
// return colorable.Color;
// }

return getGamma(modelController->saberTrail->color); // smc.Accessors.TrailColor(ref Accessors.SaberTrail(ref smc)).gamma;

}

// https://github.com/Auros/SiraUtil/blob/f40d4ca44f5e6632ed74606cb4d6676546f6f56e/SiraUtil/Sabers/SiraSaber.cs#L164
custom_types::Helpers::Coroutine ChangeColorCoroutine(Saber *instance, UnityEngine::Color color) {
co_yield reinterpret_cast<enumeratorT*>(CRASH_UNLESS(il2cpp_utils::New<UnityEngine::WaitForSecondsRealtime*>(0.05f)));
custom_types::Helpers::Coroutine ChangeColorCoroutine(Saber *saber, UnityEngine::Color color) {
co_yield reinterpret_cast<enumeratorT *>(CRASH_UNLESS(
il2cpp_utils::New<UnityEngine::WaitForSecondsRealtime *>(0.05f)));


getLogger().debug("Change color coroutine");
if (instance->get_isActiveAndEnabled()) {
// In th epc version, this starts another coroutine which does the color changing
if (saber->get_isActiveAndEnabled()) {
// In the pc version, this starts another coroutine which does the color changing
// However, I believe we can get hopefully away without making another coroutine class since
// the wait time is set to 0 anyways

getLogger().debug("Coloring saber model %d",(int) instance->get_saberType());
ChangeColorCoroutine2(saber, color);
}

auto *modelController = instance->get_gameObject()->GetComponentInChildren<SaberModelController *>(true);

UnityEngine::Color saberColor = getSaberColor(saber);
float h;
float s;
float ignored;
UnityEngine::Color::RGBToHSV(saberColor, h, s, ignored);
saberColor = UnityEngine::Color::HSVToRGB(h, s, 1.0f);

// if (modelController is IColorable colorable)
// {
// colorable.SetColor(color);
// }
auto saberBurnMarkSparkles = saber->GetComponentInChildren<SaberBurnMarkSparkles *>();

auto tintColor = modelController->initData->trailTintColor;
Array<SetSaberGlowColor *> *setSaberGlowColors = modelController->setSaberGlowColors;
Array<SetSaberFakeGlowColor *> *setSaberFakeGlowColors = modelController->setSaberFakeGlowColors;
TubeBloomPrePassLight *light = modelController->saberLight;
if (saberBurnMarkSparkles) {
auto saberMarkPS = saberBurnMarkSparkles->burnMarksPS;

modelController->saberTrail->color = (color * tintColor).get_linear();
for (int i = 0; i < saberMarkPS->Length(); i++) {
auto sPS = saberMarkPS->values[i];

for (int i = 0; i != setSaberGlowColors->Length(); i++)
{
OverrideColor(setSaberGlowColors->values[i], color);
}
for (int i = 0; i < setSaberFakeGlowColors->Length(); i++)
{
OverrideColor(setSaberFakeGlowColors->values[i], color);
}
if (light != nullptr)
{
light->color = color;
sPS->get_main().set_startColor(ParticleSystem::MinMaxGradient(saberColor));
}
}

auto saberBurnMarkArea = saber->GetComponentInChildren<SaberBurnMarkArea *>();
if (saberBurnMarkArea) {

auto lineRenderers = saberBurnMarkArea->lineRenderers;

for (int i = 0; i < lineRenderers->Length(); i++) {
auto lineRenderer = lineRenderers->values[i];

lineRenderer->set_startColor(saberColor);
lineRenderer->set_endColor(saberColor);
lineRenderer->set_positionCount(2);
}
}
coroutineSabers.erase(instance->get_saberType().value);


coroutineSabers.erase(saber->get_saberType().value);

co_return;
}

// Must be down here to avoid compile issues
Expand Down

0 comments on commit b9221e9

Please sign in to comment.