Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
LightShadowProcessor.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 using System.Collections.Generic;
5 
6 using SiliconStudio.Core;
7 using SiliconStudio.Core.Diagnostics;
8 using SiliconStudio.Core.Mathematics;
9 using SiliconStudio.Paradox.DataModel;
10 using SiliconStudio.Paradox.Engine;
11 using SiliconStudio.Paradox.EntityModel;
12 using SiliconStudio.Paradox.Graphics;
13 
14 namespace SiliconStudio.Paradox.Effects.Modules.Processors
15 {
16  public abstract class LightShadowProcessor : EntityProcessor<EntityLightShadow>
17  {
18  public static readonly Logger Logger = GlobalLogger.GetLogger("LightShadowProcessor");
19 
20  #region Protected members
21 
22  protected readonly GraphicsDevice GraphicsDevice;
23 
24  protected readonly bool ManageShadows;
25 
26  protected readonly HashSet<ShadowMapTexture> InternalShadowMapTextures;
27 
28  protected readonly HashSet<ShadowMapTexture> InternalActiveShadowMapTextures;
29 
30  protected readonly HashSet<ShadowMap> InternalShadowMaps;
31 
32  protected readonly HashSet<ShadowMap> InternalActiveShadowMaps;
33 
34  #endregion
35 
36  #region Public properties
37 
38  /// <summary>
39  /// The shadow map information per entity.
40  /// </summary>
41  public Dictionary<Entity, EntityLightShadow> Lights
42  {
43  get { return enabledEntities; }
44  }
45 
46  /// <summary>
47  /// The shadow maps.
48  /// </summary>
49  public virtual HashSet<ShadowMapTexture> ShadowMapTextures
50  {
51  get { return InternalShadowMapTextures; }
52  }
53 
54  /// <summary>
55  /// The active shadow map textures.
56  /// </summary>
57  public virtual HashSet<ShadowMapTexture> ActiveShadowMapTextures
58  {
59  get { return InternalActiveShadowMapTextures; }
60  }
61 
62  /// <summary>
63  /// The active shadow maps.
64  /// </summary>
65  public virtual HashSet<ShadowMap> ActiveShadowMaps
66  {
67  get { return InternalActiveShadowMaps; }
68  }
69 
70  #endregion
71 
72  #region Protected methods
73 
74  protected override void OnSystemRemove()
75  {
76  foreach (var shadowMap in InternalShadowMaps)
77  shadowMap.Texture = null;
78  InternalShadowMaps.Clear();
79 
80  foreach (var texture in InternalShadowMapTextures)
81  {
82  InternalShadowMapTextures.Remove(texture);
83  Utilities.Dispose(ref texture.ShadowMapDepthBuffer);
84  Utilities.Dispose(ref texture.ShadowMapDepthTexture);
85  Utilities.Dispose(ref texture.ShadowMapRenderTarget);
86  Utilities.Dispose(ref texture.ShadowMapTargetTexture);
87  Utilities.Dispose(ref texture.IntermediateBlurRenderTarget);
88  Utilities.Dispose(ref texture.IntermediateBlurTexture);
89  }
90  InternalShadowMapTextures.Clear();
91 
92  base.OnSystemRemove();
93  }
94 
95  protected LightShadowProcessor(GraphicsDevice device, bool manageShadows)
96  : base(new PropertyKey[] { LightComponent.Key })
97  {
98  GraphicsDevice = device;
99  ManageShadows = manageShadows;
100  InternalShadowMapTextures = new HashSet<ShadowMapTexture>();
101  InternalActiveShadowMapTextures = new HashSet<ShadowMapTexture>();
102  InternalShadowMaps = new HashSet<ShadowMap>();
103  InternalActiveShadowMaps = new HashSet<ShadowMap>();
104  }
105 
107  {
108  return new EntityLightShadow { Entity = entity, Light = entity.Get(LightComponent.Key), ShadowMap = null };
109  }
110 
111  /// <inheritdoc/>
112  protected override void OnEntityAdding(Entity entity, EntityLightShadow data)
113  {
114  base.OnEntityAdding(entity, data);
115  if (ManageShadows && (data.Light.Type == LightType.Directional || data.Light.Type == LightType.Spot) && data.Light.ShadowMap)
116  CreateShadowMap(data);
117  }
118 
119  protected abstract void CreateShadowMap(EntityLightShadow light);
120 
121  #endregion
122 
123  #region Protected static methods
124 
125  protected static void UpdateEntityLightShadow(EntityLightShadow light)
126  {
127  var worldDir = Vector4.Transform(new Vector4(light.Light.LightDirection, 0), light.Entity.Transformation.WorldMatrix);
128  light.ShadowMap.LightDirection = new Vector3(worldDir.X, worldDir.Y, worldDir.Z);
129  light.ShadowMap.LightPosition = light.Entity.Transformation.Translation;
130  light.ShadowMap.Fov = light.Light.SpotFieldAngle;
131  light.ShadowMap.ReceiverInfo.ShadowLightDirection = light.ShadowMap.LightDirectionNormalized;
132  light.ShadowMap.ShadowFarDistance = light.Light.ShadowFarDistance;
133  light.ShadowMap.ReceiverInfo.ShadowMapDistance = light.Light.Type == LightType.Directional ? light.ShadowMap.ShadowFarDistance : light.ShadowMap.ShadowFarDistance - light.ShadowMap.ShadowNearDistance;
134  light.ShadowMap.ReceiverVsmInfo.BleedingFactor = light.Light.BleedingFactor;
135  light.ShadowMap.ReceiverVsmInfo.MinVariance = light.Light.MinVariance;
136  }
137 
138  #endregion
139  }
140 }
Game entity. It usually aggregates multiple EntityComponent
Definition: Entity.cs:28
Represents a shadow map for the ShadowMapRenderer.
Definition: ShadowMap.cs:15
Entity processor, triggered on various EntitySystem events such as Entity and Component additions and...
Performs primitive-based rendering, creates resources, handles system-level variables, adjusts gamma ramp levels, and creates shaders. See The+GraphicsDevice+class to learn more about the class.
Base implementation for ILogger.
Definition: Logger.cs:10
override void OnEntityAdding(Entity entity, EntityLightShadow data)
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
LightType Type
Gets or sets the light type.
SiliconStudio.Core.Mathematics.Vector3 Vector3
A class that represents a tag propety.
Definition: PropertyKey.cs:17
Vector3 LightDirection
Gets or sets the light direction.