• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef RENDER_SERVICE_CLIENT_CORE_COMMON_RS_PARTICLE_H
17 #define RENDER_SERVICE_CLIENT_CORE_COMMON_RS_PARTICLE_H
18 
19 #include <cstddef>
20 #include <cstdint>
21 #include <memory>
22 #include <stdint.h>
23 #include <sys/types.h>
24 #include <vector>
25 
26 #include "animation/rs_animation_timing_curve.h"
27 #include "animation/rs_render_particle.h"
28 #include "common/rs_color.h"
29 #include "common/rs_common_def.h"
30 #include "common/rs_macros.h"
31 #include "common/rs_vector2.h"
32 #include "render/rs_image.h"
33 
34 namespace OHOS {
35 namespace Rosen {
36 
37 template<typename T>
38 class RSB_EXPORT Change {
39 public:
40     T fromValue_;
41     T toValue_;
42     int startMillis_;
43     int endMillis_;
44     RSAnimationTimingCurve curve_;
Change()45     Change() : fromValue_(), toValue_(), startMillis_(), endMillis_(), curve_(RSAnimationTimingCurve::LINEAR) {}
Change(T fromValue,T toValue,int startMillis,int endMillis,RSAnimationTimingCurve & curve)46     Change(T fromValue, T toValue, int startMillis, int endMillis, RSAnimationTimingCurve& curve)
47     {
48         fromValue_ = fromValue;
49         toValue_ = toValue;
50         startMillis_ = startMillis;
51         endMillis_ = endMillis;
52         curve_ = curve;
53     }
54     Change(const Change& change) = default;
55     Change& operator=(const Change& change) = default;
56     ~Change() = default;
57 };
58 
59 template<typename T>
60 class RSB_EXPORT ParticleParaType {
61 public:
62     Range<T> val_;
63     ParticleUpdator updator_;
64     Range<float> random_;
65     std::vector<Change<T>> valChangeOverLife_;
ParticleParaType(Range<T> & val,ParticleUpdator updator,Range<float> random,std::vector<Change<T>> & valChangeOverLife)66     ParticleParaType(
67         Range<T>& val, ParticleUpdator updator, Range<float> random, std::vector<Change<T>>& valChangeOverLife)
68     {
69         val_ = val;
70         updator_ = updator;
71         random_ = random;
72         for (size_t i = 0; i < valChangeOverLife.size(); i++) {
73             auto change = valChangeOverLife[i];
74             valChangeOverLife_.push_back(change);
75         }
76     }
ParticleParaType()77     ParticleParaType() : val_(), updator_(ParticleUpdator::NONE), random_() {}
78     ParticleParaType(const ParticleParaType& paraType) = default;
79     ParticleParaType& operator=(const ParticleParaType& paraType) = default;
80     ~ParticleParaType() = default;
81 };
82 
83 class RSB_EXPORT ParticleAcceleration {
84 public:
85     ParticleParaType<float> accelerationValue_;
86     ParticleParaType<float> accelerationAngle_;
87 
88     ParticleAcceleration() = default;
ParticleAcceleration(ParticleParaType<float> accelerationValue,ParticleParaType<float> accelerationAngle)89     ParticleAcceleration(ParticleParaType<float> accelerationValue, ParticleParaType<float> accelerationAngle)
90     {
91         accelerationValue_ = accelerationValue;
92         accelerationAngle_ = accelerationAngle;
93     }
94     ParticleAcceleration(const ParticleAcceleration& acceleration) = default;
95     ParticleAcceleration& operator=(const ParticleAcceleration& acceleration) = default;
96     ~ParticleAcceleration() = default;
97 };
98 
99 class RSB_EXPORT ParticleColorParaType {
100 public:
101     Range<Color> colorVal_;
102     ParticleUpdator updator_;
103     Range<float> redRandom_;
104     Range<float> greenRandom_;
105     Range<float> blueRandom_;
106     Range<float> alphaRandom_;
107 
108     std::vector<Change<Color>> valChangeOverLife_;
ParticleColorParaType(const Range<Color> & colorVal,const ParticleUpdator & updator,const Range<float> & redRandom,const Range<float> & greenRandom,const Range<float> & blueRandom,const Range<float> & alphaRandom,std::vector<Change<Color>> & valChangeOverLife)109     ParticleColorParaType(const Range<Color>& colorVal, const ParticleUpdator& updator, const Range<float>& redRandom,
110         const Range<float>& greenRandom, const Range<float>& blueRandom, const Range<float>& alphaRandom,
111         std::vector<Change<Color>>& valChangeOverLife)
112     {
113         colorVal_ = colorVal;
114         updator_ = updator;
115         redRandom_ = redRandom;
116         greenRandom_ = greenRandom;
117         blueRandom_ = blueRandom;
118         alphaRandom_ = alphaRandom;
119         for (size_t i = 0; i < valChangeOverLife.size(); i++) {
120             auto change = valChangeOverLife[i];
121             valChangeOverLife_.push_back(change);
122         }
123     }
ParticleColorParaType()124     ParticleColorParaType()
125         : colorVal_(), updator_(ParticleUpdator::NONE), redRandom_(), greenRandom_(), blueRandom_(), alphaRandom_(),
126           valChangeOverLife_(0)
127     {}
128     ParticleColorParaType(const ParticleColorParaType& velocity) = default;
129     ParticleColorParaType& operator=(const ParticleColorParaType& velocity) = default;
130     ~ParticleColorParaType() = default;
131 };
132 
133 class RSB_EXPORT ParticleParams {
134 public:
135     EmitterConfig emitterConfig_;
136     ParticleVelocity velocity_;
137     ParticleAcceleration acceleration_;
138     ParticleColorParaType color_;
139     ParticleParaType<float> opacity_;
140     ParticleParaType<float> scale_;
141     ParticleParaType<float> spin_;
ParticleParams(const EmitterConfig & emitterConfig,const ParticleVelocity & velocity,const ParticleAcceleration & acceleration,const ParticleColorParaType & color,const ParticleParaType<float> & opacity,const ParticleParaType<float> & scale,const ParticleParaType<float> & spin)142     explicit ParticleParams(const EmitterConfig& emitterConfig, const ParticleVelocity& velocity,
143         const ParticleAcceleration& acceleration, const ParticleColorParaType& color,
144         const ParticleParaType<float>& opacity, const ParticleParaType<float>& scale,
145         const ParticleParaType<float>& spin)
146     {
147         emitterConfig_ = emitterConfig;
148         velocity_ = velocity;
149         acceleration_ = acceleration;
150         color_ = color;
151         opacity_ = opacity;
152         scale_ = scale;
153         spin_ = spin;
154     }
ParticleParams()155     ParticleParams() : emitterConfig_(), velocity_(), acceleration_(), color_(), opacity_(), scale_(), spin_() {};
156     ParticleParams(const ParticleParams& params) = default;
157     ParticleParams& operator=(const ParticleParams& params) = default;
158     ~ParticleParams() = default;
159 
translateValToRender(const ParticleParaType<float> & val)160     RenderParticleParaType<float> translateValToRender(const ParticleParaType<float>& val) const
161     {
162         RenderParticleParaType<float> value;
163         value.val_ = val.val_;
164         value.updator_ = val.updator_;
165         if (val.updator_ == ParticleUpdator::RANDOM) {
166             value.random_ = val.random_;
167         } else if (val.updator_ == ParticleUpdator::CURVE) {
168             uint32_t size = val.valChangeOverLife_.size();
169             std::vector<std::shared_ptr<ChangeInOverLife<float>>> valChangeOverLife;
170 
171             for (uint32_t i = 0; i < size; i++) {
172                 float fromValue = val.valChangeOverLife_[i].fromValue_;
173                 float toValue = val.valChangeOverLife_[i].toValue_;
174                 float startMillis = val.valChangeOverLife_[i].startMillis_;
175                 float endMillis = val.valChangeOverLife_[i].endMillis_;
176                 int duration = endMillis - startMillis;
177                 RSAnimationTimingCurve curve = val.valChangeOverLife_[i].curve_;
178                 auto interpolator = curve.GetInterpolator(duration);
179 
180                 valChangeOverLife.push_back(std::make_shared<ChangeInOverLife<float>>(
181                     fromValue, toValue, startMillis, endMillis, interpolator));
182             }
183             value.valChangeOverLife_ = valChangeOverLife;
184         }
185         return value;
186     }
187 
translateColorToRender(const ParticleColorParaType & val)188     RenderParticleColorParaType translateColorToRender(const ParticleColorParaType& val) const
189     {
190         RenderParticleColorParaType color;
191         color.colorVal_ = val.colorVal_;
192         color.updator_ = val.updator_;
193         if (color.updator_ == ParticleUpdator::RANDOM) {
194             color.redRandom_ = val.redRandom_;
195             color.greenRandom_ = val.greenRandom_;
196             color.blueRandom_ = val.blueRandom_;
197             color.alphaRandom_ = val.alphaRandom_;
198         } else if (color.updator_ == ParticleUpdator::CURVE) {
199             uint32_t size = val.valChangeOverLife_.size();
200             std::vector<std::shared_ptr<ChangeInOverLife<Color>>> colorChangeOverLife;
201             for (uint32_t i = 0; i < size; i++) {
202                 auto fromValue = val.valChangeOverLife_[i].fromValue_;
203                 auto toValue = val.valChangeOverLife_[i].toValue_;
204                 float startMillis = val.valChangeOverLife_[i].startMillis_;
205                 float endMillis = val.valChangeOverLife_[i].endMillis_;
206                 int duration = endMillis - startMillis;
207                 RSAnimationTimingCurve curve = val.valChangeOverLife_[i].curve_;
208                 auto interpolator = curve.GetInterpolator(duration);
209 
210                 colorChangeOverLife.push_back(std::make_shared<ChangeInOverLife<Color>>(
211                     fromValue, toValue, startMillis, endMillis, interpolator));
212             }
213             color.valChangeOverLife_ = colorChangeOverLife;
214         }
215         return color;
216     }
217 
SetParamsToRenderParticle()218     std::shared_ptr<ParticleRenderParams> SetParamsToRenderParticle() const
219     {
220         auto particleRenderParams = std::make_shared<ParticleRenderParams>();
221         particleRenderParams->SetEmitConfig(emitterConfig_);
222         particleRenderParams->SetParticleVelocity(velocity_);
223 
224         auto acceleration = RenderParticleAcceleration();
225         acceleration.accelerationValue_ = translateValToRender(acceleration_.accelerationValue_);
226         acceleration.accelerationAngle_ = translateValToRender(acceleration_.accelerationAngle_);
227 
228         particleRenderParams->SetParticleAcceleration(acceleration);
229         particleRenderParams->SetParticleColor(translateColorToRender(color_));
230         particleRenderParams->SetParticleOpacity(translateValToRender(opacity_));
231         particleRenderParams->SetParticleScale(translateValToRender(scale_));
232         particleRenderParams->SetParticleSpin(translateValToRender(spin_));
233         return particleRenderParams;
234     }
235 };
236 
237 } // namespace Rosen
238 } // namespace OHOS
239 
240 #endif // RENDER_SERVICE_CLIENT_CORE_COMMON_RS_PARTICLES_H
241