PageRenderTime 162ms CodeModel.GetById 51ms app.highlight 17ms RepoModel.GetById 75ms app.codeStats 0ms

/Rendering/EffectTests/Tutorials.cs

#
C# | 393 lines | 294 code | 22 blank | 77 comment | 5 complexity | 390d9adb9d97347ecde29360b2629280 MD5 | raw file
  1using Delta.ContentSystem.Rendering;
  2using Delta.ContentSystem.Rendering.Helpers;
  3using Delta.Engine;
  4using Delta.Engine.Game;
  5using Delta.InputSystem;
  6using Delta.Rendering.Basics.Drawing;
  7using Delta.Rendering.Basics.Fonts;
  8using Delta.Rendering.Basics.Materials;
  9using Delta.Rendering.Cameras;
 10using Delta.Rendering.Effects;
 11using Delta.Rendering.Effects.Modifiers;
 12using Delta.Rendering.Enums;
 13using Delta.Utilities.Datatypes;
 14using Delta.Utilities.Helpers;
 15using Delta.Utilities.Math;
 16using NUnit.Framework;
 17
 18namespace Delta.Rendering.EffectTests
 19{
 20	/// <summary>
 21	/// Effect tutorials.
 22	/// </summary> 
 23	public class Tutorials
 24	{
 25		#region FireEffect (Static)
 26		/// <summary>
 27		/// Tutorial: Particle Effect Tutorial 1: Fire Effect
 28		/// Difficulty: Medium
 29		/// Description: Draw a simple fire effect.
 30		/// Image: http://DeltaEngine.net/Icons/EffectTutorial1.png
 31		/// </summary>
 32		[Test, Category("Visual")]
 33		public static void FireEffect()
 34		{
 35			// Create a new template.
 36			EffectData data = EffectData.Get("<FireEffect>");
 37
 38			// Add emitters and modifiers.
 39			EmitterData emitter = new EmitterData();
 40			emitter.Modifiers.Add(new SpawnIntervalModifier
 41			{
 42				SpawnInterval = 0.05f,
 43			});
 44			emitter.Modifiers.Add(new ParticleLifetimeModifier
 45			{
 46				ParticleLifetime = 3f
 47			});
 48			emitter.Modifiers.Add(new TimeRangeModifier
 49			{
 50				TimeRange = new Range(0f, float.PositiveInfinity),
 51			});
 52			Graph alphaFade = new Graph();
 53			alphaFade.Add(0f, 0f);
 54			alphaFade.Add(0.1f, 1f);
 55			alphaFade.Add(1f, 0f);
 56			emitter.Modifiers.Add(new FadeModifier
 57			{
 58				AdvancedAlphaFade = alphaFade,
 59			});
 60			emitter.Modifiers.Add(new SizeModifier
 61			{
 62				WidthRange = new Range(1f, 0.05f),
 63				HeightRange = new Range(1f, 0.05f),
 64			});
 65			emitter.Modifiers.Add(new MaterialModifier
 66			{
 67				Material = new Material2DColored(
 68					"FlamesAdditive"),
 69					//"SmokeAdditive")
 70					//{
 71					//BlendColor = new Color(1f, 0.5f, 0f),
 72					//},
 73			});
 74			emitter.Modifiers.Add(new RandomSpawnOffsetModifier
 75			{
 76				MinOffset = new Vector(-0.02f, 0f, 0f),
 77				MaxOffset = new Vector(0.02f, 0f, 0f),
 78			});
 79			emitter.Modifiers.Add(new RandomVelocityModifier
 80			{
 81				MinVelocity = new Vector(-0.01f, -0.08f, 0f),
 82				MaxVelocity = new Vector(0.01f, -0.1f, 0f),
 83			});
 84			data.Emitters.Add(emitter);
 85
 86			//Effect.Spawn(data, new Vector(0.5f, 0.5f, 0f));
 87			// Always follow the mouse :)
 88			Effect.Spawn(data, new MouseTracker());
 89
 90			Application.Start();
 91		}
 92		#endregion
 93
 94		#region SmokeEffect (Static)
 95		/// <summary>
 96		/// Tutorial: Particle Effect Tutorial 2: Smoke Effect
 97		/// Difficulty: Medium
 98		/// Description: Draw a simple smoke effect.
 99		/// Image: http://DeltaEngine.net/Icons/EffectTutorial2.png
100		/// </summary>
101		[Test, Category("Visual")]
102		public static void SmokeEffect()
103		{
104			// Create a new template.
105			EffectData data = EffectData.Get("<SmokeEffect>");
106			// Add an emitter and modifiers.
107			EmitterData emitter = new EmitterData();
108			emitter.Modifiers.Add(new SpawnIntervalModifier
109			{
110				SpawnInterval = 0.05f,
111			});
112			emitter.Modifiers.Add(new ParticleLifetimeModifier
113			{
114				ParticleLifetime = 2f
115			});
116			emitter.Modifiers.Add(new TimeRangeModifier
117			{
118				TimeRange = new Range(0f, float.PositiveInfinity),
119			});
120			emitter.Modifiers.Add(new FadeModifier
121			{
122				// Create simple graph for fading in and out!
123				AdvancedAlphaFade =
124				new Graph().Add(0, 0).Add(0.25f, 1.0f).Add(1.0f, 0.0f),
125			});
126			emitter.Modifiers.Add(new MaterialModifier
127			{
128				Material = new Material2DColored(
129					//"SmokeBrightTransparency"),
130					"Smoke"),
131					//"SmokeAdditive"),
132					//"SmokeSmallAdditive"),
133			});
134			emitter.Modifiers.Add(new RandomVelocityModifier
135			{
136				MinVelocity = new Vector(-0.05f, -0.1f, 0f),
137				MaxVelocity = new Vector(0.05f, -0.1f, 0f),
138			});
139			emitter.Modifiers.Add(new RandomRotationModifier
140			{
141				RotationRange = new Range(0, 360),
142			});
143			data.Emitters.Add(emitter);
144			//Effect.Spawn(data, new Vector(0.5f, 0.5f, 0f));
145			// Always follow the mouse :)
146			Effect.Spawn(data, new MouseTracker());
147
148			Application.Start();
149		}
150		#endregion
151
152		#region RocketEffect (Static)
153		/// <summary>
154		/// Tutorial: Particle Effect Tutorial 3: Rocket Effect
155		/// Difficulty: Medium
156		/// Description: Draw a simple rocket effect.
157		/// Image: http://DeltaEngine.net/Icons/EffectTutorial3.png
158		/// </summary>
159		[Test, Category("Visual")]
160		public static void RocketEffect()
161		{
162			// Create a new template.
163			EffectData data = EffectData.Get("<RocketEffect>");
164			// Add an emitter and modifiers.
165			EmitterData emitter = new EmitterData();
166			emitter.Modifiers.Add(new SpawnIntervalModifier
167			{
168				SpawnInterval = 0.05f,
169			});
170			emitter.Modifiers.Add(new ParticleLifetimeModifier
171			{
172				ParticleLifetime = 4f
173			});
174			emitter.Modifiers.Add(new TimeRangeModifier
175			{
176				TimeRange = new Range(0f, float.PositiveInfinity),
177			});
178			Graph alphaFade = new Graph();
179			alphaFade.Add(0f, 0f);
180			alphaFade.Add(0.1f, 1f);
181			alphaFade.Add(1f, 0f);
182			emitter.Modifiers.Add(new FadeModifier
183			{
184				AdvancedAlphaFade = alphaFade,
185			});
186			emitter.Modifiers.Add(new SizeModifier
187			{
188				HeightRange = new Range(1f, 0f),
189				WidthRange = new Range(1f, 0f),
190			});
191			emitter.Modifiers.Add(new MaterialModifier
192			{
193				Material = new Material2DColored("GlowLightEffect"),
194			});
195			emitter.Modifiers.Add(new RandomSpawnOffsetModifier
196			{
197				MinOffset = new Vector(0f, -0.01f, 0f),
198				MaxOffset = new Vector(0f, 0.01f, 0f),
199			});
200			emitter.Modifiers.Add(new RandomVelocityModifier
201			{
202				MinVelocity = new Vector(0.05f, 0f, 0f),
203				MaxVelocity = new Vector(0.05f, 0f, 0f),
204			});
205			data.Emitters.Add(emitter);
206			Effect.Spawn(data, new Vector(0.5f, 0.5f, 0f));
207
208			Application.Start();
209		}
210		#endregion
211
212		#region SparksEffect (Static)
213		/// <summary>
214		/// Tutorial: Particle Effect Tutorial 4: Sparks Effect
215		/// Difficulty: Medium
216		/// Description: Draw a simple sparks effect.
217		/// Image: http://DeltaEngine.net/Icons/EffectTutorial4.png
218		/// </summary>
219		[Test, Category("Visual")]
220		public static void SparksEffect()
221		{
222			// Create a new template.
223			EffectData data = EffectData.Get("<SparksEffect>");
224			// Add an emitter and modifiers.
225			EmitterData emitter = new EmitterData();
226			emitter.Modifiers.Add(new SpawnIntervalModifier
227			{
228				SpawnInterval = 0.1f, //1.5f,
229			});
230			emitter.Modifiers.Add(new ParticleLifetimeModifier
231			{
232				ParticleLifetime = 1.0f, //0.4f
233			});
234			emitter.Modifiers.Add(new TimeRangeModifier
235			{
236				TimeRange = new Range(0f, float.PositiveInfinity),
237			});
238			emitter.Modifiers.Add(new RotationModifier
239			{
240				Rotation = new Range(-90f, 90f),
241			});
242			emitter.Modifiers.Add(new SizeModifier
243			{
244				HeightRange = new Range(0.25f, 2.0f),
245				WidthRange = new Range(0.25f, 2.0f),
246			});
247			emitter.Modifiers.Add(new FadeModifier
248			{
249				AlphaFade = new Range(1.0f, 0.0f),
250			});
251			emitter.Modifiers.Add(new MaterialModifier
252			{
253				Material = new Material2DColored("BreakoutEffect"),
254			});
255			emitter.Modifiers.Add(new RandomSpawnOffsetModifier
256			{
257				MinOffset = new Vector(-0.025f, -0.025f, 0f),
258				MaxOffset = new Vector(0.025f, 0.025f, 0f),
259			});
260			emitter.Modifiers.Add(new RandomVelocityModifier
261			{
262				MinVelocity = new Vector(-0.15f, -0.15f, 0f),
263				MaxVelocity = new Vector(0.15f, 0.15f, 0f),
264			});
265			data.Emitters.Add(emitter);
266			// Always follow the mouse :)
267			Effect.Spawn(data, new MouseTracker());
268
269			Application.Start();
270		}
271		#endregion
272
273		#region LensFlareEffect (Static)
274		/// <summary>
275		/// Tutorial: Particle Effect Tutorial 5: Lens-Flare Effect
276		/// Difficulty: Medium
277		/// Description: Draw a simple lens flare effect.
278		/// Image: http://DeltaEngine.net/Icons/EffectTutorial5.png
279		/// </summary>
280		[Test, Category("Visual")]
281		public static void LensFlareEffect()
282		{
283			// Create a new template.
284			EffectData data = EffectData.Get("<LensFlareEffect>");
285			// Add an emitter and modifiers.
286			EmitterData emitter = new EmitterData();
287			emitter.Modifiers.Add(new SpawnOnceModifier());
288			emitter.Modifiers.Add(new ParticleLifetimeModifier
289			{
290				// only show the particle the first tick. The next tick a new one
291				// will be spawned.
292				ParticleLifetime = 0f,
293			});
294			emitter.Modifiers.Add(new MaterialModifier
295			{
296				Material = new Material2DColored("LensflareDTransparency"),
297			});
298			data.Emitters.Add(emitter);
299
300			Application.Start(delegate
301			{
302				Circle.DrawFilled(Input.Mouse.Position, 0.05f, Color.Yellow);
303
304				Vector mousePos = new Vector(Input.Mouse.Position, 0f);
305				Vector center = new Vector(0.5f, 0.5f, 0f);
306				Vector dir = mousePos - center;
307				float distanceHalf = dir.Length * 0.5f;
308				dir.Normalize();
309
310				Effect.Spawn(data, center + (dir * distanceHalf));
311			});
312		}
313		#endregion
314
315		#region BillboardTest (Static)
316		/// <summary>
317		/// Tutorial: Particle Effect Tutorial 6: Billboard Test
318		/// Difficulty: Medium
319		/// Description: Test all the billboard modes.
320		/// Image: http://DeltaEngine.net/Icons/EffectTutorial6.png
321		/// </summary>
322		[Test, Category("Visual")]
323		public static void BillboardTest()
324		{
325			// Create a default camera.
326			LookAtCamera cam = new LookAtCamera(new Vector(1, -5, 3));
327
328			BillboardMode currentMode = BillboardMode.CameraFacing;
329
330			// Create the new effect template with the first billboard mode.
331			EffectData data = EffectData.Get("<BillboardModeTest>");
332			// Add an emitter and modifiers.
333			EmitterData emitter = new EmitterData();
334			emitter.Modifiers.Add(new AlwaysSpawnModifier());
335			emitter.Modifiers.Add(new SizeModifier
336			{
337				HeightRange = new Range(10),
338				WidthRange = new Range(15),
339			});
340			MaterialModifier materialModifier = new MaterialModifier
341			{
342				Material = new MaterialColored("DeltaEngineLogo")
343				{
344					DrawLayer = RenderLayer.Normal,
345					BlendColor = Color.White,
346				},
347				BillboardMode = BillboardMode.CameraFacingPrecise,
348			};
349			materialModifier.BillboardMode = currentMode;
350			emitter.Modifiers.Add(materialModifier);
351			data.Emitters.Add(emitter);
352
353			int effectId = Effect.Spawn(data, Vector.Zero);
354
355			// Start the application.
356			Application.Start(delegate
357			{
358				// Draw a grid for orientation.
359				Grid.Draw();
360				// Show which mode we're currently using.
361				Font.Default.Draw("currentMode=" + currentMode,
362					new Rectangle(0.0f, ScreenSpace.DrawArea.Top, 1.0f,
363						Font.Default.LineHeight));
364
365				// Every time we hit enter or release a touch we want to go to the
366				// next billboard mode for easy debugging of all modes.
367				if (Input.Keyboard.EnterReleased ||
368				    Input.Touch.TouchReleased)
369				{
370					// Go to the next mode
371					currentMode = (BillboardMode)currentMode.Next();
372					// Skip the only2D mode cause this is working in all the 2d tests
373					// anyways.
374					if (currentMode == BillboardMode.Only2D)
375					{
376						currentMode = (BillboardMode)currentMode.Next();
377					}
378
379					// Kill the previous effect if it exists.
380					if (effectId != MathHelper.InvalidIndex)
381					{
382						Effect.Kill(effectId);
383					}
384
385					// Set the current mode and then spawn the new effect.
386					materialModifier.BillboardMode = currentMode;
387					effectId = Effect.Spawn(data, Vector.Zero);
388				}
389			});
390		}
391		#endregion
392	}
393}