Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ScriptParticleSmoke.cs
Go to the documentation of this file.
1 
2 // Copyright (c) 2011 ReShader - Alexandre Mutel
3 
4 using System;
5 using System.IO;
6 using System.Runtime.InteropServices;
7 using System.Threading.Tasks;
8 
9 using SiliconStudio.Paradox;
10 using SiliconStudio.Paradox.Effects;
11 using SiliconStudio.Paradox.Effects.Modules;
12 using SiliconStudio.Paradox.Engine;
13 using SiliconStudio.Paradox.EntityModel;
14 using SiliconStudio.Core.Extensions;
15 using SiliconStudio.Paradox.Graphics;
16 using SiliconStudio.Paradox.Graphics.Data;
17 using SiliconStudio.Paradox.Games.Mathematics;
18 using System.Linq;
19 
20 using ScriptShader.Effects;
22 
23 //using Vector2 = Paradox.Framework.Mathematics.Vector2;
24 //using R32G32B32_Float = Paradox.Framework.Mathematics.R32G32B32_Float;
25 
26 namespace ScriptTest
27 {
28  public class ScriptParticleSmoke
29  {
30  /// <summary>
31  /// Position for smoke emitters
32  /// </summary>
33  internal static readonly ParameterKey<SmokeEmitterDescription> SmokeEmitterKey = ParameterKeys.Value<SmokeEmitterDescription>();
34 
35  /// <summary>
36  /// Position for bullet emitters
37  /// </summary>
38  internal static readonly ParameterKey<BulletEmitterDescription> BulletEmitterKey = ParameterKeys.Value<BulletEmitterDescription>();
39 
40  /// <summary>
41  /// Position for smoke emitters
42  /// </summary>
43  internal static readonly ParameterKey<Buffer> VerticesEmitterKey = ParticleUpdaterBulletKeys.VerticesEmitter;
44 
45  /// <summary>
46  /// First Texture0.
47  /// </summary>
48  internal static readonly ParameterKey<Texture> SmokeTexture = ParticleRenderSmokeKeys.SmokeTexture;
49 
50  /// <summary>
51  /// First Texture0.
52  /// </summary>
53  internal static readonly ParameterKey<Texture> SmokeColor = ParticleRenderSmokeKeys.SmokeColor;
54 
55  // Generic particle structure
56  [StructLayout(LayoutKind.Explicit, Size = 36, Pack = 4)]
57  public struct ParticleData
58  {
59  // Position of this article in world space
60  [FieldOffset(0)]
61  public Vector3 Position;
62 
63  // Opacity of this particle
64  [FieldOffset(12)]
65  public float Time;
66 
67  // Velocity direction vector
68  [FieldOffset(16)]
69  public Half3 Velocity;
70 
71  // Size of this particle in screen space
72  [FieldOffset(22)]
73  public Half Opacity;
74 
75  // Custom factor (used for example to sample a texture array)
76  [FieldOffset(24)]
77  public Half4 Factors;
78 
79  // Time since the particle's creation
80  [FieldOffset(32)]
81  public Half Size;
82 
83  // Time since the particle's creation
84  [FieldOffset(34)]
85  public Half TimeStep;
86  };
87 
88  public static void Run(EngineContext engineContext)
89  {
90  ParticlePlugin particlePlugin;
91  if (!engineContext.DataContext.RenderPassPlugins.TryGetValueCast("ParticlePlugin", out particlePlugin))
92  return;
93 
94  //engineContext.RenderContext.UIControl.KeyUp += (sender, args) =>
95  // {
96  // if (args.KeyCode >= Keys.F1 && args.KeyCode <= Keys.F12)
97  // {
98 
99  // var stream = new FileStream("picking.txt", FileMode.Append);
100  // var streamWriter = new StreamWriter(stream);
101  // streamWriter.WriteLine("---------------------------------------------");
102  // streamWriter.WriteLine("- {0}", args.KeyCode);
103  // streamWriter.WriteLine("---------------------------------------------");
104  // streamWriter.Flush();
105  // stream.Close();
106  // }
107  // };
108 
109  var particleSystem = engineContext.EntityManager.GetSystem<ParticleProcessor>();
110 
111  var emitterPositions = new[]
112  {
113  new Vector3(-2047.287f, -613.5108f, -400.0f), // 0
114  new Vector3(-1881.002f, -564.9566f, -400.0f), // 1
115  new Vector3(-1627.844f, -449.1949f, -400.0f), // 2
116  new Vector3(-1391.335f, -423.1865f, -400.0f), // 3
117  new Vector3(-1314.865f, -482.0599f, -400.0f), // 4
118  new Vector3(-1019.54f, -932.4803f, -400.0f), // 5
119  new Vector3(-957.3735f, -988.7004f, -400.0f), // 6
120  new Vector3(-759.9126f, -1168.646f, -400.0f), // 7
121  new Vector3(-529.1716f, -1083.003f, -400.0f), // 8
122  new Vector3(-198.7756f, -1029.24f, -400.0f), // 9
123  new Vector3(309.9702f, -832.7861f, -400.0f), // 10
124  new Vector3(876.9819f, -667.9489f, -400.0f), // 11
125  new Vector3(1908.686f, -1085.583f, -400.0f), // 12
126  new Vector3(2308.45f, -995.1572f, -400.0f), // 13
127  new Vector3(2864.581f, -906.4545f, -400.0f), // 14
128  new Vector3(3770.119f, -832.0695f, -400.0f), // 15
129  new Vector3(4561.941f, -728.9376f, -400.0f), // 16
130  new Vector3(5429.49f, -722.3638f, -400.0f), // 17
131  new Vector3(6447.015f, -310.0454f, -400.0f), // 18
132  new Vector3(6420.864f, 532.3475f, -400.0f), // 19
133  new Vector3(6157.83f, 658.0294f, -400.0f), // 20
134  new Vector3(4732.579f, 955.4061f, -400.0f), // 21
135  new Vector3(1630.28f, 1551.338f, -400.0f), // 22
136  new Vector3(931.7393f, 1274.533f, -400.0f), // 23
137  new Vector3(1586.493f, 1505.558f, -400.0f), // 24
138  new Vector3(906.572f, 1268.478f, -400.0f), // 25
139  new Vector3(390.1973f, 1314.976f, -400.0f), // 26
140  new Vector3(-30.39231f, 1553.894f, -400.0f), // 27
141  new Vector3(-356.4023f, 1605.162f, -400.0f), // 28
142  new Vector3(-1055.699f, 971.7286f, -400.0f), // 29
143  new Vector3(-1218.041f, 727.1427f, -400.0f), // 30
144  new Vector3(-1377.148f, 606.9602f, -400.0f), // 31
145  new Vector3(-1676.512f, 640.7913f, -400.0f), // 32
146  new Vector3(-2089.593f, 833.8343f, -400.0f), // 33
147  new Vector3(-2290.1f, 992.6068f, -400.0f), // 34
148  new Vector3(-2196.059f, 764.4152f, -400.0f), // 35
149  new Vector3(-1448.233f, 391.5037f, -400.0f), // 36
150  new Vector3(-1337.535f, 223.827f, -400.0f), // 37
151  new Vector3(-1287.335f, -125.6966f, -400.0f), // 38
152  new Vector3(-4226.484f, -1213.027f, -400.0f), // 39 - Magma Left
153  new Vector3(-4593.09f, -1091.131f, -400.0f), // 40
154  new Vector3(-4803.661f, -958.4816f, -400.0f), // 41
155  new Vector3(-5262.959f, -1025.99f, -400.0f), // 42
156  new Vector3(-5519.119f, -881.3628f, -400.0f), // 43
157  new Vector3(-5543.972f, -547.7667f, -400.0f), // 44
158  new Vector3(-5775.069f, -294.6195f, -400.0f), // 45
159  new Vector3(-6333.859f, -423.4442f, -400.0f), // 46
160  new Vector3(-6977.528f, 840.5598f, -400.0f), // 47
161  new Vector3(-6847.938f, 1640.414f, -400.0f), // 48
162  new Vector3(-7259.18f, 1724.889f, -400.0f), // 49
163  new Vector3(-7693.181f, 1660.773f, -400.0f), // 50
164  new Vector3(-8300.401f, 1609.711f, -400.0f), // 51
165  new Vector3(-8704.221f, 1241.705f, -400.0f), // 52
166  new Vector3(-9049.8f, 905.2922f, -400.0f), // 53
167  new Vector3(-8739.72f, 105.7951f, -400.0f), // 54
168  new Vector3(-8515.267f, -371.7517f, -400.0f), // 55
169  new Vector3(-8110.098f, -316.8557f, -400.0f), // 56
170  new Vector3(-7915.391f, -304.8632f, -400.0f), // 57
171  new Vector3(-7191.82f, -353.2674f, -400.0f), // 58
172  new Vector3(-6270.604f, -2246.958f, -400.0f), // 59 - Magma right
173  new Vector3(-6655.961f, -2615.954f, -400.0f), // 60
174  new Vector3(-7056.6f, -2839.48f, -400.0f), // 61
175  new Vector3(-7632.455f, -3047.234f, -400.0f), // 62
176  new Vector3(-8325.431f, -2937.415f, -400.0f), // 63
177  new Vector3(-8273.172f, -3403.743f, -400.0f), // 64
178  new Vector3(-8179.38f, -3616.764f, -400.0f), // 65
179  new Vector3(-7814.024f, -4484.587f, -400.0f), // 66
180  new Vector3(-6525.229f, -4816.507f, -400.0f), // 67
181  new Vector3(-5648.252f, -4344.051f, -400.0f), // 68
182  new Vector3(-6140.713f, -3957.125f, -400.0f), // 69
183  new Vector3(-7001.114f, -3650.077f, -400.0f), // 70
184  };
185 
186 
187  var random = new Random(1);
188 
189  var emitters = new SmokeParticleEmitterComponent[emitterPositions.Length];
190  for (int i = 0; i < emitters.Length; i++)
191  {
192 
193  var verticalScatter = (float)(2.0 + 3.0 * random.NextDouble());
194  var horizontalScatter = (float)(3.0 + 6.0 * random.NextDouble());
195 
196  var emitter = new SmokeParticleEmitterComponent()
197  {
198  Count = 256,
199  Description = new SmokeEmitterDescription()
200  {
201  Position = emitterPositions[i],
202  Scatter = new Vector3(horizontalScatter, horizontalScatter, verticalScatter),
203  Velocity = new Vector3(0, 0.0f, 0.5f + 4.0f * (float)random.NextDouble()),
204  MaxTime = 1000.0f + 4000.0f * (float)random.NextDouble(),
205  InitialSize = 50.0f + 30.0f * (float)random.NextDouble(),
206  DeltaSize = 30.0f + 20.0f * (float)random.NextDouble(),
207  Opacity = 0.7f,
208  }
209  };
210  emitter.OnUpdateData();
211 
212  emitters[i] = emitter;
213  }
214 
215  var smokeVolTexture = (Texture2D)engineContext.AssetManager.Load<Texture>("/global_data/gdc_demo/fx/smokevol.dds");
216  var smokeGradTexture = (Texture2D)engineContext.AssetManager.Load<Texture>("/global_data/gdc_demo/fx/smokegrad.dds");
217  particlePlugin.Parameters.Set(SmokeTexture, smokeVolTexture);
218  particlePlugin.Parameters.Set(SmokeColor, smokeGradTexture);
219 
220  var particleEmitterRootEntity = new Entity("ParticleEmitters");
221  particleEmitterRootEntity.Set(TransformationComponent.Key, new TransformationComponent());
222  engineContext.EntityManager.AddEntity(particleEmitterRootEntity);
223 
224  for (int index = 0; index < emitters.Length; index++)
225  {
226  var emitter = emitters[index];
227  var entity = new Entity(string.Format("ParticleEmitter-{0}", index));
228  entity.Set(TransformationComponent.Key, new TransformationComponent(new TransformationTRS { Translation = emitter.Description.Position }));
229  entity.Set(ParticleEmitterComponent.Key, emitter);
230 
231  particleEmitterRootEntity.Transformation.Children.Add(entity.Transformation);
232  }
233  }
234  }
235 }
Key of an effect parameter.
Definition: ParameterKey.cs:15
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Defines Position, Rotation and Scale of its Entity.
SiliconStudio.Paradox.Games.Mathematics.Half4 Half4
All-in-One Buffer class linked SharpDX.Direct3D11.Buffer.
static Texture Load(GraphicsDevice device, Stream stream, TextureFlags textureFlags=TextureFlags.ShaderResource, GraphicsResourceUsage usage=GraphicsResourceUsage.Immutable)
Loads a texture from a stream.
Definition: Texture.cs:631
SiliconStudio.Paradox.Games.Mathematics.Half Half
A Texture 2D frontend to SharpDX.Direct3D11.Texture2D.
Definition: Texture2D.cs:37
SiliconStudio.Paradox.Graphics.Buffer Buffer
SiliconStudio.Core.Mathematics.Vector3 Vector3
static void Run(EngineContext engineContext)
Base class for texture resources.
Definition: Texture.cs:38