< Summary

Class:MaterialTransitionController
Assembly:MaterialTransitionController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/MaterialTransitionController/MaterialTransitionController.cs
Covered lines:101
Uncovered lines:24
Coverable lines:125
Total lines:302
Line coverage:80.8% (101 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/WorldRuntime/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
 2324    [System.NonSerialized] public float delay = 0.5f;
 2325    [System.NonSerialized] public bool useHologram = true;
 2326    [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        {
 6943            if (targetRendererValue == null)
 44            {
 2345                targetRendererValue = GetComponent<Renderer>();
 46            }
 47
 6948            return targetRendererValue;
 49        }
 50    }
 51
 17952    public GameObject placeholder { get; private set; }
 4453    public Renderer placeholderRenderer { get; private set; }
 54
 55    float lowerYRendererBounds;
 56    float topYRendererBounds;
 57    float currentCullYPlane;
 58    float time;
 59
 2023760    public bool materialReady { get; private set; }
 61
 62    public bool canSwitchMaterial
 63    {
 064        get { return materialReady && state != State.FINISHED; }
 65    }
 66
 67    public void PopulateTargetRendererWithMaterial(Material[] newMaterials, bool updateCulling = false)
 68    {
 4669        if (newMaterials == null)
 070            return;
 71
 72        Material material;
 18473        for (int i = 0; i < newMaterials.Length; i++)
 74        {
 4675            material = newMaterials[i];
 76
 4677            material.SetColor(ShaderId_LoadingColor, Color.clear);
 4678            material.SetFloat(ShaderId_FadeDirection, 0);
 4679            material.SetFloat(ShaderId_FadeThickness, fadeThickness);
 80
 4681            if (updateCulling)
 2382                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 83        }
 84
 4685        targetRenderer.sharedMaterials = newMaterials;
 4686    }
 87
 88    void UpdateCullYValueFXMaterial()
 89    {
 125790        if (cullingFXMaterials != null)
 91        {
 502892            for (int i = 0; i < cullingFXMaterials.Length; i++)
 93            {
 125794                Material material = cullingFXMaterials[i];
 125795                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 96            }
 97        }
 125798    }
 99
 100    void UpdateCullYValueHologram()
 101    {
 21448102        if (hologramMaterialCopy != null)
 21389103            hologramMaterialCopy.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 21448104    }
 105
 106    void PrepareCullingFXMaterials()
 107    {
 23108        cullingFXMaterials = new Material[finalMaterials.Length];
 92109        for (int i = 0; i < finalMaterials.Length; i++)
 110        {
 23111            cullingFXMaterials[i] = new Material(finalMaterials[i]);
 112        }
 23113    }
 114
 115    private void Awake()
 116    {
 23117        state = State.NOT_LOADED;
 23118        time = 0;
 23119        materialReady = false;
 23120    }
 121
 122    private void Start()
 123    {
 23124        Renderer tr = targetRenderer;
 125
 23126        if (float.IsInfinity(tr.bounds.min.y) || float.IsNaN(tr.bounds.min.y))
 127        {
 0128            Destroy(this);
 0129            return;
 130        }
 131
 23132        if (float.IsInfinity(tr.bounds.max.y) || float.IsNaN(tr.bounds.max.y))
 133        {
 0134            Destroy(this);
 0135            return;
 136        }
 137
 23138        tr.enabled = false;
 139
 23140        if (useHologram)
 141        {
 22142            InitHologram();
 143        }
 144
 23145        lowerYRendererBounds = GetLowerBoundsY(tr);
 23146        topYRendererBounds = GetTopBoundsY(tr);
 23147        currentCullYPlane = lowerYRendererBounds;
 23148    }
 149
 150    void InitHologram()
 151    {
 22152        placeholder = new GameObject("Load Placeholder");
 153
 22154        placeholder.transform.SetParent(transform, false);
 22155        placeholder.transform.ResetLocalTRS();
 156
 22157        placeholderRenderer = placeholder.AddComponent<MeshRenderer>();
 22158        MeshFilter newMeshFilter = placeholder.AddComponent<MeshFilter>();
 22159        newMeshFilter.sharedMesh = GetComponent<MeshFilter>().sharedMesh;
 160
 22161        if (hologramMaterial == null)
 1162            hologramMaterial = Resources.Load("Materials/HologramMaterial") as Material;
 163
 22164        hologramMaterialCopy = new Material(hologramMaterial);
 22165        placeholderRenderer.sharedMaterials = new Material[] {hologramMaterialCopy};
 22166    }
 167
 168    private void Update()
 169    {
 21471170        if (targetRendererValue == null)
 171        {
 0172            DestroyPlaceholder();
 0173            state = State.INVALID;
 0174            return;
 175        }
 176
 21471177        switch (state)
 178        {
 179            case State.NOT_LOADED:
 180            {
 20191181                currentCullYPlane += (topYRendererBounds - currentCullYPlane) * 0.1f;
 20191182                currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 20191183                time += Time.deltaTime;
 184
 20191185                UpdateCullYValueHologram();
 186
 20191187                if (materialReady && time > delay)
 188                {
 23189                    currentCullYPlane = topYRendererBounds;
 23190                    targetRendererValue.enabled = true;
 191
 23192                    PrepareCullingFXMaterials();
 23193                    PopulateTargetRendererWithMaterial(cullingFXMaterials, true);
 194
 23195                    state = State.SHOWING_LOADED;
 196                }
 197
 23198                break;
 199            }
 200
 201            case State.SHOWING_LOADED:
 202            {
 1257203                currentCullYPlane += (lowerYRendererBounds - currentCullYPlane) * 0.1f;
 1257204                currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 205
 1257206                UpdateCullYValueHologram();
 1257207                UpdateCullYValueFXMaterial();
 208
 1257209                if (currentCullYPlane <= lowerYRendererBounds + 0.1f)
 210                {
 211                    // We don't update the culling value in the final material to avoid affecting the already-loaded mes
 23212                    PopulateTargetRendererWithMaterial(finalMaterials);
 213
 23214                    DestroyPlaceholder();
 23215                    state = State.FINISHED;
 216                }
 217
 23218                break;
 219            }
 220
 221            case State.FINISHED:
 222            {
 23223                onFinishedLoading?.Invoke();
 23224                Destroy(this);
 225                break;
 226            }
 227        }
 21425228    }
 229
 230    private void OnDestroy()
 231    {
 23232        DestroyPlaceholder();
 233
 23234        if (loadingMaterialCopies != null)
 235        {
 0236            for (int i = 0; i < loadingMaterialCopies.Count; i++)
 237            {
 0238                Material m = loadingMaterialCopies[i];
 0239                if (m != null)
 240                {
 0241                    Destroy(m);
 242                }
 243            }
 244        }
 23245    }
 246
 247    void DestroyPlaceholder()
 248    {
 46249        Destroy(hologramMaterialCopy);
 46250        hologramMaterialCopy = null;
 251
 46252        if (placeholder != null)
 253        {
 22254            Destroy(placeholder);
 255        }
 256
 46257        if (cullingFXMaterials != null)
 258        {
 184259            for (int i = 0; i < cullingFXMaterials.Length; i++)
 260            {
 46261                Destroy(cullingFXMaterials[i]);
 262            }
 263        }
 46264    }
 265
 266    public void OnDidFinishLoading(Material finishMaterial)
 267    {
 23268        finalMaterials = new Material[] {finishMaterial};
 23269        materialReady = true;
 23270    }
 271
 272    public float GetLowerBoundsY(Renderer targetRenderer)
 273    {
 23274        return targetRenderer.bounds.min.y - fadeThickness;
 275    }
 276
 277    public float GetTopBoundsY(Renderer targetRenderer)
 278    {
 23279        return targetRenderer.bounds.max.y + fadeThickness;
 280    }
 281
 282    public static void ApplyToLoadedObject(GameObject meshContainer, bool useHologram = true, float fadeThickness = 20,
 283        float delay = 0)
 284    {
 0285        Renderer[] renderers = meshContainer.GetComponentsInChildren<Renderer>();
 286
 0287        for (int i = 0; i < renderers.Length; i++)
 288        {
 0289            Renderer r = renderers[i];
 290
 0291            if (r.gameObject.GetComponent<MaterialTransitionController>() != null)
 292                continue;
 293
 0294            MaterialTransitionController transition = r.gameObject.AddComponent<MaterialTransitionController>();
 0295            Material finalMaterial = r.sharedMaterial;
 0296            transition.delay = delay;
 0297            transition.useHologram = useHologram;
 0298            transition.fadeThickness = fadeThickness;
 0299            transition.OnDidFinishLoading(finalMaterial);
 300        }
 0301    }
 302}