< 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
 9824    [System.NonSerialized] public float delay = 0.5f;
 9825    [System.NonSerialized] public bool useHologram = true;
 9826    [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        {
 29443            if (targetRendererValue == null)
 44            {
 9845                targetRendererValue = GetComponent<Renderer>();
 46            }
 47
 29448            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    {
 19665        if (newMaterials == null)
 066            return;
 67
 68        Material material;
 78469        for (int i = 0; i < newMaterials.Length; i++)
 70        {
 19671            material = newMaterials[i];
 72
 19673            material.SetColor(ShaderId_LoadingColor, Color.clear);
 19674            material.SetFloat(ShaderId_FadeDirection, 0);
 19675            material.SetFloat(ShaderId_FadeThickness, fadeThickness);
 76
 19677            if (updateCulling)
 9878                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 79        }
 80
 19681        targetRenderer.sharedMaterials = newMaterials;
 19682    }
 83
 84    void UpdateCullYValueFXMaterial()
 85    {
 511586        if (cullingFXMaterials != null)
 87        {
 2046088            for (int i = 0; i < cullingFXMaterials.Length; i++)
 89            {
 511590                Material material = cullingFXMaterials[i];
 511591                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 92            }
 93        }
 511594    }
 95
 96    void UpdateCullYValueHologram()
 97    {
 1470298        if (hologramMaterialCopy != null)
 1464399            hologramMaterialCopy.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 14702100    }
 101
 102    void PrepareCullingFXMaterials()
 103    {
 98104        cullingFXMaterials = new Material[finalMaterials.Length];
 392105        for (int i = 0; i < finalMaterials.Length; i++)
 106        {
 98107            cullingFXMaterials[i] = new Material(finalMaterials[i]);
 108        }
 98109    }
 110
 111    private void Awake()
 112    {
 98113        state = State.NOT_LOADED;
 98114        time = 0;
 98115        materialReady = false;
 98116    }
 117
 118    private void Start()
 119    {
 98120        Renderer tr = targetRenderer;
 121
 98122        if (float.IsInfinity(tr.bounds.min.y) || float.IsNaN(tr.bounds.min.y))
 123        {
 0124            Destroy(this);
 0125            return;
 126        }
 127
 98128        if (float.IsInfinity(tr.bounds.max.y) || float.IsNaN(tr.bounds.max.y))
 129        {
 0130            Destroy(this);
 0131            return;
 132        }
 133
 98134        tr.enabled = false;
 135
 98136        if (useHologram)
 137        {
 97138            InitHologram();
 139        }
 140
 98141        lowerYRendererBounds = GetLowerBoundsY(tr);
 98142        topYRendererBounds = GetTopBoundsY(tr);
 98143        currentCullYPlane = lowerYRendererBounds;
 98144    }
 145
 146    void InitHologram()
 147    {
 97148        placeholder = new GameObject("Load Placeholder");
 149
 97150        placeholder.transform.SetParent(transform, false);
 97151        placeholder.transform.ResetLocalTRS();
 152
 97153        placeholderRenderer = placeholder.AddComponent<MeshRenderer>();
 97154        MeshFilter newMeshFilter = placeholder.AddComponent<MeshFilter>();
 97155        newMeshFilter.sharedMesh = GetComponent<MeshFilter>().sharedMesh;
 156
 97157        if (hologramMaterial == null)
 1158            hologramMaterial = Resources.Load("Materials/HologramMaterial") as Material;
 159
 97160        hologramMaterialCopy = new Material(hologramMaterial);
 97161        placeholderRenderer.sharedMaterials = new Material[] { hologramMaterialCopy };
 97162    }
 163
 164    private void Update()
 165    {
 14800166        if (targetRendererValue == null)
 167        {
 0168            DestroyPlaceholder();
 0169            state = State.INVALID;
 0170            return;
 171        }
 172
 14800173        switch (state)
 174        {
 175            case State.NOT_LOADED:
 176                {
 9587177                    currentCullYPlane += (topYRendererBounds - currentCullYPlane) * 0.1f;
 9587178                    currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 9587179                    time += Time.deltaTime;
 180
 9587181                    UpdateCullYValueHologram();
 182
 9587183                    if (materialReady && time > delay)
 184                    {
 98185                        currentCullYPlane = topYRendererBounds;
 98186                        targetRendererValue.enabled = true;
 187
 98188                        PrepareCullingFXMaterials();
 98189                        PopulateTargetRendererWithMaterial(cullingFXMaterials, true);
 190
 98191                        state = State.SHOWING_LOADED;
 192                    }
 193
 98194                    break;
 195                }
 196
 197            case State.SHOWING_LOADED:
 198                {
 5115199                    currentCullYPlane += (lowerYRendererBounds - currentCullYPlane) * 0.1f;
 5115200                    currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 201
 5115202                    UpdateCullYValueHologram();
 5115203                    UpdateCullYValueFXMaterial();
 204
 5115205                    if (currentCullYPlane <= lowerYRendererBounds + 0.1f)
 206                    {
 207                        // We don't update the culling value in the final material to avoid affecting the already-loaded
 98208                        PopulateTargetRendererWithMaterial(finalMaterials);
 209
 98210                        DestroyPlaceholder();
 98211                        state = State.FINISHED;
 212                    }
 213
 98214                    break;
 215                }
 216
 217            case State.FINISHED:
 218                {
 98219                    onFinishedLoading?.Invoke();
 98220                    Destroy(this);
 221                    break;
 222                }
 223        }
 14604224    }
 225
 226    private void OnDestroy()
 227    {
 98228        DestroyPlaceholder();
 229
 98230        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        }
 98241    }
 242
 243    void DestroyPlaceholder()
 244    {
 196245        Destroy(hologramMaterialCopy);
 196246        hologramMaterialCopy = null;
 247
 196248        if (placeholder != null)
 249        {
 97250            Destroy(placeholder);
 251        }
 252
 196253        if (cullingFXMaterials != null)
 254        {
 784255            for (int i = 0; i < cullingFXMaterials.Length; i++)
 256            {
 196257                Destroy(cullingFXMaterials[i]);
 258            }
 259        }
 196260    }
 261
 262    public void OnDidFinishLoading(Material finishMaterial)
 263    {
 98264        finalMaterials = new Material[] { finishMaterial };
 98265        materialReady = true;
 98266    }
 267
 98268    public float GetLowerBoundsY(Renderer targetRenderer) { return targetRenderer.bounds.min.y - fadeThickness; }
 269
 98270    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}