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