< 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
 8624    [System.NonSerialized] public float delay = 0.5f;
 8625    [System.NonSerialized] public bool useHologram = true;
 8626    [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        {
 25843            if (targetRendererValue == null)
 44            {
 8645                targetRendererValue = GetComponent<Renderer>();
 46            }
 47
 25848            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    {
 17265        if (newMaterials == null)
 066            return;
 67
 68        Material material;
 68869        for (int i = 0; i < newMaterials.Length; i++)
 70        {
 17271            material = newMaterials[i];
 72
 17273            material.SetColor(ShaderId_LoadingColor, Color.clear);
 17274            material.SetFloat(ShaderId_FadeDirection, 0);
 17275            material.SetFloat(ShaderId_FadeThickness, fadeThickness);
 76
 17277            if (updateCulling)
 8678                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 79        }
 80
 17281        targetRenderer.sharedMaterials = newMaterials;
 17282    }
 83
 84    void UpdateCullYValueFXMaterial()
 85    {
 447186        if (cullingFXMaterials != null)
 87        {
 1788488            for (int i = 0; i < cullingFXMaterials.Length; i++)
 89            {
 447190                Material material = cullingFXMaterials[i];
 447191                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 92            }
 93        }
 447194    }
 95
 96    void UpdateCullYValueHologram()
 97    {
 1356398        if (hologramMaterialCopy != null)
 1350499            hologramMaterialCopy.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 13563100    }
 101
 102    void PrepareCullingFXMaterials()
 103    {
 86104        cullingFXMaterials = new Material[finalMaterials.Length];
 344105        for (int i = 0; i < finalMaterials.Length; i++)
 106        {
 86107            cullingFXMaterials[i] = new Material(finalMaterials[i]);
 108        }
 86109    }
 110
 111    private void Awake()
 112    {
 86113        state = State.NOT_LOADED;
 86114        time = 0;
 86115        materialReady = false;
 86116    }
 117
 118    private void Start()
 119    {
 86120        Renderer tr = targetRenderer;
 121
 86122        if (float.IsInfinity(tr.bounds.min.y) || float.IsNaN(tr.bounds.min.y))
 123        {
 0124            Destroy(this);
 0125            return;
 126        }
 127
 86128        if (float.IsInfinity(tr.bounds.max.y) || float.IsNaN(tr.bounds.max.y))
 129        {
 0130            Destroy(this);
 0131            return;
 132        }
 133
 86134        tr.enabled = false;
 135
 86136        if (useHologram)
 137        {
 85138            InitHologram();
 139        }
 140
 86141        lowerYRendererBounds = GetLowerBoundsY(tr);
 86142        topYRendererBounds = GetTopBoundsY(tr);
 86143        currentCullYPlane = lowerYRendererBounds;
 86144    }
 145
 146    void InitHologram()
 147    {
 85148        placeholder = new GameObject("Load Placeholder");
 149
 85150        placeholder.transform.SetParent(transform, false);
 85151        placeholder.transform.ResetLocalTRS();
 152
 85153        placeholderRenderer = placeholder.AddComponent<MeshRenderer>();
 85154        MeshFilter newMeshFilter = placeholder.AddComponent<MeshFilter>();
 85155        newMeshFilter.sharedMesh = GetComponent<MeshFilter>().sharedMesh;
 156
 85157        if (hologramMaterial == null)
 1158            hologramMaterial = Resources.Load("Materials/HologramMaterial") as Material;
 159
 85160        hologramMaterialCopy = new Material(hologramMaterial);
 85161        placeholderRenderer.sharedMaterials = new Material[] { hologramMaterialCopy };
 85162    }
 163
 164    private void Update()
 165    {
 13649166        if (targetRendererValue == null)
 167        {
 0168            DestroyPlaceholder();
 0169            state = State.INVALID;
 0170            return;
 171        }
 172
 13649173        switch (state)
 174        {
 175            case State.NOT_LOADED:
 176                {
 9092177                    currentCullYPlane += (topYRendererBounds - currentCullYPlane) * 0.1f;
 9092178                    currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 9092179                    time += Time.deltaTime;
 180
 9092181                    UpdateCullYValueHologram();
 182
 9092183                    if (materialReady && time > delay)
 184                    {
 86185                        currentCullYPlane = topYRendererBounds;
 86186                        targetRendererValue.enabled = true;
 187
 86188                        PrepareCullingFXMaterials();
 86189                        PopulateTargetRendererWithMaterial(cullingFXMaterials, true);
 190
 86191                        state = State.SHOWING_LOADED;
 192                    }
 193
 86194                    break;
 195                }
 196
 197            case State.SHOWING_LOADED:
 198                {
 4471199                    currentCullYPlane += (lowerYRendererBounds - currentCullYPlane) * 0.1f;
 4471200                    currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 201
 4471202                    UpdateCullYValueHologram();
 4471203                    UpdateCullYValueFXMaterial();
 204
 4471205                    if (currentCullYPlane <= lowerYRendererBounds + 0.1f)
 206                    {
 207                        // We don't update the culling value in the final material to avoid affecting the already-loaded
 86208                        PopulateTargetRendererWithMaterial(finalMaterials);
 209
 86210                        DestroyPlaceholder();
 86211                        state = State.FINISHED;
 212                    }
 213
 86214                    break;
 215                }
 216
 217            case State.FINISHED:
 218                {
 86219                    onFinishedLoading?.Invoke();
 86220                    Destroy(this);
 221                    break;
 222                }
 223        }
 13477224    }
 225
 226    private void OnDestroy()
 227    {
 86228        DestroyPlaceholder();
 229
 86230        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        }
 86241    }
 242
 243    void DestroyPlaceholder()
 244    {
 172245        Destroy(hologramMaterialCopy);
 172246        hologramMaterialCopy = null;
 247
 172248        if (placeholder != null)
 249        {
 85250            Destroy(placeholder);
 251        }
 252
 172253        if (cullingFXMaterials != null)
 254        {
 688255            for (int i = 0; i < cullingFXMaterials.Length; i++)
 256            {
 172257                Destroy(cullingFXMaterials[i]);
 258            }
 259        }
 172260    }
 261
 262    public void OnDidFinishLoading(Material finishMaterial)
 263    {
 86264        finalMaterials = new Material[] { finishMaterial };
 86265        materialReady = true;
 86266    }
 267
 86268    public float GetLowerBoundsY(Renderer targetRenderer) { return targetRenderer.bounds.min.y - fadeThickness; }
 269
 86270    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}