< Summary

Class:MaterialTransitionController
Assembly:MaterialTransitionController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MaterialTransitionController/MaterialTransitionController.cs
Covered lines:98
Uncovered lines:27
Coverable lines:125
Total lines:291
Line coverage:78.4% (98 of 125)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
MaterialTransitionController()0%110100%
MaterialTransitionController()0%110100%
PopulateTargetRendererWithMaterial(...)0%4.014092.31%
UpdateCullYValueFXMaterial()0%330100%
UpdateCullYValueHologram()0%220100%
PrepareCullingFXMaterials()0%220100%
Awake()0%110100%
Start()0%6.846071.43%
InitHologram()0%220100%
Update()0%9.19089.29%
OnDestroy()0%8.744033.33%
DestroyPlaceholder()0%440100%
OnDidFinishLoading(...)0%110100%
GetLowerBoundsY(...)0%110100%
GetTopBoundsY(...)0%110100%
ApplyToLoadedObject(...)0%12300%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/MaterialTransitionController/MaterialTransitionController.cs

#LineLine coverage
 1using DCL.Helpers;
 2using System.Collections.Generic;
 3using UnityEngine;
 4
 5public class MaterialTransitionController : MonoBehaviour
 6{
 7    enum State
 8    {
 9        NOT_LOADED,
 10        SHOWING_LOADED,
 11        FINISHED,
 12        INVALID,
 13    }
 14
 15    State state = State.NOT_LOADED;
 16
 117    private static int ShaderId_CullYPlane = Shader.PropertyToID("_CullYPlane");
 118    private static int ShaderId_FadeThickness = Shader.PropertyToID("_FadeThickness");
 119    private static int ShaderId_FadeDirection = Shader.PropertyToID("_FadeDirection");
 120    private static int ShaderId_LoadingColor = Shader.PropertyToID("_LoadingColor");
 21
 22    Material loadingMaterial;
 23
 10424    [System.NonSerialized] public float delay = 0.5f;
 10425    [System.NonSerialized] public bool useHologram = true;
 10426    [System.NonSerialized] public float fadeThickness = 10;
 27    [System.NonSerialized] public System.Action onFinishedLoading;
 28
 29    static Material hologramMaterial;
 30
 31    List<Material> loadingMaterialCopies;
 32    Material hologramMaterialCopy;
 33
 34    public Material[] finalMaterials;
 35    Material[] cullingFXMaterials;
 36
 37    Renderer targetRendererValue;
 38
 39    Renderer targetRenderer
 40    {
 41        get
 42        {
 30643            if (targetRendererValue == null)
 44            {
 10445                targetRendererValue = GetComponent<Renderer>();
 46            }
 47
 30648            return targetRendererValue;
 49        }
 50    }
 51
 052    public GameObject placeholder { get; private set; }
 053    public Renderer placeholderRenderer { get; private set; }
 54
 55    float lowerYRendererBounds;
 56    float topYRendererBounds;
 57    float currentCullYPlane;
 58    float time;
 59
 060    public bool materialReady { get; private set; }
 061    public bool canSwitchMaterial { get { return materialReady && state != State.FINISHED; } }
 62
 63    public void PopulateTargetRendererWithMaterial(Material[] newMaterials, bool updateCulling = false)
 64    {
 20265        if (newMaterials == null)
 066            return;
 67
 68        Material material;
 80869        for (int i = 0; i < newMaterials.Length; i++)
 70        {
 20271            material = newMaterials[i];
 72
 20273            material.SetColor(ShaderId_LoadingColor, Color.clear);
 20274            material.SetFloat(ShaderId_FadeDirection, 0);
 20275            material.SetFloat(ShaderId_FadeThickness, fadeThickness);
 76
 20277            if (updateCulling)
 10178                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 79        }
 80
 20281        targetRenderer.sharedMaterials = newMaterials;
 20282    }
 83
 84    void UpdateCullYValueFXMaterial()
 85    {
 527886        if (cullingFXMaterials != null)
 87        {
 2111288            for (int i = 0; i < cullingFXMaterials.Length; i++)
 89            {
 527890                Material material = cullingFXMaterials[i];
 527891                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 92            }
 93        }
 527894    }
 95
 96    void UpdateCullYValueHologram()
 97    {
 1509698        if (hologramMaterialCopy != null)
 1503799            hologramMaterialCopy.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 15096100    }
 101
 102    void PrepareCullingFXMaterials()
 103    {
 101104        cullingFXMaterials = new Material[finalMaterials.Length];
 404105        for (int i = 0; i < finalMaterials.Length; i++)
 106        {
 101107            cullingFXMaterials[i] = new Material(finalMaterials[i]);
 108        }
 101109    }
 110
 111    private void Awake()
 112    {
 104113        state = State.NOT_LOADED;
 104114        time = 0;
 104115        materialReady = false;
 104116    }
 117
 118    private void Start()
 119    {
 104120        Renderer tr = targetRenderer;
 121
 104122        if (float.IsInfinity(tr.bounds.min.y) || float.IsNaN(tr.bounds.min.y))
 123        {
 0124            Destroy(this);
 0125            return;
 126        }
 127
 104128        if (float.IsInfinity(tr.bounds.max.y) || float.IsNaN(tr.bounds.max.y))
 129        {
 0130            Destroy(this);
 0131            return;
 132        }
 133
 104134        tr.enabled = false;
 135
 104136        if (useHologram)
 137        {
 103138            InitHologram();
 139        }
 140
 104141        lowerYRendererBounds = GetLowerBoundsY(tr);
 104142        topYRendererBounds = GetTopBoundsY(tr);
 104143        currentCullYPlane = lowerYRendererBounds;
 104144    }
 145
 146    void InitHologram()
 147    {
 103148        placeholder = new GameObject("Load Placeholder");
 149
 103150        placeholder.transform.SetParent(transform, false);
 103151        placeholder.transform.ResetLocalTRS();
 152
 103153        placeholderRenderer = placeholder.AddComponent<MeshRenderer>();
 103154        MeshFilter newMeshFilter = placeholder.AddComponent<MeshFilter>();
 103155        newMeshFilter.sharedMesh = GetComponent<MeshFilter>().sharedMesh;
 156
 103157        if (hologramMaterial == null)
 1158            hologramMaterial = Resources.Load("Materials/HologramMaterial") as Material;
 159
 103160        hologramMaterialCopy = new Material(hologramMaterial);
 103161        placeholderRenderer.sharedMaterials = new Material[] { hologramMaterialCopy };
 103162    }
 163
 164    private void Update()
 165    {
 15197166        if (targetRendererValue == null)
 167        {
 0168            DestroyPlaceholder();
 0169            state = State.INVALID;
 0170            return;
 171        }
 172
 15197173        switch (state)
 174        {
 175            case State.NOT_LOADED:
 176                {
 9818177                    currentCullYPlane += (topYRendererBounds - currentCullYPlane) * 0.1f;
 9818178                    currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 9818179                    time += Time.deltaTime;
 180
 9818181                    UpdateCullYValueHologram();
 182
 9818183                    if (materialReady && time > delay)
 184                    {
 101185                        currentCullYPlane = topYRendererBounds;
 101186                        targetRendererValue.enabled = true;
 187
 101188                        PrepareCullingFXMaterials();
 101189                        PopulateTargetRendererWithMaterial(cullingFXMaterials, true);
 190
 101191                        state = State.SHOWING_LOADED;
 192                    }
 193
 101194                    break;
 195                }
 196
 197            case State.SHOWING_LOADED:
 198                {
 5278199                    currentCullYPlane += (lowerYRendererBounds - currentCullYPlane) * 0.1f;
 5278200                    currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 201
 5278202                    UpdateCullYValueHologram();
 5278203                    UpdateCullYValueFXMaterial();
 204
 5278205                    if (currentCullYPlane <= lowerYRendererBounds + 0.1f)
 206                    {
 207                        // We don't update the culling value in the final material to avoid affecting the already-loaded
 101208                        PopulateTargetRendererWithMaterial(finalMaterials);
 209
 101210                        DestroyPlaceholder();
 101211                        state = State.FINISHED;
 212                    }
 213
 101214                    break;
 215                }
 216
 217            case State.FINISHED:
 218                {
 101219                    onFinishedLoading?.Invoke();
 101220                    Destroy(this);
 221                    break;
 222                }
 223        }
 14995224    }
 225
 226    private void OnDestroy()
 227    {
 104228        DestroyPlaceholder();
 229
 104230        if (loadingMaterialCopies != null)
 231        {
 0232            for (int i = 0; i < loadingMaterialCopies.Count; i++)
 233            {
 0234                Material m = loadingMaterialCopies[i];
 0235                if (m != null)
 236                {
 0237                    Destroy(m);
 238                }
 239            }
 240        }
 104241    }
 242
 243    void DestroyPlaceholder()
 244    {
 205245        Destroy(hologramMaterialCopy);
 205246        hologramMaterialCopy = null;
 247
 205248        if (placeholder != null)
 249        {
 103250            Destroy(placeholder);
 251        }
 252
 205253        if (cullingFXMaterials != null)
 254        {
 808255            for (int i = 0; i < cullingFXMaterials.Length; i++)
 256            {
 202257                Destroy(cullingFXMaterials[i]);
 258            }
 259        }
 205260    }
 261
 262    public void OnDidFinishLoading(Material finishMaterial)
 263    {
 103264        finalMaterials = new Material[] { finishMaterial };
 103265        materialReady = true;
 103266    }
 267
 104268    public float GetLowerBoundsY(Renderer targetRenderer) { return targetRenderer.bounds.min.y - fadeThickness; }
 269
 104270    public float GetTopBoundsY(Renderer targetRenderer) { return targetRenderer.bounds.max.y + fadeThickness; }
 271
 272    public static void ApplyToLoadedObject(GameObject meshContainer, bool useHologram = true, float fadeThickness = 20, 
 273    {
 0274        Renderer[] renderers = meshContainer.GetComponentsInChildren<Renderer>();
 275
 0276        for (int i = 0; i < renderers.Length; i++)
 277        {
 0278            Renderer r = renderers[i];
 279
 0280            if (r.gameObject.GetComponent<MaterialTransitionController>() != null)
 281                continue;
 282
 0283            MaterialTransitionController transition = r.gameObject.AddComponent<MaterialTransitionController>();
 0284            Material finalMaterial = r.sharedMaterial;
 0285            transition.delay = delay;
 0286            transition.useHologram = useHologram;
 0287            transition.fadeThickness = fadeThickness;
 0288            transition.OnDidFinishLoading(finalMaterial);
 289        }
 0290    }
 291}