Skip to content

Commit bf22416

Browse files
committed
Game gizmos
1 parent 35c89be commit bf22416

6 files changed

+192
-0
lines changed

Scripts/Util/GameGizmoUtils.cs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using ElasticSea.Framework.Extensions;
4+
using UnityEngine;
5+
6+
namespace ElasticSea.Framework.Scripts.Util
7+
{
8+
public class GameGizmoUtils
9+
{
10+
public static GameObject DrawArrow(Vector3 from, Vector3 to, float arrowHeadLength, Color bottom, Color top, float killDelay = 0f)
11+
{
12+
var go = new GameObject("Arrow");
13+
go.AddComponent<MeshFilter>().mesh = BuildArrowMesh(to.Distance(from), arrowHeadLength, bottom, top);
14+
go.AddComponent<MeshRenderer>().material = new Material(Shader.Find("Unlit/Vertex Color"));
15+
go.transform.position = from;
16+
go.transform.LookAt(to);
17+
go.transform.rotation *= Quaternion.Euler(90, 0, 0);
18+
19+
if (killDelay > 0)
20+
{
21+
Object.Destroy(go, killDelay);
22+
}
23+
return go;
24+
}
25+
26+
private static Mesh BuildArrowMesh(float distance, float arrowHeadLength, Color bottom, Color top)
27+
{
28+
var arrowHeadRadius = arrowHeadLength / 2.5f;
29+
var arrowLength = distance - arrowHeadLength;
30+
var segments = 32;
31+
var lineRadius = arrowHeadRadius / 4;
32+
33+
var points = new List<Vector3>();
34+
var segment = Mathf.PI * 2 / segments;
35+
for (int i = 0; i < segments; i++)
36+
{
37+
var x = Mathf.Cos(segment * i) * arrowHeadRadius;
38+
var y = Mathf.Sin(segment * i) * arrowHeadRadius;
39+
points.Add(new Vector3(x, arrowLength, y));
40+
}
41+
42+
var meshBuilder = new MeshBuilder();
43+
44+
// arrow head
45+
meshBuilder.AddCircle(points.ToArray(), new Vector3(0, arrowLength + arrowHeadLength, 0));
46+
meshBuilder.AddCircle(points.ToArray(), new Vector3(0, arrowLength, 0), true);
47+
48+
var points2 = new List<Vector3>();
49+
for (int i = 0; i < segments; i++)
50+
{
51+
var x = Mathf.Cos(segment * i) * lineRadius;
52+
var y = Mathf.Sin(segment * i) * lineRadius;
53+
points2.Add(new Vector3(x, 0, y));
54+
}
55+
56+
var pointsBottom = points2.Select(p => new Vector3(p.x, arrowLength, p.z)).ToArray();
57+
meshBuilder.AddStrip(points2.ToArray(), pointsBottom);
58+
meshBuilder.AddCircle(points2.ToArray(), new Vector3(0, 0, 0), true);
59+
var buildArrow = meshBuilder.GetMesh();
60+
buildArrow.colors = buildArrow.vertices.Select(v => Color.Lerp(bottom, top, v.y / distance)).ToArray();
61+
return buildArrow;
62+
}
63+
}
64+
}

Scripts/Util/GameGizmoUtils.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Util/MeshBuilder.cs

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace ElasticSea.Framework.Scripts.Util
6+
{
7+
public class MeshBuilder
8+
{
9+
private List<Vector3> vertices = new();
10+
private List<int> triangles = new();
11+
12+
public void AddTriangle(Vector3 p0, Vector3 p1, Vector3 p2, bool flip = false)
13+
{
14+
var vCount = vertices.Count;
15+
if (flip)
16+
{
17+
(p2, p1) = (p1, p2);
18+
}
19+
20+
vertices.Add(p0);
21+
vertices.Add(p1);
22+
vertices.Add(p2);
23+
triangles.Add(vCount + 0);
24+
triangles.Add(vCount + 1);
25+
triangles.Add(vCount + 2);
26+
}
27+
28+
public void AddQuad(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, bool flip = false)
29+
{
30+
AddTriangle(p0, p1, p2, flip);
31+
AddTriangle(p0, p2, p3, flip);
32+
}
33+
34+
public void AddStrip(Vector3[] points0, Vector3[] points1, bool flip = false)
35+
{
36+
if (points0.Length != points1.Length)
37+
{
38+
throw new Exception("Points needs to be same length");
39+
}
40+
41+
var length = points0.Length;
42+
for (var i = 0; i < length; i++)
43+
{
44+
var p00 = points0[i + 0];
45+
var p01 = points0[(i + 1) % length];
46+
var p10 = points1[i + 0];
47+
var p11 = points1[(i + 1) % length];
48+
49+
AddQuad(p00, p10, p11, p01, flip);
50+
}
51+
}
52+
53+
public void AddCircle(Vector3[] points, Vector3 center, bool flip = false)
54+
{
55+
for (var i = 0; i < points.Length; i++)
56+
{
57+
var p0 = points[i + 0];
58+
var p1 = points[(i + 1) % points.Length];
59+
AddTriangle(p0, center, p1, flip);
60+
}
61+
}
62+
63+
public Mesh GetMesh()
64+
{
65+
var mesh = new Mesh()
66+
{
67+
vertices = vertices.ToArray(),
68+
triangles = triangles.ToArray()
69+
};
70+
mesh.RecalculateBounds();
71+
return mesh;
72+
}
73+
}
74+
}

Scripts/Util/MeshBuilder.cs.meta

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Shaders/Vertex Color.shader

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
Shader "Unlit/Vertex Color"
2+
{
3+
SubShader
4+
{
5+
Pass
6+
{
7+
CGPROGRAM
8+
#pragma vertex vert
9+
#pragma fragment frag
10+
11+
struct appdata
12+
{
13+
float4 vertex : POSITION;
14+
float4 color : COLOR;
15+
};
16+
17+
struct v2f
18+
{
19+
float4 vertex : SV_POSITION;
20+
float4 color : COLOR;
21+
};
22+
23+
v2f vert (appdata v)
24+
{
25+
v2f o;
26+
o.vertex = UnityObjectToClipPos(v.vertex);
27+
o.color = v.color;
28+
return o;
29+
}
30+
31+
fixed4 frag (v2f i) : SV_Target
32+
{
33+
return i.color;
34+
}
35+
ENDCG
36+
}
37+
}
38+
}

Shaders/Vertex Color.shader.meta

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)