| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using DCL.ECS7.InternalComponents; |
| | 4 | | using DCL.ECSRuntime; |
| | 5 | | using UnityEngine; |
| | 6 | | using UnityEngine.Rendering; |
| | 7 | |
|
| | 8 | | namespace ECSSystems.MaterialSystem |
| | 9 | | { |
| | 10 | | public static class ECSMaterialSystem |
| | 11 | | { |
| | 12 | | private class State |
| | 13 | | { |
| | 14 | | public IECSReadOnlyComponentsGroup<InternalMaterial, InternalTexturizable> componentsGroup; |
| | 15 | | public IInternalECSComponent<InternalTexturizable> texturizableComponent; |
| | 16 | | public IInternalECSComponent<InternalMaterial> materialComponent; |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public static Action CreateSystem(IECSReadOnlyComponentsGroup<InternalMaterial, InternalTexturizable> components |
| | 20 | | IInternalECSComponent<InternalTexturizable> texturizableComponent, |
| | 21 | | IInternalECSComponent<InternalMaterial> materialComponent) |
| | 22 | | { |
| 3 | 23 | | var state = new State() |
| | 24 | | { |
| | 25 | | componentsGroup = componentsGroup, |
| | 26 | | texturizableComponent = texturizableComponent, |
| | 27 | | materialComponent = materialComponent |
| | 28 | | }; |
| 9 | 29 | | return () => Update(state); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | private static void Update(State state) |
| | 33 | | { |
| 6 | 34 | | var componentGroup = state.componentsGroup.group; |
| | 35 | |
|
| 36 | 36 | | for (int i = 0; i < componentGroup.Count; i++) |
| | 37 | | { |
| 12 | 38 | | var entityData = componentGroup[i]; |
| 12 | 39 | | InternalMaterial materialModel = entityData.componentData1.model; |
| 12 | 40 | | InternalTexturizable texturizableModel = entityData.componentData2.model; |
| | 41 | |
|
| | 42 | | // if neither component has changed then we skip this entity |
| 12 | 43 | | if (!materialModel.dirty && !texturizableModel.dirty) |
| | 44 | | continue; |
| | 45 | |
|
| 7 | 46 | | IList<Renderer> renderers = texturizableModel.renderers; |
| 7 | 47 | | Material material = materialModel.material; |
| | 48 | |
|
| 28 | 49 | | for (int j = 0; j < renderers.Count; j++) |
| | 50 | | { |
| 7 | 51 | | Renderer renderer = renderers[j]; |
| 7 | 52 | | if (renderer.sharedMaterial != material) |
| | 53 | | { |
| 7 | 54 | | renderer.sharedMaterial = material; |
| | 55 | | } |
| 7 | 56 | | renderer.shadowCastingMode = materialModel.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Of |
| | 57 | | } |
| | 58 | | } |
| 6 | 59 | | } |
| | 60 | | } |
| | 61 | | } |