< 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:83
Uncovered lines:9
Coverable lines:92
Total lines:204
Line coverage:90.2% (83 of 92)
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%
DetachFrom(...)0%110100%
ApplyChanges()0%9.219086.36%
OnMaterialAttached(...)0%330100%
InitMaterial(...)0%8.228085%
OnShapeUpdated(...)0%220100%
OnMaterialDetached(...)0%440100%
Dispose()0%330100%

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
 046        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            entity.RemoveSharedComponent(typeof(PBRMaterial));
 4056            base.AttachTo(entity, overridenAttachedType);
 4057        }
 58
 59        public override void DetachFrom(IDCLEntity entity, System.Type overridenAttachedType = null)
 60        {
 2861            base.DetachFrom(entity, overridenAttachedType);
 2862        }
 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                }
 4195            }
 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        {
 77135            var meshGameObject = entity.meshRootGameObject;
 136
 77137            if (meshGameObject == null)
 8138                return;
 139
 69140            var meshRenderer = meshGameObject.GetComponent<MeshRenderer>();
 141
 69142            if (meshRenderer == null)
 0143                return;
 144
 69145            Model model = (Model) this.model;
 146
 69147            meshRenderer.shadowCastingMode = model.castShadows ? ShadowCastingMode.On : ShadowCastingMode.Off;
 148
 69149            if (meshRenderer.sharedMaterial == material)
 32150                return;
 151
 37152            MaterialTransitionController
 153                matTransition = meshGameObject.GetComponent<MaterialTransitionController>();
 154
 37155            if (matTransition != null && matTransition.canSwitchMaterial)
 156            {
 0157                matTransition.finalMaterials = new Material[] { material };
 0158                matTransition.PopulateTargetRendererWithMaterial(matTransition.finalMaterials);
 159            }
 160
 37161            SRPBatchingHelper.OptimizeMaterial(material);
 162
 37163            Material oldMaterial = meshRenderer.sharedMaterial;
 37164            meshRenderer.sharedMaterial = material;
 165
 37166            DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.id, entity.entityId, oldMaterial);
 37167            DataStore.i.sceneWorldObjects.AddMaterial(scene.sceneData.id, entity.entityId, material);
 37168        }
 169
 170        private void OnShapeUpdated(IDCLEntity entity)
 171        {
 20172            if (entity != null)
 20173                InitMaterial(entity);
 20174        }
 175
 176        void OnMaterialDetached(IDCLEntity entity)
 177        {
 28178            if (entity.meshRootGameObject == null)
 2179                return;
 180
 26181            entity.OnShapeUpdated -= OnShapeUpdated;
 182
 26183            var meshRenderer = entity.meshRootGameObject.GetComponent<MeshRenderer>();
 184
 26185            if (meshRenderer && meshRenderer.sharedMaterial == material)
 26186                meshRenderer.sharedMaterial = null;
 187
 26188            DataStore.i.sceneWorldObjects.RemoveMaterial(scene.sceneData.id, entity.entityId, material);
 26189        }
 190
 191        public override void Dispose()
 192        {
 40193            dclTexture?.DetachFrom(this);
 194
 60195            while ( attachedEntities.Count > 0 )
 196            {
 20197                DetachFrom(attachedEntities.First());
 198            }
 199
 40200            Object.Destroy(material);
 40201            base.Dispose();
 40202        }
 203    }
 204}