| | 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 | | { |
| 2 | 23 | | var state = new State() |
| | 24 | | { |
| | 25 | | componentsGroup = componentsGroup, |
| | 26 | | texturizableComponent = texturizableComponent, |
| | 27 | | materialComponent = materialComponent |
| | 28 | | }; |
| 8 | 29 | | return () => Update(state); |
| | 30 | | } |
| | 31 | |
|
| | 32 | | private static void Update(State state) |
| | 33 | | { |
| 6 | 34 | | var componentGroup = state.componentsGroup.group; |
| | 35 | |
|
| 34 | 36 | | for (int i = 0; i < componentGroup.Count; i++) |
| | 37 | | { |
| 11 | 38 | | var entityData = componentGroup[i]; |
| 11 | 39 | | InternalMaterial materialModel = entityData.componentData1.model; |
| 11 | 40 | | InternalTexturizable texturizableModel = entityData.componentData2.model; |
| | 41 | |
|
| | 42 | | // if neither component has changed then we skip this entity |
| 11 | 43 | | if (!materialModel.dirty && !texturizableModel.dirty) |
| | 44 | | continue; |
| | 45 | |
|
| 6 | 46 | | IList<Renderer> renderers = texturizableModel.renderers; |
| 6 | 47 | | Material material = materialModel.material; |
| 6 | 48 | | materialModel.renderers = renderers; |
| | 49 | |
|
| 24 | 50 | | for (int j = 0; j < renderers.Count; j++) |
| | 51 | | { |
| 6 | 52 | | Renderer renderer = renderers[j]; |
| 6 | 53 | | if (renderer.sharedMaterial != material) |
| | 54 | | { |
| 6 | 55 | | renderer.sharedMaterial = material; |
| | 56 | | } |
| 6 | 57 | | renderer.shadowCastingMode = materialModel.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Of |
| | 58 | | } |
| | 59 | |
|
| 6 | 60 | | materialModel.dirty = false; |
| 6 | 61 | | texturizableModel.dirty = false; |
| | 62 | |
|
| 6 | 63 | | state.materialComponent.PutFor(entityData.scene, entityData.entity, materialModel); |
| 6 | 64 | | state.texturizableComponent.PutFor(entityData.scene, entityData.entity, texturizableModel); |
| | 65 | | } |
| 6 | 66 | | } |
| | 67 | | } |
| | 68 | | } |