5 using System.Collections.Generic;
8 using SiliconStudio.Core;
9 using SiliconStudio.Paradox.DataModel;
10 using SiliconStudio.Paradox.Engine;
11 using SiliconStudio.Paradox.EntityModel;
12 using SiliconStudio.Paradox.Games;
13 using SiliconStudio.Paradox.Graphics;
15 namespace SiliconStudio.
Paradox.Effects.Modules.Processors
22 #region Private members
24 private readonly Dictionary<ShadowMapTexture, List<ShadowMap>> texturesDefault;
26 private readonly Dictionary<ShadowMapTexture, List<ShadowMap>> texturesVsm;
33 : base(device, manageShadows)
35 texturesDefault =
new Dictionary<ShadowMapTexture, List<ShadowMap>>();
36 texturesVsm =
new Dictionary<ShadowMapTexture, List<ShadowMap>>();
41 #region Public methods
49 InternalActiveShadowMaps.Clear();
50 InternalActiveShadowMapTextures.Clear();
52 foreach (var light
in Lights)
55 if (light.Value.Light.ShadowMap && light.Value.ShadowMap == null)
56 CreateShadowMap(light.Value);
60 if (light.Value.Light.Enabled && light.Value.Light.ShadowMap && light.Value.ShadowMap != null && light.Value.ShadowMap.Update)
62 UpdateEntityLightShadow(light.Value);
63 InternalActiveShadowMaps.Add(light.Value.ShadowMap);
64 InternalActiveShadowMapTextures.Add(light.Value.ShadowMap.Texture);
72 #region Protected methods
77 if (ManageShadows && data.
ShadowMap != null)
79 InternalShadowMaps.Remove(data.ShadowMap);
81 List<ShadowMap> shadowMaps = null;
85 if (shadowMaps == null)
86 throw new Exception(
"Untracked shadow map texture");
88 shadowMaps.Remove(data.ShadowMap);
91 if (shadowMaps.Count == 0)
93 InternalShadowMapTextures.Remove(data.ShadowMap.Texture);
94 Utilities.Dispose(ref data.ShadowMap.Texture.ShadowMapDepthBuffer);
95 Utilities.Dispose(ref data.ShadowMap.Texture.ShadowMapDepthTexture);
96 Utilities.Dispose(ref data.ShadowMap.Texture.ShadowMapRenderTarget);
97 Utilities.Dispose(ref data.ShadowMap.Texture.ShadowMapTargetTexture);
98 Utilities.Dispose(ref data.ShadowMap.Texture.IntermediateBlurRenderTarget);
99 Utilities.Dispose(ref data.ShadowMap.Texture.IntermediateBlurTexture);
105 base.OnEntityRemoved(entity, data);
113 LightDirection = light.Light.LightDirection,
114 LightPosition = light.Entity.Transformation.Translation,
115 ShadowMapSize = light.Light.ShadowMapMaxSize,
116 ShadowNearDistance = light.Light.ShadowNearDistance,
117 ShadowFarDistance = light.Light.ShadowFarDistance,
118 CascadeCount = light.Light.Type == LightType.Directional ? light.Light.ShadowMapCascadeCount : 1,
120 Fov = light.Light.SpotFieldAngle,
122 Layers = light.Light.Layers
129 shadowMap.Texture = chosenTexture;
130 InternalShadowMaps.Add(shadowMap);
131 light.ShadowMap = shadowMap;
136 #region Private methods
143 foreach (var shadowMapTexture
in shadowMapTextures)
145 var shadowTexture = shadowMapTexture.Key;
146 var shadowMaps = shadowMapTexture.Value;
148 shadowTexture.GuillotinePacker.Clear(shadowTexture.ShadowMapDepthTexture.Width, shadowTexture.ShadowMapDepthTexture.Height);
150 var useShadowTexture =
true;
151 for (var i = 0; i < shadowMaps.Count && useShadowTexture; ++i)
152 useShadowTexture = shadowTexture.GuillotinePacker.TryInsert(shadowMaps[i].ShadowMapSize, shadowMaps[i].ShadowMapSize, shadowMaps[i].CascadeCount);
154 useShadowTexture = useShadowTexture && shadowTexture.GuillotinePacker.TryInsert(newShadowMap.ShadowMapSize, newShadowMap.ShadowMapSize, newShadowMap.CascadeCount);
156 shadowTexture.GuillotinePacker.Clear();
158 if (useShadowTexture)
160 chosenTexture = shadowMapTexture.Key;
165 if (chosenTexture == null)
169 chosenTexture.GuillotinePacker.Clear(chosenTexture.ShadowMapDepthTexture.Width, chosenTexture.ShadowMapDepthTexture.Height);
174 var message =
new StringBuilder();
175 message.AppendFormat(
"Unable to allocate shadow map texture. The default size (2048 x 2048) is too small for the shadow map ({0} cascade(s) with size {1}).", newShadowMap.CascadeCount, newShadowMap.ShadowMapSize);
179 chosenTexture.GuillotinePacker.Clear();
181 InternalShadowMapTextures.Add(chosenTexture);
182 var shadowMaps =
new List<ShadowMap> { newShadowMap };
183 shadowMapTextures.Add(chosenTexture, shadowMaps);
187 shadowMapTextures[chosenTexture].Add(newShadowMap);
190 return chosenTexture;
ShadowMapTexture Texture
The shadow map texture.
Game entity. It usually aggregates multiple EntityComponent
Represents a shadow map for the ShadowMapRenderer.
override void Update(GameTime time)
ShadowMapFilterType Filter
The shadow map filter.
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.
Current timing used for variable-step (real time) or fixed-step (game time) games.
int CascadeCount
The number of shadow map cascades.
DynamicLightShadowProcessor(GraphicsDevice device, bool manageShadows)
override void CreateShadowMap(EntityLightShadow light)
ShadowMapFilterType ShadowMapFilterType
Gets or sets the shadow map filtering.
Represents a texture to use with ShadowMapRenderer.
A class allocating as many shadow map as possible without any predefined memory budget.
override void OnEntityRemoved(Entity entity, EntityLightShadow data)