Particle System Demo
[platform/core/uifw/dali-demo.git] / examples / particle-system / effects / image-effect-modifier.cpp
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "image-effect-modifier.h"
19 #include "image-effect-source.h"
20 namespace Dali::ParticleEffect
21 {
22
23 #define RAD(x) (float(x)*M_PI/180.0f)
24
25 ImageExplodeEffectModifier::ImageExplodeEffectModifier(ParticleEmitter& emitter)
26 : mEmitter(emitter)
27 {
28
29 }
30
31 bool ImageExplodeEffectModifier::IsMultiThreaded()
32 {
33   return false;
34 }
35
36 void ImageExplodeEffectModifier::Update(ParticleList& particleList, uint32_t first, uint32_t count)
37 {
38   // If no acive particles return
39   if(!particleList.GetActiveParticleCount())
40   {
41     return;
42   }
43
44   // Retrieve the Source and get the stream
45   if(!mStreamBasePos)
46   {
47     mStreamBasePos = static_cast<ImageExplodeEffectSource*>(&mEmitter.GetSource().GetSourceCallback())->mStreamBasePos;
48   }
49
50   // Missing stream, return!
51   if(!mStreamBasePos)
52   {
53     return;
54   }
55
56   auto& activeParticles = particleList.GetActiveParticles();
57
58   auto it = activeParticles.begin();
59   std::advance(it, first);
60
61   mAngle += 5.0f;
62
63   for(; count; ++it, count--)
64   {
65     // Acquire stream data
66     auto&                  particle = *it;
67     auto&                  position = particle.Get<Vector3>(ParticleStream::POSITION_STREAM_BIT);
68     auto&                  color    = particle.Get<Vector4>(ParticleStream::COLOR_STREAM_BIT);
69
70     // Get base positions
71     auto& basePos = particle.GetByIndex<Vector3>(mStreamBasePos);
72     position.z = 200.f * sin(RAD(mAngle+basePos.x));
73     color.a = position.z < 0.0f ? 1.0f : 1.0f - position.z/500.0f;
74     position.z = 500 + position.z;
75   }
76 }
77 }