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

84 lines
2.8 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;
private Quaternion _headOriginRot;
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);
_headOriginRot = _references.Head.transform.localRotation;
//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;
//lookDir = transform.TransformDirection(lookDir);
Vector3 up = Vector3.up;
//Quaternion targetRot = Quaternion.LookRotation(lookDir, up);
Quaternion targetRot = Quaternion.Euler(0, 0, 0);
targetRot *= Quaternion.Inverse(transform.parent.rotation);
//targetRot *= _headOriginRot;
//targetRot = Quaternion.Lerp(_headOriginRot, 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;
}
}