1
0
mirror of synced 2024-12-18 17:25:54 +01:00
WACVR/Assets/Script/LightManager/LightManager.cs

117 lines
3.5 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
2022-07-29 18:49:56 +02:00
using System.IO.MemoryMappedFiles;
using System.Security.Principal;
using UnityEngine;
public class LightManager : MonoBehaviour
{
public List<GameObject> Lights = new List<GameObject>();
2022-07-29 18:49:56 +02:00
List<Material> Materials = new List<Material>();
[SerializeField]
2022-10-05 22:47:49 +02:00
private bool isIPCIdle = true;
[SerializeField]
2022-10-05 22:47:49 +02:00
private bool useIPCLighting = true;
2022-07-29 18:49:56 +02:00
static Texture2D RGBColor2D;
private IEnumerator[] coroutines = new IEnumerator[240];
2022-07-29 18:49:56 +02:00
public float FadeDuration = 0.5f;
private void Start()
{
2022-10-03 02:04:54 +02:00
ConfigManager.EnsureInitialization();
ConfigManager.onConfigChanged += UpdateConfig;
UpdateConfig();
2022-07-29 18:49:56 +02:00
for (int i = 0; i < Lights.Count; i++)
2022-07-29 18:49:56 +02:00
Materials.Add(Lights[i].GetComponent<Renderer>().material);
if (useIPCLighting)
{
2022-07-29 18:49:56 +02:00
RGBColor2D = new Texture2D(480, 1, TextureFormat.RGBA32, false);
//RGBColor2D.filterMode = FilterMode.Point; //for debugging
//GetComponent<Renderer>().material.mainTexture = RGBColor2D; //for debugging
}
}
2022-07-29 18:49:56 +02:00
private void Update()
{
if (!useIPCLighting)
2022-10-05 22:47:49 +02:00
{
isIPCIdle = true;
2022-10-03 02:04:54 +02:00
return;
2022-10-05 22:47:49 +02:00
}
if (IPCManager.sharedBuffer != null)
{
GetTextureFromBytes(IPCManager.GetLightData());
CheckIPCState();
if (!isIPCIdle)
UpdateLED();
}
else
{
isIPCIdle = true;
}
2022-07-29 18:49:56 +02:00
}
2022-10-03 02:04:54 +02:00
void UpdateConfig()
{
useIPCLighting = ConfigManager.config.useIPCLighting;
2022-10-03 02:04:54 +02:00
}
private void CheckIPCState()
{
2022-10-03 02:04:54 +02:00
if (RGBColor2D.GetPixel(0 , 0).a == 1)
isIPCIdle = false;
else
2022-10-03 02:04:54 +02:00
isIPCIdle = true;
}
2022-07-29 18:49:56 +02:00
private void UpdateLED()
{
2022-07-29 18:49:56 +02:00
int index = 0;
for (int i = 0; i < 30; i++)
{
for (int ii = 0; ii < 4; ii++)
{
Materials[119 - i - ii * 30].SetColor("_EmissionColor", RGBColor2D.GetPixel(index * 2, 0));
2022-07-31 22:58:57 +02:00
Materials[119 - i - ii * 30].SetColor("_EmissionColor2", RGBColor2D.GetPixel(index * 2 + 1, 0));
2022-07-29 18:49:56 +02:00
Materials[210 + i - ii * 30].SetColor("_EmissionColor", RGBColor2D.GetPixel((index + 120) * 2, 0));
2022-07-31 22:58:57 +02:00
Materials[210 + i - ii * 30].SetColor("_EmissionColor2", RGBColor2D.GetPixel((index + 120) * 2 + 1, 0));
2022-07-29 18:49:56 +02:00
index++;
}
}
}
void GetTextureFromBytes(byte[] bytes)
{
RGBColor2D.LoadRawTextureData(bytes);
RGBColor2D.Apply();
}
2022-10-05 22:47:49 +02:00
public void UpdateFadeLight(int Area, bool State)
2022-07-29 18:49:56 +02:00
{
2022-10-05 22:47:49 +02:00
if(!isIPCIdle | useIPCLighting)
2022-07-29 18:49:56 +02:00
return;
Area -= 1;
if (State)
{
Materials[Area].SetColor("_EmissionColor", new Color(1f, 1f, 1f, 1f));
Materials[Area].SetColor("_EmissionColor2", new Color(1f, 1f, 1f, 1f));
}
else
{
if (coroutines[Area] != null)
StopCoroutine(coroutines[Area]);
coroutines[Area] = FadeOut(Area, Materials[Area]);
StartCoroutine(coroutines[Area]);
}
}
public IEnumerator FadeOut(int Area, Material mat)
{
for (float time = 0f; time < FadeDuration; time += Time.deltaTime)
{
float p = 1 - time / FadeDuration;
mat.SetColor("_EmissionColor", new Color(p, p, p, 1f));
mat.SetColor("_EmissionColor2", new Color(p, p, p, 1f));
yield return null;
}
}
}