Files
HCI_GazeLab/Assets/GazeLab/Scripts/Data/CentroidOpacityProfile.cs
T

117 lines
6.3 KiB
C#

using UnityEngine;
namespace GazeLab.Data
{
[CreateAssetMenu(fileName = "CentroidOpacityProfile", menuName = "GazeLab/VFX/Centroid Opacity Profile", order = 1)]
public sealed class CentroidOpacityProfile : ScriptableObject
{
[Header("Reference & Tint")]
[Tooltip("Optional reference sprite for previewing the centroid and radial falloff in the Inspector.")]
public Sprite previewSprite;
public Color tintColor = Color.white;
[Range(0.5f, 4.0f)] public float glowBoost = 1.35f;
[Header("Centroid & Radial Opacity Controls")]
[Tooltip("Normalized UV center (0..1, 0..1) inside the sliced sprite rect. (0.5, 0.5) is center; use ~(0.5, 0.15) for 180-degree arc shields.")]
public Vector2 centroidUV = new Vector2(0.5f, 0.5f);
[Tooltip("Radius around the centroid where opacity remains at Center Opacity Strength (transparent interior to reveal ships).")]
[Range(0f, 1.2f)] public float innerTransparentRadius = 0.20f;
[Tooltip("Radius where opacity reaches Rim Opacity Strength.")]
[Range(0.05f, 1.5f)] public float outerRadius = 0.48f;
[Tooltip("Soft fade-out width outside Outer Radius to prevent hard rectangular clipping.")]
[Range(0f, 0.5f)] public float outerEdgeSoftness = 0.06f;
[Tooltip("Opacity strength at the centroid interior (0 = completely see-through so ship is visible).")]
[Range(0f, 1f)] public float centerOpacityStrength = 0.0f;
[Tooltip("Opacity strength at the outer rim of the shield or shockwave.")]
[Range(0f, 1f)] public float rimOpacityStrength = 0.95f;
[Tooltip("Controls how steeply transparency transitions from the center toward the outer rim.")]
[Range(0.2f, 6.0f)] public float radialFalloffPower = 1.65f;
[Header("Dark Background Halo Removal")]
[Tooltip("Keys out dark navy/black pixels around the sprite so only the energy glow remains visible.")]
[Range(0f, 0.6f)] public float darkHaloRemovalThreshold = 0.12f;
[Range(0.01f, 0.5f)] public float darkHaloSoftness = 0.15f;
[Header("Shield Impact & Shatter Sprites (Optional)")]
public Sprite activeShieldSprite;
public Sprite hitRippleOverlaySprite;
public Sprite brokenShieldSprite;
[Header("Explosion / Shockwave Animation (Optional)")]
[Tooltip("If enabled, innerTransparentRadius expands over lifetime so static explosion sprites hollow out like a shockwave ring.")]
public bool animateHollowingOverLifetime;
[Range(0f, 1.0f)] public float shockwaveStartInnerRadius = 0.0f;
[Range(0f, 1.2f)] public float shockwaveEndInnerRadius = 0.44f;
public AnimationCurve scaleOverLifetime = AnimationCurve.EaseInOut(0f, 0.25f, 1f, 1.15f);
public AnimationCurve alphaOverLifetime = new AnimationCurve(
new Keyframe(0f, 1f),
new Keyframe(0.65f, 0.9f),
new Keyframe(1f, 0f));
[Min(0.05f)] public float effectDuration = 0.45f;
private static readonly int PropSpriteUVRect = Shader.PropertyToID("_SpriteUVRect");
private static readonly int PropCentroidUV = Shader.PropertyToID("_CentroidUV");
private static readonly int PropInnerRadius = Shader.PropertyToID("_InnerRadius");
private static readonly int PropOuterRadius = Shader.PropertyToID("_OuterRadius");
private static readonly int PropOuterFadeWidth = Shader.PropertyToID("_OuterFadeWidth");
private static readonly int PropCenterAlpha = Shader.PropertyToID("_CenterAlpha");
private static readonly int PropEdgeAlpha = Shader.PropertyToID("_EdgeAlpha");
private static readonly int PropFalloffPower = Shader.PropertyToID("_FalloffPower");
private static readonly int PropDarkKeyThreshold = Shader.PropertyToID("_DarkKeyThreshold");
private static readonly int PropDarkKeySoftness = Shader.PropertyToID("_DarkKeySoftness");
private static readonly int PropEmissionBoost = Shader.PropertyToID("_EmissionBoost");
private static readonly int PropHitPointUV = Shader.PropertyToID("_HitPointUV");
private static readonly int PropHitRippleIntensity = Shader.PropertyToID("_HitRippleIntensity");
private static readonly int PropHitRippleRadius = Shader.PropertyToID("_HitRippleRadius");
public static Vector4 CalculateSpriteUVRect(Sprite sprite)
{
if (sprite == null || sprite.texture == null)
return new Vector4(0f, 0f, 1f, 1f);
Rect r = sprite.textureRect;
float texW = Mathf.Max(1f, sprite.texture.width);
float texH = Mathf.Max(1f, sprite.texture.height);
return new Vector4(r.x / texW, r.y / texH, r.width / texW, r.height / texH);
}
public void ApplyToPropertyBlock(
MaterialPropertyBlock block,
Sprite currentSprite,
Vector2 hitLocalUV,
float hitRippleIntensity = 0f,
float normalizedLifetime = 0f)
{
if (block == null) return;
Sprite refSprite = currentSprite != null ? currentSprite : (activeShieldSprite != null ? activeShieldSprite : previewSprite);
block.SetVector(PropSpriteUVRect, CalculateSpriteUVRect(refSprite));
block.SetVector(PropCentroidUV, new Vector4(centroidUV.x, centroidUV.y, 0f, 0f));
float inner = animateHollowingOverLifetime
? Mathf.Lerp(shockwaveStartInnerRadius, shockwaveEndInnerRadius, Mathf.Clamp01(normalizedLifetime))
: innerTransparentRadius;
block.SetFloat(PropInnerRadius, inner);
block.SetFloat(PropOuterRadius, Mathf.Max(inner + 0.01f, outerRadius));
block.SetFloat(PropOuterFadeWidth, outerEdgeSoftness);
block.SetFloat(PropCenterAlpha, centerOpacityStrength);
block.SetFloat(PropEdgeAlpha, rimOpacityStrength);
block.SetFloat(PropFalloffPower, radialFalloffPower);
block.SetFloat(PropDarkKeyThreshold, darkHaloRemovalThreshold);
block.SetFloat(PropDarkKeySoftness, darkHaloSoftness);
block.SetFloat(PropEmissionBoost, glowBoost);
block.SetVector(PropHitPointUV, new Vector4(hitLocalUV.x, hitLocalUV.y, 0f, 0f));
block.SetFloat(PropHitRippleIntensity, hitRippleIntensity);
block.SetFloat(PropHitRippleRadius, 0.38f);
}
}
}