Skip to content

Instantly share code, notes, and snippets.

@nothke
Created November 29, 2017 00:12
Show Gist options
  • Save nothke/094157037a61671865b0bb61bb51c5e3 to your computer and use it in GitHub Desktop.
Save nothke/094157037a61671865b0bb61bb51c5e3 to your computer and use it in GitHub Desktop.
/// FloatPlotter, plots values on screen
/// by Nothke
///
/// How to use:
///
/// 1. Add this script to your main camera
/// 2. Create a FloatPlot object like:
/// FloatPlot plot = new FloatPlot(minimumValue, maximumValue, RectOnScreen);
/// 2b. Optional: you can set plot.color and plot.backgroundColor
/// 3. Push values to it, for example every Update or FixedUpdate
/// plot.Push(yourValue);
///
/// That's it, now you should see your value being drawn on screen!
///
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloatPlot
{
public Rect rect;
public float minValue;
public float maxValue;
public const int MAX_SIZE = 100;
public Color color = Color.white;
public Color backgroundColor = Color.black * 0.5f;
public FloatPlot(float minValue, float maxValue, Rect rect)
{
this.minValue = minValue;
this.maxValue = maxValue;
this.rect = rect;
valueQueue = new Queue<float>();
FloatPlotter.e.InsertPlot(this);
}
public Queue<float> valueQueue;
public void Push(float value)
{
valueQueue.Enqueue(value);
if (valueQueue.Count > MAX_SIZE)
valueQueue.Dequeue();
}
}
public class FloatPlotter : MonoBehaviour
{
public static FloatPlotter e;
void Awake() { e = this; }
public Material lineMaterial;
public HashSet<FloatPlot> plotSet = new HashSet<FloatPlot>();
public void InsertPlot(FloatPlot plot)
{
plotSet.Add(plot);
}
void Plot(FloatPlot plot)
{
// Background
GL.Begin(GL.QUADS);
GL.Color(plot.backgroundColor);
GL.Vertex(PixelToScreen(plot.rect.xMin, plot.rect.yMin));
GL.Vertex(PixelToScreen(plot.rect.xMin, plot.rect.yMax));
GL.Vertex(PixelToScreen(plot.rect.xMax, plot.rect.yMax));
GL.Vertex(PixelToScreen(plot.rect.xMax, plot.rect.yMin));
GL.Vertex(PixelToScreen(plot.rect.xMin, plot.rect.yMin));
GL.End();
// RECT around
GL.Begin(GL.LINE_STRIP);
GL.Color(Color.black);
GL.Vertex(PixelToScreen(plot.rect.xMin, plot.rect.yMin));
GL.Vertex(PixelToScreen(plot.rect.xMin, plot.rect.yMax));
GL.Vertex(PixelToScreen(plot.rect.xMax, plot.rect.yMax));
GL.Vertex(PixelToScreen(plot.rect.xMax, plot.rect.yMin));
GL.Vertex(PixelToScreen(plot.rect.xMin, plot.rect.yMin));
GL.End();
// The plot line
GL.Begin(GL.LINE_STRIP);
GL.Color(plot.color);
int i = 0;
foreach (var value in plot.valueQueue)
{
float normValue = Mathf.InverseLerp(plot.minValue, plot.maxValue, value);
GL.Vertex(PixelToScreen(
plot.rect.x + i * (plot.rect.width / global::FloatPlot.MAX_SIZE),
plot.rect.y + normValue * plot.rect.height));
i++;
}
GL.End();
}
void OnPostRender()
{
if (!lineMaterial)
{
Debug.LogError("Please assign a line material on the inspector");
return;
}
GL.PushMatrix();
lineMaterial.SetPass(0);
GL.LoadOrtho();
//GL.Begin(GL.LINES);
GL.Color(Color.red);
foreach (var plot in plotSet)
{
Plot(plot);
}
//GL.End();
GL.PopMatrix();
}
Vector3 PixelToScreen(float x, float y)
{
return new Vector3(x / Screen.width, y / Screen.height);
}
Vector3 PixelToScreen(Vector3 pixelCoord)
{
return new Vector3(pixelCoord.x / Screen.width, pixelCoord.y / Screen.height);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment