diff options
author | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
commit | 15740faf9fe9fe4be08965098bbf2947e096aeeb (patch) | |
tree | a730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Graphics/ParticleSystem/Modules/UVModule.cpp |
Diffstat (limited to 'Runtime/Graphics/ParticleSystem/Modules/UVModule.cpp')
-rw-r--r-- | Runtime/Graphics/ParticleSystem/Modules/UVModule.cpp | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/Runtime/Graphics/ParticleSystem/Modules/UVModule.cpp b/Runtime/Graphics/ParticleSystem/Modules/UVModule.cpp new file mode 100644 index 0000000..67d49b3 --- /dev/null +++ b/Runtime/Graphics/ParticleSystem/Modules/UVModule.cpp @@ -0,0 +1,105 @@ +#include "UnityPrefix.h" +#include "UVModule.h" +#include "Runtime/BaseClasses/ObjectDefines.h" +#include "Runtime/Serialize/TransferFunctions/SerializeTransfer.h" +#include "Runtime/Graphics/ParticleSystem/ParticleSystemParticle.h" +#include "Runtime/Graphics/ParticleSystem/ParticleSystemCurves.h" +#include "Runtime/Graphics/ParticleSystem/ParticleSystemUtils.h" +#include "Runtime/Math/Random/Random.h" + +template<ParticleSystemCurveEvalMode mode> +void UpdateWholeSheetTpl(float cycles, const MinMaxCurve& curve, const ParticleSystemParticles& ps, float* tempSheetIndex, size_t fromIndex, size_t toIndex) +{ + for (size_t q = fromIndex; q < toIndex; ++q) + tempSheetIndex[q] = Repeat (cycles * Evaluate(curve, NormalizedTime(ps, q), GenerateRandom(ps.randomSeed[q] + kParticleSystemUVCurveId)), 1.0f); +} + +UVModule::UVModule () : ParticleSystemModule(false) +, m_TilesX (1), m_TilesY (1) +, m_AnimationType (kWholeSheet) +, m_RowIndex (0) +, m_Cycles (1.0f) +, m_RandomRow (true) +{} + +void UVModule::Update (const ParticleSystemParticles& ps, float* tempSheetIndex, size_t fromIndex, size_t toIndex) +{ + const float cycles = m_Cycles; + + DebugAssert(toIndex <= ps.array_size ()); + if (m_AnimationType == kSingleRow) // row + { + int rows = m_TilesY; + float animRange = (1.0f / (m_TilesX * rows)) * m_TilesX; + if(m_RandomRow) + { + for (size_t q = fromIndex; q < toIndex; ++q) + { + const float t = cycles * Evaluate(m_Curve, NormalizedTime(ps, q), GenerateRandom(ps.randomSeed[q] + kParticleSystemUVCurveId)); + const float x = Repeat (t, 1.0f); + const float randomValue = GenerateRandom(ps.randomSeed[q] + kParticleSystemUVRowSelectionId); + const float startRow = Floorf (randomValue * rows); + float from = startRow * animRange; + float to = from + animRange; + tempSheetIndex[q] = Lerp (from, to, x); + } + } + else + { + const float startRow = Floorf(m_RowIndex * animRange * rows); + float from = startRow * animRange; + float to = from + animRange; + for (size_t q = fromIndex; q < toIndex; ++q) + { + const float t = cycles * Evaluate(m_Curve, NormalizedTime(ps, q), GenerateRandom(ps.randomSeed[q] + kParticleSystemUVCurveId)); + const float x = Repeat (t, 1.0f); + tempSheetIndex[q] = Lerp (from, to, x); + } + } + } + else if (m_AnimationType == kWholeSheet) // grid || row + { + if(m_Curve.minMaxState == kMMCScalar) + UpdateWholeSheetTpl<kEMScalar>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex); + else if (m_Curve.IsOptimized() && m_Curve.UsesMinMax ()) + UpdateWholeSheetTpl<kEMOptimizedMinMax>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex); + else if(m_Curve.IsOptimized()) + UpdateWholeSheetTpl<kEMOptimized>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex); + else + UpdateWholeSheetTpl<kEMSlow>(m_Cycles, m_Curve, ps, tempSheetIndex, fromIndex, toIndex); + } + else + { + Assert(!"Animation mode not implemented!"); + } +} + +void UVModule::CheckConsistency () +{ + m_AnimationType = clamp<int> (m_AnimationType, 0, kNumAnimationTypes-1); + m_TilesX = std::max<int> (1, m_TilesX); + m_TilesY = std::max<int> (1, m_TilesY); + m_Cycles = std::max<int> (1, (int)m_Cycles); + m_RowIndex = clamp<int> (m_RowIndex, 0, m_TilesY-1); + m_Curve.SetScalar(clamp<float> (m_Curve.GetScalar(), 0.0f, 1.0f)); +} + +void UVModule::GetNumTiles(int& uvTilesX, int& uvTilesY) const +{ + uvTilesX = m_TilesX; + uvTilesY = m_TilesY; +} + +template<class TransferFunction> +void UVModule::Transfer (TransferFunction& transfer) +{ + ParticleSystemModule::Transfer (transfer); + transfer.Transfer (m_Curve, "frameOverTime"); + transfer.Transfer (m_TilesX, "tilesX"); + transfer.Transfer (m_TilesY, "tilesY"); + transfer.Transfer (m_AnimationType, "animationType"); + transfer.Transfer (m_RowIndex, "rowIndex"); + transfer.Transfer (m_Cycles, "cycles"); + transfer.Transfer (m_RandomRow, "randomRow"); transfer.Align (); +} +INSTANTIATE_TEMPLATE_TRANSFER(UVModule) |