/src/ogre/PlugIns/ParticleFX/src/OgreTextureAnimatorAffector.cpp
Line | Count | Source |
1 | | // This file is part of the OGRE project. |
2 | | // It is subject to the license terms in the LICENSE file found in the top-level directory |
3 | | // of this distribution and at https://www.ogre3d.org/licensing. |
4 | | // SPDX-License-Identifier: MIT |
5 | | #include "OgreTextureAnimatorAffector.h" |
6 | | #include "OgreParticleSystem.h" |
7 | | #include "OgreStringConverter.h" |
8 | | #include "OgreParticle.h" |
9 | | |
10 | | |
11 | | namespace Ogre { |
12 | | |
13 | | class CmdStart : public ParamCommand |
14 | | { |
15 | | public: |
16 | | String doGet(const void* target) const override |
17 | 0 | { |
18 | 0 | return StringConverter::toString( |
19 | 0 | static_cast<const TextureAnimatorAffector*>(target)->getTexcoordStart()); |
20 | 0 | } |
21 | | void doSet(void* target, const String& val) override |
22 | 0 | { |
23 | 0 | static_cast<TextureAnimatorAffector*>(target)->setTexcoordStart(StringConverter::parseInt(val)); |
24 | 0 | } |
25 | | }; |
26 | | class CmdCount : public ParamCommand |
27 | | { |
28 | | public: |
29 | | String doGet(const void* target) const override |
30 | 0 | { |
31 | 0 | return StringConverter::toString( |
32 | 0 | static_cast<const TextureAnimatorAffector*>(target)->getTexcoordCount()); |
33 | 0 | } |
34 | | void doSet(void* target, const String& val) override |
35 | 0 | { |
36 | 0 | static_cast<TextureAnimatorAffector*>(target)->setTexcoordCount(StringConverter::parseInt(val)); |
37 | 0 | } |
38 | | }; |
39 | | class CmdDuration : public ParamCommand |
40 | | { |
41 | | public: |
42 | | String doGet(const void* target) const override |
43 | 0 | { |
44 | 0 | return StringConverter::toString( |
45 | 0 | static_cast<const TextureAnimatorAffector*>(target)->getDuration()); |
46 | 0 | } |
47 | | void doSet(void* target, const String& val) override |
48 | 0 | { |
49 | 0 | static_cast<TextureAnimatorAffector*>(target)->setDuration(StringConverter::parseReal(val)); |
50 | 0 | } |
51 | | }; |
52 | | class CmdOffset : public ParamCommand |
53 | | { |
54 | | public: |
55 | | String doGet(const void* target) const override |
56 | 0 | { |
57 | 0 | return StringConverter::toString( |
58 | 0 | static_cast<const TextureAnimatorAffector*>(target)->isRandomStartOffset()); |
59 | 0 | } |
60 | | void doSet(void* target, const String& val) override |
61 | 0 | { |
62 | 0 | static_cast<TextureAnimatorAffector*>(target)->useRandomStartOffset(StringConverter::parseBool(val)); |
63 | 0 | } |
64 | | }; |
65 | | |
66 | | // init statics |
67 | | static CmdStart msStartCmd; |
68 | | static CmdCount msCountCmd; |
69 | | static CmdDuration msDurationCmd; |
70 | | static CmdOffset msOffset; |
71 | | |
72 | | //----------------------------------------------------------------------- |
73 | 0 | TextureAnimatorAffector::TextureAnimatorAffector(ParticleSystem* psys) : ParticleAffector(psys) |
74 | 0 | { |
75 | 0 | mTexcoordStart = mTexcoordCount = mDuration = 0; |
76 | 0 | mRandomStartOffset = false; |
77 | 0 | mType = "TextureAnimator"; |
78 | | |
79 | | // Init parameters |
80 | 0 | if (createParamDictionary("TextureAnimatorAffector")) |
81 | 0 | { |
82 | 0 | ParamDictionary* dict = getParamDictionary(); |
83 | |
|
84 | 0 | dict->addParameter(ParameterDef("texcoord_start", "", PT_INT), &msStartCmd); |
85 | 0 | dict->addParameter(ParameterDef("texcoord_count", "", PT_INT), &msCountCmd); |
86 | 0 | dict->addParameter(ParameterDef("duration", "", PT_REAL), &msDurationCmd); |
87 | 0 | dict->addParameter(ParameterDef("random_offset", "", PT_BOOL), &msOffset); |
88 | 0 | } |
89 | 0 | } |
90 | | //----------------------------------------------------------------------- |
91 | | void TextureAnimatorAffector::_initParticle(Particle* pParticle) |
92 | 0 | { |
93 | 0 | pParticle->mTexcoordIndex = 0; |
94 | |
|
95 | 0 | if (!mRandomStartOffset) |
96 | 0 | return; |
97 | | |
98 | 0 | pParticle->mRandomTexcoordOffset = Math::UnitRandom() * mTexcoordCount; |
99 | 0 | pParticle->mTexcoordIndex = pParticle->mRandomTexcoordOffset; |
100 | 0 | } |
101 | | |
102 | | void TextureAnimatorAffector::_affectParticles(ParticleSystem* pSystem, Real timeElapsed) |
103 | 0 | { |
104 | | // special case: randomly pick one cell in sprite-sheet |
105 | 0 | if(mDuration < 0) |
106 | 0 | return; |
107 | | |
108 | 0 | for (auto p : pSystem->_getActiveParticles()) |
109 | 0 | { |
110 | 0 | float particle_time = 1.0f - (p->mTimeToLive / p->mTotalTimeToLive); |
111 | |
|
112 | 0 | float speed = mDuration ? (p->mTotalTimeToLive / mDuration) : 1.0f; |
113 | 0 | uint8 idx = uint8(particle_time * speed * mTexcoordCount + p->mRandomTexcoordOffset) % mTexcoordCount; |
114 | |
|
115 | 0 | p->mTexcoordIndex = idx + mTexcoordStart; |
116 | 0 | } |
117 | 0 | } |
118 | | } |