133 lines
3.7 KiB
C#
133 lines
3.7 KiB
C#
using UnityEngine;
|
|
using System.Collections;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
#endif
|
|
|
|
|
|
public class AdvancedGizmo {
|
|
public static void DrawCircle(Vector3 center, Vector3 normal, float radius, Color color, int tesselation = 12, bool drawNormal = false, int diagonalCount = 0){
|
|
if (tesselation < 3) {
|
|
Debug.LogError("Invalid tesselation value! Should be >= 3");
|
|
}
|
|
Vector3 right = Vector3.right;
|
|
if (Mathf.Abs (Vector3.Dot (right, normal)) > 0.999f) {
|
|
right = Vector3.up;
|
|
}
|
|
|
|
Vector3 front = Vector3.Cross (right, normal).normalized;
|
|
|
|
Gizmos.color = color;
|
|
|
|
for(int i = 0; i < tesselation; ++i){
|
|
//Gizmos.DrawLine(
|
|
Gizmos.DrawLine(
|
|
center + Quaternion.AngleAxis((360.0f / tesselation) * (i + 0), normal) * front * radius,
|
|
center + Quaternion.AngleAxis((360.0f / tesselation) * (i + 1), normal) * front * radius
|
|
);
|
|
}
|
|
|
|
for (int i = 0; i < diagonalCount; ++i) {
|
|
float a1 = (180.0f / diagonalCount) * i;
|
|
float a2 = a1 + 180.0f;
|
|
|
|
Gizmos.DrawLine(
|
|
center + Quaternion.AngleAxis(a1, normal) * front * radius,
|
|
center + Quaternion.AngleAxis(a2, normal) * front * radius
|
|
);
|
|
}
|
|
}
|
|
|
|
public static void DrawGrid(Vector3 center, float width, float height, int wSections = 1, int hSections = 1){
|
|
Vector3 topLeft = new Vector3 (-width * 0.5f, 0, height * 0.5f);
|
|
Vector3 wStep = new Vector3 (width / wSections, 0, 0);
|
|
Vector3 hStep = new Vector3 (0, 0, -height / hSections);
|
|
|
|
|
|
//draw horizontal lines
|
|
for (int i = 0; i <= hSections; ++i) {
|
|
Vector3 p1 = topLeft + hStep * i + center;
|
|
Debug.DrawLine(p1, p1 + new Vector3(width, 0, 0));
|
|
}
|
|
|
|
//draw vertical lines
|
|
for (int i = 0; i <= hSections; ++i) {
|
|
Vector3 p1 = topLeft + wStep * i + center;
|
|
Debug.DrawLine(p1, p1 + new Vector3(0, 0, -height));
|
|
}
|
|
}
|
|
|
|
public delegate float fx(float x);
|
|
public static void Plot2D(fx func, Matrix4x4 tranfsorm, Vector3 center, Vector3 xAxis, Vector3 yAxis, float xLen, float xScale, float yScale, int sampleCount, Color color){
|
|
float xStep = xLen / (sampleCount - 1);
|
|
Vector3[] points = new Vector3[sampleCount];
|
|
|
|
for (int i = 0; i < sampleCount; ++i) {
|
|
float x1 = i * xStep;
|
|
float arg1 = (x1 / xLen) * xScale;
|
|
float y1 = func(arg1) * yScale;
|
|
|
|
points[i] = center + xAxis * x1 + yAxis * y1;
|
|
}
|
|
|
|
#if UNITY_EDITOR
|
|
Color backup = Handles.color;
|
|
Handles.matrix = tranfsorm;
|
|
Handles.color = color;
|
|
Handles.DrawPolyLine(points);
|
|
Handles.color = backup;
|
|
Handles.matrix = Matrix4x4.identity;
|
|
#endif
|
|
}
|
|
|
|
public static void DrawMesh(Vector3[] vertices, int[] indices, bool drawNormals){
|
|
for(int i = 0; i < indices.Length; i+=3){
|
|
Vector3 v0 = vertices[indices[i + 0]];
|
|
Vector3 v1 = vertices[indices[i + 1]];
|
|
Vector3 v2 = vertices[indices[i + 2]];
|
|
Debug.DrawLine(v0, v1);
|
|
Debug.DrawLine(v1, v2);
|
|
Debug.DrawLine(v2, v0);
|
|
|
|
if(drawNormals){
|
|
Vector3 center = (v0 + v1 + v2) / 3.0f;
|
|
Vector3 normal = Vector3.Cross((v1 - v0), (v2 - v0));
|
|
Debug.DrawRay(center, normal);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public static void DrawMesh(Vector3[] vertices, bool drawNormals){
|
|
for(int i = 0; i < vertices.Length; i+=3){
|
|
Vector3 v0 = vertices[i + 0];
|
|
Vector3 v1 = vertices[i + 1];
|
|
Vector3 v2 = vertices[i + 2];
|
|
Debug.DrawLine(v0, v1);
|
|
Debug.DrawLine(v1, v2);
|
|
Debug.DrawLine(v2, v0);
|
|
|
|
if(drawNormals){
|
|
Vector3 center = (v0 + v1 + v2) / 3.0f;
|
|
Vector3 normal = Vector3.Cross((v1 - v0), (v2 - v0));
|
|
Debug.DrawRay(center, normal);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void DrawTriangle(Vector3 v0, Vector3 v1, Vector3 v2, bool drawNormal){
|
|
Debug.DrawLine(v0, v1);
|
|
Debug.DrawLine(v1, v2);
|
|
Debug.DrawLine(v2, v0);
|
|
|
|
if(drawNormal){
|
|
Vector3 center = (v0 + v1 + v2) / 3.0f;
|
|
Vector3 normal = Vector3.Cross((v1 - v0), (v2 - v0));
|
|
Debug.DrawRay(center, normal);
|
|
}
|
|
}
|
|
|
|
|
|
}
|