Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BulletParticleEmitterComponent.cs
Go to the documentation of this file.
1 using System;
2 using System.Linq;
3 
4 using SiliconStudio.Paradox;
5 using SiliconStudio.Paradox.DataModel;
6 using SiliconStudio.Paradox.Effects;
7 using SiliconStudio.Paradox.Engine;
8 using SiliconStudio.Paradox.EntityModel;
9 using SiliconStudio.Paradox.Extensions;
10 using SiliconStudio.Paradox.Games;
11 using SiliconStudio.Paradox.Games.Collections;
12 using SiliconStudio.Paradox.Games.Serialization.Contents;
13 using Paradox.Framework.Shaders;
19 
20 namespace ScriptTest
21 {
22  [ContentSerializer(typeof(EntityComponentContentSerializer<BulletParticleEmitterComponent>))]
23  [Display]
25  {
26  internal EntitySystem EntitySystem;
27  internal RenderContextBase renderContext;
28 
29  private TimeSpan lastTime;
30 
31  private Entity dragonHead = null;
32 
34  {
35  Type = ParticleEmitterType.GpuStatic;
36  Shader = new ShaderClassSource("ParticleUpdaterBullet");
37  MeshUpdate += OnMeshUpdate;
38  UpdateData += UpdateParticlesData;
42  }
43 
44  public Entity RootAnimation { get; set; }
45 
46  public override void OnAddToSystem(EntitySystem EntitySystem, RenderContextBase renderContext)
47  {
48  base.OnAddToSystem(EntitySystem, renderContext);
49  this.EntitySystem = EntitySystem;
50  this.renderContext = renderContext;
51  }
52 
53  private void OnUpdateSystem(ParticleEmitterComponent particleEmitterComponent)
54  {
55  if (dragonHead == null)
56  dragonHead = EntitySystem.Entities.FirstOrDefault(x => x.Name == "English DragonPelvis");
57 
58  var desc = Description;
59 
60  if (dragonHead != null)
61  {
62  var dragonTransform = dragonHead.Transformation;
63  if (dragonTransform != null)
64  {
65  desc.TargetOld = desc.Target;
66  desc.Target = (Vector3)dragonTransform.WorldMatrix.Row4;
67  }
68  }
69 
70  var animationComponent = RootAnimation.GetOrCreate(AnimationComponent.Key);
71  if (animationComponent != null)
72  {
73  desc.AnimationTime = (float)animationComponent.CurrentTime.TotalSeconds;
74  // If we reset, reupload the particle buffer
75  if ((animationComponent.CurrentTime.Ticks - lastTime.Ticks) < 0)
76  particleEmitterComponent.UpdateNextBuffer = true;
77 
78  lastTime = animationComponent.CurrentTime;
79  }
80 
81  Description = desc;
82  }
83 
84  private void OnMeshUpdate(ParticleEmitterComponent particleEmitterComponent, Entity entity, TrackingCollectionChangedEventArgs arg3)
85  {
86  if (entity.Name.StartsWith("maguma_"))
87  {
88  var meshComponent = entity.Get(ModelComponent.Key);
89  if (meshComponent == null)
90  return;
91 
92  foreach (var effectMesh in meshComponent.SubMeshes)
93  {
94  var subMeshData = effectMesh.MeshData.Value.SubMeshDatas[MeshData.StandardSubMeshData];
95  var buffer = subMeshData.GetVertexBufferData<Vector3>("POSITION");
96  var gpuBuffer = Buffer.Structured.New(renderContext.GraphicsDevice, buffer);
97  Parameters.Set(ScriptParticleSmoke.VerticesEmitterKey, gpuBuffer);
98  break;
99  }
100  }
101  }
102 
103  private void UpdateParticlesData(ParticleEmitterComponent smokeParticleEmitterComponent)
104  {
105  bool isUptoDate = true;
106  if (ParticleData == null || ParticleData.Length != Count)
107  {
108  ParticleData = new ScriptParticleSmoke.ParticleData[Count];
109  isUptoDate = false;
110  }
111 
112  if (isUptoDate)
113  return;
114 
115  var description = Description;
116  var random = new Random(0);
117  var particlesBuffer = (ScriptParticleSmoke.ParticleData[])ParticleData;
118 
119  for (int i = 0; i < particlesBuffer.Length; i++)
120  {
121  var timeProb = (float)random.NextDouble();
122  var timeStepFactor = 50 * Math.Pow(1 - timeProb, 3.0) + 1.0;
123  var time = (((int)(random.NextDouble() * description.MaxTimeTarget / timeStepFactor) + 1) * timeStepFactor) % description.MaxTimeTarget;
124  particlesBuffer[i] = new ScriptParticleSmoke.ParticleData
125  {
126  Time = (float)time,
127  Opacity = (Half)0.0f,
128  Factors = new Half4(Half.Zero, Half.Zero, Half.One, Half.Zero),
129  TimeStep = (Half)timeStepFactor,
130  };
131  }
132  }
133 
134  [DataMemberConvert]
135  [Display]
136  public BulletEmitterDescription Description
137  {
138  get
139  {
140  return Parameters.TryGet(ScriptParticleSmoke.BulletEmitterKey);
141  }
142  set
143  {
144  Parameters.Set(ScriptParticleSmoke.BulletEmitterKey, value);
145  }
146  }
147  }
148 }
SiliconStudio.Paradox.Graphics.Buffer Buffer
bool UpdateNextBuffer
Gets or sets a value indicating whether [update next buffer].
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
SiliconStudio.Paradox.Games.Mathematics.Half4 Half4
SiliconStudio.Paradox.Games.Mathematics.Vector3 Vector3
Action< ParticleEmitterComponent, Entity, TrackingCollectionChangedEventArgs > MeshUpdate
A callback called whenever the component is updated by the ParticleSystem.
SiliconStudio.Paradox.Games.Utilities Utilities
override void OnAddToSystem(EntitySystem EntitySystem, RenderContextBase renderContext)
SiliconStudio.Paradox.Graphics.Buffer Buffer
Definition: BasicEffect.cs:15
int ParticleElementSize
Gets or sets the size of the particle element.
ShaderClassSource Shader
Gets or sets the shader.
SiliconStudio.Core.Utilities Utilities
Definition: Texture.cs:29
Manage a collection of entities.
Definition: EntitySystem.cs:22
SiliconStudio.Paradox.Games.Mathematics.Half Half
string Name
Gets or sets the name of this component.
ParticleEmitterType Type
Gets or sets the type of this emitter..
SiliconStudio.Core.Mathematics.Vector3 Vector3