< Summary

Class:CharacterPreviewController
Assembly:AvatarEditorHUD
File(s):/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/CharacterPreviewController.cs
Covered lines:10
Uncovered lines:53
Coverable lines:63
Total lines:168
Line coverage:15.8% (10 of 63)
Covered branches:0
Total branches:0

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Awake()0%110100%
UpdateModel(...)0%110100%
OnDestroy()0%110100%
UpdateModelRoutine()0%440100%
TakeSnapshots(...)0%12300%
TakeSnapshots_Routine()0%30500%
Snapshot(...)0%2100%
SetFocus(...)0%2100%
SetFocus(...)0%12300%
CameraTransition()0%20400%
Rotate(...)0%2100%

File(s)

/tmp/workspace/unity-renderer/unity-renderer/Assets/Scripts/MainScripts/DCL/Controllers/HUD/AvatarEditorHUD/Scripts/CharacterPreviewController.cs

#LineLine coverage
 1using DCL;
 2using System;
 3using System.Collections;
 4using System.Collections.Generic;
 5using UnityEngine;
 6
 7public class CharacterPreviewController : MonoBehaviour
 8{
 9    private const int SNAPSHOT_BODY_WIDTH_RES = 256;
 10    private const int SNAPSHOT_BODY_HEIGHT_RES = 512;
 11
 12    private const int SNAPSHOT_FACE_WIDTH_RES = 512;
 13    private const int SNAPSHOT_FACE_HEIGHT_RES = 512;
 14
 15    private const int SNAPSHOT_FACE_256_WIDTH_RES = 256;
 16    private const int SNAPSHOT_FACE_256_HEIGHT_RES = 256;
 17
 18    private const int SNAPSHOT_FACE_128_WIDTH_RES = 128;
 19    private const int SNAPSHOT_FACE_128_HEIGHT_RES = 128;
 20
 21    private const int SUPERSAMPLING = 1;
 22    private const float CAMERA_TRANSITION_TIME = 0.5f;
 23
 24    public delegate void OnSnapshotsReady(Texture2D face, Texture2D face128, Texture2D face256, Texture2D body);
 25
 26    public enum CameraFocus
 27    {
 28        DefaultEditing,
 29        FaceEditing,
 30        FaceSnapshot,
 31        BodySnapshot
 32    }
 33
 34    private System.Collections.Generic.Dictionary<CameraFocus, Transform> cameraFocusLookUp;
 35
 36    public new Camera camera;
 37    public AvatarRenderer avatarRenderer;
 38
 39    public Transform defaultEditingTemplate;
 40    public Transform faceEditingTemplate;
 41
 42    public Transform faceSnapshotTemplate;
 43    public Transform bodySnapshotTemplate;
 44
 45    private Coroutine updateModelRoutine;
 46
 47    private bool avatarLoadFailed = false;
 48
 49    private void Awake()
 50    {
 4451        cameraFocusLookUp = new Dictionary<CameraFocus, Transform>()
 52        {
 53            { CameraFocus.DefaultEditing, defaultEditingTemplate },
 54            { CameraFocus.FaceEditing, faceEditingTemplate },
 55            { CameraFocus.FaceSnapshot, faceSnapshotTemplate },
 56            { CameraFocus.BodySnapshot, bodySnapshotTemplate },
 57        };
 4458    }
 59
 23460    public void UpdateModel(AvatarModel newModel, Action onDone) { updateModelRoutine = CoroutineStarter.Start(UpdateMod
 61
 8862    private void OnDestroy() { CoroutineStarter.Stop(updateModelRoutine); }
 63
 64    private IEnumerator UpdateModelRoutine(AvatarModel newModel, Action onDone)
 65    {
 11766        bool avatarDone = false;
 11767        avatarLoadFailed = false;
 68
 16069        avatarRenderer.ApplyModel(newModel, () => avatarDone = true, () => avatarLoadFailed = true);
 70
 1814171        yield return new DCL.WaitUntil(() => avatarDone || avatarLoadFailed);
 2072        onDone?.Invoke();
 2073    }
 74
 75    public void TakeSnapshots(OnSnapshotsReady onSuccess, Action onFailed)
 76    {
 077        if (avatarLoadFailed)
 78        {
 079            onFailed?.Invoke();
 080            return;
 81        }
 82
 083        StartCoroutine(TakeSnapshots_Routine(onSuccess));
 084    }
 85
 86    private IEnumerator TakeSnapshots_Routine(OnSnapshotsReady callback)
 87    {
 088        DCL.Environment.i.platform.cullingController.Stop();
 89
 090        var current = camera.targetTexture;
 091        camera.targetTexture = null;
 092        var avatarAnimator = avatarRenderer.gameObject.GetComponent<AvatarAnimatorLegacy>();
 93
 094        SetFocus(CameraFocus.FaceSnapshot, false);
 095        avatarAnimator.Reset();
 096        yield return null;
 097        Texture2D face = Snapshot(SNAPSHOT_FACE_WIDTH_RES, SNAPSHOT_FACE_HEIGHT_RES);
 098        Texture2D face128 = Snapshot(SNAPSHOT_FACE_128_WIDTH_RES, SNAPSHOT_FACE_128_HEIGHT_RES);
 099        Texture2D face256 = Snapshot(SNAPSHOT_FACE_256_WIDTH_RES, SNAPSHOT_FACE_256_HEIGHT_RES);
 100
 0101        SetFocus(CameraFocus.BodySnapshot, false);
 0102        avatarAnimator.Reset();
 0103        yield return null;
 0104        Texture2D body = Snapshot(SNAPSHOT_BODY_WIDTH_RES, SNAPSHOT_BODY_HEIGHT_RES);
 105
 0106        SetFocus(CameraFocus.DefaultEditing, false);
 107
 0108        camera.targetTexture = current;
 109
 0110        DCL.Environment.i.platform.cullingController.Start();
 0111        callback?.Invoke(face, face128, face256, body);
 0112    }
 113
 114    private Texture2D Snapshot(int width, int height)
 115    {
 0116        RenderTexture rt = new RenderTexture(width * SUPERSAMPLING, height * SUPERSAMPLING, 32);
 0117        camera.targetTexture = rt;
 0118        Texture2D screenShot = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, false);
 0119        camera.Render();
 0120        RenderTexture.active = rt;
 0121        screenShot.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
 0122        screenShot.Apply();
 123
 0124        return screenShot;
 125    }
 126
 127    private Coroutine cameraTransitionCoroutine;
 128
 0129    public void SetFocus(CameraFocus focus, bool useTransition = true) { SetFocus(cameraFocusLookUp[focus], useTransitio
 130
 131    private void SetFocus(Transform transform, bool useTransition = true)
 132    {
 0133        if (cameraTransitionCoroutine != null)
 134        {
 0135            StopCoroutine(cameraTransitionCoroutine);
 136        }
 137
 0138        if (useTransition)
 139        {
 0140            cameraTransitionCoroutine = StartCoroutine(CameraTransition(camera.transform.position, transform.position, c
 0141        }
 142        else
 143        {
 0144            var cameraTransform = camera.transform;
 0145            cameraTransform.position = transform.position;
 0146            cameraTransform.rotation = transform.rotation;
 147        }
 0148    }
 149
 150    private IEnumerator CameraTransition(Vector3 initPos, Vector3 endPos, Quaternion initRotation, Quaternion endRotatio
 151    {
 0152        var cameraTransform = camera.transform;
 0153        float currentTime = 0;
 154
 0155        float inverseTime = 1 / time;
 0156        while (currentTime < time)
 157        {
 0158            currentTime = Mathf.Clamp(currentTime + Time.deltaTime, 0, time);
 0159            cameraTransform.position = Vector3.Lerp(initPos, endPos, currentTime * inverseTime);
 0160            cameraTransform.rotation = Quaternion.Lerp(initRotation, endRotation, currentTime * inverseTime);
 0161            yield return null;
 162        }
 163
 0164        cameraTransitionCoroutine = null;
 0165    }
 166
 0167    public void Rotate(float rotationVelocity) { avatarRenderer.transform.Rotate(Time.deltaTime * rotationVelocity * Vec
 168}