2 using System.Collections.Generic;
3 using System.Collections.Specialized;
4 using System.ComponentModel;
6 using SiliconStudio.Paradox.DataModel;
7 using SiliconStudio.Paradox.Effects;
8 using SiliconStudio.Paradox.Engine;
9 using SiliconStudio.Paradox.EntityModel;
10 using SiliconStudio.Paradox.Games;
11 using SiliconStudio.Core;
12 using SiliconStudio.Core.Collections;
14 namespace ScriptShader.Effects
18 private HashSet<ShadowMap> activeShadowMaps =
new HashSet<ShadowMap>();
24 this.lightProcessor = lightProcessor;
25 var globalLights =
new TrackingCollection<LightComponent>();
26 globalLights.CollectionChanged += globalLights_CollectionChanged;
27 GlobalLights = globalLights;
32 if (e.
Action == NotifyCollectionChangedAction.Add)
34 else if (e.
Action == NotifyCollectionChangedAction.Remove)
38 public IList<LightComponent> GlobalLights {
get;
private set; }
49 lightProcessor.LightComponentAdded += LightProcessorLightComponentAdded;
50 lightProcessor.LightComponentRemoved += LightProcessorLightComponentRemoved;
55 lightProcessor.LightComponentAdded -= LightProcessorLightComponentAdded;
56 lightProcessor.LightComponentRemoved -= LightProcessorLightComponentRemoved;
59 private void LightProcessorLightComponentAdded(
object sender,
LightComponent e)
61 if (!GlobalLights.Contains(e))
63 bool globalLightUpdated = (GlobalLights.Contains(e));
64 foreach (var matchingEntity
in matchingEntities)
66 if (globalLightUpdated
67 || matchingEntity.Value.LightReceiverComponent.LightComponents.Contains(e))
69 matchingEntity.Value.LightingPermutationUpdated =
true;
74 private void LightProcessorLightComponentRemoved(
object sender,
LightComponent e)
76 GlobalLights.Remove(e);
77 foreach (var matchingEntity
in matchingEntities)
79 matchingEntity.Value.LightingPermutationUpdated =
true;
85 base.OnEntityAdded(entity, data);
87 data.LightComponentsChanged = (sender, e) =>
89 data.LightingPermutationUpdated =
true;
92 data.LightingPermutationUpdated =
true;
98 base.OnEntityRemoved(entity, data);
105 foreach (var matchingEntity
in enabledEntities)
107 if (matchingEntity.Value.LightingPermutationUpdated)
109 matchingEntity.Value.LightingPermutationUpdated =
false;
111 var lightComponents = matchingEntity.Value.LightReceiverComponent.LightComponents.Count > 0
112 ? GlobalLights.Concat(matchingEntity.Value.LightReceiverComponent.LightComponents)
115 int shadowMapCount = 0;
116 int normalLightCount = 0;
119 foreach (var lightComponent
in lightComponents)
123 else if (!lightComponent.Deferred)
131 int lightingPermutationIndex = 0;
132 int shadowMapPermutationIndex = 0;
133 foreach (var lightComponent
in lightComponents)
135 if (lightComponent.ShadowMap)
137 var currentLight = lightComponent.Get(LightProcessor.ShadowMapKey);
138 if (activeShadowMaps.Add(currentLight.ShadowMap))
142 shadowMapPermutationArray.ShadowMaps[shadowMapPermutationIndex++] = currentLight;
144 else if (!lightComponent.Deferred)
150 bool forcePerPixelLighting = matchingEntity.Value.LightReceiverComponent.Get(LightReceiverComponent.ForcePerPixelLighting);
151 if (forcePerPixelLighting)
153 for (
int index = 0; index < lightBindings.Length; index++)
155 lightBindings[index].LightShaderType = LightShaderType.DiffuseSpecularPixel;
162 matchingEntity.Value.MeshComponent.Model.Permutations.Set(LightingPermutation.Key, lightingPermutation);
163 matchingEntity.Value.MeshComponent.Model.Permutations.Set(ShadowMapPermutationArray.Key, shadowMapPermutationArray);
LightReceiverProcessor(LightProcessor lightProcessor)
Add a light to an Entity, that will be used during rendering.
Game entity. It usually aggregates multiple EntityComponent
IList< LightComponent > LightComponents
Additional lights applied on this object.
MeshComponent MeshComponent
override void OnSystemAdd()
override void OnEntityAdded(Entity entity, AssociatedData data)
Entity processor, triggered on various EntitySystem events such as Entity and Component additions and...
static readonly PropertyKey< Light > LightKey
LightReceiverComponent LightReceiverComponent
override AssociatedData GenerateAssociatedData(Entity entity)
EventHandler< TrackingCollectionChangedEventArgs > LightComponentsChanged
bool LightingPermutationUpdated
object Item
Gets the added or removed item (if dictionary, value only).
NotifyCollectionChangedAction Action
Gets the type of action performed. Allowed values are NotifyCollectionChangedAction.Add and NotifyCollectionChangedAction.Remove.
override void OnSystemRemove()
static readonly PropertyKey< ShadowMapPermutation > ShadowMapKey
override void OnEntityRemoved(Entity entity, AssociatedData data)
A class that represents a tag propety.