Skip to content

Instantly share code, notes, and snippets.

@Janooba
Created March 13, 2019 02:49
Show Gist options
  • Save Janooba/dc1ea25089534128d8dfae64e5a1b90d to your computer and use it in GitHub Desktop.
Save Janooba/dc1ea25089534128d8dfae64e5a1b90d to your computer and use it in GitHub Desktop.
The abstracted grapher for graphy
/* ---------------------------------------
* Author: Martin Pane (martintayx@gmail.com) (@tayx94)
* Collaborators: Lars Aalbertsen (@Rockylars)
* Project: Graphy - Ultimate Stats Monitor
* Date: 15-Dec-17
* Studio: Tayx
*
* This project is released under the MIT license.
* Attribution is not required, but it is always welcomed!
* -------------------------------------*/
using Tayx.Graphy.Graph;
using UnityEngine;
using UnityEngine.UI;
using Tayx.Graphy;
using System;
#if UNITY_5_5_OR_NEWER
using UnityEngine.Profiling;
#endif
namespace Archaic.GraphyExtensions
{
[System.Serializable]
public class GraphSettings
{
public int thickness = 2;
public Color color = Color.white;
public Image image;
public bool resize = false;
public float maxValue = 100;
public int resolution = 150;
public G_GraphShader shaderGraph;
[HideInInspector]
public float[] valueArray;
public Func<float> GetLatestValue;
}
public class G_AbstractedGraph : MonoBehaviour
{
/* ----- TODO: ----------------------------
* Add summaries to the variables.
* Add summaries to the functions.
* Check if we should add a "RequireComponent" for "RamMonitor".
* --------------------------------------*/
public Shader Shader = null;
private GraphSettings[] graphSettings;
// Initialization
public void Init(GraphSettings[] settings)
{
graphSettings = settings;
foreach (GraphSettings data in graphSettings)
{
data.shaderGraph = new G_GraphShader();
data.shaderGraph.Image = data.image;
UpdateParameters(data);
}
}
protected void UpdateParameters(GraphSettings data)
{
if (data.shaderGraph == null)
{
return;
}
data.shaderGraph.ArrayMaxSize = G_GraphShader.ArrayMaxSizeFull;
data.shaderGraph.Image.material = new Material(Shader);
data.shaderGraph.InitializeShader();
CreatePoints(data);
}
protected void CreatePoints(GraphSettings data)
{
data.shaderGraph.Array = new float[data.resolution];
data.valueArray = new float[data.resolution];
for (int i = 0; i < data.resolution; i++)
{
data.shaderGraph.Array[i] = 0;
}
// Initialize the material values
// Colors
data.shaderGraph.GoodColor = data.color;
data.shaderGraph.CautionColor = data.color;
data.shaderGraph.CriticalColor = data.color;
data.shaderGraph.UpdateColors();
// My addition
data.shaderGraph.Image.material.SetInt("_LineThickness", data.thickness);
// Thresholds
data.shaderGraph.GoodThreshold = 0;
data.shaderGraph.CautionThreshold = 0;
data.shaderGraph.UpdateThresholds();
data.shaderGraph.UpdateArray();
// Average
data.shaderGraph.Average = 0;
data.shaderGraph.UpdateAverage();
}
// Updating
public void UpdateGraph()
{
foreach (GraphSettings data in graphSettings)
{
float newData = data.GetLatestValue();
if (data.resize)
data.maxValue = 0;
for (int i = 0; i <= data.resolution - 1; i++)
{
if (i >= data.resolution - 1)
{
data.valueArray[i] = newData;
}
else
{
data.valueArray[i] = data.valueArray[i + 1];
}
if (data.resize && data.maxValue < data.valueArray[i])
{
data.maxValue = data.valueArray[i];
}
}
for (int i = 0; i <= data.resolution - 1; i++)
{
data.shaderGraph.Array[i] = data.valueArray[i] / data.maxValue;
}
data.shaderGraph.UpdatePoints();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment