< Summary

Class:DCL.Components.BasicMaterial
Assembly:DCL.Components.Materials
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Components/Materials/BasicMaterial.cs
Covered lines:83
Uncovered lines:8
Coverable lines:91
Total lines:204
Line coverage:91.2% (83 of 91)
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%110100%
GetClassId()0%2100%
AttachTo(...)0%2.032080%
DetachFrom(...)0%110100%
ApplyChanges()0%9.249085.71%
OnMaterialAttached(...)0%330100%
InitMaterial(...)0%8.228085%
OnShapeUpdated(...)0%220100%
OnMaterialDetached(...)0%440100%
Dispose()0%440100%

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 System.Linq;
 7using UnityEngine;
 8using UnityEngine.Rendering;
 9
 10namespace DCL.Components
 11{
 12    public class BasicMaterial : BaseDisposable
 13    {
 14        [System.Serializable]
 15        public class Model : BaseModel
 16        {
 17            public string texture;
 18
 19            // value that defines if a pixel is visible or invisible (no transparency gradients)
 20            [Range(0f, 1f)]
 15121            public float alphaTest = 0.5f;
 22
 15123            public bool castShadows = true;
 24
 5525            public override BaseModel GetDataFromJSON(string json) { return Utils.SafeFromJson<Model>(json); }
 26        }
 27
 28        public Material material;
 29
 30        private DCLTexture dclTexture = null;
 31
 132        private static readonly int _BaseMap = Shader.PropertyToID("_BaseMap");
 133        private static readonly int _AlphaClip = Shader.PropertyToID("_AlphaClip");
 134        private static readonly int _Cutoff = Shader.PropertyToID("_Cutoff");
 135        private static readonly int _ZWrite = Shader.PropertyToID("_ZWrite");
 36
 4137        public BasicMaterial()
 38        {
 4139            material = new Material(Utils.EnsureResourcesMaterial("Materials/BasicShapeMaterial"));
 40
 4141            OnAttach += OnMaterialAttached;
 4142            OnDetach += OnMaterialDetached;
 4143            model = new Model();
 4144        }
 45
 446        new public Model GetModel() { return (Model) model; }
 47
 048        public override int GetClassId() { return (int) CLASS_ID.BASIC_MATERIAL; }
 49
 50        public override void AttachTo(IDCLEntity entity, System.Type overridenAttachedType = null)
 51        {
 4052            if (attachedEntities.Contains(entity))
 053                return;
 54
 4055            scene.componentsManagerLegacy.RemoveSharedComponent(entity, (typeof(PBRMaterial)));
 4056            base.AttachTo(entity, overridenAttachedType);
 4057        }
 58
 59        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null)
 60        {
 4061            base.DetachFrom(entity, overridenAttachedType);
 4062        }
 63
 64        public override IEnumerator ApplyChanges(BaseModel newModel)
 65        {
 5566            if (material == null)
 67            {
 068                yield break; // We escape ApplyChanges called in the parent's constructor
 69            }
 70
 71#if UNITY_EDITOR
 5572            material.name = "BasicMaterial_" + id;
 73#endif
 74
 5575            Model model = (Model) newModel;
 76
 5577            if (!string.IsNullOrEmpty(model.texture))
 78            {
 4179                if (dclTexture == null || dclTexture.id != model.texture)
 80                {
 4181                    yield return DCLTexture.FetchTextureComponent(scene, model.texture,
 82                        (downloadedTexture) =>
 83                        {
 4184                            if ( dclTexture != null )
 85                            {
 1086                                dclTexture.DetachFrom(this);
 87                            }
 88
 4189                            material.SetTexture(_BaseMap, downloadedTexture.texture);
 4190                            dclTexture = downloadedTexture;
 4191                            dclTexture.AttachTo(this);
 4192                        }
 93                    );
 94                }
 95            }
 96            else
 97            {
 1498                material.mainTexture = null;
 99
 14100                if ( dclTexture != null )
 101                {
 0102                    dclTexture.DetachFrom(this);
 0103                    dclTexture = null;
 104                }
 105            }
 106
 55107            material.EnableKeyword("_ALPHATEST_ON");
 55108            material.SetInt(_ZWrite, 1);
 55109            material.SetFloat(_AlphaClip, 1);
 55110            material.SetFloat(_Cutoff, model.alphaTest);
 55111            material.renderQueue = (int) UnityEngine.Rendering.RenderQueue.AlphaTest;
 112
 184113            foreach (IDCLEntity entity in attachedEntities)
 114            {
 37115                InitMaterial(entity);
 116            }
 55117        }
 118
 119        void OnMaterialAttached(IDCLEntity entity)
 120        {
 40121            entity.OnShapeUpdated -= OnShapeUpdated;
 40122            entity.OnShapeUpdated += OnShapeUpdated;
 123
 40124            if (entity.meshRootGameObject != null)
 125            {
 20126                var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>();
 127
 20128                if (meshRenderer != null)
 20129                    InitMaterial(entity);
 130            }
 40131        }
 132
 133        void InitMaterial(IDCLEntity entity)
 134        {
 98135            var meshGameObject = entity.meshRootGameObject;
 136
 98137            if (meshGameObject == null)
 11138                return;
 139
 87140            var meshRenderer = meshGameObject.GetComponent<MeshRenderer>();
 141
 87142            if (meshRenderer == null)
 0143                return;
 144
 87145            Model model = (Model) this.model;
 146
 87147            meshRenderer.shadowCastingMode = model.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off;
 148
 87149            if (meshRenderer.sharedMaterial == material)
 52150                return;
 151
 35152            MaterialTransitionController
 153                matTransition = meshGameObject.GetComponent<MaterialTransitionController>();
 154
 35155            if (matTransition != null && matTransition.canSwitchMaterial)
 156            {
 0157                matTransition.finalMaterials = new Material[] { material };
 0158                matTransition.PopulateTargetRendererWithMaterial(matTransition.finalMaterials);
 159            }
 160
 35161            SRPBatchingHelper.OptimizeMaterial(material);
 162
 35163            Material oldMaterial = meshRenderer.sharedMaterial;
 35164            meshRenderer.sharedMaterial = material;
 165
 35166            DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.sceneNumber, entity.entityId, oldMaterial);
 35167            DataStore.i.sceneWorldObjects.AddMaterial(scene.sceneData.sceneNumber, entity.entityId, material);
 35168        }
 169
 170        private void OnShapeUpdated(IDCLEntity entity)
 171        {
 41172            if (entity != null)
 41173                InitMaterial(entity);
 41174        }
 175
 176        void OnMaterialDetached(IDCLEntity entity)
 177        {
 40178            if (entity.meshRootGameObject == null)
 7179                return;
 180
 33181            entity.OnShapeUpdated -= OnShapeUpdated;
 182
 33183            var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>();
 184
 33185            if (meshRenderer && meshRenderer.sharedMaterial == material)
 31186                meshRenderer.sharedMaterial = null;
 187
 33188            DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.sceneNumber, entity.entityId, material);
 33189        }
 190
 191        public override void Dispose()
 192        {
 40193            dclTexture?.DetachFrom(this);
 194
 60195            while (attachedEntities != null && attachedEntities.Count > 0 )
 196            {
 20197                DetachFrom(attachedEntities.First());
 198            }
 199
 40200            Object.Destroy(material);
 40201            base.Dispose();
 40202        }
 203    }
 204}