220 lines
5.0 KiB
C#
220 lines
5.0 KiB
C#
using UnityEngine;
|
|||
|
|
using System.Collections;
|
||
|
|
using System;
|
||
|
|
|
||
|
|
[Serializable]
|
||
|
|
public class OscilloscopeChannel{
|
||
|
|
public bool sample = true;
|
||
|
|
public bool draw = true;
|
||
|
|
public Color color = Color.green;
|
||
|
|
public float cellTime = 0.2f;
|
||
|
|
public float cellValue = 0.2f;
|
||
|
|
public OscilloscopeSampler sampler;
|
||
|
|
|
||
|
|
|
||
|
|
private float[] sampleValueBuffer;
|
||
|
|
|
||
|
|
public float[] SampleValueBuffer {
|
||
|
|
get {
|
||
|
|
return sampleValueBuffer;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private float[] sampleTimeBuffer;
|
||
|
|
|
||
|
|
public float[] SampleTimeBuffer {
|
||
|
|
get {
|
||
|
|
return sampleTimeBuffer;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private int sampleBufferPosition;
|
||
|
|
|
||
|
|
public int SampleBufferPosition {
|
||
|
|
get {
|
||
|
|
return sampleBufferPosition;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Reset(int cellCount, int sampleRate){
|
||
|
|
float totalTime = cellCount * cellTime;
|
||
|
|
int maxSampleCount = Mathf.CeilToInt (totalTime * sampleRate);
|
||
|
|
sampleTimeBuffer = new float[maxSampleCount];
|
||
|
|
sampleValueBuffer = new float[maxSampleCount];
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Sample(){
|
||
|
|
sampleBufferPosition = (sampleBufferPosition + 1) % sampleValueBuffer.Length;
|
||
|
|
float val = sampler == null ? 0 : sampler();
|
||
|
|
sampleTimeBuffer[sampleBufferPosition] = Time.time;
|
||
|
|
sampleValueBuffer[sampleBufferPosition] = val;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public delegate float OscilloscopeSampler();
|
||
|
|
|
||
|
|
[ExecuteInEditMode]
|
||
|
|
public class Oscilloscope : MonoBehaviour {
|
||
|
|
public Vector2 pos = new Vector2(10,10);
|
||
|
|
public Vector2 size;
|
||
|
|
|
||
|
|
public OscilloscopeChannel[] channels;
|
||
|
|
|
||
|
|
|
||
|
|
private Vector3[] screenCorners;
|
||
|
|
private const float cellSize = 40.0f;
|
||
|
|
private const int hCellCountHalf = 6;
|
||
|
|
private const int vCellCountHalf = 4;
|
||
|
|
public int sampleRate = 50;
|
||
|
|
private float lastSampleTime;
|
||
|
|
public bool showInEditor = true;
|
||
|
|
|
||
|
|
public int xMatrixOffset = 0;
|
||
|
|
public int yMatrixOffset = 0;
|
||
|
|
private GUIStyle richLabelStyle;
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
// Use this for initialization
|
||
|
|
void Start () {
|
||
|
|
size = new Vector2 (hCellCountHalf * 2 * cellSize, vCellCountHalf * 2 * cellSize);
|
||
|
|
screenCorners = new []{
|
||
|
|
new Vector3(pos.x, pos.y, 0.5f),
|
||
|
|
new Vector3(pos.x + size.x, pos.y, 0.5f),
|
||
|
|
new Vector3(pos.x + size.x, pos.y + size.y, 0.5f),
|
||
|
|
new Vector3(pos.x, pos.y + size.y, 0.5f)
|
||
|
|
};
|
||
|
|
|
||
|
|
foreach (var c in channels) {
|
||
|
|
c.Reset(hCellCountHalf * 2, sampleRate);
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void UpdateSamples(){
|
||
|
|
float currentTime = Time.time;
|
||
|
|
float timeElapsed = currentTime - lastSampleTime;
|
||
|
|
|
||
|
|
if(timeElapsed >= (1.0f / sampleRate)){
|
||
|
|
if(channels != null){
|
||
|
|
foreach( var c in channels){
|
||
|
|
if(c.sample)
|
||
|
|
c.Sample();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
lastSampleTime = currentTime;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
Vector2[] GenerateLineDividers(Vector2 from, Vector2 to, float step, int count, float leftSize, float rightSize){
|
||
|
|
Vector2[] result = new Vector2[count * 2];
|
||
|
|
Vector2 dir = (to - from).normalized;
|
||
|
|
Vector2 right = new Vector2 (dir.y, -dir.x);
|
||
|
|
Vector2 leftOffset = right * -leftSize;
|
||
|
|
Vector2 rightOffset = right * rightSize;
|
||
|
|
|
||
|
|
for (int i = 0; i < count; ++i) {
|
||
|
|
var p = from + dir * step * i;
|
||
|
|
result[i * 2 + 0] = p + leftOffset;
|
||
|
|
result[i * 2 + 1] = p + rightOffset;
|
||
|
|
}
|
||
|
|
|
||
|
|
return result;
|
||
|
|
}
|
||
|
|
|
||
|
|
void DrawLinesArray(Vector2[] lines){
|
||
|
|
for (int i = 0; i < lines.Length / 2; ++i) {
|
||
|
|
var p0 = lines[i * 2 + 0];
|
||
|
|
var p1 = lines[i * 2 + 1];
|
||
|
|
GL.Vertex(new Vector3(p0.x, p0.y, 0.4f));
|
||
|
|
GL.Vertex(new Vector3(p1.x, p1.y, 0.4f));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnValidate(){
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
void DrawDisplay(){
|
||
|
|
size = new Vector2 (hCellCountHalf * 2 * cellSize, vCellCountHalf * 2 * cellSize);
|
||
|
|
screenCorners = new []{
|
||
|
|
new Vector3(pos.x, pos.y, 0.5f),
|
||
|
|
new Vector3(pos.x + size.x, pos.y, 0.5f),
|
||
|
|
new Vector3(pos.x + size.x, pos.y + size.y, 0.5f),
|
||
|
|
new Vector3(pos.x, pos.y + size.y, 0.5f)
|
||
|
|
};
|
||
|
|
|
||
|
|
UpdateSamples ();
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
void DrawHandles(){
|
||
|
|
/*Rect bottomRect = new Rect (pos.x, pos.y + size.y, size.x, 20);
|
||
|
|
|
||
|
|
int digitWidth = 20;
|
||
|
|
int labelWidth = 60;
|
||
|
|
int channelInfoLength = digitWidth + labelWidth;
|
||
|
|
|
||
|
|
if (channels != null) {
|
||
|
|
int channelId = 0;
|
||
|
|
|
||
|
|
foreach(var c in channels){
|
||
|
|
if(c != null){
|
||
|
|
float offsetX = channelInfoLength * channelId + bottomRect.x;
|
||
|
|
Rect digitRect = new Rect(offsetX, bottomRect.y, digitWidth, bottomRect.height);
|
||
|
|
Rect labelRect = new Rect(offsetX + digitWidth, bottomRect.y, labelWidth, bottomRect.height);
|
||
|
|
|
||
|
|
GUI.Label(digitRect, string.Format("<color=green>{0}</color>", channelId.ToString()),richLabelStyle);
|
||
|
|
GUI.Label(labelRect, string.Format("<color=white>{0}</color>", c.cellValue.ToString()),richLabelStyle);
|
||
|
|
|
||
|
|
int colorLineHeight = 3;
|
||
|
|
Rect colorLineRect = new Rect(offsetX, labelRect.yMax - colorLineHeight, channelInfoLength / 3, colorLineHeight);
|
||
|
|
DrawRect(colorLineRect, c.color, 0.3f);
|
||
|
|
|
||
|
|
channelId++;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}*/
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnWillRenderObject() {
|
||
|
|
Debug.Log ("On will render");
|
||
|
|
}
|
||
|
|
|
||
|
|
void Draw(){
|
||
|
|
if (!enabled) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
if (richLabelStyle == null) {
|
||
|
|
richLabelStyle = new GUIStyle ();
|
||
|
|
richLabelStyle.richText = true;
|
||
|
|
}
|
||
|
|
GL.PushMatrix();
|
||
|
|
GL.LoadPixelMatrix (0, Screen.width + xMatrixOffset, Screen.height + yMatrixOffset, 0);
|
||
|
|
|
||
|
|
DrawDisplay ();
|
||
|
|
//DrawHandles ();
|
||
|
|
GL.PopMatrix();
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnPostRender(){
|
||
|
|
Draw ();
|
||
|
|
}
|
||
|
|
|
||
|
|
void OnDrawGizmos(){
|
||
|
|
//if (showInEditor) {
|
||
|
|
Draw ();
|
||
|
|
//}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Update is called once per frame
|
||
|
|
void Update () {
|
||
|
|
|
||
|
|
}
|
||
|
|
}
|