Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
LightShaftsProcessor.cs
Go to the documentation of this file.
1 using System;
2 using SiliconStudio.Paradox.Effects;
3 using SiliconStudio.Paradox.Engine;
4 using SiliconStudio.Paradox.EntityModel;
5 using SiliconStudio.Paradox.Games;
6 using SiliconStudio.Core;
7 
8 namespace ScriptShader.Effects
9 {
10  public class LightShaftsProcessor : EntityProcessor<LightShaftsProcessor.AssociatedData>
11  {
12  public static readonly PropertyKey<LightShaftsPlugin> LightShaftsPluginKey = new PropertyKey<LightShaftsPlugin>("LightShaftsPlugin", typeof(LightShaftsProcessor));
13 
14  private IRenderSystem renderSystem;
15  private MainPlugin mainPlugin;
16  private RenderTargetsPlugin mainTargetPlugin;
17  private RenderPass lightShaftsPass;
18 
19  public LightShaftsProcessor(MainPlugin mainPlugin, RenderTargetsPlugin mainTargetPlugin, RenderPass lightShaftsPass)
20  : base(new PropertyKey[] { LightShaftsComponent.Key, LightComponent.Key })
21  {
22  this.mainPlugin = mainPlugin;
23  this.mainTargetPlugin = mainTargetPlugin;
24  this.lightShaftsPass = lightShaftsPass;
25  }
26 
27  protected override void OnSystemAdd()
28  {
29  renderSystem = Services.GetSafeServiceAs<IRenderSystem>();
30  }
31 
32  protected override AssociatedData GenerateAssociatedData(Entity entity)
33  {
34  return new AssociatedData()
35  {
36  LightShaftsComponent = entity.Get(LightShaftsComponent.Key),
37  LightComponent = entity.Get(LightComponent.Key),
38  };
39  }
40 
41  protected override void OnEntityAdded(Entity entity, AssociatedData data)
42  {
43  var shadowMapPermutation = data.LightComponent.Get(LightProcessor.ShadowMapKey);
44 
45  data.CurrentShadowMapPermutation = shadowMapPermutation;
46  if (shadowMapPermutation != null)
47  {
48  data.LightShaftsPlugin = new LightShaftsPlugin("LightShaftsPlugin")
49  {
50  Debug = false,
51  RenderPass = lightShaftsPass,
52  ShadowMap = entity.Get(LightComponent.Key).Get(LightProcessor.ShadowMapKey).ShadowMap,
53  RenderTarget = mainTargetPlugin.RenderTarget,
54  DepthStencil = mainTargetPlugin.DepthStencil,
55  ViewParameters = mainPlugin.ViewParameters,
56  };
57  data.LightShaftsPlugin.BoundingBoxes.AddRange(data.LightShaftsComponent.LightShaftsBoundingBoxes);
58 
59  renderSystem.RenderPassPlugins.Add(data.LightShaftsPlugin);
60  }
61  }
62 
63  protected override void OnEntityRemoved(Entity entity, AssociatedData data)
64  {
65  if (data.LightShaftsPlugin != null)
66  {
67  renderSystem.RenderPassPlugins.Remove(data.LightShaftsPlugin);
68  data.LightShaftsPlugin = null;
69  }
70  }
71 
72  public override void Update()
73  {
74  foreach (var matchingEntity in enabledEntities)
75  {
76  var lightShaftsComponent = matchingEntity.Value.LightShaftsComponent;
77  var lightShaftsPlugin = matchingEntity.Value.LightShaftsPlugin;
78 
79  // If shadow map changed, readd it
80  if (matchingEntity.Value.CurrentShadowMapPermutation != matchingEntity.Value.LightComponent.Get(LightProcessor.ShadowMapKey))
81  {
82  EntityReadd(matchingEntity.Key);
83  }
84 
85  if (lightShaftsPlugin != null)
86  {
87  lightShaftsPlugin.LightColor = lightShaftsComponent.Color;
88  }
89  }
90  }
91 
92  public class AssociatedData
93  {
97 
99  }
100  }
101 }
override void OnEntityRemoved(Entity entity, AssociatedData data)
Add a light to an Entity, that will be used during rendering.
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Entity processor, triggered on various EntitySystem events such as Entity and Component additions and...
override void OnEntityAdded(Entity entity, AssociatedData data)
Represents a ShadowMap.
Definition: ShadowMap.cs:11
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
override AssociatedData GenerateAssociatedData(Entity entity)
Plugin used for the main rendering view.
Definition: MainPlugin.cs:15
static PropertyKey< LightComponent > Key
Level10 render pass using a depth buffer and a render target.
static readonly PropertyKey< ShadowMapPermutation > ShadowMapKey
RenderPass is a hierarchy that defines how to collect and render meshes.
Definition: RenderPass.cs:19
A class that represents a tag propety.
Definition: PropertyKey.cs:17
LightShaftsProcessor(MainPlugin mainPlugin, RenderTargetsPlugin mainTargetPlugin, RenderPass lightShaftsPass)