particles-example.cpp
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright (c) 2021 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// INTERNAL INCLUDES
#include <fstream>
#include <iostream>
#include <list>
#include <memory>
#include <numeric>
#include <random>
#include "dali/devel-api/adaptor-framework/tilt-sensor.h"
#include "dali/public-api/actors/camera-actor.h"
#include "dali/public-api/actors/layer.h"
#include "dali/public-api/adaptor-framework/application.h"
#include "dali/public-api/adaptor-framework/key.h"
#include "dali/public-api/events/pan-gesture-detector.h"
#include "dali/public-api/events/tap-gesture-detector.h"
#include "dali/public-api/events/touch-event.h"
#include "dali/public-api/object/property-index-ranges.h"
#include "dali/public-api/render-tasks/render-task-list.h"
#include "dali/public-api/render-tasks/render-task.h"
#include "float-rand.h"
#include "particle-field.h"
#include "particle-view.h"
#include "utils.h"
using namespace Dali;
namespace
{
const float PARTICLE_ALPHA = .75;
const float ALPHA_TEST_REF_VALUE = .0625;
const float NEAR_FADE = 0.04; // normalized depth
const float FAR_FADE = 0.8; // normalized depth
const ParticleField PARTICLE_FIELD = {
200.f, // particle size
Vector3(2500., 2500., 7800.), // box size - depth needs to be >= camera depth range
Vector3(6., 6., 12.), // number of particles
.333, // size variance
1., // noise amount
200., // disperse
250., // motion scale
15., // motion cycle length
6., // twinkle frequency
.11, // twinkle size scale
.333 // twinkle opacity weight
};
const float WORLD_ANGULAR_VELOCITY = .08; // radians per seconds
const float WORLD_LINEAR_VELOCITY = -500; // units along z
const float SCATTER_RADIUS = 450.0f;
const float SCATTER_AMOUNT = 180.0f;
const float SCATTER_DURATION_OUT = .8f;
const float SCATTER_DURATION_IN = 1.5f;
const float FADE_DURATION = 1.2f;
const float FADEOUT_SPEED_MULTIPLIER = 4.f; // speed multiplier upon fading out.
const float FOCAL_LENGTH = 0.5f; // normalized depth value where particles appear sharp.
const float APERTURE = 2.2f; // distance scale - the higher it is, the quicker the particles get blurred out moving away from the focal length.
const ColorRange DEFAULT_COLOR_RANGE{Vector3(0., 48. / 255., 1.), Vector3(0., 216. / 255., 1.)};
const float TILT_SCALE = 0.2;
const float TILT_RANGE_DEGREES = 30.f;
FloatRand sFloatRand;
class TiltFilter
{
public:
void Reset()
{
std::fill(mTiltSamples, mTiltSamples + FILTER_SIZE, Vector2(.0f, .0f));
}
void Add(Dali::Vector2 tilt)
{
mTiltSamples[mIdxNextSample] = tilt;
mIdxNextSample = (mIdxNextSample + 1) % FILTER_SIZE;
}
Dali::Vector2 Filter() const
{
return std::accumulate(mTiltSamples, mTiltSamples + FILTER_SIZE, Vector2(.0f, .0f)) / FILTER_SIZE;
}
private:
enum
{
FILTER_SIZE = 8u
};
Dali::Vector2 mTiltSamples[FILTER_SIZE];
size_t mIdxNextSample = 0;
};
class ParticlesExample : public ConnectionTracker
{
public:
ParticlesExample(Application& app)
: mApp(app)
{
mApp.InitSignal().Connect(this, &ParticlesExample::OnInit);
mApp.TerminateSignal().Connect(this, &ParticlesExample::OnTerminate);
}
~ParticlesExample() = default;
private:
Application& mApp;
CameraActor mCamera;
Actor mWorld;
Vector2 mAngularPosition;
ColorRange mColors;
std::unique_ptr<ParticleView> mParticles;
std::unique_ptr<ParticleView> mExpiringParticles;
TapGestureDetector mDoubleTapGesture;
TiltSensor mTiltSensor;
TiltFilter mTiltFilter;
PanGestureDetector mPanGesture;
void OnInit(Application& application)
{
Window window = application.GetWindow();
auto rootLayer = window.GetRootLayer();
rootLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::Behavior::LAYER_3D);
window.KeyEventSignal().Connect(this, &ParticlesExample::OnKeyEvent);
window.GetRootLayer().TouchedSignal().Connect(this, &ParticlesExample::OnTouched);
auto tiltSensor = TiltSensor::Get();
if(tiltSensor.Start())
{
// Get notifications when the device is tilted
tiltSensor.TiltedSignal().Connect(this, &ParticlesExample::OnTilted);
}
else
{
mPanGesture = PanGestureDetector::New();
mPanGesture.Attach(rootLayer);
mPanGesture.DetectedSignal().Connect(this, &ParticlesExample::OnPan);
}
// Get camera
RenderTaskList tasks = window.GetRenderTaskList();
RenderTask mainPass = tasks.GetTask(0);
CameraActor camera = mainPass.GetCameraActor();
mCamera = camera;
// Create world - particles and clock are added to it; this is what we apply tilt to.
auto world = CreateActor();
world.SetResizePolicy(ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS);
window.Add(world);
mWorld = world;
// Create particles
TriggerColorTransition(DEFAULT_COLOR_RANGE);
// Setup double tap detector for color change
mDoubleTapGesture = TapGestureDetector::New(2);
mDoubleTapGesture.Attach(rootLayer);
mDoubleTapGesture.DetectedSignal().Connect(this, &ParticlesExample::OnDoubleTap);
}
void OnTerminate(Application& app)
{
UnparentAndReset(mWorld);
mDoubleTapGesture.Reset();
mPanGesture.Reset();
mTiltSensor.Reset();
}
void OnPause(Application& app)
{
mTiltSensor.Stop();
}
void OnResume(Application& app)
{
mTiltSensor.Start();
}
void OnKeyEvent(const KeyEvent& event)
{
if(event.GetState() == KeyEvent::UP) // single keystrokes
{
if(IsKey(event, DALI_KEY_ESCAPE) || IsKey(event, DALI_KEY_BACK))
{
mApp.Quit();
}
}
}
bool OnTouched(Actor a, const TouchEvent& event)
{
if(event.GetPointCount() > 0)
{
auto screenPos = event.GetScreenPosition(0);
switch(event.GetState(0))
{
case PointState::STARTED:
{
mParticles->Scatter(SCATTER_RADIUS, SCATTER_AMOUNT, SCATTER_DURATION_OUT, SCATTER_DURATION_IN);
Vector3 ray = GetViewRay(screenPos);
mParticles->SetScatterRay(ray);
}
break;
default:
break;
}
}
return false;
}
void OnDoubleTap(Actor /*actor*/, const TapGesture& /*gesture*/)
{
if(!mExpiringParticles)
{
mColors.rgb0 = Vector3::ONE - mColors.rgb1;
mColors.rgb1 = FromHueSaturationLightness(Vector3(sFloatRand() * 360.f, sFloatRand() * .5f + .5f, sFloatRand() * .25 + .75f));
TriggerColorTransition(mColors);
}
}
void OnPan(Actor actor, const PanGesture& gesture)
{
auto tilt = gesture.GetDisplacement() / Vector2(mApp.GetWindow().GetSize()) * TILT_SCALE;
Quaternion q(Radian(-tilt.y), Radian(tilt.x), Radian(0.f));
Quaternion q0 = mWorld.GetProperty(Actor::Property::ORIENTATION).Get<Quaternion>();
mWorld.SetProperty(Actor::Property::ORIENTATION, q * q0);
}
void OnTilted(const TiltSensor& sensor)
{
mTiltFilter.Add(Vector2(sensor.GetPitch(), sensor.GetRoll()));
Vector2 tilt = mTiltFilter.Filter() * TILT_RANGE_DEGREES;
Quaternion q(Radian(Degree(tilt.x)), Radian(Degree(tilt.y)), Radian(0.f));
mWorld.SetProperty(Actor::Property::ORIENTATION, q);
}
Vector3 GetViewRay(const Vector2& screenPos)
{
Vector2 screenSize = mApp.GetWindow().GetSize();
Vector2 normScreenPos = (screenPos / screenSize) * 2.f - Vector2::ONE;
const float fov = mCamera.GetProperty(CameraActor::Property::FIELD_OF_VIEW).Get<float>();
const float tanFov = std::tan(fov);
const float zNear = mCamera.GetProperty(CameraActor::Property::NEAR_PLANE_DISTANCE).Get<float>();
const float hProj = zNear * tanFov;
const float aspectRatio = mCamera.GetProperty(CameraActor::Property::ASPECT_RATIO).Get<float>();
const float wProj = hProj * aspectRatio;
// Get camera orientation for view space ray casting. Assume:
// - this to be world space, i.e. no parent transforms;
// - no scaling;
Quaternion cameraOrientation = mCamera.GetProperty(Actor::Property::ORIENTATION).Get<Quaternion>();
Matrix worldCamera;
worldCamera.SetTransformComponents(Vector3::ONE, cameraOrientation, Vector3::ZERO);
float* data = worldCamera.AsFloat();
Vector3 xWorldCamera(data[0], data[4], data[8]);
xWorldCamera *= wProj * normScreenPos.x / xWorldCamera.Length();
Vector3 yWorldCamera(data[1], data[5], data[9]);
yWorldCamera *= hProj * normScreenPos.y / yWorldCamera.Length();
Vector3 zWorldCamera(data[2], data[6], data[10]);
Vector3 ray = xWorldCamera + yWorldCamera + zWorldCamera * -zNear;
ray.Normalize();
return ray;
}
void TriggerColorTransition(const ColorRange& range)
{
if(mParticles)
{
mExpiringParticles = std::move(mParticles);
// NOTE: this will break the perfect looping, but we can get away with it because we're fading out.
mExpiringParticles->SetLinearVelocity(WORLD_LINEAR_VELOCITY * FADEOUT_SPEED_MULTIPLIER);
auto& p = mExpiringParticles;
mExpiringParticles->Fade(FADE_DURATION, 0.f, AlphaFunction::EASE_IN, [&p](Animation&) {
p.reset();
});
}
mParticles.reset(new ParticleView(PARTICLE_FIELD, mWorld, mCamera));
mParticles->SetColorRange(range);
mParticles->SetFocalLength(FOCAL_LENGTH);
mParticles->SetAperture(APERTURE);
mParticles->SetAlphaTestRefValue(ALPHA_TEST_REF_VALUE);
mParticles->SetFadeRange(NEAR_FADE, FAR_FADE);
mParticles->SetAngularVelocity(WORLD_ANGULAR_VELOCITY);
mParticles->SetLinearVelocity(WORLD_LINEAR_VELOCITY);
mParticles->Fade(FADE_DURATION, PARTICLE_ALPHA, 0.f, AlphaFunction::EASE_OUT);
}
};
} // namespace
int DALI_EXPORT_API main(int argc, char** argv)
{
Application application = Application::New(&argc, &argv, DEMO_THEME_PATH);
ParticlesExample example(application);
application.MainLoop();
return 0;
}