< Summary

Class:DCL.ECSComponents.MaterialHandler
Assembly:DCL.ECSComponents.Material
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Material/MaterialHandler.cs
Covered lines:26
Uncovered lines:13
Coverable lines:39
Total lines:111
Line coverage:66.6% (26 of 39)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MaterialHandler(...)0%2100%
OnComponentCreated(...)0%2100%
OnComponentRemoved(...)0%110100%
OnComponentModelUpdated(...)0%6.566075%
CreatePBRMaterialPromiseModel(...)0%2100%
CreateBasicMaterialPromiseModel(...)0%110100%
CreateMaterialPromiseTextureModel(...)0%5.264057.14%
IsPbrMaterial(...)0%13130100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/DCLPlugins/ECS7/ECSComponents/Material/MaterialHandler.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.ECS7.InternalComponents;
 3using DCL.ECSRuntime;
 4using DCL.Models;
 5
 6namespace DCL.ECSComponents
 7{
 8    public class MaterialHandler : IECSComponentHandler<PBMaterial>
 9    {
 10        private PBMaterial lastModel = null;
 11        internal AssetPromise_Material promiseMaterial;
 12
 13        private readonly IInternalECSComponent<InternalMaterial> materialInternalComponent;
 14
 015        public MaterialHandler(IInternalECSComponent<InternalMaterial> materialInternalComponent)
 16        {
 017            this.materialInternalComponent = materialInternalComponent;
 018        }
 19
 020        public void OnComponentCreated(IParcelScene scene, IDCLEntity entity) { }
 21
 22        public void OnComponentRemoved(IParcelScene scene, IDCLEntity entity)
 23        {
 224            materialInternalComponent.RemoveFor(scene, entity, new InternalMaterial() { material = null });
 225            AssetPromiseKeeper_Material.i.Forget(promiseMaterial);
 226        }
 27
 28        public void OnComponentModelUpdated(IParcelScene scene, IDCLEntity entity, PBMaterial model)
 29        {
 1030            if (lastModel != null && lastModel.Equals(model))
 231                return;
 32
 833            lastModel = model;
 34
 835            AssetPromise_Material_Model? promiseModel = null;
 836            AssetPromise_Material_Model.Texture? albedoTexture = CreateMaterialPromiseTextureModel(model.Texture, scene)
 37
 838            if (IsPbrMaterial(model))
 39            {
 040                AssetPromise_Material_Model.Texture? alphaTexture = CreateMaterialPromiseTextureModel(model.AlphaTexture
 041                AssetPromise_Material_Model.Texture? emissiveTexture = CreateMaterialPromiseTextureModel(model.EmissiveT
 042                AssetPromise_Material_Model.Texture? bumpTexture = CreateMaterialPromiseTextureModel(model.BumpTexture, 
 043                promiseModel = CreatePBRMaterialPromiseModel(model, albedoTexture, alphaTexture, emissiveTexture, bumpTe
 044            }
 845            else if (albedoTexture.HasValue)
 46            {
 847                promiseModel = CreateBasicMaterialPromiseModel(model, albedoTexture.Value);
 48            }
 49
 850            AssetPromise_Material prevPromise = promiseMaterial;
 51
 852            if (promiseModel.HasValue)
 53            {
 854                promiseMaterial = new AssetPromise_Material(promiseModel.Value);
 855                promiseMaterial.OnSuccessEvent += materialAsset =>
 56                {
 857                    materialInternalComponent.PutFor(scene, entity, new InternalMaterial()
 58                    {
 59                        material = materialAsset.material
 60                    });
 861                };
 862                AssetPromiseKeeper_Material.i.Keep(promiseMaterial);
 63            }
 64
 865            AssetPromiseKeeper_Material.i.Forget(prevPromise);
 866        }
 67
 68        private static AssetPromise_Material_Model CreatePBRMaterialPromiseModel(PBMaterial model,
 69            AssetPromise_Material_Model.Texture? albedoTexture, AssetPromise_Material_Model.Texture? alphaTexture,
 70            AssetPromise_Material_Model.Texture? emissiveTexture, AssetPromise_Material_Model.Texture? bumpTexture)
 71        {
 072            return AssetPromise_Material_Model.CreatePBRMaterial(albedoTexture, alphaTexture, emissiveTexture, bumpTextu
 73                model.GetAlphaTest(), model.GetAlbedoColor().ToUnityColor(), model.GetEmissiveColor().ToUnityColor(),
 74                model.GetReflectiveColor().ToUnityColor(), (AssetPromise_Material_Model.TransparencyMode)model.GetTransp
 75                model.GetRoughness(), model.GetGlossiness(), model.GetSpecularIntensity(),
 76                model.GetEmissiveIntensity(), model.GetDirectIntensity());
 77        }
 78
 79        private static AssetPromise_Material_Model CreateBasicMaterialPromiseModel(PBMaterial model, AssetPromise_Materi
 80        {
 881            return AssetPromise_Material_Model.CreateBasicMaterial(albedoTexture, model.GetAlphaTest());
 82        }
 83
 84        private static AssetPromise_Material_Model.Texture? CreateMaterialPromiseTextureModel(PBMaterial.Types.Texture t
 85        {
 886            if (textureModel == null)
 087                return null;
 88
 889            if (string.IsNullOrEmpty(textureModel.Src))
 090                return null;
 91
 892            if (!scene.contentProvider.TryGetContentsUrl(textureModel.Src, out string url))
 93            {
 094                return null;
 95            }
 96
 897            return new AssetPromise_Material_Model.Texture(url,
 98                (UnityEngine.TextureWrapMode)textureModel.GetWrapMode(),
 99                (UnityEngine.FilterMode)textureModel.GetFilterMode());
 100        }
 101
 102        private static bool IsPbrMaterial(PBMaterial model)
 103        {
 8104            return (model.AlphaTexture != null || model.BumpTexture != null || model.EmissiveTexture != null
 105                    || model.HasGlossiness || model.HasMetallic || model.HasRoughness || model.HasDirectIntensity
 106                    || model.HasEmissiveIntensity || model.HasSpecularIntensity
 107                    || model.AlbedoColor != null || model.EmissiveColor != null || model.ReflectivityColor != null
 108                    || model.HasTransparencyMode);
 109        }
 110    }
 111}