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 #include "animation/rs_render_particle_emitter.h" 17 18 #include <cmath> 19 #include <cstdint> 20 #include <vector> 21 22 namespace OHOS { 23 namespace Rosen { 24 constexpr int MAX_EMIT_RATE = 5000; RSRenderParticleEmitter(std::shared_ptr<ParticleRenderParams> particleParams)25RSRenderParticleEmitter::RSRenderParticleEmitter(std::shared_ptr<ParticleRenderParams> particleParams) 26 : particleParams_(particleParams) 27 {} 28 PreEmit()29void RSRenderParticleEmitter::PreEmit() 30 { 31 if (particleParams_ == nullptr) { 32 emitFinish_ = true; 33 return; 34 } 35 auto particleType = particleParams_->GetParticleType(); 36 auto opacityUpdater = particleParams_->GetOpacityUpdator(); 37 auto scaleUpdater = particleParams_->GetScaleUpdator(); 38 if (opacityUpdater == ParticleUpdator::NONE && particleParams_->GetOpacityEndValue() <= 0.f) { 39 emitFinish_ = true; 40 return; 41 } else if (opacityUpdater == ParticleUpdator::RANDOM && particleParams_->GetOpacityEndValue() <= 0.f && 42 particleParams_->GetOpacityRandomEnd() <= 0.f) { 43 emitFinish_ = true; 44 return; 45 } 46 if (scaleUpdater == ParticleUpdator::NONE && particleParams_->GetScaleEndValue() <= 0.f) { 47 emitFinish_ = true; 48 return; 49 } else if (scaleUpdater == ParticleUpdator::RANDOM && particleParams_->GetScaleEndValue() <= 0.f && 50 particleParams_->GetScaleRandomEnd() <= 0.f) { 51 emitFinish_ = true; 52 return; 53 } 54 if (particleType == ParticleType::IMAGES) { 55 auto particleImage = particleParams_->GetParticleImage(); 56 auto imageSize = particleParams_->GetImageSize(); 57 if (particleImage == nullptr || (imageSize.x_ <= 0.f || imageSize.y_ <= 0.f)) { 58 emitFinish_ = true; 59 return; 60 } 61 } 62 if (particleType == ParticleType::POINTS) { 63 auto radius = particleParams_->GetParticleRadius(); 64 if (radius <= 0.f) { 65 emitFinish_ = true; 66 return; 67 } 68 } 69 } 70 EmitParticle(int64_t deltaTime)71void RSRenderParticleEmitter::EmitParticle(int64_t deltaTime) 72 { 73 PreEmit(); 74 if (particleParams_ == nullptr || emitFinish_ == true) { 75 return; 76 } 77 auto emitRate = std::min(particleParams_->GetEmitRate(), MAX_EMIT_RATE); 78 auto maxParticle = particleParams_->GetParticleCount(); 79 auto lifeTimeStart = particleParams_->GetLifeTimeStartValue(); 80 auto lifeTimeEnd = particleParams_->GetLifeTimeEndValue(); 81 float last = particleCount_; 82 particles_.clear(); 83 if (maxParticle == -1) { 84 maxParticle = INT32_MAX; 85 } 86 if (maxParticle <= 0 || (lifeTimeStart == 0 && lifeTimeEnd == 0) || last > static_cast<float>(maxParticle)) { 87 emitFinish_ = true; 88 return; 89 } 90 particleCount_ += static_cast<float>(emitRate * deltaTime) / NS_TO_S; 91 spawnNum_ += particleCount_ - last; 92 93 if (ROSEN_EQ(spawnNum_, 0.f)) { 94 return; 95 } 96 if (ROSEN_EQ(last, 0.f)) { 97 for (int32_t i = 0; i < std::min(static_cast<int32_t>(spawnNum_), maxParticle); i++) { 98 auto particle = std::make_shared<RSRenderParticle>(particleParams_); 99 particles_.push_back(particle); 100 spawnNum_ -= 1.f; 101 } 102 if (particleCount_ > static_cast<float>(maxParticle)) { 103 return; 104 } 105 } 106 while (spawnNum_ >= 1.f && std::ceil(last) <= static_cast<float>(maxParticle)) { 107 auto particle = std::make_shared<RSRenderParticle>(particleParams_); 108 particles_.push_back(particle); 109 spawnNum_ -= 1.f; 110 last += 1.f; 111 } 112 } 113 IsEmitterFinish()114bool RSRenderParticleEmitter::IsEmitterFinish() 115 { 116 return emitFinish_; 117 } 118 GetParticles()119const std::vector<std::shared_ptr<RSRenderParticle>>& RSRenderParticleEmitter::GetParticles() 120 { 121 return particles_; 122 } 123 124 } // namespace Rosen 125 } // namespace OHOS 126