Skip to content

Instantly share code, notes, and snippets.

@asus4
Last active December 11, 2015 10:08
Show Gist options
  • Save asus4/4584069 to your computer and use it in GitHub Desktop.
Save asus4/4584069 to your computer and use it in GitHub Desktop.
Unity用デバッググラフ(3軸)を描画するクラス。
using UnityEngine;
using System.Collections.Generic;
/// <summary>
/// Debug grapher.
/// </summary>
[RequireComponent (typeof(Camera))]
public class DebugGrapher : MonoBehaviour
{
#region Privates
List<Vector3> values;
Transform cameraPositon;
#endregion
#region Props
[SerializeField]
int maxValues;
[SerializeField]
Vector2 scale;
[SerializeField]
Vector3 position;
#endregion
#region Lifecyecles
void Start ()
{
values = new List<Vector3> ();
this.cameraPositon = transform;
}
void OnPostRender ()
{
CreateLineMaterial ();
// set the current material
lineMaterial.SetPass (0);
float num = values.Count;
if (num < 2) {
return;
}
Vector3 pos = cameraPositon.position + position;
GL.Begin (GL.LINES);
// draw x
GL.Color (Color.red);
for (int i=1; i<num; ++i) {
GL.Vertex (pos + new Vector3 (((i-1) / num - 0.5f) * scale.x, values [i-1].x * scale.y, 10));
GL.Vertex (pos + new Vector3 ((i / num - 0.5f) * scale.x, values [i].x * scale.y, 10));
}
// draw y
GL.Color (Color.green);
for (int i=1; i<num; ++i) {
GL.Vertex (pos + new Vector3 (((i-1) / num - 0.5f) * scale.x, values [i-1].y * scale.y, 10));
GL.Vertex (pos + new Vector3 ((i / num - 0.5f) * scale.x, values [i].y * scale.y, 10));
}
// draw z
GL.Color (Color.blue);
for (int i=1; i<num; ++i) {
GL.Vertex (pos + new Vector3 (((i-1) / num - 0.5f) * scale.x, values [i-1].z * scale.y, 10));
GL.Vertex (pos + new Vector3 ((i / num - 0.5f) * scale.x, values [i].z * scale.y, 10));
}
GL.End ();
}
#endregion
#region private
void Draw (int index, int num)
{
}
#endregion
#region Public
public void AddValue (Vector3 value)
{
values.Add (value);
while (values.Count > maxValues) {
values.RemoveAt (0);
}
}
public void AddValue (float x, float y, float z)
{
AddValue(new Vector3(x,y,z));
}
#endregion
#region LineMaterial
static Material lineMaterial;
static void CreateLineMaterial ()
{
if (!lineMaterial) {
lineMaterial = new Material ("Shader \"Lines/Colored Blended\" {" +
"SubShader { Pass { " +
" Blend SrcAlpha OneMinusSrcAlpha " +
" ZWrite Off Cull Off Fog { Mode Off } " +
" BindChannels {" +
" Bind \"vertex\", vertex Bind \"color\", color }" +
"} } }");
lineMaterial.hideFlags = HideFlags.HideAndDontSave;
lineMaterial.shader.hideFlags = HideFlags.HideAndDontSave;
}
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment