-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
116 lines (100 loc) · 4.34 KB
/
Utils.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace SubnauticaSeaTruckFlexible
{
internal static class Utils
{
const string DebugPrimitiveName = "DebugPrimitive";
public static GameObject DrawDebugPrimitive(GameObject go)
{
return DrawDebugPrimitive(go, Vector3.zero);
}
public static GameObject DrawDebugPrimitive(GameObject go, Vector3 localPosition)
{
var primTransform = go.transform.Find(DebugPrimitiveName);
if (!primTransform)
{
var spherePrim = GameObject.CreatePrimitive(PrimitiveType.Sphere);
spherePrim.name = DebugPrimitiveName;
if (spherePrim.TryGetComponent<Collider>(out var collider))
{
UnityEngine.Object.Destroy(collider);
}
primTransform = spherePrim.transform;
primTransform.parent = go.transform;
}
primTransform.localPosition = localPosition;
primTransform.localScale = Vector3.one * 0.25f;
return primTransform.gameObject;
}
public static void ClearDebugPrimitives(GameObject go)
{
foreach (Transform transform in go.transform)
{
if (transform.gameObject.name == DebugPrimitiveName)
{
UnityEngine.Object.Destroy(transform.gameObject);
}
}
}
public static void DrawConnectorColliderLines(SeaTruckSegment segment)
{
if (!segment.isRearConnected)
{
return;
}
var openedGo = segment.rearConnection.openedGo;
if (!openedGo.GetComponentInChildren<LineRenderer>())
{
Material material = new Material(Shader.Find("Unlit/Color"));
Color color = Color.green;
material.color = color;
float width = 0.01f;
foreach (var meshCollider in openedGo.GetComponentsInChildren<MeshCollider>())
{
var triVertices = meshCollider.sharedMesh.vertices;
for (var i = 0; i < triVertices.Length; i += 3)
{
DrawLine(meshCollider.gameObject, i, color, material, width);
DrawLine(meshCollider.gameObject, i + 1, color, material, width);
DrawLine(meshCollider.gameObject, i + 2, color, material, width);
}
}
}
foreach (var meshCollider in openedGo.GetComponentsInChildren<MeshCollider>())
{
var triVertexIndices = meshCollider.sharedMesh.triangles;
var vertices = meshCollider.sharedMesh.vertices;
for (var i = 0; i < triVertexIndices.Length; i += 3)
{
PositionLine(meshCollider.gameObject, i, vertices[triVertexIndices[i]], vertices[triVertexIndices[i + 1]]);
PositionLine(meshCollider.gameObject, i + 1, vertices[triVertexIndices[i + 1]], vertices[triVertexIndices[i + 2]]);
PositionLine(meshCollider.gameObject, i + 2, vertices[triVertexIndices[i + 2]], vertices[triVertexIndices[i]]);
}
}
}
static void DrawLine(GameObject attachTo, int index, Color color, Material material, float width = 0.01f)
{
LineRenderer line = new GameObject($"Line_{index}").AddComponent<LineRenderer>();
line.material = material;
line.startColor = color;
line.endColor = color;
line.startWidth = width;
line.endWidth = width;
line.positionCount = 2;
line.useWorldSpace = true;
line.transform.SetParent(attachTo.transform);
line.allowOcclusionWhenDynamic = false;
}
static void PositionLine(GameObject attachTo, int index, Vector3 start, Vector3 end)
{
LineRenderer line = attachTo.transform.Find($"Line_{index}").GetComponent<LineRenderer>();
line.SetPosition(0, attachTo.transform.TransformPoint(start));
line.SetPosition(1, attachTo.transform.TransformPoint(end));
}
}
}