This repository was archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathUnityRandomEditorDraw.cs
More file actions
201 lines (177 loc) · 5.77 KB
/
UnityRandomEditorDraw.cs
File metadata and controls
201 lines (177 loc) · 5.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Text;
using System.IO;
using URandom;
public class UnityRandomEditorDraw
{
public static Texture2D aaLineTex = null;
public static Texture2D lineTex = null;
public static Texture2D tex = null;
public static void DrawLine(Vector2 pointA, Vector2 pointB, Color color, float width, bool antiAlias)
{
Color savedColor = GUI.color;
Matrix4x4 savedMatrix = GUI.matrix;
if (!lineTex)
{
lineTex = new Texture2D(1, 1, TextureFormat.ARGB32, true);
lineTex.SetPixel(0, 1, Color.white);
lineTex.Apply();
}
if (!aaLineTex)
{
aaLineTex = new Texture2D(1, 3, TextureFormat.ARGB32, true);
aaLineTex.SetPixel(0, 0, new Color(1, 1, 1, 0));
aaLineTex.SetPixel(0, 1, Color.white);
aaLineTex.SetPixel(0, 2, new Color(1, 1, 1, 0));
aaLineTex.Apply();
}
if (antiAlias) width *= 3;
float angle = Vector3.Angle(pointB - pointA, Vector2.right) * (pointA.y <= pointB.y?1:-1);
float m = (pointB - pointA).magnitude;
if (m > 0.01f)
{
Vector3 dz = new Vector3(pointA.x, pointA.y, 0);
GUI.color = color;
GUI.matrix = translationMatrix(dz) * GUI.matrix;
GUIUtility.ScaleAroundPivot(new Vector2(m, width), new Vector3(-0.5f, 0, 0));
GUI.matrix = translationMatrix(-dz) * GUI.matrix;
GUIUtility.RotateAroundPivot(angle, Vector2.zero);
GUI.matrix = translationMatrix(dz + new Vector3(width / 2, -m / 2) * Mathf.Sin(angle * Mathf.Deg2Rad)) * GUI.matrix;
if (!antiAlias)
GUI.DrawTexture(new Rect(0, 0, 1, 1), lineTex);
else
GUI.DrawTexture(new Rect(0, 0, 1, 1), aaLineTex);
}
GUI.matrix = savedMatrix;
GUI.color = savedColor;
}
private static Matrix4x4 translationMatrix(Vector3 v)
{
return Matrix4x4.TRS(v,Quaternion.identity,Vector3.one);
}
public static void DrawXYPlot(ArrayList rlist, int width, int height)
{
DrawXYPoints(rlist, 0, 1, width, height);
}
public static void DrawXYPlot(ArrayList rlist, int width, int height, bool scale)
{
// SCALE
float _min, _max;
if (scale) {
_min = Convert.ToSingle(rlist[0]);
_max = Convert.ToSingle(rlist[rlist.Count - 1]);
} else {
_min = 0;
_max = 1;
}
DrawXYPoints(rlist, _min, _max, width, height);
}
public static void DrawXYPlot(ArrayList rlist, int width, int height, float min, float max)
{
DrawXYPoints(rlist, min, max, width, height);
}
private static void DrawXYPoints(ArrayList rlist, float _min, float _max, int width, int height)
{
int counter = 0;
foreach ( object obj in rlist )
{
counter++;
float x, y, val;
val = Convert.ToSingle(obj);
val = ( (val - _min) / (_max - _min) );
y = val * height;
x = counter * (Convert.ToSingle(width) / rlist.Count);
DrawPoint(new Vector2(x,y));
}
}
public static void DrawV3Plot(ArrayList rlist, int width, int height, UnityRandomEditorWindow.RandomVector3DType _type, float alpha, float beta)
{
foreach ( object obj in rlist )
{
Vector2 _pos = MapTo2D( (Vector3) obj, alpha, beta); // rotation (alpha,beta)
// move from space -1 - 1 to space 0-1
_pos = ((_pos + Vector2.one) * 0.5f);
DrawPoint( Vector2.Scale(_pos, new Vector2(width, height)));
}
}
private static Vector2 MapTo2D(Vector3 pos, float a, float b)
{
// MAP a 3D point in a 2D space (a and b gives you the rotation)
float ca = (float) Math.Cos(a); float sa = (float) Math.Sin(a);
float cb = (float) Math.Cos(b); float sb = (float) Math.Sin(b);
float xx = ((pos.x * ca) + (pos.y*sa))*cb + pos.z*sb;
float yy = (pos.y*ca) - (pos.x*sa);
return new Vector2(xx,yy);
}
public static void DrawV2Plot(ArrayList rlist, int width, int height, UnityRandomEditorWindow.RandomVector2DType _type)
{
foreach ( object obj in rlist )
{
Vector2 pos = ((Vector2) obj + Vector2.one) * 0.5f;
switch (_type) {
case UnityRandomEditorWindow.RandomVector2DType.SQUARE:
DrawPoint( Vector2.Scale( pos, new Vector2(width, height)) );
break;
case UnityRandomEditorWindow.RandomVector2DType.CIRCLE:
DrawPoint( Vector2.Scale( pos, new Vector2(width, height) ) );
break;
case UnityRandomEditorWindow.RandomVector2DType.DISK:
DrawPoint( Vector2.Scale( pos, new Vector2(width, height) ) );
break;
default:
break;
}
}
}
private static void DrawPoint(Vector2 pos)
{
// 1. make a Cross around zero
Vector2 _pointA = new Vector2(0.0f,-1.0f);
Vector2 _pointB = new Vector2(0.0f,1.0f);
Vector2 _pointC = new Vector2(1.0f,0.0f);
Vector2 _pointD = new Vector2(-1.0f,0.0f);
// 2. move the cross into place
// Y: value
// X: Position
Vector2 _a = _pointA + pos;
Vector2 _b = _pointB + pos;
Vector2 _c = _pointC + pos;
Vector2 _d = _pointD + pos;
UnityRandomEditorDraw.DrawLine(_a,_b,Color.blue,1.0f,true);
UnityRandomEditorDraw.DrawLine(_c,_d,Color.blue,1.0f,true);
}
public static void DrawColorPlot(ArrayList rlist, int width, int height)
{
float x,y;
x = y = 0.0f;
float grid_space = 25.0f;
if (!tex) {
tex = new Texture2D(1,1,TextureFormat.ARGB32,false);
}
// make a simple grid
foreach ( object obj in rlist )
{
Vector2 a = new Vector2(x,y);
Vector2 b = new Vector2((x+grid_space),y);
Vector2 c = new Vector2(x,(y+grid_space));
Vector2 d = new Vector2((x+grid_space),(y+grid_space));
DrawPoint(a);
DrawPoint(b);
DrawPoint(c);
DrawPoint(d);
// make a new texture with the color
tex.SetPixel(0, 1, (Color) obj);
tex.Apply();
GUI.DrawTexture(new Rect(a.x,a.y,grid_space,grid_space), tex);
if (x < (width - grid_space)) {
x += grid_space;
} else {
x = 0;
y += grid_space;
}
}
}
}