Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Last active October 22, 2022 00:00
Show Gist options
  • Save mandarinx/b1759f08a20b0f3e68fe6f60a7dd2b3a to your computer and use it in GitHub Desktop.
Save mandarinx/b1759f08a20b0f3e68fe6f60a7dd2b3a to your computer and use it in GitHub Desktop.
Visualize SunTable
using Splines.Habrador;
using Telia.Core;
using Telia.Environment;
using UnityEngine;
public class VisualizeSunTable : MonoBehaviour
{
[Min(1f)] public float length;
public Material lineMat;
[Range(0f, 1f)] public float endWidth;
[Range(0f, 1f)] public float midWidth;
private SunTable suntable;
private LineRenderer[] lines;
private void OnEnable()
{
suntable = new SunTable();
lines = new LineRenderer[31 * 12];
for (int i = 0; i < lines.Length; ++i)
{
GameObject l = new GameObject($"Day {(i + 1)}", typeof(LineRenderer));
l.transform.position = new Vector3(i / (12f * 31f) * length, 0f, 0f);
lines[i] = l.GetComponent<LineRenderer>();
lines[i].useWorldSpace = false;
lines[i].alignment = LineAlignment.TransformZ;
lines[i].material = lineMat;
}
}
private void Update()
{
if (suntable == null)
{
return;
}
for (int day = 0; day < (31 * 12); ++day)
{
float dayF = day / (31f * 12f);
int month = (Mathf.FloorToInt(day / 31f)) + 1;
int dayOfMonth = (day % 31) + 1;
Sun sun = suntable.Get(month, dayOfMonth);
if (sun == null)
{
Debug.LogError($"Could not find sun data for month: {month}, day: {dayOfMonth}");
return;
}
LineRenderer lr = lines[day];
AnimationCurve width = new AnimationCurve();
width.AddKey(new Keyframe(0f, endWidth));
float left = -12f;
float sunRiseStart = left + sun.sunRiseStart * 24f;
float sunRise = left + sun.sunRise * 24f;
float sunSetStart = left + sun.sunSetStart * 24f;
float sunSet = left + sun.sunSet * 24f;
float right = 12f;
float z = (day / (12f * 31f)) * length;
int pos = 0;
lr.positionCount = 26;
lr.SetPosition(pos++, new Vector3(0f, left, 0f));
Vector3 posA = new Vector3(0f, sunRiseStart, 0f);
Vector3 posB = new Vector3(0f, sunSet, 0f);
Vector3 handlePosA = new Vector3(0f, sunRise, -2f);
Vector3 handlePosB = new Vector3(0f, sunSetStart, -2f);
for (int s = 0; s < 24; ++s)
{
lr.SetPosition(pos++, BezierCubic.GetPosition(posA, posB, handlePosA, handlePosB, s / 24f));
if (s == 11)
{
width.AddKey(new Keyframe(0.5f, midWidth));
}
}
lr.SetPosition(pos++, new Vector3(0f, right, 0f));
width.AddKey(new Keyframe(1f, endWidth));
lr.widthCurve = width;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment