2 using System.Collections.Generic;
3 using System.Threading;
4 using Tizen.NUI.BaseComponents;
5 using Tizen.NUI.Components;
6 using Tizen.NUI.ParticleSystem;
8 namespace Tizen.NUI.ParticleSystem.Sample
10 // SparkleEffectSource spawns particle from the middle of the
12 class SparkleEffectSource : ParticleSourceInterface
14 public override void Construct(params object[] list)
17 mRadius = new Vector2(list[0] as Vector2);
20 public override uint Update(ParticleEmitterProxy emitterProxy, uint count)
22 if(mStreamBasePos == 0 || mStreamBaseAngle == 0) // streams must exist
29 // Create new particle (lifetime 5 seconds of each)
30 var particle = emitterProxy.NewParticle(5.0f);
36 UpdateParticle(ref particle);
43 public override void Init()
45 // Add local stream of Vector3 type
46 mStreamBasePos = Emitter.AddLocalStreamVector3(Vector3.Zero);
48 // Add local stream of float type
49 mStreamBaseAngle = Emitter.AddLocalStreamFloat(0.0f);
52 void UpdateParticle(ref Particle p)
54 float posRadians = ((mRandom.Next() % 360) * (float)Math.PI) / 180.0f;
55 p.Position = new Vector3(mRadius.X * (float)Math.Sin(posRadians), mRadius.Y * (float)Math.Cos(posRadians), 0.0f);
56 p.SetStreamValue(p.Position, mStreamBasePos);
57 p.Color = Vector4.One;
58 p.SetStreamValue(mAngle, mStreamBaseAngle);
59 mAngle = ((mAngle+5)%360);
60 float rad = ((mRandom.Next() % 360) * (float)Math.PI) / 180.0f;
61 float speed = ((mRandom.Next() % 5) + 5);
62 p.Velocity = new Vector3((float)Math.Sin(rad) * speed, (float)Math.Cos(rad) * speed, 0);
64 // Random initial scale
65 float initialScale = (float)(mRandom.Next() % 32) + 32;
66 p.Scale = new Vector3(initialScale, initialScale, 1.0f);
69 private static float mAngle = 0;
70 private Random mRandom = new Random();
71 public uint mStreamBasePos = 0;
72 public uint mStreamBaseAngle = 0;
73 private Vector2 mRadius;
76 // SparkleEffectModifier spawns particle from the middle of the
78 class SparkleEffectModifier : ParticleModifierInterface
80 public override void Construct(params object[] list)
83 mSource = list[0] as SparkleEffectSource;
86 public override void Update(ParticleEmitterProxy proxy, List<Particle> particles)
88 if (particles.Count == 0)
93 if (mStreamBasePos == 0)
95 mStreamBasePos = mSource.mStreamBasePos;
98 if (mStreamBaseAngle == 0)
100 mStreamBaseAngle = mSource.mStreamBaseAngle;
103 if (mStreamBasePos == 0)
108 for (uint i = 0; i < particles.Count; ++i)
110 var p = particles[(int)i];
112 float angle = p.GetStreamValue(mStreamBaseAngle);
113 Vector3 basePos = p.GetStreamValue(mStreamBaseAngle);
114 float radians = (float)((angle * Math.PI)/180.0f);
115 float lifetime = p.Lifetime;
116 Vector3 pos = p.Position;
117 var vel = p.Velocity;
118 p.Position = new Vector3(pos.X + vel.X * (float)Math.Cos(radians),pos.Y + vel.Y * (float)Math.Sin(radians),
121 p.Velocity = (vel * 0.990f);
122 float normalizedTime = (lifetime / p.LifetimeBase);
124 var color = new Vector4( 1, 1, 1, normalizedTime);
126 p.Scale = new Vector3(64.0f*(normalizedTime * normalizedTime * normalizedTime * normalizedTime), 64.0f*(normalizedTime * normalizedTime * normalizedTime * normalizedTime), 1.0f);
131 private uint mStreamBasePos = 0;
132 private uint mStreamBaseAngle = 0;
133 private SparkleEffectSource mSource;
137 class ParticleSystemSample : NUIApplication
139 static string IMAGE_DIR = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "image/";
141 private Window mWindow;
143 private ParticleEmitter mEmitter;
144 private ParticleSource<SparkleEffectSource> mSource;
145 private ParticleModifier<SparkleEffectModifier> mModifier;
147 public void Activate()
149 mWindow = Window.Instance;
150 mWindow.BackgroundColor = Color.Black;
152 var view = new View();
153 view.BackgroundColor = Color.Wheat;
154 view.Size = new Size(mWindow.Size.Width, mWindow.Size.Height);
155 view.PivotPoint = PivotPoint.Center;
156 view.ParentOrigin = ParentOrigin.TopLeft;
157 view.Position2D = new Position2D(0, 0);
159 // Attach emitter to view
160 mEmitter = new ParticleEmitter(view)
162 ParticleCount = 10000,
164 InitialParticleCount = 0,
165 RendererBlendingMode = ParticleBlendingMode.Screen
168 mSource = new ParticleSource<SparkleEffectSource>(new Vector2(50, 50));
169 mModifier = new ParticleModifier<SparkleEffectModifier>(mSource.Callback);
171 mEmitter.SetSource(mSource);
172 mEmitter.AddModifier(mModifier);
175 mEmitter.TextureResourceUrl = IMAGE_DIR + "/blue-part2.png";
179 protected override void OnCreate()
181 // Up call to the Base class first
187 /// The main entry point for the application.
189 [STAThread] // Forces app to use one thread to access NUI
190 static void Main(string[] args)
192 ParticleSystemSample example = new ParticleSystemSample();