103 lines
4.4 KiB
C#
103 lines
4.4 KiB
C#
using UnityEngine;
|
|
|
|
namespace SomniumSpace.Worlds.TheCloud
|
|
{
|
|
/// <summary>
|
|
/// Drives the UnifiedMinds/SpotlightBeam shader and (optionally) a matching Unity spot Light.
|
|
/// Every field is public/serialized, so you can keyframe it in an Animation clip, Timeline,
|
|
/// or drive it from script (e.g. a DMX bridge: set Intensity / Color / Angle / Pan / Tilt).
|
|
/// Beam mesh convention: apex at local origin, extends along +Z (see SpotlightBeamMenu).
|
|
/// </summary>
|
|
[ExecuteAlways]
|
|
[RequireComponent(typeof(MeshRenderer))]
|
|
public class SpotlightBeam : MonoBehaviour
|
|
{
|
|
[Header("Main")]
|
|
[ColorUsage(false, true)] public Color color = new Color(1f, 0.9f, 0.7f, 1f);
|
|
[Range(0f, 10f)] public float intensity = 1f;
|
|
|
|
[Header("Shape")]
|
|
[Range(1f, 120f)] public float angle = 25f; // full cone angle in degrees
|
|
[Min(0.1f)] public float length = 12f; // metres
|
|
[Range(0.1f, 8f)] public float lengthFade = 1.5f;
|
|
[Range(0.1f, 8f)] public float edgeSoftness = 2f;
|
|
|
|
[Header("Streaks")]
|
|
[Range(0f, 64f)] public float streaks = 12f;
|
|
[Range(0f, 1f)] public float streakContrast = 0.35f;
|
|
public float spin = 0.4f;
|
|
|
|
[Header("Dust")]
|
|
[Range(0f, 1f)] public float dustAmount = 0.35f;
|
|
public float dustSpeed = 0.15f;
|
|
|
|
[Header("Flicker")]
|
|
[Range(0f, 1f)] public float flicker = 0f;
|
|
public float flickerSpeed = 12f;
|
|
|
|
[Header("Optional: sync a Unity spot Light")]
|
|
public Light linkedLight;
|
|
public float lightIntensityScale = 1000f; // URP lumens-ish; tune to taste
|
|
|
|
[Header("Optional: pan / tilt (degrees, applied to this transform)")]
|
|
public bool usePanTilt = false;
|
|
[Range(-270f, 270f)] public float pan = 0f;
|
|
[Range(-135f, 135f)] public float tilt = 0f;
|
|
|
|
static readonly int ColorId = Shader.PropertyToID("_Color");
|
|
static readonly int IntensityId = Shader.PropertyToID("_Intensity");
|
|
static readonly int LengthFadeId = Shader.PropertyToID("_LengthFade");
|
|
static readonly int EdgeSoftnessId = Shader.PropertyToID("_EdgeSoftness");
|
|
static readonly int StreaksId = Shader.PropertyToID("_Streaks");
|
|
static readonly int StreakContrastId = Shader.PropertyToID("_StreakContrast");
|
|
static readonly int SpinId = Shader.PropertyToID("_Spin");
|
|
static readonly int DustAmountId = Shader.PropertyToID("_DustAmount");
|
|
static readonly int DustSpeedId = Shader.PropertyToID("_DustSpeed");
|
|
static readonly int FlickerId = Shader.PropertyToID("_Flicker");
|
|
static readonly int FlickerSpeedId = Shader.PropertyToID("_FlickerSpeed");
|
|
|
|
Renderer _renderer;
|
|
MaterialPropertyBlock _mpb;
|
|
|
|
void OnEnable() { Apply(); }
|
|
void OnValidate(){ Apply(); }
|
|
void LateUpdate(){ Apply(); } // keeps Animator/Timeline-driven values live in play and edit mode
|
|
|
|
public void Apply()
|
|
{
|
|
if (_renderer == null) _renderer = GetComponent<Renderer>();
|
|
if (_renderer == null) return;
|
|
if (_mpb == null) _mpb = new MaterialPropertyBlock();
|
|
|
|
// Size the cone: base radius from angle, z scale = length
|
|
float radius = length * Mathf.Tan(angle * 0.5f * Mathf.Deg2Rad);
|
|
transform.localScale = new Vector3(radius, radius, length);
|
|
|
|
if (usePanTilt)
|
|
transform.localRotation = Quaternion.Euler(tilt, pan, 0f);
|
|
|
|
_renderer.GetPropertyBlock(_mpb);
|
|
_mpb.SetColor(ColorId, color);
|
|
_mpb.SetFloat(IntensityId, intensity);
|
|
_mpb.SetFloat(LengthFadeId, lengthFade);
|
|
_mpb.SetFloat(EdgeSoftnessId, edgeSoftness);
|
|
_mpb.SetFloat(StreaksId, streaks);
|
|
_mpb.SetFloat(StreakContrastId, streakContrast);
|
|
_mpb.SetFloat(SpinId, spin);
|
|
_mpb.SetFloat(DustAmountId, dustAmount);
|
|
_mpb.SetFloat(DustSpeedId, dustSpeed);
|
|
_mpb.SetFloat(FlickerId, flicker);
|
|
_mpb.SetFloat(FlickerSpeedId, flickerSpeed);
|
|
_renderer.SetPropertyBlock(_mpb);
|
|
|
|
if (linkedLight != null)
|
|
{
|
|
linkedLight.color = color;
|
|
linkedLight.intensity = intensity * lightIntensityScale;
|
|
linkedLight.spotAngle = angle;
|
|
linkedLight.range = length;
|
|
}
|
|
}
|
|
}
|
|
}
|