Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ScriptParticles.cs
Go to the documentation of this file.
1 
2 // Copyright (c) 2011 ReShader - Alexandre Mutel
3 
4 using System;
5 using System.Runtime.InteropServices;
6 using System.Threading.Tasks;
7 
8 using SiliconStudio.Paradox;
9 using SiliconStudio.Paradox.Effects;
10 using SiliconStudio.Paradox.Engine;
11 using SiliconStudio.Paradox.Games;
12 using SiliconStudio.Core.Extensions;
13 using SiliconStudio.Paradox.Graphics;
14 using SiliconStudio.Paradox.Graphics.Data;
15 using SiliconStudio.Paradox.Games.Mathematics;
16 using Paradox.Framework.Shaders;
19 
20 namespace ScriptTest
21 {
22  public class ScriptParticles
23  {
24  // Generic particle structure 3 * float4 = 48 bytes
25  [StructLayout(LayoutKind.Explicit, Size = 48)]
26  struct ParticleData
27  {
28  // Position of this article in world space
29  [FieldOffset(0)]
30  public Vector3 Position;
31 
32  // Opacity of this particle
33  [FieldOffset(12)]
34  public float Opacity;
35 
36  // Velocity direction vector
37  [FieldOffset(16)]
38  public Vector3 Velocity;
39 
40  // Size of this particle in screen space
41  [FieldOffset(28)]
42  public float Time;
43 
44  // Custom factor (used for example to sample a texture array)
45  [FieldOffset(32)]
46  public Vector3 Factors;
47 
48  // Time since the particle's creation
49  [FieldOffset(44)]
50  public float Size;
51  };
52 
53  public static async Task Run(EngineContext engineContext)
54  {
55  ParticlePlugin particlePlugin;
56  if (!engineContext.DataContext.RenderPassPlugins.TryGetValueCast("ParticlePlugin", out particlePlugin))
57  return;
58 
59  var count = particlePlugin.CapacityCount;
60 
61  var particlesBuffer = new ParticleData[count];
62  var random = new Random();
63  for (int i = 0; i < particlesBuffer.Length; i++)
64  {
65  particlesBuffer[i] = new ParticleData
66  {
67  Position = new Vector3(1000.0f - (float)random.NextDouble() * 6000, 1500.0f - (float)random.NextDouble() * 3000.0f, 0),
68  Velocity = new Vector3(0, 0, 2.0f + 10.0f * (float)random.NextDouble()),
69  Time = 5000.0f * (float)random.NextDouble(),
70  Size = 1.0f + (float)random.NextDouble() * 10.0f,
71  Factors = new Vector3(1.0f + ((i & 255) == 0 ? (25.0f + 50.0f * (float)random.NextDouble()) : -(float)random.NextDouble() * ((i & 3) == 0 ? 2.0f : 1.0f)), 0, 0),
72  Opacity = 1.0f,
73  };
74  particlesBuffer[i].Position.Z = particlesBuffer[i].Velocity.Z * particlesBuffer[i].Time / 100.0f;
75  }
76 
77  var particleUpdater = new ParticleEmitterComponent()
78  {
79  Type = ParticleEmitterType.GpuStatic,
80  Count = count,
81  Shader = new ShaderClassSource("ParticleUpdaterTest1"),
82  };
83  particleUpdater.ParticleData = particlesBuffer;
84  particleUpdater.ParticleElementSize = Utilities.SizeOf<ParticleData>();
85 
86  // Add this particle updater to the particle engine
87  particlePlugin.Updaters.Add(particleUpdater);
88  }
89  }
90 }
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
_In_ size_t count
Definition: DirectXTexP.h:174
SiliconStudio.Paradox.Games.Mathematics.Vector3 Vector3
SiliconStudio.Core.Mathematics.Vector3 Vector3
static async Task Run(EngineContext engineContext)