wip : scenes transitions

This commit is contained in:
2026-07-17 18:30:24 +02:00
parent 6dd3db9a07
commit 50f65e99a7
23 changed files with 915 additions and 1171 deletions
@@ -1,171 +1,56 @@
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using OctoCore.Transitions;
using Unheimlich.Transitions;
namespace Unheimlich.Features.Hallucination
{
/// <summary>
/// Plays a short hallucination video using an eye blink transition on the HUD canvas.
/// Movement is handled via LeanTween.
/// Call Play() to trigger the hallucination.
/// Scene-local trigger for a double-blink hallucination sequence. Delegates all
/// visual work to the persistent OctoEyelidBlink singleton.
/// </summary>
public class HallucinationFlash : MonoBehaviour
{
[Header("Target")]
[SerializeField] private VideoPlayer _videoPlayer;
[SerializeField] private RawImage _hallucinationVideo;
[SerializeField] private Image _topEyelid;
[SerializeField] private Image _bottomEyelid;
[SerializeField] private VideoClip _clip;
[SerializeField] private Material _material;
[Header("Timings")]
[SerializeField, Min(0f)] private float _closeDuration = 0.1f;
[SerializeField, Min(0f)] private float _holdClosed = 0.05f;
[SerializeField, Min(0f)] private float _openDuration = 0.15f;
[SerializeField, Min(0f)] private float _visibleHold = 0.5f;
[Header("Video Timing")]
[SerializeField, Min(0f)] private float _holdClosed = 0.05f;
[SerializeField, Min(0f)] private float _openDuration = 0.15f;
[SerializeField, Min(0f)] private float _visibleHold = 0.5f;
[SerializeField, Range(0f, 1f)] private float _showVideoAtCloseProgress = 0.25f;
[Header("Video Playback")]
[SerializeField, Min(0f)] private float _playbackSpeed = 1f;
[SerializeField] private bool _skipOnDrop = true;
[SerializeField] private bool _logVideoDebug = false;
[Header("Positions")]
[SerializeField] private float _openOffset = 600f;
[SerializeField] private float _closedOffset = 0f;
private int _topTweenId = -1;
private int _bottomTweenId = -1;
private int _delayTweenId = -1;
private int _videoTweenId = -1;
private bool _isWarmedUp;
private bool _playWhenReady;
private bool _videoShown;
private void Awake()
{
SetHallucinationVisible(false);
SetEyelidsOpen();
ConfigureVideoPlayer();
}
private void Start()
{
StartCoroutine(WarmUpVideo());
}
/// <summary>Triggers the hallucination sequence. No-op if the singleton is missing or busy.</summary>
public void Play()
{
if (!_isWarmedUp)
OctoEyelidBlink blink = OctoEyelidBlink.Instance;
if (blink == null)
{
_playWhenReady = true;
Debug.LogWarning("[HallucinationFlash] OctoEyelidBlink singleton not found in scene.");
return;
}
PlayPreparedSequence();
}
private void ConfigureVideoPlayer()
{
if (_videoPlayer == null)
if (blink.IsBusy)
return;
_videoPlayer.playOnAwake = false;
_videoPlayer.isLooping = false;
_videoPlayer.waitForFirstFrame = true;
_videoPlayer.skipOnDrop = _skipOnDrop;
_videoPlayer.playbackSpeed = _playbackSpeed;
blink.SetClip(_clip, _material);
if (_logVideoDebug)
blink.CloseEyes(_closeDuration, () =>
{
Debug.Log(
"[HallucinationFlash] ConfigureVideoPlayer"
+ " | speed=" + _videoPlayer.playbackSpeed
+ " | canSetSpeed=" + _videoPlayer.canSetPlaybackSpeed
+ " | skipOnDrop=" + _videoPlayer.skipOnDrop
+ " | updateMode=" + _videoPlayer.timeUpdateMode
);
}
}
private IEnumerator WarmUpVideo()
{
if (_videoPlayer == null)
yield break;
SetHallucinationVisible(false);
ConfigureVideoPlayer();
_videoPlayer.Prepare();
while (!_videoPlayer.isPrepared)
yield return null;
ConfigureVideoPlayer();
// Play once while hidden to force Unity to decode the first frames.
_videoPlayer.Play();
while (!_videoPlayer.isPlaying)
yield return null;
// Wait a few frames so the RenderTexture actually receives video data.
yield return null;
yield return null;
yield return null;
_videoPlayer.Pause();
_videoPlayer.time = 0.0;
ConfigureVideoPlayer();
// Wait one frame after seeking back to the beginning.
yield return null;
_isWarmedUp = true;
if (!_playWhenReady)
yield break;
_playWhenReady = false;
PlayPreparedSequence();
}
private void PlayPreparedSequence()
{
CancelCurrent();
ResetVideoToStart();
SetHallucinationVisible(false);
SetEyelidsOpen();
_videoShown = false;
CloseEyes(() =>
{
if (!_videoShown)
ShowVideo();
HoldClosed(() =>
blink.Hold(_holdClosed, () =>
{
OpenEyes(() =>
blink.OpenEyes(_openDuration, () =>
{
HoldVisible(() =>
blink.Hold(_visibleHold, () =>
{
CloseEyes(() =>
blink.CloseEyes(_closeDuration, () =>
{
HideVideo();
HoldClosed(() =>
blink.HideVideo();
blink.Hold(_holdClosed, () =>
{
OpenEyes(() =>
{
ClearTweenIds();
});
blink.OpenEyes(_openDuration, null);
});
});
});
@@ -173,201 +58,7 @@ namespace Unheimlich.Features.Hallucination
});
});
ShowVideoDuringClose();
}
private void CloseEyes(System.Action onComplete)
{
RectTransform top = _topEyelid.rectTransform;
RectTransform bottom = _bottomEyelid.rectTransform;
_topTweenId = LeanTween.moveY(top, _closedOffset, _closeDuration)
.setEase(LeanTweenType.easeOutQuad)
.setOnComplete(() =>
{
_topTweenId = -1;
onComplete?.Invoke();
})
.id;
_bottomTweenId = LeanTween.moveY(bottom, -_closedOffset, _closeDuration)
.setEase(LeanTweenType.easeOutQuad)
.setOnComplete(() =>
{
_bottomTweenId = -1;
})
.id;
}
private void OpenEyes(System.Action onComplete)
{
RectTransform top = _topEyelid.rectTransform;
RectTransform bottom = _bottomEyelid.rectTransform;
_topTweenId = LeanTween.moveY(top, _openOffset, _openDuration)
.setEase(LeanTweenType.easeInQuad)
.setOnComplete(() =>
{
_topTweenId = -1;
onComplete?.Invoke();
})
.id;
_bottomTweenId = LeanTween.moveY(bottom, -_openOffset, _openDuration)
.setEase(LeanTweenType.easeInQuad)
.setOnComplete(() =>
{
_bottomTweenId = -1;
})
.id;
}
private void ShowVideoDuringClose()
{
float delay = _closeDuration * _showVideoAtCloseProgress;
_videoTweenId = LeanTween.delayedCall(gameObject, delay, () =>
{
_videoTweenId = -1;
ShowVideo();
}).id;
}
private void ShowVideo()
{
if (_videoShown)
return;
_videoShown = true;
SetHallucinationVisible(true);
PlayVideo();
}
private void HideVideo()
{
PauseVideoAtStart();
SetHallucinationVisible(false);
}
private void HoldClosed(System.Action onComplete)
{
_delayTweenId = LeanTween.delayedCall(gameObject, _holdClosed, () =>
{
_delayTweenId = -1;
onComplete?.Invoke();
}).id;
}
private void HoldVisible(System.Action onComplete)
{
_delayTweenId = LeanTween.delayedCall(gameObject, _visibleHold, () =>
{
_delayTweenId = -1;
onComplete?.Invoke();
}).id;
}
private void PlayVideo()
{
if (_videoPlayer == null)
return;
ConfigureVideoPlayer();
// Do not Stop() here. Stop() can reintroduce decoding latency.
_videoPlayer.Play();
if (_logVideoDebug)
{
Debug.Log(
"[HallucinationFlash] PlayVideo"
+ " | speed=" + _videoPlayer.playbackSpeed
+ " | canSetSpeed=" + _videoPlayer.canSetPlaybackSpeed
+ " | time=" + _videoPlayer.time
+ " | frame=" + _videoPlayer.frame
);
}
}
private void ResetVideoToStart()
{
if (_videoPlayer == null)
return;
_videoPlayer.Pause();
_videoPlayer.time = 0.0;
ConfigureVideoPlayer();
}
private void PauseVideoAtStart()
{
if (_videoPlayer == null)
return;
_videoPlayer.Pause();
_videoPlayer.time = 0.0;
ConfigureVideoPlayer();
}
private void SetHallucinationVisible(bool visible)
{
if (_hallucinationVideo == null)
return;
_hallucinationVideo.enabled = visible;
}
private void SetEyelidsOpen()
{
if (_topEyelid == null || _bottomEyelid == null)
return;
RectTransform top = _topEyelid.rectTransform;
RectTransform bottom = _bottomEyelid.rectTransform;
top.anchoredPosition = new Vector2(top.anchoredPosition.x, _openOffset);
bottom.anchoredPosition = new Vector2(bottom.anchoredPosition.x, -_openOffset);
}
private void CancelCurrent()
{
_playWhenReady = false;
_videoShown = false;
if (_topTweenId != -1)
{
LeanTween.cancel(_topTweenId);
_topTweenId = -1;
}
if (_bottomTweenId != -1)
{
LeanTween.cancel(_bottomTweenId);
_bottomTweenId = -1;
}
if (_delayTweenId != -1)
{
LeanTween.cancel(_delayTweenId);
_delayTweenId = -1;
}
if (_videoTweenId != -1)
{
LeanTween.cancel(_videoTweenId);
_videoTweenId = -1;
}
}
private void ClearTweenIds()
{
_topTweenId = -1;
_bottomTweenId = -1;
_delayTweenId = -1;
_videoTweenId = -1;
blink.ShowVideoAfterDelay(_closeDuration * _showVideoAtCloseProgress);
}
}
}
@@ -0,0 +1,296 @@
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
using OctoCore.Transitions;
using Unheimlich.Core;
namespace Unheimlich.Transitions
{
/// <summary>
/// Persistent singleton engine for "eyelid blink" transitions (eyelids + optional video overlay).
/// Lives outside the OctoCore package (this project already references LeanTween here), and
/// implements IOctoSceneTransition so OctoNavigation can drive it without the package ever
/// depending on LeanTween or this concrete type. Registers itself with OctoTransitionProvider
/// once ready so the package can find it at runtime.
/// </summary>
public class OctoEyelidBlink : Singleton<OctoEyelidBlink>, IOctoSceneTransition
{
[Header("Eyelids")]
[SerializeField] private Image _topEyelid;
[SerializeField] private Image _bottomEyelid;
[Header("Video")]
[Tooltip("RawImage displaying the shared RenderTexture. Its material is swapped per call.")]
[SerializeField] private RawImage _videoDisplay;
[SerializeField] private VideoPlayer _videoPlayer;
[SerializeField] private bool _skipOnDrop = true;
[SerializeField] private bool _logVideoDebug = false;
[Header("Positions")]
[SerializeField] private float _openOffset = 600f;
[SerializeField] private float _closedOffset = 0f;
/// <summary>True while a blink sequence is currently animating.</summary>
public bool IsBusy { get; private set; }
private int _topTweenId = -1;
private int _bottomTweenId = -1;
private int _delayTweenId = -1;
private int _videoTweenId = -1;
private bool _isWarmedUp;
private bool _videoShown;
private VideoClip _pendingClip;
private Material _defaultMaterial;
private bool HasVideo => _videoPlayer != null && _videoDisplay != null && _videoPlayer.clip != null;
protected override void OnAwake()
{
OctoTransitionProvider.Current = this;
// Only call DontDestroyOnLoad on a root object. If this component already lives
// under the persistent Octo prefab, the parent handles persistence and this is a no-op.
if (transform.parent == null)
DontDestroyOnLoad(gameObject);
if (_videoDisplay != null)
_defaultMaterial = _videoDisplay.material;
SetEyelidsOpen();
SetVideoVisible(false);
ConfigureVideoPlayer();
}
// Re-registers with the transition provider whenever Instance is restored
// (e.g. after a SubsystemRegistration reset with Domain Reload disabled).
protected override void OnEnabled()
{
OctoTransitionProvider.Current = this;
}
protected override void OnDestroyInstance()
{
if (OctoTransitionProvider.Current == (IOctoSceneTransition)this)
OctoTransitionProvider.Current = null;
}
// ---------- IOctoSceneTransition ----------
/// <summary>
/// Assigns the video clip and optional material to use during the next CloseEyes() call
/// and starts warming the clip up. Pass a null clip to perform a plain blink with no video.
/// A null material falls back to the RawImage's default material.
/// </summary>
public void SetClip(VideoClip clip, Material material)
{
if (_videoDisplay != null)
_videoDisplay.material = material != null ? material : _defaultMaterial;
if (_videoPlayer == null)
return;
_isWarmedUp = false;
_pendingClip = clip;
_videoPlayer.clip = clip;
if (clip != null)
StartCoroutine(WarmUpVideo());
}
/// <summary>Closes the eyelids over the given duration and invokes the callback on completion.</summary>
public void CloseEyes(float duration, Action onComplete)
{
IsBusy = true;
CancelCurrent();
ResetVideoToStart();
_videoShown = false;
RectTransform top = _topEyelid.rectTransform;
RectTransform bottom = _bottomEyelid.rectTransform;
_topTweenId = LeanTween.moveY(top, _closedOffset, duration)
.setEase(LeanTweenType.easeOutQuad)
.setOnComplete(() =>
{
_topTweenId = -1;
onComplete?.Invoke();
})
.id;
_bottomTweenId = LeanTween.moveY(bottom, -_closedOffset, duration)
.setEase(LeanTweenType.easeOutQuad)
.setOnComplete(() => _bottomTweenId = -1)
.id;
}
/// <summary>Opens the eyelids over the given duration and invokes the callback on completion.</summary>
public void OpenEyes(float duration, Action onComplete)
{
RectTransform top = _topEyelid.rectTransform;
RectTransform bottom = _bottomEyelid.rectTransform;
_topTweenId = LeanTween.moveY(top, _openOffset, duration)
.setEase(LeanTweenType.easeInQuad)
.setOnComplete(() =>
{
_topTweenId = -1;
onComplete?.Invoke();
})
.id;
_bottomTweenId = LeanTween.moveY(bottom, -_openOffset, duration)
.setEase(LeanTweenType.easeInQuad)
.setOnComplete(() =>
{
_bottomTweenId = -1;
IsBusy = false;
})
.id;
}
/// <summary>Stops playback, resets the video to its first frame, and hides the overlay.</summary>
public void HideVideo()
{
if (!HasVideo)
return;
_videoPlayer.Pause();
_videoPlayer.time = 0.0;
SetVideoVisible(false);
_videoShown = false;
}
/// <summary>Reveals and plays the video overlay after the given delay, once warm-up has completed.</summary>
public void ShowVideoAfterDelay(float delay)
{
if (!HasVideo)
return;
_videoTweenId = LeanTween.delayedCall(gameObject, delay, () =>
{
_videoTweenId = -1;
ShowVideo();
}).id;
}
// ---------- Extra API (not part of the interface, used by HallucinationFlash directly) ----------
/// <summary>Waits for the given duration before invoking the callback.</summary>
public void Hold(float duration, Action onComplete)
{
_delayTweenId = LeanTween.delayedCall(gameObject, duration, () =>
{
_delayTweenId = -1;
onComplete?.Invoke();
}).id;
}
// ---------- Video internals ----------
private void ConfigureVideoPlayer()
{
if (_videoPlayer == null)
return;
_videoPlayer.playOnAwake = false;
_videoPlayer.isLooping = false;
_videoPlayer.waitForFirstFrame = true;
_videoPlayer.skipOnDrop = _skipOnDrop;
}
/// <summary>
/// Prepares and briefly plays the assigned clip while hidden, forcing Unity to
/// decode the first frames ahead of time. Prevents a blank/garbage frame from
/// being visible the first time the video is actually shown.
/// </summary>
private IEnumerator WarmUpVideo()
{
if (!HasVideo)
yield break;
SetVideoVisible(false);
ConfigureVideoPlayer();
_videoPlayer.Prepare();
while (!_videoPlayer.isPrepared)
yield return null;
_videoPlayer.Play();
while (!_videoPlayer.isPlaying)
yield return null;
// Wait a few frames so the RenderTexture actually receives video data.
yield return null;
yield return null;
yield return null;
_videoPlayer.Pause();
_videoPlayer.time = 0.0;
// Wait one additional frame after seeking back to the start.
yield return null;
_isWarmedUp = true;
if (_logVideoDebug)
Debug.Log($"[OctoEyelidBlink] Warm-up complete for clip: {_pendingClip?.name}");
}
private void ShowVideo()
{
if (!HasVideo || _videoShown)
return;
// Safety check: if warm-up hasn't completed yet (e.g. clip assigned too late),
// skip showing the video rather than risk a corrupted first frame. The eyelid
// blink itself remains unaffected.
if (!_isWarmedUp)
{
if (_logVideoDebug)
Debug.LogWarning("[OctoEyelidBlink] Video not warmed up in time, skipping display.");
return;
}
_videoShown = true;
SetVideoVisible(true);
_videoPlayer.Play();
}
private void ResetVideoToStart()
{
if (!HasVideo)
return;
_videoPlayer.Pause();
_videoPlayer.time = 0.0;
}
private void SetVideoVisible(bool visible)
{
if (_videoDisplay != null)
_videoDisplay.enabled = visible;
}
// ---------- Eyelid internals ----------
private void SetEyelidsOpen()
{
_topEyelid.rectTransform.anchoredPosition =
new Vector2(_topEyelid.rectTransform.anchoredPosition.x, _openOffset);
_bottomEyelid.rectTransform.anchoredPosition =
new Vector2(_bottomEyelid.rectTransform.anchoredPosition.x, -_openOffset);
}
private void CancelCurrent()
{
if (_topTweenId != -1) { LeanTween.cancel(_topTweenId); _topTweenId = -1; }
if (_bottomTweenId != -1) { LeanTween.cancel(_bottomTweenId); _bottomTweenId = -1; }
if (_delayTweenId != -1) { LeanTween.cancel(_delayTweenId); _delayTweenId = -1; }
if (_videoTweenId != -1) { LeanTween.cancel(_videoTweenId); _videoTweenId = -1; }
}
}
}
@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 6a2c4b14bcd9d8a4aa612f357af4125b