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 const ShapeType& ParticleRenderParams::GetEmitShape() const
33 {
34 return emitterConfig_.emitShape_;
35 }
GetEmitPosition() const36 const Vector2f& ParticleRenderParams::GetEmitPosition() const
37 {
38 return emitterConfig_.position_;
39 }
GetEmitSize() const40 const 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 const ParticleType& ParticleRenderParams::GetParticleType() const
53 {
54 return emitterConfig_.type_;
55 }
GetParticleRadius() const56 float ParticleRenderParams::GetParticleRadius() const
57 {
58 return emitterConfig_.radius_;
59 }
GetParticleImage()60 const std::shared_ptr<RSImage>& ParticleRenderParams::GetParticleImage()
61 {
62 return emitterConfig_.image_;
63 }
GetImageSize() const64 const 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 const ParticleUpdator& ParticleRenderParams::GetAccelerationValueUpdator()
103 {
104 return acceleration_.accelerationValue_.updator_;
105 }
GetAccelerationAngleUpdator()106 const 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 const Color& ParticleRenderParams::GetColorStartValue()
128 {
129 return color_.colorVal_.start_;
130 }
GetColorEndValue()131 const Color& ParticleRenderParams::GetColorEndValue()
132 {
133 return color_.colorVal_.end_;
134 }
GetColorUpdator()135 const 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 const 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 const 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 const 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(const std::shared_ptr<ParticleRenderParams> & particleParams)264 RSRenderParticle::RSRenderParticle(const std::shared_ptr<ParticleRenderParams>& particleParams)
265 : particleRenderParams_(particleParams)
266 {
267 InitProperty(particleRenderParams_);
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 const Vector2f& RSRenderParticle::GetPosition()
363 {
364 return position_;
365 }
366
GetVelocity()367 const Vector2f& RSRenderParticle::GetVelocity()
368 {
369 return velocity_;
370 }
371
GetAcceleration()372 const 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 const 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 const std::shared_ptr<RSImage>& RSRenderParticle::GetImage()
478 {
479 return image_;
480 }
481
GetImageSize()482 const Vector2f& RSRenderParticle::GetImageSize()
483 {
484 return imageSize_;
485 }
486
GetParticleType()487 const ParticleType& RSRenderParticle::GetParticleType()
488 {
489 return particleType_;
490 }
491
GetActiveTime()492 int64_t RSRenderParticle::GetActiveTime()
493 {
494 return activeTime_;
495 }
496
GetParticleRenderParams()497 const std::shared_ptr<ParticleRenderParams>& RSRenderParticle::GetParticleRenderParams()
498 {
499 return particleRenderParams_;
500 }
501
GetAccelerationValueUpdator()502 const ParticleUpdator& RSRenderParticle::GetAccelerationValueUpdator()
503 {
504 return particleRenderParams_->acceleration_.accelerationValue_.updator_;
505 }
GetAccelerationAngleUpdator()506 const ParticleUpdator& RSRenderParticle::GetAccelerationAngleUpdator()
507 {
508 return particleRenderParams_->acceleration_.accelerationAngle_.updator_;
509 }
GetColorUpdator()510 const ParticleUpdator& RSRenderParticle::GetColorUpdator()
511 {
512 return particleRenderParams_->color_.updator_;
513 }
GetOpacityUpdator()514 const ParticleUpdator& RSRenderParticle::GetOpacityUpdator()
515 {
516 return particleRenderParams_->opacity_.updator_;
517 }
GetScaleUpdator()518 const ParticleUpdator& RSRenderParticle::GetScaleUpdator()
519 {
520 return particleRenderParams_->scale_.updator_;
521 }
GetSpinUpdator()522 const ParticleUpdator& RSRenderParticle::GetSpinUpdator()
523 {
524 return particleRenderParams_->spin_.updator_;
525 }
GetAcceValChangeOverLife()526 const std::vector<std::shared_ptr<ChangeInOverLife<float>>>& RSRenderParticle::GetAcceValChangeOverLife()
527 {
528 return particleRenderParams_->acceleration_.accelerationValue_.valChangeOverLife_;
529 }
GetAcceAngChangeOverLife()530 const std::vector<std::shared_ptr<ChangeInOverLife<float>>>& RSRenderParticle::GetAcceAngChangeOverLife()
531 {
532 return particleRenderParams_->acceleration_.accelerationAngle_.valChangeOverLife_;
533 }
GetOpacityChangeOverLife()534 const std::vector<std::shared_ptr<ChangeInOverLife<float>>>& RSRenderParticle::GetOpacityChangeOverLife()
535 {
536 return particleRenderParams_->opacity_.valChangeOverLife_;
537 }
GetScaleChangeOverLife()538 const std::vector<std::shared_ptr<ChangeInOverLife<float>>>& RSRenderParticle::GetScaleChangeOverLife()
539 {
540 return particleRenderParams_->scale_.valChangeOverLife_;
541 }
GetSpinChangeOverLife()542 const std::vector<std::shared_ptr<ChangeInOverLife<float>>>& RSRenderParticle::GetSpinChangeOverLife()
543 {
544 return particleRenderParams_->spin_.valChangeOverLife_;
545 }
GetColorChangeOverLife()546 const std::vector<std::shared_ptr<ChangeInOverLife<Color>>>& RSRenderParticle::GetColorChangeOverLife()
547 {
548 return particleRenderParams_->color_.valChangeOverLife_;
549 }
550 // Other methods
InitProperty(const std::shared_ptr<ParticleRenderParams> & particleParams)551 void RSRenderParticle::InitProperty(const std::shared_ptr<ParticleRenderParams>& particleParams)
552 {
553 // Initialize particle properties
554 auto emitShape = particleParams->GetEmitShape();
555 auto position = particleParams->GetEmitPosition();
556 auto emitSize = particleParams->GetEmitSize();
557 position_ = CalculateParticlePosition(emitShape, position, emitSize);
558
559 float velocityValue =
560 GetRandomValue(particleParams->GetVelocityStartValue(), particleParams->GetVelocityEndValue());
561 float velocityAngle =
562 GetRandomValue(particleParams->GetVelocityStartAngle(), particleParams->GetVelocityEndAngle());
563 velocityAngle *= DEGREE_TO_RADIAN;
564 velocity_ = Vector2f { velocityValue * std::cos(velocityAngle), velocityValue * std::sin(velocityAngle) };
565
566 accelerationValue_ =
567 GetRandomValue(particleParams->GetAccelerationStartValue(), particleParams->GetAccelerationEndValue());
568 accelerationAngle_ =
569 GetRandomValue(particleParams->GetAccelerationStartAngle(), particleParams->GetAccelerationEndAngle());
570 acceleration_ = Vector2f { accelerationValue_ * std::cos(accelerationAngle_ * DEGREE_TO_RADIAN),
571 accelerationValue_ * std::sin(accelerationAngle_ * DEGREE_TO_RADIAN) };
572
573 spin_ = GetRandomValue(particleParams->GetSpinStartValue(), particleParams->GetSpinEndValue());
574 opacity_ = GetRandomValue(particleParams->GetOpacityStartValue(), particleParams->GetOpacityEndValue());
575 scale_ = GetRandomValue(particleParams->GetScaleStartValue(), particleParams->GetScaleEndValue());
576 opacitySpeed_ = GetRandomValue(particleParams->GetOpacityRandomStart(), particleParams->GetOpacityRandomEnd());
577 scaleSpeed_ = GetRandomValue(particleParams->GetScaleRandomStart(), particleParams->GetScaleRandomEnd());
578 spinSpeed_ = GetRandomValue(particleParams->GetSpinRandomStart(), particleParams->GetSpinRandomEnd());
579 accelerationValueSpeed_ =
580 GetRandomValue(particleParams->GetAccelRandomValueStart(), particleParams->GetAccelRandomValueEnd());
581 accelerationAngleSpeed_ =
582 GetRandomValue(particleParams->GetAccelRandomAngleStart(), particleParams->GetAccelRandomAngleEnd());
583
584 particleType_ = particleParams->GetParticleType();
585 if (particleType_ == ParticleType::POINTS) {
586 float colorRandomValue = GetRandomValue(0.0f, 1.0f);
587 color_ = Lerp(particleParams->GetColorStartValue(), particleParams->GetColorEndValue(), colorRandomValue);
588 redSpeed_ = GetRandomValue(particleParams->GetRedRandomStart(), particleParams->GetRedRandomEnd());
589 greenSpeed_ = GetRandomValue(particleParams->GetGreenRandomStart(), particleParams->GetGreenRandomEnd());
590 blueSpeed_ = GetRandomValue(particleParams->GetBlueRandomStart(), particleParams->GetBlueRandomEnd());
591 alphaSpeed_ = GetRandomValue(particleParams->GetAlphaRandomStart(), particleParams->GetAlphaRandomEnd());
592 radius_ = particleParams->GetParticleRadius();
593 } else if (particleType_ == ParticleType::IMAGES) {
594 image_ = particleParams->GetParticleImage();
595 imageSize_ = particleParams->GetImageSize();
596 if (image_ != nullptr) {
597 auto pixelMap = image_->GetPixelMap();
598 if (pixelMap != nullptr) {
599 image_->SetDstRect(RectF(position_.x_, position_.y_, pixelMap->GetWidth(), pixelMap->GetHeight()));
600 }
601 }
602 }
603 activeTime_ = 0;
604 lifeTime_ = particleParams->GetParticleLifeTime();
605 }
606
IsAlive() const607 bool RSRenderParticle::IsAlive() const
608 {
609 if (dead_ == true) {
610 return false;
611 }
612 if (lifeTime_ == -1 * NS_PER_MS) {
613 return true;
614 }
615 return activeTime_ < lifeTime_;
616 }
617
SetIsDead()618 void RSRenderParticle::SetIsDead()
619 {
620 dead_ = true;
621 }
622
GetRandomValue(float min,float max)623 float RSRenderParticle::GetRandomValue(float min, float max)
624 {
625 if (ROSEN_EQ(min, max)) {
626 return min;
627 }
628 if (min > max) {
629 std::swap(min, max);
630 }
631 std::random_device rd;
632 if (rd.entropy() > 0) {
633 std::mt19937_64 gen(rd());
634 std::uniform_real_distribution<float> dis(min, max);
635 return dis(gen);
636 } else {
637 return min + (max - min) / 2.0f;
638 }
639 }
640
CalculateParticlePosition(const ShapeType & emitShape,const Vector2f & position,const Vector2f & emitSize)641 Vector2f RSRenderParticle::CalculateParticlePosition(
642 const ShapeType& emitShape, const Vector2f& position, const Vector2f& emitSize)
643 {
644 float positionX = 0.f;
645 float positionY = 0.f;
646 if (emitShape == ShapeType::RECT) {
647 float minX = position.x_;
648 float maxX = position.x_ + emitSize.x_;
649 positionX = GetRandomValue(minX, maxX);
650 float minY = position.y_;
651 float maxY = position.y_ + emitSize.y_;
652 positionY = GetRandomValue(minY, maxY);
653 }
654 if (emitShape == ShapeType::CIRCLE || emitShape == ShapeType::ELLIPSE) {
655 float dx = emitSize.x_;
656 float dy = emitSize.y_;
657 float x = position.x_ + dx / 2;
658 float y = position.y_ + dy / 2;
659 float theta = GetRandomValue(0.f, 2 * PI);
660 if (emitShape == ShapeType::CIRCLE) {
661 float d = std::min(emitSize.x_, emitSize.y_);
662 float r = GetRandomValue(0.f, d) / 2;
663 positionX = x + r * cos(theta);
664 positionY = y + r * sin(theta);
665 } else {
666 float rx = GetRandomValue(0.f, dx) / 2;
667 float ry = GetRandomValue(0.f, dy) / 2;
668 positionX = x + rx * cos(theta);
669 positionY = y + ry * sin(theta);
670 }
671 }
672 return Vector2f { positionX, positionY };
673 }
674
Lerp(const Color & start,const Color & end,float t)675 Color RSRenderParticle::Lerp(const Color& start, const Color& end, float t)
676 {
677 Color result;
678 result.SetRed(start.GetRed() + static_cast<int>(std::round((end.GetRed() - start.GetRed()) * t)));
679 result.SetGreen(start.GetGreen() + static_cast<int>(std::round((end.GetGreen() - start.GetGreen()) * t)));
680 result.SetBlue(start.GetBlue() + static_cast<int>(std::round((end.GetBlue() - start.GetBlue()) * t)));
681 result.SetAlpha(start.GetAlpha() + static_cast<int>(std::round((end.GetAlpha() - start.GetAlpha()) * t)));
682 return result;
683 }
684
685 } // namespace Rosen
686 } // namespace OHOS
687