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