Files
Ipseity-Demo/Assets/_Content/Scripts/Features/TvHead/TvHead.cs
T
2026-06-05 22:53:25 +02:00

78 lines
2.6 KiB
C#

using UnityEngine;
using static UnityEngine.Rendering.DebugUI;
public class TvHead : MonoBehaviour
{
[System.Serializable]
public class References
{
public Renderer Head;
public Renderer Ray;
}
[System.Serializable]
public class Settings
{
public float OffMin = 5f;
public float OffMax = 9f;
public float OnMin = 4f;
public float OnMax = 8f;
}
[System.Serializable]
public class State
{
}
[SerializeField] private References _references;
[SerializeField] private Settings _settings;
[SerializeField] private State _state;
private float _value;
private float _seed;
private float _offDuration;
private float _onDuration;
private Vector4 _headColor;
private Vector4 _screenColor;
const string ColorAttribute = "_Color";
void Awake()
{
_seed = transform.GetSiblingIndex();
_offDuration = Mathf.Lerp(_settings.OffMin, _settings.OffMax, (_seed * 6.72f) % 1);
_onDuration = Mathf.Lerp(_settings.OnMin, _settings.OnMax, (_seed * 4.56f) % 1);
_headColor = _references.Ray.material.GetColor(ColorAttribute);
_screenColor = _references.Head.materials[1].GetColor(ColorAttribute);
//Off();
}
void Update()
{
float t = Time.timeSinceLevelLoad + _seed * 13.23f;
float totalDuration = _offDuration + _onDuration;
float targetValue = (t / totalDuration) % 1;
targetValue = targetValue * totalDuration < _offDuration ? 0 : 1;
_value = Mathf.Lerp(_value, targetValue, Time.deltaTime * 10);
float noisedValue = _value + Mathf.Cos(Time.timeSinceLevelLoad * 50) * (1 - Mathf.Abs(_value * 2 - 1));
noisedValue = Mathf.Clamp01(noisedValue);
Vector3 lookDir = (Camera.main.transform.position - _references.Head.transform.position).normalized;
Quaternion targetRot = Quaternion.Inverse(_references.Head.transform.parent.rotation);
targetRot *= Quaternion.LookRotation(lookDir, Vector3.up);
targetRot *= _references.Head.transform.parent.localRotation;
targetRot = Quaternion.Lerp(Quaternion.Euler(0, 0, 0), targetRot, Mathf.Lerp(_value, noisedValue, .5f));
Color headColor = Color.Lerp(Color.black, _headColor , noisedValue);
Color screenColor = Color.Lerp(Color.black, _screenColor, noisedValue);
_references.Ray.material.SetColor(ColorAttribute, headColor);
_references.Head.materials[1].SetColor(ColorAttribute, screenColor);
_references.Head.transform.localRotation = targetRot;
}
}