GruppeC/frontend/SmartLectureRoom/Assets/SmartRoom/Scripts/Simulation/NoiseVisualizer.cs

143 lines
6.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using UnityEngine;
namespace SmartRoom
{
/// <summary>
/// Macht den Außenlärm (Lautstärkesensor MAX9814 bzw. Simulation) im 3D-Raum sichtbar: Schallwellen-Ringe
/// vor der Fensterfront, je lauter, desto mehr, größer und schneller. Über dem Grenzwert (Standard 70 dB) werden
/// sie rot – dann gilt das Sensor-Veto: Fenster gesperrt, Lüftungsanlage statt Fenster (außer die Mehrheit will lüften). Ein Schild an der Fassade zeigt den Pegel.
/// </summary>
public class NoiseVisualizer : MonoBehaviour
{
public RoomEnvironment env;
public ClimateController controller;
public ArduinoBridge bridge;
[Tooltip("Unterhalb dieses Pegels keine Wellen (dB)")] public float quietDb = 55f;
static readonly Color Calm = new Color(0.55f, 0.72f, 0.64f, 0.55f);
static readonly Color Loud = new Color(0.66f, 0.25f, 0.21f, 0.8f);
ParticleSystem waves;
TextMesh label;
Material ringMat;
Texture2D ringTex;
void Start()
{
// Wellen kommen von der Straße links vor der Fensterfront (x < 0) und laufen auf den Raum zu
var go = new GameObject("Schallwellen");
go.transform.SetParent(transform, false);
go.transform.localPosition = new Vector3(-RoomBuilder.W / 2f - 2.2f, 1.7f, 0f);
waves = go.AddComponent<ParticleSystem>();
waves.Stop(true, ParticleSystemStopBehavior.StopEmittingAndClear);
var main = waves.main;
main.loop = true;
main.startLifetime = 2.2f;
main.startSpeed = 0f;
main.startSize = 0.4f;
main.maxParticles = 80;
main.simulationSpace = ParticleSystemSimulationSpace.World;
main.playOnAwake = true;
var shape = waves.shape;
shape.shapeType = ParticleSystemShapeType.Box;
shape.scale = new Vector3(0.2f, 1.2f, RoomBuilder.D * 0.8f);
var vel = waves.velocityOverLifetime;
vel.enabled = true;
vel.space = ParticleSystemSimulationSpace.World;
vel.x = new ParticleSystem.MinMaxCurve(0.6f);
vel.y = new ParticleSystem.MinMaxCurve(0f);
vel.z = new ParticleSystem.MinMaxCurve(0f);
var size = waves.sizeOverLifetime;
size.enabled = true;
size.size = new ParticleSystem.MinMaxCurve(1f, AnimationCurve.Linear(0f, 0.3f, 1f, 3.2f));
var col = waves.colorOverLifetime;
col.enabled = true;
var g = new Gradient();
g.SetKeys(new[] { new GradientColorKey(Color.white, 0f), new GradientColorKey(Color.white, 1f) },
new[] { new GradientAlphaKey(0f, 0f), new GradientAlphaKey(1f, 0.15f), new GradientAlphaKey(0f, 1f) });
col.color = g;
ringTex = RingTexture(64);
ringMat = new Material(Shader.Find("Sprites/Default")) { mainTexture = ringTex };
var r = go.GetComponent<ParticleSystemRenderer>();
r.material = ringMat;
r.renderMode = ParticleSystemRenderMode.Billboard;
r.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
r.receiveShadows = false;
var emission = waves.emission;
emission.rateOverTime = 0f;
waves.Play();
// Pegelanzeige an der Fassade (von innen durch die Fenster lesbar)
var sign = new GameObject("Pegelanzeige");
sign.transform.SetParent(transform, false);
sign.transform.localPosition = new Vector3(-RoomBuilder.W / 2f - 0.6f, RoomBuilder.WinTopY + 0.32f, 0f);
sign.transform.localRotation = Quaternion.Euler(0f, -90f, 0f);
label = sign.AddComponent<TextMesh>();
label.anchor = TextAnchor.MiddleCenter;
label.alignment = TextAlignment.Center;
label.characterSize = 0.05f;
label.fontSize = 64;
var font = Prim.BuiltinFont();
if (font != null)
{
label.font = font;
sign.GetComponent<MeshRenderer>().sharedMaterial = font.material;
}
}
void Update()
{
if (env == null || waves == null) return;
float db = env.outdoorNoise;
float limit = bridge != null && bridge.CentralActive ? bridge.Central.grenzwerte.laermDb
: controller != null ? controller.limits.noiseLimit : 70f;
bool veto = db > limit;
float k = Mathf.InverseLerp(quietDb, 95f, db); // 0 = ruhig, 1 = sehr laut
var emission = waves.emission;
emission.rateOverTime = db <= quietDb ? 0f : Mathf.Lerp(2f, 22f, k);
var main = waves.main;
main.startColor = veto ? Loud : Color.Lerp(Calm, new Color(0.85f, 0.6f, 0.25f, 0.65f), k);
main.startSize = Mathf.Lerp(0.35f, 0.9f, k);
var vel = waves.velocityOverLifetime;
vel.x = new ParticleSystem.MinMaxCurve(Mathf.Lerp(0.4f, 1.6f, k));
if (label != null)
{
bool sensor = env.IsMeasured("nout");
label.text = $"Außenlärm {db:0} dB{(sensor ? " · Sensor" : "")}{(veto ? "\nFenster gesperrt" : "")}";
label.color = veto ? new Color(0.75f, 0.2f, 0.15f) : new Color(0.25f, 0.33f, 0.3f);
}
}
static Texture2D RingTexture(int n)
{
var t = new Texture2D(n, n, TextureFormat.RGBA32, false) { wrapMode = TextureWrapMode.Clamp, hideFlags = HideFlags.HideAndDontSave };
var px = new Color[n * n];
for (int y = 0; y < n; y++)
for (int x = 0; x < n; x++)
{
float d = Vector2.Distance(new Vector2(x + 0.5f, y + 0.5f), new Vector2(n / 2f, n / 2f)) / (n / 2f);
float a = Mathf.Clamp01(1f - Mathf.Abs(d - 0.82f) / 0.12f); // Ring bei 82 % des Radius
px[y * n + x] = new Color(1f, 1f, 1f, a * a);
}
t.SetPixels(px);
t.Apply();
return t;
}
void OnDestroy()
{
if (ringMat != null) Destroy(ringMat);
if (ringTex != null) Destroy(ringTex);
}
}
}