< Summary

Class:MaterialTransitionController
Assembly:MaterialTransitionController
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/WorldRuntime/MaterialTransitionController/MaterialTransitionController.cs
Covered lines:88
Uncovered lines:43
Coverable lines:131
Total lines:313
Line coverage:67.1% (88 of 131)
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%5.335076.47%
UpdateCullYValueFXMaterial()0%330100%
UpdateCullYValueHologram()0%2.152066.67%
PrepareCullingFXMaterials()0%220100%
Awake()0%110100%
Start()0%7.646064.29%
InitHologram()0%6200%
Update()0%9.19089.29%
OnDestroy()0%8.744033.33%
DestroyPlaceholder()0%4.024090%
OnDidFinishLoading(...)0%110100%
GetLowerBoundsY(...)0%110100%
GetTopBoundsY(...)0%110100%
ApplyToLoadedObject(...)0%12300%
ForceStop()0%110100%

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
 35224    [System.NonSerialized] public float delay = 0.5f;
 35225    [System.NonSerialized] public bool useHologram = true;
 35226    [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        {
 75743            if (targetRendererValue == null)
 44            {
 33345                targetRendererValue = GetComponent<Renderer>();
 46            }
 47
 75748            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; }
 61
 62    public bool canSwitchMaterial
 63    {
 9864        get { return materialReady && state != State.FINISHED; }
 65    }
 66
 67    public void PopulateTargetRendererWithMaterial(Material[] newMaterials, bool updateCulling = false)
 68    {
 42469        if (newMaterials == null)
 070            return;
 71
 72        Material material;
 169673        for (int i = 0; i < newMaterials.Length; i++)
 74        {
 42475            material = newMaterials[i];
 76
 42477            if (material == null)
 78            {
 079                Debug.Log("ENTRE A UN CASO ROTO");
 080                state = State.FINISHED;
 081                return;
 82            }
 83
 42484            material.SetColor(ShaderId_LoadingColor, Color.clear);
 42485            material.SetFloat(ShaderId_FadeDirection, 0);
 42486            material.SetFloat(ShaderId_FadeThickness, fadeThickness);
 87
 42488            if (updateCulling)
 31889                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 90        }
 91
 42492        targetRenderer.sharedMaterials = newMaterials;
 42493    }
 94
 95    void UpdateCullYValueFXMaterial()
 96    {
 188997        if (cullingFXMaterials != null)
 98        {
 755699            for (int i = 0; i < cullingFXMaterials.Length; i++)
 100            {
 1889101                Material material = cullingFXMaterials[i];
 1889102                material.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 103            }
 104        }
 1889105    }
 106
 107    void UpdateCullYValueHologram()
 108    {
 2207109        if (hologramMaterialCopy != null)
 0110            hologramMaterialCopy.SetFloat(ShaderId_CullYPlane, currentCullYPlane);
 2207111    }
 112
 113    void PrepareCullingFXMaterials()
 114    {
 318115        cullingFXMaterials = new Material[finalMaterials.Length];
 1272116        for (int i = 0; i < finalMaterials.Length; i++)
 117        {
 318118            cullingFXMaterials[i] = new Material(finalMaterials[i]);
 119        }
 318120    }
 121
 122    private void Awake()
 123    {
 340124        state = State.NOT_LOADED;
 340125        time = 0;
 340126        materialReady = false;
 340127    }
 128
 129    private void Start()
 130    {
 333131        Renderer tr = targetRenderer;
 132
 333133        if (float.IsInfinity(tr.bounds.min.y) || float.IsNaN(tr.bounds.min.y))
 134        {
 0135            Destroy(this);
 0136            return;
 137        }
 138
 333139        if (float.IsInfinity(tr.bounds.max.y) || float.IsNaN(tr.bounds.max.y))
 140        {
 0141            Destroy(this);
 0142            return;
 143        }
 144
 333145        tr.enabled = false;
 146
 333147        if (useHologram)
 148        {
 0149            InitHologram();
 150        }
 151
 333152        lowerYRendererBounds = GetLowerBoundsY(tr);
 333153        topYRendererBounds = GetTopBoundsY(tr);
 333154        currentCullYPlane = lowerYRendererBounds;
 333155    }
 156
 157    void InitHologram()
 158    {
 0159        placeholder = new GameObject("Load Placeholder");
 160
 0161        placeholder.transform.SetParent(transform, false);
 0162        placeholder.transform.ResetLocalTRS();
 163
 0164        placeholderRenderer = placeholder.AddComponent<MeshRenderer>();
 0165        MeshFilter newMeshFilter = placeholder.AddComponent<MeshFilter>();
 0166        newMeshFilter.sharedMesh = GetComponent<MeshFilter>().sharedMesh;
 167
 0168        if (hologramMaterial == null)
 0169            hologramMaterial = Resources.Load("Materials/HologramMaterial") as Material;
 170
 0171        hologramMaterialCopy = new Material(hologramMaterial);
 0172        placeholderRenderer.sharedMaterials = new Material[] {hologramMaterialCopy};
 0173    }
 174
 175    private void Update()
 176    {
 2233177        if (targetRendererValue == null)
 178        {
 0179            DestroyPlaceholder();
 0180            state = State.INVALID;
 0181            return;
 182        }
 183
 2233184        switch (state)
 185        {
 186            case State.NOT_LOADED:
 187            {
 318188                currentCullYPlane += (topYRendererBounds - currentCullYPlane) * 0.1f;
 318189                currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 318190                time += Time.deltaTime;
 191
 318192                UpdateCullYValueHologram();
 193
 318194                if (materialReady && time > delay)
 195                {
 318196                    currentCullYPlane = topYRendererBounds;
 318197                    targetRendererValue.enabled = true;
 198
 318199                    PrepareCullingFXMaterials();
 318200                    PopulateTargetRendererWithMaterial(cullingFXMaterials, true);
 201
 318202                    state = State.SHOWING_LOADED;
 203                }
 204
 318205                break;
 206            }
 207
 208            case State.SHOWING_LOADED:
 209            {
 1889210                currentCullYPlane += (lowerYRendererBounds - currentCullYPlane) * 0.05f;
 1889211                currentCullYPlane = Mathf.Clamp(currentCullYPlane, lowerYRendererBounds, topYRendererBounds);
 212
 1889213                UpdateCullYValueHologram();
 1889214                UpdateCullYValueFXMaterial();
 215
 1889216                if (currentCullYPlane <= lowerYRendererBounds + 0.1f)
 217                {
 218                    // We don't update the culling value in the final material to avoid affecting the already-loaded mes
 8219                    PopulateTargetRendererWithMaterial(finalMaterials);
 220
 8221                    DestroyPlaceholder();
 8222                    state = State.FINISHED;
 223                }
 224
 8225                break;
 226            }
 227
 228            case State.FINISHED:
 229            {
 26230                onFinishedLoading?.Invoke();
 26231                Destroy(this);
 232                break;
 233            }
 234        }
 1907235    }
 236
 237    private void OnDestroy()
 238    {
 340239        DestroyPlaceholder();
 240
 340241        if (loadingMaterialCopies != null)
 242        {
 0243            for (int i = 0; i < loadingMaterialCopies.Count; i++)
 244            {
 0245                Material m = loadingMaterialCopies[i];
 0246                if (m != null)
 247                {
 0248                    Destroy(m);
 249                }
 250            }
 251        }
 340252    }
 253
 254    void DestroyPlaceholder()
 255    {
 348256        Destroy(hologramMaterialCopy);
 348257        hologramMaterialCopy = null;
 258
 348259        if (placeholder != null)
 260        {
 0261            Destroy(placeholder);
 262        }
 263
 348264        if (cullingFXMaterials != null)
 265        {
 1304266            for (int i = 0; i < cullingFXMaterials.Length; i++)
 267            {
 326268                Destroy(cullingFXMaterials[i]);
 269            }
 270        }
 348271    }
 272
 273    public void OnDidFinishLoading(Material finishMaterial)
 274    {
 365275        finalMaterials = new Material[] {finishMaterial};
 365276        materialReady = true;
 365277    }
 278
 279    public float GetLowerBoundsY(Renderer targetRenderer)
 280    {
 333281        return targetRenderer.bounds.min.y - fadeThickness;
 282    }
 283
 284    public float GetTopBoundsY(Renderer targetRenderer)
 285    {
 333286        return targetRenderer.bounds.max.y + fadeThickness;
 287    }
 288
 289    public static void ApplyToLoadedObject(GameObject meshContainer, bool useHologram = true, float fadeThickness = 20,
 290        float delay = 0)
 291    {
 0292        Renderer[] renderers = meshContainer.GetComponentsInChildren<Renderer>();
 293
 0294        for (int i = 0; i < renderers.Length; i++)
 295        {
 0296            Renderer r = renderers[i];
 297
 0298            if (r.gameObject.GetComponent<MaterialTransitionController>() != null)
 299                continue;
 300
 0301            MaterialTransitionController transition = r.gameObject.AddComponent<MaterialTransitionController>();
 0302            Material finalMaterial = r.sharedMaterial;
 0303            transition.delay = delay;
 0304            transition.useHologram = useHologram;
 0305            transition.fadeThickness = fadeThickness;
 0306            transition.OnDidFinishLoading(finalMaterial);
 307        }
 0308    }
 309    public void ForceStop()
 310    {
 22311        state = State.FINISHED;
 22312    }
 313}