Add opacity curve to rings

This commit is contained in:
Noah Pilarski 2022-08-23 13:19:20 -04:00
parent 93a26e6341
commit a84c243f7a
4 changed files with 55 additions and 1 deletions

Binary file not shown.

View File

@ -133,6 +133,13 @@ namespace NewHorizons.Builder.Body
levelController.SetScaleCurve(ring.curve);
}
if (ring.opacity != null)
{
var ringOC = ringGO.AddComponent<RingOpacityController>();
ringOC.SetOpacityCurve(ring.opacity);
ringOC.SetMeshRenderer(ringMR);
}
return ringGO;
}

View File

@ -0,0 +1,42 @@
using NewHorizons.External.Modules.VariableSize;
using UnityEngine;
namespace NewHorizons.Components
{
public class RingOpacityController : MonoBehaviour
{
private static readonly int Alpha = Shader.PropertyToID("_Alpha");
public AnimationCurve opacityCurve { get; protected set; }
public float CurrentOpacity { get; protected set; }
private MeshRenderer _meshRenderer;
protected void FixedUpdate()
{
if (opacityCurve != null)
{
CurrentOpacity = opacityCurve.Evaluate(TimeLoop.GetMinutesElapsed());
}
else
{
CurrentOpacity = 1;
}
if (_meshRenderer == null) return;
_meshRenderer.material.SetFloat(Alpha, CurrentOpacity);
}
public void SetOpacityCurve(VariableSizeModule.TimeValuePair[] curve)
{
opacityCurve = new AnimationCurve();
foreach (var pair in curve)
{
opacityCurve.AddKey(new Keyframe(pair.time, pair.value));
}
}
public void SetMeshRenderer(MeshRenderer meshRenderer) => _meshRenderer = meshRenderer;
}
}

View File

@ -1,4 +1,4 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
namespace NewHorizons.External.Modules.VariableSize
@ -45,5 +45,10 @@ namespace NewHorizons.External.Modules.VariableSize
/// Should this ring be unlit?
/// </summary>
public bool unlit;
/// <summary>
/// Fade this module over time
/// </summary>
public TimeValuePair[] opacity;
}
}