Skip to content

Instantly share code, notes, and snippets.

@mandarinx
Last active October 22, 2022 00:00
Show Gist options
  • Select an option

  • Save mandarinx/b1759f08a20b0f3e68fe6f60a7dd2b3a to your computer and use it in GitHub Desktop.

Select an option

Save mandarinx/b1759f08a20b0f3e68fe6f60a7dd2b3a to your computer and use it in GitHub Desktop.

Revisions

  1. mandarinx revised this gist Feb 2, 2021. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions VisualizeSunTable.cs
    Original file line number Diff line number Diff line change
    @@ -13,11 +13,9 @@ public class VisualizeSunTable : MonoBehaviour

    private SunTable suntable;
    private LineRenderer[] lines;
    private Screenshot screenshot;

    private void OnEnable()
    {
    screenshot = new Screenshot("Screenshots", 2, 4);
    suntable = new SunTable();
    lines = new LineRenderer[31 * 12];
    for (int i = 0; i < lines.Length; ++i)
    @@ -85,7 +83,5 @@ private void Update()
    width.AddKey(new Keyframe(1f, endWidth));
    lr.widthCurve = width;
    }

    screenshot.Update(KeyCode.S);
    }
    }
  2. mandarinx created this gist Feb 2, 2021.
    91 changes: 91 additions & 0 deletions VisualizeSunTable.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    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 Screenshot screenshot;

    private void OnEnable()
    {
    screenshot = new Screenshot("Screenshots", 2, 4);
    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;
    }

    screenshot.Update(KeyCode.S);
    }
    }