• 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 #include "animation/rs_render_particle.h"
16 
17 #include <algorithm>
18 #include <cstdint>
19 #include <random>
20 
21 #include "animation/rs_interpolator.h"
22 #include "animation/rs_render_particle_system.h"
23 #include "common/rs_color.h"
24 namespace OHOS {
25 namespace Rosen {
26 constexpr float DEGREE_TO_RADIAN = M_PI / 180;
27 
GetEmitRate() const28 int ParticleRenderParams::GetEmitRate() const
29 {
30     return emitterConfig_.emitRate_;
31 }
GetEmitShape() const32 ShapeType ParticleRenderParams::GetEmitShape() const
33 {
34     return emitterConfig_.emitShape_;
35 }
GetEmitPosition() const36 Vector2f ParticleRenderParams::GetEmitPosition() const
37 {
38     return emitterConfig_.position_;
39 }
GetEmitSize() const40 Vector2f ParticleRenderParams::GetEmitSize() const
41 {
42     return emitterConfig_.emitSize_;
43 }
GetParticleCount() const44 int32_t ParticleRenderParams::GetParticleCount() const
45 {
46     return emitterConfig_.particleCount_;
47 }
GetParticleLifeTime() const48 int64_t ParticleRenderParams::GetParticleLifeTime() const
49 {
50     return emitterConfig_.lifeTime_ * NS_PER_MS;
51 }
GetParticleType() const52 ParticleType ParticleRenderParams::GetParticleType() const
53 {
54     return emitterConfig_.type_;
55 }
GetParticleRadius() const56 float ParticleRenderParams::GetParticleRadius() const
57 {
58     return emitterConfig_.radius_;
59 }
GetParticleImage()60 std::shared_ptr<RSImage> ParticleRenderParams::GetParticleImage()
61 {
62     return emitterConfig_.image_;
63 }
GetImageSize() const64 Vector2f ParticleRenderParams::GetImageSize() const
65 {
66     return emitterConfig_.imageSize_;
67 }
68 
GetVelocityStartValue() const69 float ParticleRenderParams::GetVelocityStartValue() const
70 {
71     return velocity_.velocityValue_.start_;
72 }
GetVelocityEndValue() const73 float ParticleRenderParams::GetVelocityEndValue() const
74 {
75     return velocity_.velocityValue_.end_;
76 }
GetVelocityStartAngle() const77 float ParticleRenderParams::GetVelocityStartAngle() const
78 {
79     return velocity_.velocityAngle_.start_;
80 }
GetVelocityEndAngle() const81 float ParticleRenderParams::GetVelocityEndAngle() const
82 {
83     return velocity_.velocityAngle_.end_;
84 }
85 
GetAccelerationStartValue() const86 float ParticleRenderParams::GetAccelerationStartValue() const
87 {
88     return acceleration_.accelerationValue_.val_.start_;
89 }
GetAccelerationEndValue() const90 float ParticleRenderParams::GetAccelerationEndValue() const
91 {
92     return acceleration_.accelerationValue_.val_.end_;
93 }
GetAccelerationStartAngle() const94 float ParticleRenderParams::GetAccelerationStartAngle() const
95 {
96     return acceleration_.accelerationAngle_.val_.start_;
97 }
GetAccelerationEndAngle() const98 float ParticleRenderParams::GetAccelerationEndAngle() const
99 {
100     return acceleration_.accelerationAngle_.val_.end_;
101 }
GetAccelerationValueUpdator()102 ParticleUpdator ParticleRenderParams::GetAccelerationValueUpdator()
103 {
104     return acceleration_.accelerationValue_.updator_;
105 }
GetAccelerationAngleUpdator()106 ParticleUpdator ParticleRenderParams::GetAccelerationAngleUpdator()
107 {
108     return acceleration_.accelerationAngle_.updator_;
109 }
GetAccelRandomValueStart() const110 float ParticleRenderParams::GetAccelRandomValueStart() const
111 {
112     return acceleration_.accelerationValue_.random_.start_;
113 }
GetAccelRandomValueEnd() const114 float ParticleRenderParams::GetAccelRandomValueEnd() const
115 {
116     return acceleration_.accelerationValue_.random_.end_;
117 }
GetAccelRandomAngleStart() const118 float ParticleRenderParams::GetAccelRandomAngleStart() const
119 {
120     return acceleration_.accelerationAngle_.random_.start_;
121 }
GetAccelRandomAngleEnd() const122 float ParticleRenderParams::GetAccelRandomAngleEnd() const
123 {
124     return acceleration_.accelerationAngle_.random_.end_;
125 }
126 
GetColorStartValue()127 Color ParticleRenderParams::GetColorStartValue()
128 {
129     return color_.colorVal_.start_;
130 }
GetColorEndValue()131 Color ParticleRenderParams::GetColorEndValue()
132 {
133     return color_.colorVal_.end_;
134 }
GetColorUpdator()135 ParticleUpdator ParticleRenderParams::GetColorUpdator()
136 {
137     return color_.updator_;
138 }
GetRedRandomStart() const139 float ParticleRenderParams::GetRedRandomStart() const
140 {
141     return color_.redRandom_.start_;
142 }
GetRedRandomEnd() const143 float ParticleRenderParams::GetRedRandomEnd() const
144 {
145     return color_.redRandom_.end_;
146 }
GetGreenRandomStart() const147 float ParticleRenderParams::GetGreenRandomStart() const
148 {
149     return color_.greenRandom_.start_;
150 }
GetGreenRandomEnd() const151 float ParticleRenderParams::GetGreenRandomEnd() const
152 {
153     return color_.greenRandom_.end_;
154 }
GetBlueRandomStart() const155 float ParticleRenderParams::GetBlueRandomStart() const
156 {
157     return color_.blueRandom_.start_;
158 }
GetBlueRandomEnd() const159 float ParticleRenderParams::GetBlueRandomEnd() const
160 {
161     return color_.blueRandom_.end_;
162 }
GetAlphaRandomStart() const163 float ParticleRenderParams::GetAlphaRandomStart() const
164 {
165     return color_.alphaRandom_.start_;
166 }
GetAlphaRandomEnd() const167 float ParticleRenderParams::GetAlphaRandomEnd() const
168 {
169     return color_.alphaRandom_.end_;
170 }
171 
GetOpacityStartValue()172 float ParticleRenderParams::GetOpacityStartValue()
173 {
174     return opacity_.val_.start_;
175 }
GetOpacityEndValue()176 float ParticleRenderParams::GetOpacityEndValue()
177 {
178     return opacity_.val_.end_;
179 }
GetOpacityUpdator()180 ParticleUpdator ParticleRenderParams::GetOpacityUpdator()
181 {
182     return opacity_.updator_;
183 }
GetOpacityRandomStart() const184 float ParticleRenderParams::GetOpacityRandomStart() const
185 {
186     return opacity_.random_.start_;
187 }
GetOpacityRandomEnd() const188 float ParticleRenderParams::GetOpacityRandomEnd() const
189 {
190     return opacity_.random_.end_;
191 }
192 
GetScaleStartValue()193 float ParticleRenderParams::GetScaleStartValue()
194 {
195     return scale_.val_.start_;
196 }
GetScaleEndValue()197 float ParticleRenderParams::GetScaleEndValue()
198 {
199     return scale_.val_.end_;
200 }
GetScaleUpdator()201 ParticleUpdator ParticleRenderParams::GetScaleUpdator()
202 {
203     return scale_.updator_;
204 }
GetScaleRandomStart() const205 float ParticleRenderParams::GetScaleRandomStart() const
206 {
207     return scale_.random_.start_;
208 }
GetScaleRandomEnd() const209 float ParticleRenderParams::GetScaleRandomEnd() const
210 {
211     return scale_.random_.end_;
212 }
213 
GetSpinStartValue()214 float ParticleRenderParams::GetSpinStartValue()
215 {
216     return spin_.val_.start_;
217 }
GetSpinEndValue()218 float ParticleRenderParams::GetSpinEndValue()
219 {
220     return spin_.val_.end_;
221 }
GetSpinUpdator()222 ParticleUpdator ParticleRenderParams::GetSpinUpdator()
223 {
224     return spin_.updator_;
225 }
GetSpinRandomStart() const226 float ParticleRenderParams::GetSpinRandomStart() const
227 {
228     return spin_.random_.start_;
229 }
GetSpinRandomEnd() const230 float ParticleRenderParams::GetSpinRandomEnd() const
231 {
232     return spin_.random_.end_;
233 }
234 
SetEmitConfig(const EmitterConfig & emiterConfig)235 void ParticleRenderParams::SetEmitConfig(const EmitterConfig& emiterConfig)
236 {
237     emitterConfig_ = emiterConfig;
238 }
SetParticleVelocity(const ParticleVelocity & velocity)239 void ParticleRenderParams::SetParticleVelocity(const ParticleVelocity& velocity)
240 {
241     velocity_ = velocity;
242 }
SetParticleAcceleration(const RenderParticleAcceleration & acceleration)243 void ParticleRenderParams::SetParticleAcceleration(const RenderParticleAcceleration& acceleration)
244 {
245     acceleration_ = acceleration;
246 }
SetParticleColor(const RenderParticleColorParaType & color)247 void ParticleRenderParams::SetParticleColor(const RenderParticleColorParaType& color)
248 {
249     color_ = color;
250 }
SetParticleOpacity(const RenderParticleParaType<float> & opacity)251 void ParticleRenderParams::SetParticleOpacity(const RenderParticleParaType<float>& opacity)
252 {
253     opacity_ = opacity;
254 }
SetParticleScale(const RenderParticleParaType<float> & scale)255 void ParticleRenderParams::SetParticleScale(const RenderParticleParaType<float>& scale)
256 {
257     scale_ = scale;
258 }
SetParticleSpin(const RenderParticleParaType<float> & spin)259 void ParticleRenderParams::SetParticleSpin(const RenderParticleParaType<float>& spin)
260 {
261     spin_ = spin;
262 }
263 
RSRenderParticle(std::shared_ptr<ParticleRenderParams> particleParams)264 RSRenderParticle::RSRenderParticle(std::shared_ptr<ParticleRenderParams> particleParams)
265 {
266     particleRenderParams_ = particleParams;
267     InitProperty(particleParams);
268 }
269 
270 // Set methods
SetPosition(const Vector2f & position)271 void RSRenderParticle::SetPosition(const Vector2f& position)
272 {
273     position_ = position;
274 }
275 
SetVelocity(const Vector2f & velocity)276 void RSRenderParticle::SetVelocity(const Vector2f& velocity)
277 {
278     velocity_ = velocity;
279 }
280 
SetAcceleration(const Vector2f & acceleration)281 void RSRenderParticle::SetAcceleration(const Vector2f& acceleration)
282 {
283     acceleration_ = acceleration;
284 }
285 
SetSpin(const float & spin)286 void RSRenderParticle::SetSpin(const float& spin)
287 {
288     spin_ = spin;
289 }
290 
SetOpacity(const float & opacity)291 void RSRenderParticle::SetOpacity(const float& opacity)
292 {
293     opacity_ = opacity;
294 }
295 
SetColor(const Color & color)296 void RSRenderParticle::SetColor(const Color& color)
297 {
298     color_ = color;
299 }
300 
SetScale(const float & scale)301 void RSRenderParticle::SetScale(const float& scale)
302 {
303     scale_ = scale;
304 }
305 
SetRadius(const float & radius)306 void RSRenderParticle::SetRadius(const float& radius)
307 {
308     radius_ = radius;
309 }
310 
SetImage(const std::shared_ptr<RSImage> & image)311 void RSRenderParticle::SetImage(const std::shared_ptr<RSImage>& image)
312 {
313     image_ = image;
314 }
315 
SetImageSize(const Vector2f & imageSize)316 void RSRenderParticle::SetImageSize(const Vector2f& imageSize)
317 {
318     imageSize_ = imageSize;
319 }
320 
SetParticleType(const ParticleType & particleType)321 void RSRenderParticle::SetParticleType(const ParticleType& particleType)
322 {
323     particleType_ = particleType;
324 }
325 
SetActiveTime(const int64_t & activeTime)326 void RSRenderParticle::SetActiveTime(const int64_t& activeTime)
327 {
328     activeTime_ = activeTime;
329 }
330 
SetAccelerationValue(float accelerationValue)331 void RSRenderParticle::SetAccelerationValue(float accelerationValue)
332 {
333     accelerationValue_ = accelerationValue;
334 }
335 
SetAccelerationAngle(float accelerationAngle)336 void RSRenderParticle::SetAccelerationAngle(float accelerationAngle)
337 {
338     accelerationAngle_ = accelerationAngle;
339 }
340 
SetRedF(float redF)341 void RSRenderParticle::SetRedF(float redF)
342 {
343     redF_ = redF;
344 }
345 
SetGreenF(float greenF)346 void RSRenderParticle::SetGreenF(float greenF)
347 {
348     greenF_ = greenF;
349 }
350 
SetBlueF(float blueF)351 void RSRenderParticle::SetBlueF(float blueF)
352 {
353     blueF_ = blueF;
354 }
355 
SetAlphaF(float alphaF)356 void RSRenderParticle::SetAlphaF(float alphaF)
357 {
358     alphaF_ = alphaF;
359 }
360 
361 // Get methods
GetPosition()362 Vector2f RSRenderParticle::GetPosition()
363 {
364     return position_;
365 }
366 
GetVelocity()367 Vector2f RSRenderParticle::GetVelocity()
368 {
369     return velocity_;
370 }
371 
GetAcceleration()372 Vector2f RSRenderParticle::GetAcceleration()
373 {
374     return acceleration_;
375 }
376 
GetSpin()377 float RSRenderParticle::GetSpin()
378 {
379     return spin_;
380 }
381 
GetOpacity()382 float RSRenderParticle::GetOpacity()
383 {
384     return opacity_;
385 }
386 
GetColor()387 Color RSRenderParticle::GetColor()
388 {
389     return color_;
390 }
391 
GetScale()392 float RSRenderParticle::GetScale()
393 {
394     return scale_;
395 }
396 
GetRadius()397 float RSRenderParticle::GetRadius()
398 {
399     return radius_;
400 }
401 
GetAccelerationValue()402 float RSRenderParticle::GetAccelerationValue()
403 {
404     return accelerationValue_;
405 }
406 
GetAccelerationAngle()407 float RSRenderParticle::GetAccelerationAngle()
408 {
409     return accelerationAngle_;
410 }
411 
GetRedSpeed()412 float RSRenderParticle::GetRedSpeed()
413 {
414     return redSpeed_;
415 }
416 
GetGreenSpeed()417 float RSRenderParticle::GetGreenSpeed()
418 {
419     return greenSpeed_;
420 }
421 
GetBlueSpeed()422 float RSRenderParticle::GetBlueSpeed()
423 {
424     return blueSpeed_;
425 }
426 
GetAlphaSpeed()427 float RSRenderParticle::GetAlphaSpeed()
428 {
429     return alphaSpeed_;
430 }
431 
GetOpacitySpeed()432 float RSRenderParticle::GetOpacitySpeed()
433 {
434     return opacitySpeed_;
435 }
436 
GetScaleSpeed()437 float RSRenderParticle::GetScaleSpeed()
438 {
439     return scaleSpeed_;
440 }
441 
GetSpinSpeed()442 float RSRenderParticle::GetSpinSpeed()
443 {
444     return spinSpeed_;
445 }
446 
GetAccelerationValueSpeed()447 float RSRenderParticle::GetAccelerationValueSpeed()
448 {
449     return accelerationValueSpeed_;
450 }
451 
GetAccelerationAngleSpeed()452 float RSRenderParticle::GetAccelerationAngleSpeed()
453 {
454     return accelerationAngleSpeed_;
455 }
456 
GetRedF()457 float RSRenderParticle::GetRedF()
458 {
459     return redF_;
460 }
461 
GetGreenF()462 float RSRenderParticle::GetGreenF()
463 {
464     return greenF_;
465 }
466 
GetBlueF()467 float RSRenderParticle::GetBlueF()
468 {
469     return blueF_;
470 }
471 
GetAlphaF()472 float RSRenderParticle::GetAlphaF()
473 {
474     return alphaF_;
475 }
476 
GetImage()477 std::shared_ptr<RSImage> RSRenderParticle::GetImage()
478 {
479     return image_;
480 }
481 
GetImageSize()482 Vector2f RSRenderParticle::GetImageSize()
483 {
484     return imageSize_;
485 }
486 
GetParticleType()487 ParticleType RSRenderParticle::GetParticleType()
488 {
489     return particleType_;
490 }
491 
GetActiveTime()492 int64_t RSRenderParticle::GetActiveTime()
493 {
494     return activeTime_;
495 }
496 
GetParticleRenderParams()497 std::shared_ptr<ParticleRenderParams> RSRenderParticle::GetParticleRenderParams()
498 {
499     return particleRenderParams_;
500 }
501 
502 // Other methods
InitProperty(std::shared_ptr<ParticleRenderParams> particleParams)503 void RSRenderParticle::InitProperty(std::shared_ptr<ParticleRenderParams> particleParams)
504 {
505     // Initialize particle properties
506     auto emitShape = particleParams->GetEmitShape();
507     auto position = particleParams->GetEmitPosition();
508     auto emitSize = particleParams->GetEmitSize();
509     position_ = CalculateParticlePosition(emitShape, position, emitSize);
510 
511     float velocityValue =
512         GetRandomValue(particleParams->GetVelocityStartValue(), particleParams->GetVelocityEndValue());
513     float velocityAngle =
514         GetRandomValue(particleParams->GetVelocityStartAngle(), particleParams->GetVelocityEndAngle());
515     velocityAngle *= DEGREE_TO_RADIAN;
516     velocity_ = Vector2f { velocityValue * std::cos(velocityAngle), velocityValue * std::sin(velocityAngle) };
517 
518     accelerationValue_ =
519         GetRandomValue(particleParams->GetAccelerationStartValue(), particleParams->GetAccelerationEndValue());
520     accelerationAngle_ =
521         GetRandomValue(particleParams->GetAccelerationStartAngle(), particleParams->GetAccelerationEndAngle());
522     acceleration_ = Vector2f { accelerationValue_ * std::cos(accelerationAngle_ * DEGREE_TO_RADIAN),
523         accelerationValue_ * std::sin(accelerationAngle_ * DEGREE_TO_RADIAN) };
524 
525     spin_ = GetRandomValue(particleParams->GetSpinStartValue(), particleParams->GetSpinEndValue());
526     opacity_ = GetRandomValue(particleParams->GetOpacityStartValue(), particleParams->GetOpacityEndValue());
527     scale_ = GetRandomValue(particleParams->GetScaleStartValue(), particleParams->GetScaleEndValue());
528     opacitySpeed_ = GetRandomValue(particleParams->GetOpacityRandomStart(), particleParams->GetOpacityRandomEnd());
529     scaleSpeed_ = GetRandomValue(particleParams->GetScaleRandomStart(), particleParams->GetScaleRandomEnd());
530     spinSpeed_ = GetRandomValue(particleParams->GetSpinRandomStart(), particleParams->GetSpinRandomEnd());
531     accelerationValueSpeed_ =
532         GetRandomValue(particleParams->GetAccelRandomValueStart(), particleParams->GetAccelRandomValueEnd());
533     accelerationAngleSpeed_ =
534         GetRandomValue(particleParams->GetAccelRandomAngleStart(), particleParams->GetAccelRandomAngleEnd());
535 
536     particleType_ = particleParams->GetParticleType();
537     if (particleType_ == ParticleType::POINTS) {
538         float colorRandomValue = GetRandomValue(0.0f, 1.0f);
539         color_ = Lerp(particleParams->GetColorStartValue(), particleParams->GetColorEndValue(), colorRandomValue);
540         redSpeed_ = GetRandomValue(particleParams->GetRedRandomStart(), particleParams->GetRedRandomEnd());
541         greenSpeed_ = GetRandomValue(particleParams->GetGreenRandomStart(), particleParams->GetGreenRandomEnd());
542         blueSpeed_ = GetRandomValue(particleParams->GetBlueRandomStart(), particleParams->GetBlueRandomEnd());
543         alphaSpeed_ = GetRandomValue(particleParams->GetAlphaRandomStart(), particleParams->GetAlphaRandomEnd());
544         radius_ = particleParams->GetParticleRadius();
545     } else if (particleType_ == ParticleType::IMAGES) {
546         image_ = particleParams->GetParticleImage();
547         imageSize_ = particleParams->GetImageSize();
548         if (image_ != nullptr) {
549             auto pixelMap = image_->GetPixelMap();
550             if (pixelMap != nullptr) {
551                 image_->SetDstRect(RectF(position_.x_, position_.y_, pixelMap->GetWidth(), pixelMap->GetHeight()));
552             }
553         }
554     }
555     activeTime_ = 0;
556     lifeTime_ = particleParams->GetParticleLifeTime();
557 }
558 
IsAlive() const559 bool RSRenderParticle::IsAlive() const
560 {
561     if (dead_ == true) {
562         return false;
563     }
564     if (lifeTime_ == -1 * NS_PER_MS) {
565         return true;
566     }
567     return activeTime_ < lifeTime_;
568 }
569 
SetIsDead()570 void RSRenderParticle::SetIsDead()
571 {
572     dead_ = true;
573 }
574 
GetRandomValue(float min,float max)575 float RSRenderParticle::GetRandomValue(float min, float max)
576 {
577     if (ROSEN_EQ(min, max)) {
578         return min;
579     }
580     if (min > max) {
581         std::swap(min, max);
582     }
583     std::random_device rd;
584     std::mt19937_64 gen(rd());
585     std::uniform_real_distribution<float> dis(min, max);
586     return dis(gen);
587 }
588 
CalculateParticlePosition(const ShapeType & emitShape,const Vector2f & position,const Vector2f & emitSize)589 Vector2f RSRenderParticle::CalculateParticlePosition(
590     const ShapeType& emitShape, const Vector2f& position, const Vector2f& emitSize)
591 {
592     float positionX = 0.f;
593     float positionY = 0.f;
594     if (emitShape == ShapeType::RECT) {
595         float minX = position.x_;
596         float maxX = position.x_ + emitSize.x_;
597         positionX = GetRandomValue(minX, maxX);
598         float minY = position.y_;
599         float maxY = position.y_ + emitSize.y_;
600         positionY = GetRandomValue(minY, maxY);
601     }
602     if (emitShape == ShapeType::CIRCLE || emitShape == ShapeType::ELLIPSE) {
603         float dx = emitSize.x_;
604         float dy = emitSize.y_;
605         float x = position.x_ + dx / 2;
606         float y = position.y_ + dy / 2;
607         float theta = GetRandomValue(0.f, 2 * PI);
608         if (emitShape == ShapeType::CIRCLE) {
609             float d = std::min(emitSize.x_, emitSize.y_);
610             float r = GetRandomValue(0.f, d) / 2;
611             positionX = x + r * cos(theta);
612             positionY = y + r * sin(theta);
613         } else {
614             float rx = GetRandomValue(0.f, dx) / 2;
615             float ry = GetRandomValue(0.f, dy) / 2;
616             positionX = x + rx * cos(theta);
617             positionY = y + ry * sin(theta);
618         }
619     }
620     return Vector2f { positionX, positionY };
621 }
622 
Lerp(const Color & start,const Color & end,float t)623 Color RSRenderParticle::Lerp(const Color& start, const Color& end, float t)
624 {
625     Color result;
626     result.SetRed(start.GetRed() + static_cast<int>(std::round((end.GetRed() - start.GetRed()) * t)));
627     result.SetGreen(start.GetGreen() + static_cast<int>(std::round((end.GetGreen() - start.GetGreen()) * t)));
628     result.SetBlue(start.GetBlue() + static_cast<int>(std::round((end.GetBlue() - start.GetBlue()) * t)));
629     result.SetAlpha(start.GetAlpha() + static_cast<int>(std::round((end.GetAlpha() - start.GetAlpha()) * t)));
630     return result;
631 }
632 
633 } // namespace Rosen
634 } // namespace OHOS
635