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 #ifndef RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PARTICLE_H 16 #define RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PARTICLE_H 17 18 #include <memory> 19 #include <sys/types.h> 20 #include <vector> 21 #include "pixel_map.h" 22 23 #include "animation/rs_interpolator.h" 24 #include "common/rs_color.h" 25 #include "common/rs_color_palette.h" 26 #include "common/rs_common_def.h" 27 #include "common/rs_macros.h" 28 #include "common/rs_vector2.h" 29 #include "render/rs_image.h" 30 namespace OHOS { 31 namespace Rosen { 32 enum class ParticleUpdator: uint32_t { NONE = 0, RANDOM, CURVE }; 33 34 enum class ShapeType: uint32_t { RECT = 0, CIRCLE, ELLIPSE }; 35 36 enum class ParticleType: uint32_t { 37 POINTS = 0, 38 IMAGES 39 }; 40 41 template<typename T> 42 struct Range { 43 T start_, end_; RangeRange44 Range() : start_(), end_() {} RangeRange45 Range(T a, T b) : start_(a), end_(b) {} 46 WidthRange47 T Width() const 48 { 49 return end_ - start_; 50 } 51 }; 52 53 template<typename T> 54 class RSB_EXPORT ChangeInOverLife { 55 public: 56 T fromValue_; 57 T toValue_; 58 int startMillis_; 59 int endMillis_; 60 std::shared_ptr<RSInterpolator> interpolator_ { RSInterpolator::DEFAULT }; ChangeInOverLife()61 ChangeInOverLife() : fromValue_(), toValue_(), startMillis_(), endMillis_(), interpolator_() {} ChangeInOverLife(const T & fromValue,const T & toValue,const int & startMillis,const int & endMillis,const std::shared_ptr<RSInterpolator> & interpolator)62 ChangeInOverLife(const T& fromValue, const T& toValue, const int& startMillis, const int& endMillis, 63 const std::shared_ptr<RSInterpolator>& interpolator) 64 { 65 fromValue_ = fromValue; 66 toValue_ = toValue; 67 startMillis_ = startMillis; 68 endMillis_ = endMillis; 69 interpolator_ = interpolator; 70 } 71 ChangeInOverLife(const ChangeInOverLife& change) = default; 72 ChangeInOverLife& operator=(const ChangeInOverLife& change) = default; 73 ~ChangeInOverLife() = default; 74 }; 75 76 template<typename T> 77 class RSB_EXPORT RenderParticleParaType { 78 public: 79 Range<T> val_; 80 ParticleUpdator updator_; 81 Range<float> random_; 82 std::vector<std::shared_ptr<ChangeInOverLife<T>>> valChangeOverLife_; RenderParticleParaType(const Range<T> & val,const ParticleUpdator & updator,const Range<float> & random,const std::vector<std::shared_ptr<ChangeInOverLife<T>>> & valChangeOverLife)83 RenderParticleParaType(const Range<T>& val, const ParticleUpdator& updator, const Range<float>& random, 84 const std::vector<std::shared_ptr<ChangeInOverLife<T>>>& valChangeOverLife) 85 { 86 val_ = val; 87 updator_ = updator; 88 random_ = random; 89 for (size_t i = 0; i < valChangeOverLife.size(); i++) { 90 auto change = valChangeOverLife[i]; 91 valChangeOverLife_.push_back(change); 92 } 93 } RenderParticleParaType()94 RenderParticleParaType() : val_(), updator_(ParticleUpdator::NONE), random_() {} 95 RenderParticleParaType(const RenderParticleParaType& paraType) = default; 96 RenderParticleParaType& operator=(const RenderParticleParaType& paraType) = default; 97 ~RenderParticleParaType() = default; 98 }; 99 100 class RSB_EXPORT EmitterConfig { 101 public: 102 int emitRate_; 103 ShapeType emitShape_; 104 Vector2f position_; 105 Vector2f emitSize_; 106 int32_t particleCount_; 107 int64_t lifeTime_; 108 ParticleType type_; 109 float radius_; 110 std::shared_ptr<RSImage> image_; 111 Vector2f imageSize_; 112 EmitterConfig()113 EmitterConfig() 114 : emitRate_(), emitShape_(ShapeType::RECT), position_(), emitSize_(), particleCount_(), lifeTime_(), 115 type_(ParticleType::POINTS), radius_(), image_(), imageSize_() 116 {} EmitterConfig(const int & emitRate,const ShapeType & emitShape,const Vector2f & position,const Vector2f & emitSize,const int32_t & particleCount,const int64_t & lifeTime,const ParticleType & type,const float & radius,const std::shared_ptr<RSImage> & image,Vector2f imageSize)117 EmitterConfig(const int& emitRate, const ShapeType& emitShape, const Vector2f& position, const Vector2f& emitSize, 118 const int32_t& particleCount, const int64_t& lifeTime, const ParticleType& type, const float& radius, 119 const std::shared_ptr<RSImage>& image, Vector2f imageSize) 120 { 121 emitRate_ = emitRate; 122 emitShape_ = emitShape; 123 position_ = position; 124 emitSize_ = emitSize; 125 particleCount_ = particleCount; 126 lifeTime_ = lifeTime; 127 type_ = type; 128 radius_ = radius; 129 image_ = image; 130 imageSize_ = imageSize; 131 if (image_ != nullptr) { 132 auto pixelMap = image_->GetPixelMap(); 133 if (pixelMap != nullptr) { 134 image_->SetDstRect(RectF(position_.x_, position_.y_, static_cast<float>(pixelMap->GetWidth()), 135 static_cast<float>(pixelMap->GetHeight()))); 136 } 137 } 138 } 139 EmitterConfig(const EmitterConfig& config) = default; 140 EmitterConfig& operator=(const EmitterConfig& config) = default; 141 ~EmitterConfig() = default; 142 }; 143 144 class RSB_EXPORT ParticleVelocity { 145 public: 146 Range<float> velocityValue_; 147 Range<float> velocityAngle_; 148 ParticleVelocity()149 ParticleVelocity() : velocityValue_(), velocityAngle_() {} ParticleVelocity(const Range<float> & velocityValue,const Range<float> & velocityAngle)150 ParticleVelocity(const Range<float>& velocityValue, const Range<float>& velocityAngle) 151 { 152 velocityValue_ = Range(velocityValue.start_, velocityValue.end_); 153 velocityAngle_ = Range(velocityAngle.start_, velocityAngle.end_); 154 } 155 ParticleVelocity(const ParticleVelocity& velocity) = default; 156 ParticleVelocity& operator=(const ParticleVelocity& velocity) = default; 157 ~ParticleVelocity() = default; 158 }; 159 160 class RSB_EXPORT RenderParticleAcceleration { 161 public: 162 RenderParticleParaType<float> accelerationValue_; 163 RenderParticleParaType<float> accelerationAngle_; 164 165 RenderParticleAcceleration() = default; RenderParticleAcceleration(const RenderParticleParaType<float> & accelerationValue,const RenderParticleParaType<float> & accelerationAngle)166 RenderParticleAcceleration( 167 const RenderParticleParaType<float>& accelerationValue, const RenderParticleParaType<float>& accelerationAngle) 168 { 169 accelerationValue_ = accelerationValue; 170 accelerationAngle_ = accelerationAngle; 171 } 172 RenderParticleAcceleration(const RenderParticleAcceleration& acceleration) = default; 173 RenderParticleAcceleration& operator=(const RenderParticleAcceleration& acceleration) = default; 174 ~RenderParticleAcceleration() = default; 175 }; 176 177 class RSB_EXPORT RenderParticleColorParaType { 178 public: 179 Range<Color> colorVal_; 180 ParticleUpdator updator_; 181 Range<float> redRandom_; 182 Range<float> greenRandom_; 183 Range<float> blueRandom_; 184 Range<float> alphaRandom_; 185 186 std::vector<std::shared_ptr<ChangeInOverLife<Color>>> valChangeOverLife_; RenderParticleColorParaType(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<std::shared_ptr<ChangeInOverLife<Color>>> & valChangeOverLife)187 RenderParticleColorParaType(const Range<Color>& colorVal, const ParticleUpdator& updator, 188 const Range<float>& redRandom, const Range<float>& greenRandom, const Range<float>& blueRandom, 189 const Range<float>& alphaRandom, std::vector<std::shared_ptr<ChangeInOverLife<Color>>>& valChangeOverLife) 190 { 191 colorVal_ = colorVal; 192 updator_ = updator; 193 redRandom_ = redRandom; 194 greenRandom_ = greenRandom; 195 blueRandom_ = blueRandom; 196 alphaRandom_ = alphaRandom; 197 for (size_t i = 0; i < valChangeOverLife.size(); i++) { 198 auto change = valChangeOverLife[i]; 199 valChangeOverLife_.push_back(change); 200 } 201 } RenderParticleColorParaType()202 RenderParticleColorParaType() 203 : colorVal_(), updator_(ParticleUpdator::NONE), redRandom_(), greenRandom_(), blueRandom_(), alphaRandom_() 204 {} 205 RenderParticleColorParaType(const RenderParticleColorParaType& velocity) = default; 206 RenderParticleColorParaType& operator=(const RenderParticleColorParaType& velocity) = default; 207 ~RenderParticleColorParaType() = default; 208 }; 209 210 class RSB_EXPORT ParticleRenderParams { 211 public: 212 EmitterConfig emitterConfig_; 213 ParticleVelocity velocity_; 214 RenderParticleAcceleration acceleration_; 215 RenderParticleColorParaType color_; 216 RenderParticleParaType<float> opacity_; 217 RenderParticleParaType<float> scale_; 218 RenderParticleParaType<float> spin_; ParticleRenderParams(const EmitterConfig & emitterConfig,const ParticleVelocity & velocity,const RenderParticleAcceleration & acceleration,const RenderParticleColorParaType & color,const RenderParticleParaType<float> & opacity,const RenderParticleParaType<float> & scale,const RenderParticleParaType<float> & spin)219 explicit ParticleRenderParams(const EmitterConfig& emitterConfig, const ParticleVelocity& velocity, 220 const RenderParticleAcceleration& acceleration, const RenderParticleColorParaType& color, 221 const RenderParticleParaType<float>& opacity, const RenderParticleParaType<float>& scale, 222 const RenderParticleParaType<float>& spin) 223 { 224 emitterConfig_ = emitterConfig; 225 velocity_ = velocity; 226 acceleration_ = acceleration; 227 color_ = color; 228 opacity_ = opacity; 229 scale_ = scale; 230 spin_ = spin; 231 } ParticleRenderParams()232 ParticleRenderParams() : emitterConfig_(), velocity_(), acceleration_(), color_(), opacity_(), scale_(), spin_() {}; 233 ParticleRenderParams(const ParticleRenderParams& params) = default; 234 ParticleRenderParams& operator=(const ParticleRenderParams& params) = default; 235 ~ParticleRenderParams() = default; 236 237 int GetEmitRate() const; 238 ShapeType GetEmitShape() const; 239 Vector2f GetEmitPosition() const; 240 Vector2f GetEmitSize() const; 241 int32_t GetParticleCount() const; 242 int64_t GetParticleLifeTime() const; 243 ParticleType GetParticleType() const; 244 float GetParticleRadius() const; 245 std::shared_ptr<RSImage> GetParticleImage(); 246 Vector2f GetImageSize() const; 247 248 float GetVelocityStartValue() const; 249 float GetVelocityEndValue() const; 250 float GetVelocityStartAngle() const; 251 float GetVelocityEndAngle() const; 252 253 float GetAccelerationStartValue() const; 254 float GetAccelerationEndValue() const; 255 float GetAccelerationStartAngle() const; 256 float GetAccelerationEndAngle() const; 257 ParticleUpdator GetAccelerationValueUpdator(); 258 ParticleUpdator GetAccelerationAngleUpdator(); 259 float GetAccelRandomValueStart() const; 260 float GetAccelRandomValueEnd() const; 261 float GetAccelRandomAngleStart() const; 262 float GetAccelRandomAngleEnd() const; 263 264 Color GetColorStartValue(); 265 Color GetColorEndValue(); 266 ParticleUpdator GetColorUpdator(); 267 float GetRedRandomStart() const; 268 float GetRedRandomEnd() const; 269 float GetGreenRandomStart() const; 270 float GetGreenRandomEnd() const; 271 float GetBlueRandomStart() const; 272 float GetBlueRandomEnd() const; 273 float GetAlphaRandomStart() const; 274 float GetAlphaRandomEnd() const; 275 276 float GetOpacityStartValue(); 277 float GetOpacityEndValue(); 278 ParticleUpdator GetOpacityUpdator(); 279 float GetOpacityRandomStart() const; 280 float GetOpacityRandomEnd() const; 281 282 float GetScaleStartValue(); 283 float GetScaleEndValue(); 284 ParticleUpdator GetScaleUpdator(); 285 float GetScaleRandomStart() const; 286 float GetScaleRandomEnd() const; 287 288 float GetSpinStartValue(); 289 float GetSpinEndValue(); 290 ParticleUpdator GetSpinUpdator(); 291 float GetSpinRandomStart() const; 292 float GetSpinRandomEnd() const; 293 294 void SetEmitConfig(const EmitterConfig& emitConfig); 295 void SetParticleVelocity(const ParticleVelocity& velocity); 296 void SetParticleAcceleration(const RenderParticleAcceleration& acceleration); 297 void SetParticleColor(const RenderParticleColorParaType& color); 298 void SetParticleOpacity(const RenderParticleParaType<float>& opacity); 299 void SetParticleScale(const RenderParticleParaType<float>& scale); 300 void SetParticleSpin(const RenderParticleParaType<float>& spin); 301 }; 302 303 class RSB_EXPORT RSRenderParticle { 304 public: 305 explicit RSRenderParticle(std::shared_ptr<ParticleRenderParams> particleParams); 306 RSRenderParticle() = default; 307 ~RSRenderParticle() = default; 308 309 // Set methods 310 void SetPosition(const Vector2f& position); 311 void SetVelocity(const Vector2f& velocity); 312 void SetAcceleration(const Vector2f& acceleration); 313 void SetSpin(const float& spin); 314 void SetOpacity(const float& opacity); 315 void SetColor(const Color& color); 316 void SetScale(const float& scale); 317 void SetRadius(const float& radius); 318 void SetImage(const std::shared_ptr<RSImage>& image); 319 void SetImageSize(const Vector2f& imageSize); 320 void SetParticleType(const ParticleType& particleType); 321 void SetActiveTime(const int64_t& activeTime); 322 void SetAccelerationValue(float accelerationValue); 323 void SetAccelerationAngle(float accelerationAngle); 324 void SetRedF(float redF); 325 void SetGreenF(float greenF); 326 void SetBlueF(float blueF); 327 void SetAlphaF(float alphaF); 328 329 // Get methods 330 Vector2f GetPosition(); 331 Vector2f GetVelocity(); 332 Vector2f GetAcceleration(); 333 float GetSpin(); 334 float GetOpacity(); 335 Color GetColor(); 336 float GetScale(); 337 float GetRadius(); 338 float GetAccelerationValue(); 339 float GetAccelerationAngle(); 340 float GetRedSpeed(); 341 float GetGreenSpeed(); 342 float GetBlueSpeed(); 343 float GetAlphaSpeed(); 344 float GetOpacitySpeed(); 345 float GetScaleSpeed(); 346 float GetSpinSpeed(); 347 float GetAccelerationValueSpeed(); 348 float GetAccelerationAngleSpeed(); 349 float GetRedF(); 350 float GetGreenF(); 351 float GetBlueF(); 352 float GetAlphaF(); 353 std::shared_ptr<RSImage> GetImage(); 354 Vector2f GetImageSize(); 355 ParticleType GetParticleType(); 356 int64_t GetActiveTime(); 357 std::shared_ptr<ParticleRenderParams> GetParticleRenderParams(); 358 359 // Other methods 360 void InitProperty(std::shared_ptr<ParticleRenderParams> particleParams); 361 bool IsAlive() const; 362 void SetIsDead(); 363 static float GetRandomValue(float min, float max); 364 Vector2f CalculateParticlePosition(const ShapeType& emitShape, const Vector2f& position, const Vector2f& emitSize); 365 Color Lerp(const Color& start, const Color& end, float t); 366 std::shared_ptr<ParticleRenderParams> particleRenderParams_; 367 368 bool operator==(const RSRenderParticle& rhs) 369 { 370 return (position_ == rhs.position_) && (velocity_ == rhs.velocity_) && 371 (acceleration_ == rhs.acceleration_) && (scale_ == rhs.scale_) && (spin_ == rhs.spin_) && 372 (opacity_ == rhs.opacity_) && (color_ == rhs.color_) && (radius_ == rhs.radius_) && 373 (particleType_ == rhs.particleType_) && (activeTime_ == rhs.activeTime_) && 374 (lifeTime_ == rhs.lifeTime_) && (imageSize_ == rhs.imageSize_); 375 } 376 377 private: 378 Vector2f position_ = {0.f, 0.f}; 379 Vector2f velocity_ = {0.f, 0.f}; 380 Vector2f acceleration_ = {0.f, 0.f}; 381 float scale_ = 1.f; 382 float spin_ = 0.f; 383 float opacity_ = 1.f; 384 Color color_ = RgbPalette::White(); 385 float radius_ = 0.f; 386 std::shared_ptr<RSImage> image_; 387 Vector2f imageSize_; 388 ParticleType particleType_ = ParticleType::POINTS; 389 int64_t activeTime_ = 0; 390 int64_t lifeTime_ = 0; 391 bool dead_ = false; 392 float accelerationValue_ = 0.f; 393 float accelerationAngle_ = 0.f; 394 float redSpeed_ = 0.f; 395 float greenSpeed_ = 0.f; 396 float blueSpeed_ = 0.f; 397 float alphaSpeed_ = 0.f; 398 float opacitySpeed_ = 0.f; 399 float scaleSpeed_ = 0.f; 400 float spinSpeed_ = 0.f; 401 float accelerationValueSpeed_ = 0.f; 402 float accelerationAngleSpeed_ = 0.f; 403 float redF_ = 0.f; 404 float greenF_ = 0.f; 405 float blueF_ = 0.f; 406 float alphaF_ = 0.f; 407 }; 408 409 class RSB_EXPORT RSRenderParticleVector { 410 public: RSRenderParticleVector(std::vector<std::shared_ptr<RSRenderParticle>> renderParticleVector)411 explicit RSRenderParticleVector(std::vector<std::shared_ptr<RSRenderParticle>> renderParticleVector) 412 { 413 renderParticleVector_ = renderParticleVector; 414 } 415 RSRenderParticleVector() = default; 416 ~RSRenderParticleVector() = default; GetParticleSize()417 int GetParticleSize() 418 { 419 return renderParticleVector_.size(); 420 } 421 GetParticleVector()422 std::vector<std::shared_ptr<RSRenderParticle>> GetParticleVector() 423 { 424 return renderParticleVector_; 425 } 426 427 bool operator==(const RSRenderParticleVector& rhs) const 428 { 429 bool equal = false; 430 if (this->renderParticleVector_.size() != rhs.renderParticleVector_.size()) { 431 return false; 432 } 433 if (this->renderParticleVector_.size() == 0) { 434 return true; 435 } 436 for (size_t i = 0; i < this->renderParticleVector_.size(); i++) { 437 equal = equal && (this->renderParticleVector_[i] == rhs.renderParticleVector_[i]) && 438 (*(this->renderParticleVector_[i]) == *(rhs.renderParticleVector_[i])); 439 } 440 return equal; 441 } 442 443 std::vector<std::shared_ptr<RSRenderParticle>> renderParticleVector_; 444 }; 445 } // namespace Rosen 446 } // namespace OHOS 447 448 #endif // RENDER_SERVICE_CLIENT_CORE_ANIMATION_RS_RENDER_PARTICLE_H 449