Files
2025-05-13 05:19:58 +03:00

141 lines
3.9 KiB
C#

using UnityEngine;
using System.Collections;
#if UNITY_EDITOR
using UnityEditor.VersionControl;
using UnityEditorInternal;
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 / (float)tesselation) * (float)(i + 0), normal) * front * radius,
center + Quaternion.AngleAxis((360.0f / (float)tesselation) * (float)(i + 1), normal) * front * radius
);
}
for (int i = 0; i < diagonalCount; ++i) {
float a1 = (180.0f / (float)diagonalCount) * (float)(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 / (float)wSections, 0, 0);
Vector3 hStep = new Vector3 (0, 0, -height / (float)hSections);
//draw horizontal lines
for (int i = 0; i <= hSections; ++i) {
Vector3 p1 = topLeft + hStep * (float)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 * (float)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 / (float)(sampleCount - 1);
Vector3[] points = new Vector3[sampleCount];
for (int i = 0; i < sampleCount; ++i) {
float x1 = (float)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);
}
}
}