Tanoda
DiscontinuityCalculator.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.Collections.Generic;
11using UnityEngine;
12using UnityEngine.Playables;
13using UnityEngine.Timeline;
14
15namespace Leap.Unity.Recording {
16 using Attributes;
17 using RuntimeGizmos;
18
19 [RequireComponent(typeof(PlayableDirector))]
20 public class DiscontinuityCalculator : MonoBehaviour {
21
22 public List<string> tracksToWatch = new List<string>();
23
24 public bool drawGizmoOnDiscontinuity = true;
25
26 [DisableIf("drawGizmoOnDiscontinuity", isEqualTo: false)]
27 public Transform gizmoLocation;
28
29 public Action<bool> OnUpdate;
30
31 private PlayableDirector _director;
32 private List<TimelineClip> _clipsToWatch = new List<TimelineClip>();
33
34 private PlayableAsset _prevAsset;
35 private double _prevTime;
36 private PlayState _prevPlayState;
37
38 private bool _willBeDiscontinuousNextFrame = false;
39
40 private float _gizmoRadius = 0;
41
42 private void Awake() {
43 _director = GetComponent<PlayableDirector>();
44 _prevPlayState = _director.state;
45 }
46
47 private void LateUpdate() {
48 bool isDiscontinuity = false;
49
50 //Check if the previous frame said we were going to be discontinuous
51 if (_willBeDiscontinuousNextFrame) {
52 isDiscontinuity = true;
53 _willBeDiscontinuousNextFrame = false;
54 }
55
56 //If we switched to a different asset
57 if (_director.playableAsset != _prevAsset) {
58 isDiscontinuity = true;
59 recalculateClips();
60 }
61
62 //If we moved back in time
63 if (_director.time < _prevTime) {
64 isDiscontinuity = true;
65 _willBeDiscontinuousNextFrame = true;
66 }
67
68 //Or if we moved forward in time more than we should have
69 if (_director.time - _prevTime > Time.deltaTime * 3) {
70 isDiscontinuity = true;
71 }
72
73 //Or if the current state is different from the previous state
74 if (_director.state != _prevPlayState) {
75 isDiscontinuity = true;
76 _willBeDiscontinuousNextFrame = true;
77 }
78
79 //Or if we entered or left any clips that we are watching
80 foreach (var clip in _clipsToWatch) {
81 if (didEnterOrLeaveClip(clip)) {
82 isDiscontinuity = true;
83 break;
84 }
85 }
86
88 if (isDiscontinuity) {
89 _gizmoRadius += 0.1f;
90 } else {
91 _gizmoRadius *= 0.95f;
92 }
93
94 RuntimeGizmoDrawer drawer;
95 if (RuntimeGizmoManager.TryGetGizmoDrawer(out drawer)) {
96 drawer.color = Color.red;
97 drawer.DrawSphere(gizmoLocation.position, _gizmoRadius);
98 }
99 }
100
101 if (OnUpdate != null) {
102 OnUpdate(isDiscontinuity);
103 }
104
105 _prevTime = _director.time;
106 _prevAsset = _director.playableAsset;
107 _prevPlayState = _director.state;
108 }
109
110 private void recalculateClips() {
111 _clipsToWatch.Clear();
112
113 var timeline = _director.playableAsset as TimelineAsset;
114 if (timeline == null) {
115 return;
116 }
117
118 for (int i = 0; i < timeline.outputTrackCount; i++) {
119 var track = timeline.GetOutputTrack(i);
120 Debug.Log(track.name);
121 if (tracksToWatch.Contains(track.name)) {
122 _clipsToWatch.AddRange(track.GetClips());
123 }
124 }
125 }
126
127 private bool didEnterOrLeaveClip(TimelineClip clip) {
128 bool wasInside = _prevTime > clip.start && _prevTime < clip.end;
129 bool isInside = _director.time > clip.start && _director.time < clip.end;
130 return wasInside != isInside;
131 }
132 }
133}
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
UnityEngine.Color Color
Definition: TestScript.cs:32