< Summary

Class:DCL.Components.BasicMaterial
Assembly:MainScripts
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Materials/BasicMaterial.cs
Covered lines:73
Uncovered lines:7
Coverable lines:80
Total lines:173
Line coverage:91.2% (73 of 80)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Model()0%110100%
GetDataFromJSON(...)0%110100%
BasicMaterial()0%110100%
BasicMaterial()0%110100%
GetModel()0%2100%
GetClassId()0%2100%
AttachTo(...)0%2.032080%
ApplyChanges()0%9.019095.24%
OnMaterialAttached(...)0%330100%
InitMaterial(...)0%8.518080%
OnShapeUpdated(...)0%220100%
OnMaterialDetached(...)0%440100%
Dispose()0%220100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Materials/BasicMaterial.cs

#LineLine coverage
 1using DCL.Controllers;
 2using DCL.Helpers;
 3using DCL.Models;
 4using System.Collections;
 5using System.Collections.Generic;
 6using UnityEngine;
 7using UnityEngine.Rendering;
 8
 9namespace DCL.Components
 10{
 11    public class BasicMaterial : BaseDisposable
 12    {
 13        [System.Serializable]
 14        public class Model : BaseModel
 15        {
 16            public string texture;
 17
 18            // value that defines if a pixel is visible or invisible (no transparency gradients)
 19            [Range(0f, 1f)]
 7120            public float alphaTest = 0.5f;
 21
 7122            public bool castShadows = true;
 23
 2524            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 25        }
 26
 27        public Material material;
 28
 29        private DCLTexture dclTexture = null;
 30
 131        private static readonly int _BaseMap = Shader.PropertyToID("_BaseMap");
 132        private static readonly int _AlphaClip = Shader.PropertyToID("_AlphaClip");
 133        private static readonly int _Cutoff = Shader.PropertyToID("_Cutoff");
 134        private static readonly int _ZWrite = Shader.PropertyToID("_ZWrite");
 35
 2136        public BasicMaterial()
 37        {
 2138            material = new Material(Utils.EnsureResourcesMaterial("Materials/BasicShapeMaterial"));
 39
 2140            OnAttach += OnMaterialAttached;
 2141            OnDetach += OnMaterialDetached;
 2142            model = new Model();
 2143        }
 44
 045        new public Model GetModel() { return (Model) model; }
 46
 047        public override int GetClassId() { return (int) CLASS_ID.BASIC_MATERIAL; }
 48
 49        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 50        {
 1951            if (attachedEntities.Contains(entity))
 052                return;
 53
 1954            entity.RemoveSharedComponent(typeof(PBRMaterial));
 1955            base.AttachTo(entity);
 1956        }
 57
 58        public override IEnumerator ApplyChanges(BaseModel newModel)
 59        {
 2560            if (material == null)
 61            {
 062                yield break; // We escape ApplyChanges called in the parent's constructor
 63            }
 64
 65#if UNITY_EDITOR
 2566            material.name = "BasicMaterial_" + id;
 67#endif
 68
 2569            Model model = (Model) newModel;
 70
 2571            if (!string.IsNullOrEmpty(model.texture))
 72            {
 1573                if (dclTexture == null || dclTexture.id != model.texture)
 74                {
 1575                    yield return DCLTexture.FetchTextureComponent(scene, model.texture, (downloadedTexture) =>
 76                    {
 1577                        dclTexture?.DetachFrom(this);
 1578                        material.SetTexture(_BaseMap, downloadedTexture.texture);
 1579                        dclTexture = downloadedTexture;
 1580                        dclTexture.AttachTo(this);
 1581                    });
 82                }
 1583            }
 84            else
 85            {
 1086                material.mainTexture = null;
 1087                dclTexture?.DetachFrom(this);
 1088                dclTexture = null;
 89            }
 90
 2591            material.EnableKeyword("_ALPHATEST_ON");
 2592            material.SetInt(_ZWrite, 1);
 2593            material.SetFloat(_AlphaClip, 1);
 2594            material.SetFloat(_Cutoff, model.alphaTest);
 2595            material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.AlphaTest;
 96
 7297            foreach (IDCLEntity decentralandEntity in attachedEntities)
 98            {
 1199                InitMaterial(decentralandEntity.meshRootGameObject);
 100            }
 25101        }
 102
 103        void OnMaterialAttached(IDCLEntity entity)
 104        {
 19105            entity.OnShapeUpdated -= OnShapeUpdated;
 19106            entity.OnShapeUpdated += OnShapeUpdated;
 107
 19108            if (entity.meshRootGameObject != null)
 109            {
 8110                var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>();
 111
 8112                if (meshRenderer != null)
 8113                    InitMaterial(entity.meshRootGameObject);
 114            }
 19115        }
 116
 117        void InitMaterial(GameObject meshGameObject)
 118        {
 33119            if (meshGameObject == null)
 11120                return;
 121
 22122            var meshRenderer = meshGameObject.GetComponent<MeshRenderer>();
 22123            if (meshRenderer == null)
 0124                return;
 125
 22126            Model model = (Model) this.model;
 127
 22128            meshRenderer.shadowCastingMode = model.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off;
 22129            if (meshRenderer.sharedMaterial != material)
 130            {
 16131                MaterialTransitionController
 132                    matTransition = meshGameObject.GetComponent<MaterialTransitionController>();
 133
 16134                if (matTransition != null && matTransition.canSwitchMaterial)
 135                {
 0136                    matTransition.finalMaterials = new Material[] { material };
 0137                    matTransition.PopulateTargetRendererWithMaterial(matTransition.finalMaterials);
 138                }
 139
 16140                SRPBatchingHelper.OptimizeMaterial(material);
 16141                meshRenderer.sharedMaterial = material;
 142            }
 22143        }
 144
 145        private void OnShapeUpdated(IDCLEntity entity)
 146        {
 14147            if (entity != null)
 14148                InitMaterial(entity.meshRootGameObject);
 14149        }
 150
 151        void OnMaterialDetached(IDCLEntity entity)
 152        {
 19153            if (entity.meshRootGameObject == null)
 7154                return;
 155
 12156            entity.OnShapeUpdated -= OnShapeUpdated;
 157
 12158            var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>();
 159
 12160            if (meshRenderer && meshRenderer.sharedMaterial == material)
 12161                meshRenderer.sharedMaterial = null;
 12162        }
 163
 164        public override void Dispose()
 165        {
 22166            dclTexture?.DetachFrom(this);
 167
 22168            Object.Destroy(material);
 169
 22170            base.Dispose();
 22171        }
 172    }
 173}