metaball-explosion-example.cpp
16.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* 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.
*
*/
// EXTERNAL INCLUDES
#include <cstdint> // uint32_t, uint16_t etc
#include <cstdio>
#include <string>
#include <dali/public-api/math/random.h>
#include <dali/public-api/rendering/frame-buffer.h>
#include <dali/public-api/rendering/renderer.h>
#include <dali/public-api/rendering/texture-set.h>
#include <dali/public-api/rendering/texture.h>
// INTERNAL INCLUDES
#include "generated/metaball-frag.h"
#include "generated/metaball-refraction-frag.h"
#include "generated/metaball-vert.h"
#include "shared/utility.h" // DemoHelper::LoadTexture
using namespace Dali;
namespace // unnamed namespace for constants
{
// background image
const char* const BACKGROUND_IMAGE(DEMO_IMAGE_DIR "background-2.jpg");
// number of metaballs
constexpr uint32_t METABALL_NUMBER = 6;
/**
* Metadata for each ball
*/
struct MetaballInfo
{
Actor actor;
Vector2 position;
float radius;
float initRadius;
//new shader stuff
Property::Index positionIndex;
Property::Index positionVarIndex;
};
} // unnamed namespace
/**
* Demo using Metaballs
*
* When the metaball is clicked it explodes to smaller balls
*/
class MetaballExplosionController : public ConnectionTracker
{
public:
/**
* Constructor
* @param application
*/
MetaballExplosionController(Application& application);
/**
* Destructor
*/
virtual ~MetaballExplosionController();
/**
* Creates the metaballs and initializes the scene
*/
void Create(Application& app);
/**
* Touch event handler to center metaballs at touch position
* and start explosion animation on release
*/
bool OnTouch(Actor actor, const TouchEvent& touch);
/**
* Key event handler to quit application on escape or back key
*/
void OnKeyEvent(const KeyEvent& event);
private: // Data
Application& mApplication;
Vector2 mScreenSize;
Texture mBackgroundTexture;
FrameBuffer mMetaballFBO;
Actor mMetaballRoot;
MetaballInfo mMetaballs[METABALL_NUMBER];
Property::Index mPositionIndex;
Actor mCompositionActor;
//Motion
Vector2 mCurrentTouchPosition;
Vector2 mMetaballPosVariation;
Vector2 mMetaballPosVariationFrom;
Vector2 mMetaballPosVariationTo;
Vector2 mMetaballCenter;
//Animations
Animation mPositionVarAnimation[METABALL_NUMBER];
uint32_t mDispersion;
Animation mDispersionAnimation[METABALL_NUMBER];
Timer mTimerDispersion;
float mTimeMultiplier;
// Private helper functions
/**
* Create a mesh data with the geometry for the metaball rendering
* @param aspectMappedTexture whether texture coords should be mapped based on aspect ratio
*/
Geometry CreateGeometry(bool aspectMappedTexture = true);
/**
* Create a actors and renderers for the metaballs
*/
void CreateMetaballActors();
/**
* Create the render task and FBO to render the metaballs into a texture
*/
void CreateMetaballImage();
/**
* Create the the final composition
*/
void CreateComposition();
/**
* Function to create animations for the small variations of position inside the metaball
*/
void CreateAnimations();
/**
* Function to reset metaball state
*/
void ResetMetaballs(bool resetAnims);
/**
* Function to create disperse each of the ball that compose the metaball when exploding
*/
void DisperseBallAnimation(uint32_t ball);
/**
* Function to make metaballs come back to reset position
*/
void LaunchResetMetaballPosition(Animation& source);
/**
* Function to set things at the end of the animation
*/
void EndDisperseAnimation(Animation& source);
/**
* Function to init dispersion of the metaballs one by one using a timer
* (so not all the balls begin moving at the same time)
*/
bool OnTimerDispersionTick();
/**
* Function to set the actual position of the metaballs when the user clicks the screen
*/
void SetPositionToMetaballs(const Vector2& metaballCenter);
};
/**
* Implementation
*/
MetaballExplosionController::MetaballExplosionController(Application& application)
: mApplication(application),
mScreenSize(),
mBackgroundTexture(),
mMetaballFBO(),
mMetaballRoot(),
mMetaballs(),
mPositionIndex(),
mCompositionActor(),
mCurrentTouchPosition(),
mMetaballPosVariation(),
mMetaballPosVariationFrom(),
mMetaballPosVariationTo(),
mMetaballCenter(),
mPositionVarAnimation(),
mDispersion(0),
mDispersionAnimation(),
mTimerDispersion(),
mTimeMultiplier(1.0f)
{
// Connect to the Application's Init signal
mApplication.InitSignal().Connect(this, &MetaballExplosionController::Create);
}
MetaballExplosionController::~MetaballExplosionController()
{
// Nothing to do here;
}
void MetaballExplosionController::Create(Application& app)
{
Window window = app.GetWindow();
window.KeyEventSignal().Connect(this, &MetaballExplosionController::OnKeyEvent);
mScreenSize = window.GetSize();
mTimeMultiplier = 1.0f;
window.SetBackgroundColor(Color::BLACK);
// Load background texture
mBackgroundTexture = DemoHelper::LoadTexture(BACKGROUND_IMAGE);
srand(static_cast<uint32_t>(time(0)));
//Create internal data
CreateMetaballActors();
CreateMetaballImage();
CreateComposition();
CreateAnimations();
mDispersion = 0;
mTimerDispersion = Timer::New(150);
mTimerDispersion.TickSignal().Connect(this, &MetaballExplosionController::OnTimerDispersionTick);
// Connect the callback to the touch signal on the mesh actor
window.GetRootLayer().TouchedSignal().Connect(this, &MetaballExplosionController::OnTouch);
}
Geometry MetaballExplosionController::CreateGeometry(bool aspectMappedTexture)
{
const float aspect = mScreenSize.y / mScreenSize.x;
// Create vertices and specify their color
const float xsize = mScreenSize.x * 0.5;
// Create the meshdata for the metaballs
struct VertexPosition
{
Vector2 position;
};
struct VertexTexture
{
Vector2 texture;
};
VertexPosition vertices[] =
{
{Vector2(-xsize, -xsize * aspect)},
{Vector2(xsize, -xsize * aspect)},
{Vector2(-xsize, xsize * aspect)},
{Vector2(xsize, xsize * aspect)}};
const float textureAspect = (aspectMappedTexture) ? aspect : 1.0f;
VertexTexture textures[] =
{
{Vector2(0.0f, 0.0f)},
{Vector2(1.0f, 0.0f)},
{Vector2(0.0f, 1.0f * textureAspect)},
{Vector2(1.0f, 1.0f * textureAspect)}};
uint32_t numberOfVertices = sizeof(vertices) / sizeof(VertexPosition);
// Vertices
Property::Map positionVertexFormat;
positionVertexFormat["aPosition"] = Property::VECTOR2;
VertexBuffer positionVertices = VertexBuffer::New(positionVertexFormat);
positionVertices.SetData(vertices, numberOfVertices);
// Textures
Property::Map textureVertexFormat;
textureVertexFormat["aTexture"] = Property::VECTOR2;
VertexBuffer textureVertices = VertexBuffer::New(textureVertexFormat);
textureVertices.SetData(textures, numberOfVertices);
// Indices
const uint16_t indices[] = {0, 3, 1, 0, 2, 3};
// Create the geometry object
Geometry texturedQuadGeometry = Geometry::New();
texturedQuadGeometry.AddVertexBuffer(positionVertices);
texturedQuadGeometry.AddVertexBuffer(textureVertices);
texturedQuadGeometry.SetIndexBuffer(&indices[0], sizeof(indices) / sizeof(indices[0]));
return texturedQuadGeometry;
}
void MetaballExplosionController::CreateMetaballActors()
{
// Create the shader for the metaballs, tell DALi that shader modifies geometry so we dont need to set a meaningless size
Shader shader = Shader::New(SHADER_METABALL_VERT, SHADER_METABALL_FRAG, Shader::Hint::MODIFIES_GEOMETRY);
Geometry metaballGeom = CreateGeometry();
// Reuse same renderer for each actor
Renderer renderer = Renderer::New(metaballGeom, shader);
renderer.SetProperty(Renderer::Property::BLEND_MODE, BlendMode::ON);
renderer.SetProperty(Renderer::Property::BLEND_FACTOR_SRC_RGB, BlendFactor::ONE);
renderer.SetProperty(Renderer::Property::BLEND_FACTOR_DEST_RGB, BlendFactor::ONE);
renderer.SetProperty(Renderer::Property::BLEND_FACTOR_SRC_ALPHA, BlendFactor::ONE);
renderer.SetProperty(Renderer::Property::BLEND_FACTOR_DEST_ALPHA, BlendFactor::ONE);
//Initialization of each of the metaballs
for(uint32_t i = 0; i < METABALL_NUMBER; i++)
{
mMetaballs[i].position = Vector2(0.0f, 0.0f);
mMetaballs[i].radius = mMetaballs[i].initRadius = Random::Range(0.05f, 0.07f);
mMetaballs[i].actor = Actor::New();
mMetaballs[i].actor.SetProperty(Dali::Actor::Property::NAME, "Metaball");
mMetaballs[i].actor.SetProperty(Actor::Property::SCALE, 1.0f);
mMetaballs[i].actor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mMetaballs[i].actor.AddRenderer(renderer);
mMetaballs[i].positionIndex = mMetaballs[i].actor.RegisterProperty("uPositionMetaball", mMetaballs[i].position);
mMetaballs[i].positionVarIndex = mMetaballs[i].actor.RegisterProperty("uPositionVar", Vector2(0.f, 0.f));
mMetaballs[i].actor.RegisterProperty("uGravityVector", Vector2(Random::Range(-0.2, 0.2), Random::Range(-0.2, 0.2)));
mMetaballs[i].actor.RegisterProperty("uRadius", mMetaballs[i].radius);
mMetaballs[i].actor.RegisterProperty("uRadiusVar", 0.f);
}
// Root creation
mMetaballRoot = Actor::New();
mMetaballRoot.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
for(uint32_t i = 0; i < METABALL_NUMBER; i++)
{
mMetaballRoot.Add(mMetaballs[i].actor);
}
}
void MetaballExplosionController::CreateMetaballImage()
{
// Create an FBO and a render task to create to render the metaballs with a fragment shader
Window window = mApplication.GetWindow();
mMetaballFBO = FrameBuffer::New(mScreenSize.x, mScreenSize.y);
window.Add(mMetaballRoot);
// Create the render task used to render the metaballs
RenderTaskList taskList = window.GetRenderTaskList();
RenderTask task = taskList.CreateTask();
task.SetRefreshRate(RenderTask::REFRESH_ALWAYS);
task.SetSourceActor(mMetaballRoot);
task.SetExclusive(true);
task.SetClearColor(Color::BLACK);
task.SetClearEnabled(true);
task.SetFrameBuffer(mMetaballFBO);
}
void MetaballExplosionController::CreateComposition()
{
//Create new shader
Shader shader = Shader::New(SHADER_METABALL_VERT, SHADER_METABALL_REFRACTION_FRAG);
// Create new texture set
auto textureSet = TextureSet::New();
textureSet.SetTexture(0u, mBackgroundTexture);
textureSet.SetTexture(1u, mMetaballFBO.GetColorTexture());
// Create geometry
Geometry metaballGeom = CreateGeometry(false);
Renderer mRenderer = Renderer::New(metaballGeom, shader);
mRenderer.SetTextures(textureSet);
// Create actor
mCompositionActor = Actor::New();
mCompositionActor.SetProperty(Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
mCompositionActor.SetProperty(Actor::Property::POSITION, Vector3(0.0f, 0.0f, 0.0f));
mCompositionActor.SetProperty(Actor::Property::SIZE, Vector2(mScreenSize.x, mScreenSize.y));
mCompositionActor.AddRenderer(mRenderer);
Vector2 metaballCenter(0.0, 0);
metaballCenter.x = metaballCenter.x * 0.5;
metaballCenter.y = metaballCenter.y * 0.5;
mPositionIndex = mCompositionActor.RegisterProperty("uPositionMetaball", metaballCenter);
SetPositionToMetaballs(metaballCenter);
mCompositionActor.SetProperty(Actor::Property::SIZE, Vector2(mScreenSize.x, mScreenSize.y));
Window window = mApplication.GetWindow();
window.Add(mCompositionActor);
}
void MetaballExplosionController::CreateAnimations()
{
Vector2 direction;
for(uint32_t i = 0; i < METABALL_NUMBER; i++)
{
KeyFrames keySinCosVariation = KeyFrames::New();
Vector2 sinCosVariation(0, 0);
direction.x = Random::Range(-100.f, 100.f);
direction.y = Random::Range(-100.f, 100.f);
direction.Normalize();
direction *= 0.1f;
for(uint32_t j = 0; j < 360; j++)
{
sinCosVariation.x = sinf(j * Math::PI / 180.f) * direction.x;
sinCosVariation.y = cosf(j * Math::PI / 180.f) * direction.y;
float key = j / 360.f;
keySinCosVariation.Add(key, sinCosVariation);
}
mPositionVarAnimation[i] = Animation::New(3.f);
mPositionVarAnimation[i].AnimateBetween(Property(mMetaballs[i].actor, mMetaballs[i].positionVarIndex), keySinCosVariation);
mPositionVarAnimation[i].SetLooping(true);
mPositionVarAnimation[i].Play();
}
}
void MetaballExplosionController::ResetMetaballs(bool resetAnims)
{
for(uint32_t i = 0; i < METABALL_NUMBER; i++)
{
if(mDispersionAnimation[i])
{
mDispersionAnimation[i].Clear();
}
mMetaballs[i].position = Vector2(0.0f, 0.0f);
mMetaballs[i].actor.SetProperty(mMetaballs[i].positionIndex, mMetaballs[i].position);
}
mTimerDispersion.Stop();
mDispersion = 0;
mCompositionActor.SetProperty(mPositionIndex, Vector2(0, 0));
}
void MetaballExplosionController::DisperseBallAnimation(uint32_t ball)
{
Vector2 position;
position.x = Random::Range(-1.5f, 1.5f);
position.y = Random::Range(-1.5f, 1.5f);
mDispersionAnimation[ball] = Animation::New(2.0f * mTimeMultiplier);
mDispersionAnimation[ball].AnimateTo(Property(mMetaballs[ball].actor, mMetaballs[ball].positionIndex), position);
mDispersionAnimation[ball].Play();
if(ball == METABALL_NUMBER - 1)
{
mDispersionAnimation[ball].FinishedSignal().Connect(this, &MetaballExplosionController::LaunchResetMetaballPosition);
}
}
void MetaballExplosionController::LaunchResetMetaballPosition(Animation& source)
{
for(uint32_t i = 0; i < METABALL_NUMBER; i++)
{
mDispersionAnimation[i] = Animation::New(1.5f + i * 0.25f * mTimeMultiplier);
mDispersionAnimation[i].AnimateTo(Property(mMetaballs[i].actor, mMetaballs[i].positionIndex), Vector2(0, 0));
mDispersionAnimation[i].Play();
if(i == METABALL_NUMBER - 1)
{
mDispersionAnimation[i].FinishedSignal().Connect(this, &MetaballExplosionController::EndDisperseAnimation);
}
}
}
void MetaballExplosionController::EndDisperseAnimation(Animation& source)
{
mCompositionActor.SetProperty(mPositionIndex, Vector2(0, 0));
}
bool MetaballExplosionController::OnTimerDispersionTick()
{
if(mDispersion < METABALL_NUMBER)
{
DisperseBallAnimation(mDispersion);
mDispersion++;
}
return true;
}
void MetaballExplosionController::SetPositionToMetaballs(const Vector2& metaballCenter)
{
//We set the position for the metaballs based on click position
for(uint32_t i = 0; i < METABALL_NUMBER; i++)
{
mMetaballs[i].position = metaballCenter;
mMetaballs[i].actor.SetProperty(mMetaballs[i].positionIndex, mMetaballs[i].position);
}
mCompositionActor.SetProperty(mPositionIndex, metaballCenter);
}
bool MetaballExplosionController::OnTouch(Actor actor, const TouchEvent& touch)
{
float aspectR = mScreenSize.y / mScreenSize.x;
switch(touch.GetState(0))
{
case PointState::DOWN:
{
ResetMetaballs(true);
const Vector2 screen = touch.GetScreenPosition(0);
Vector2 metaballCenter = Vector2((screen.x / mScreenSize.x) - 0.5f, (aspectR * (mScreenSize.y - screen.y) / mScreenSize.y) - 0.5f) * 2.0f;
SetPositionToMetaballs(metaballCenter);
break;
}
case PointState::MOTION:
{
const Vector2 screen = touch.GetScreenPosition(0);
Vector2 metaballCenter = Vector2((screen.x / mScreenSize.x) - 0.5f, (aspectR * (mScreenSize.y - screen.y) / mScreenSize.y) - 0.5f) * 2.0f;
SetPositionToMetaballs(metaballCenter);
break;
}
case PointState::UP:
case PointState::LEAVE:
case PointState::INTERRUPTED:
{
mTimerDispersion.Start();
break;
}
default:
break;
}
return true;
}
void MetaballExplosionController::OnKeyEvent(const KeyEvent& event)
{
if(event.GetState() == KeyEvent::DOWN)
{
if(IsKey(event, Dali::DALI_KEY_ESCAPE) || IsKey(event, Dali::DALI_KEY_BACK))
{
mApplication.Quit();
}
}
}
/**
* Main entry point
*/
int32_t DALI_EXPORT_API main(int argc, char** argv)
{
Application application = Application::New(&argc, &argv);
MetaballExplosionController test(application);
application.MainLoop();
return 0;
}