161 lines
4.5 KiB
C#
161 lines
4.5 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
using System.Linq;
|
||
|
|
|
||
|
|
/*
|
||
|
|
public class Plot2D {
|
||
|
|
private Curve[] _curves;
|
||
|
|
private Rect _dataBox;
|
||
|
|
private string _title = "";
|
||
|
|
private float _titleHeight = 20.0f;
|
||
|
|
|
||
|
|
public string Title{
|
||
|
|
get{ return _title;}
|
||
|
|
set{ _title = value;}
|
||
|
|
}
|
||
|
|
|
||
|
|
public Plot2D(Curve curve) : this(new []{curve}) {
|
||
|
|
}
|
||
|
|
|
||
|
|
public Plot2D(Curve[] curves){
|
||
|
|
_curves = curves;
|
||
|
|
|
||
|
|
if (_curves == null || _curves.Length == 0) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
_dataBox = new Rect ();
|
||
|
|
_dataBox.x = _curves.Select (v => v.Args.Min()).Min();
|
||
|
|
_dataBox.y = _curves.Select (v => v.Vals.Max()).Max();
|
||
|
|
_dataBox.width = _curves.Select (v => v.Args.Last ()).Max () - _dataBox.x;
|
||
|
|
_dataBox.height = _curves.Select (v => v.Vals.Min()).Min() - _dataBox.y;
|
||
|
|
}
|
||
|
|
|
||
|
|
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;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//
|
||
|
|
|
||
|
|
public void SetCurve(Curve curve){
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public void OnGUI(Rect rect){
|
||
|
|
CreateLineMaterial ();
|
||
|
|
|
||
|
|
//Plot header
|
||
|
|
if (!string.IsNullOrEmpty (Title)) {
|
||
|
|
var headerRect = new Rect(rect.x, rect.y - _titleHeight, rect.width, _titleHeight);
|
||
|
|
lineMaterial.SetPass( 0 );
|
||
|
|
Tools.DrawRectangle(headerRect, new Color(0,0,0,0.5f));
|
||
|
|
|
||
|
|
var cache = GUI.skin.label.alignment;
|
||
|
|
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
|
||
|
|
GUI.Label (headerRect, Title);
|
||
|
|
GUI.skin.label.alignment = cache;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
lineMaterial.SetPass( 0 );
|
||
|
|
|
||
|
|
//background quad
|
||
|
|
Tools.DrawRectangle (rect, new Color(0,0,0.1f,0.25f));
|
||
|
|
|
||
|
|
|
||
|
|
//horizontal lines
|
||
|
|
GL.Begin(GL.LINES);
|
||
|
|
GL.Color(new Color(1,1,0,0.4f));
|
||
|
|
int hlines = 5;
|
||
|
|
for(int i = 0; i < hlines; ++i){
|
||
|
|
float step = rect.height / (float)(hlines - 1);
|
||
|
|
GL.Vertex3(rect.x, rect.y + step * (float)i, 0);
|
||
|
|
GL.Vertex3(rect.x + rect.width, rect.y + step * (float)i, 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
int vlines = 7;
|
||
|
|
for(int i = 0; i < vlines; ++i){
|
||
|
|
float step = rect.width / (float)(vlines - 1);
|
||
|
|
GL.Vertex3(rect.x + step * (float)i, rect.y, 0);
|
||
|
|
GL.Vertex3(rect.x + step * (float)i, rect.y + rect.height, 0);
|
||
|
|
}
|
||
|
|
GL.End();
|
||
|
|
|
||
|
|
float textHeight = 15.0f;
|
||
|
|
float textWidth = 74.0f;
|
||
|
|
for(int i = 0; i < hlines; ++i){
|
||
|
|
float step = rect.height / (float)(hlines - 1);
|
||
|
|
var pt = new Vector3(rect.x ,rect.y + step * (float)i, 0);
|
||
|
|
|
||
|
|
lineMaterial.SetPass( 0 );
|
||
|
|
|
||
|
|
var r = new Rect(pt.x - textWidth - 10, pt.y - textHeight*0.5f, textWidth,textHeight);
|
||
|
|
Tools.DrawRectangle(r, new Color(0,0,0,0.6f));
|
||
|
|
|
||
|
|
var val = (_dataBox.height / (float)(hlines - 1)) * (float)i + _dataBox.y;
|
||
|
|
|
||
|
|
var cache = GUI.skin.label.alignment;
|
||
|
|
GUI.skin.label.alignment = TextAnchor.MiddleRight;
|
||
|
|
GUI.Label (r, val.ToString("0.0000"));
|
||
|
|
GUI.skin.label.alignment = cache;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
for(int i = 0; i < vlines; ++i){
|
||
|
|
float step = rect.width / (float)(vlines - 1);
|
||
|
|
var pt = new Vector3(rect.x + step * (float)i , rect.y + rect.height, 0);
|
||
|
|
|
||
|
|
lineMaterial.SetPass( 0 );
|
||
|
|
|
||
|
|
var r = new Rect(pt.x, pt.y + (i % 2) * textHeight * 1.25f + 5, textWidth,textHeight);
|
||
|
|
|
||
|
|
Tools.DrawRectangle(r, new Color(0,0,0,0.6f));
|
||
|
|
|
||
|
|
var val = (_dataBox.width / (float)(vlines - 1)) * (float)i + _dataBox.x;
|
||
|
|
|
||
|
|
var cache = GUI.skin.label.alignment;
|
||
|
|
GUI.skin.label.alignment = TextAnchor.MiddleLeft;
|
||
|
|
GUI.Label (r, val.ToString("0.0"));
|
||
|
|
GUI.skin.label.alignment = cache;
|
||
|
|
}
|
||
|
|
|
||
|
|
lineMaterial.SetPass( 0 );
|
||
|
|
|
||
|
|
Color[] colors = new Color[]{Color.red, Color.green, Color.blue};
|
||
|
|
|
||
|
|
for(int j = 0; j < _curves.Length; ++j){
|
||
|
|
var curve = _curves[j];
|
||
|
|
GL.Begin(GL.LINES);
|
||
|
|
GL.Color(colors[j % colors.Length]);
|
||
|
|
float xScale = _dataBox.width / rect.width;
|
||
|
|
float yScale = _dataBox.height / rect.height;
|
||
|
|
for (int i = 0; i < (curve.Args.Length - 1); ++i) {
|
||
|
|
float x0 = ((curve.Args[i] - _dataBox.x) / xScale) + rect.x;
|
||
|
|
float y0 = ((curve.Vals[i] - _dataBox.y) / yScale) + rect.y;
|
||
|
|
float x1 = ((curve.Args[i + 1] - _dataBox.x) / xScale) + rect.x;
|
||
|
|
float y1 = ((curve.Vals[i + 1] - _dataBox.y) / yScale) + rect.y;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
GL.Vertex3(x0, y0, 0);
|
||
|
|
GL.Vertex3(x1, y1, 0);
|
||
|
|
}
|
||
|
|
GL.End();
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
}*/
|