|
1 |
| -using UnityEngine; |
| 1 | +using DebugHelper.Managers; |
2 | 2 | using SMLHelper.V2.Commands;
|
3 |
| -using System.Collections; |
4 |
| -using System.Collections.Generic; |
5 |
| -using UWE; |
6 | 3 |
|
7 | 4 | namespace DebugHelper.Commands
|
8 | 5 | {
|
9 | 6 | public static class ColliderCommands
|
10 | 7 | {
|
11 |
| - private static Material colliderMaterial; // generic colliders |
12 |
| - private static Material physicsColliderMaterial; // colliders on non-kinematic rigidbodies |
13 |
| - private static Material triggerMaterial; // colliders with isTrigger set to true |
14 |
| - private static Material meshColliderMaterial; // anything with the MeshCollider component |
15 |
| - |
16 |
| - private static List<GameObject> colliderRendererObjects = new List<GameObject>(); |
17 |
| - |
18 |
| - private static IEnumerator LoadStasisFieldMaterial() |
19 |
| - { |
20 |
| - CoroutineTask<GameObject> task = CraftData.GetPrefabForTechTypeAsync(TechType.StasisRifle); |
21 |
| - yield return task; |
22 |
| - |
23 |
| - GameObject stasisRifle = task.GetResult(); |
24 |
| - var stasisBall = stasisRifle.GetComponent<StasisRifle>().effectSpherePrefab.GetComponentInChildren<Renderer>(); |
25 |
| - colliderMaterial = new Material(stasisBall.materials[1]); |
26 |
| - colliderMaterial.color = new Color(0.3f, 1f, 0f); |
27 |
| - physicsColliderMaterial = new Material(stasisBall.materials[1]); |
28 |
| - physicsColliderMaterial.color = new Color(1f, 0.2f, 0.2f); |
29 |
| - triggerMaterial = new Material(stasisBall.materials[1]); |
30 |
| - triggerMaterial.color = new Color(0.5f, 0.5f, 0.5f); |
31 |
| - meshColliderMaterial = new Material(stasisBall.materials[1]); |
32 |
| - meshColliderMaterial.color = new Color(0.2f, 0.2f, 1f); |
33 |
| - } |
34 | 8 |
|
| 9 | + /// <summary> |
| 10 | + /// Shows all colliders in wanted range |
| 11 | + /// </summary> |
35 | 12 | [ConsoleCommand("showcolliders")]
|
36 |
| - public static void ShowColliders(bool hitsTriggers = false) |
| 13 | + public static void ShowCollidersInRange(float range = 50f, bool hideMessage = false) |
37 | 14 | {
|
38 |
| - CoroutineHost.StartCoroutine(ShowCollidersCoroutine(hitsTriggers)); |
39 |
| - } |
40 |
| - |
41 |
| - [ConsoleCommand("showallcolliders")] |
42 |
| - public static void ShowCollidersInRange(float maxRange, bool hideMessages = false) |
43 |
| - { |
44 |
| - CoroutineHost.StartCoroutine(ShowCollidersInRangeCoroutine(maxRange, hideMessages)); |
| 15 | + DebugCollidersManager.main.ShowCollidersRange(range); |
| 16 | + if (!hideMessage) ErrorMessage.AddMessage($"All colliders in {DebugCollidersManager.main.targetRange}m range are visible now."); |
45 | 17 | }
|
| 18 | + /// <summary> |
| 19 | + /// Shows all colliders in wanted range (alias) |
| 20 | + /// </summary> |
| 21 | + [ConsoleCommand("sw_colls")] |
| 22 | + public static void ShowCollidersInRange2(float range = 50f, bool hideMessage = false) => ShowCollidersInRange(range, hideMessage); |
46 | 23 |
|
| 24 | + /// <summary> |
| 25 | + /// Hides all shown colliders |
| 26 | + /// </summary> |
47 | 27 | [ConsoleCommand("hidecolliders")]
|
48 |
| - public static void HideColliders(bool hideMessages = false) |
49 |
| - { |
50 |
| - int destruido = 0; |
51 |
| - foreach (var obj in colliderRendererObjects) |
52 |
| - { |
53 |
| - if (obj != null) |
54 |
| - { |
55 |
| - Object.Destroy(obj); |
56 |
| - destruido++; |
57 |
| - } |
58 |
| - } |
59 |
| - colliderRendererObjects.Clear(); |
60 |
| - if (!hideMessages) ErrorMessage.AddMessage($"Destroyed all {destruido} collider renderers."); |
61 |
| - } |
62 |
| - |
63 |
| - private static IEnumerator ShowCollidersCoroutine(bool hitsTriggers) |
64 |
| - { |
65 |
| - if (colliderMaterial == null) |
66 |
| - { |
67 |
| - yield return CoroutineHost.StartCoroutine(LoadStasisFieldMaterial()); |
68 |
| - } |
69 |
| - Transform scanTransform = SNCameraRoot.main.transform; |
70 |
| - if (Physics.Raycast(scanTransform.position + scanTransform.forward, scanTransform.forward, out RaycastHit hit, float.MaxValue, -1, hitsTriggers ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore)) |
71 |
| - { |
72 |
| - if (hit.collider.GetComponentInParent<Player>()) |
73 |
| - { |
74 |
| - ErrorMessage.AddMessage("Warning: raycast hit player"); |
75 |
| - yield break; |
76 |
| - } |
77 |
| - Transform root = hit.collider.transform; |
78 |
| - var prefabIdentifier = hit.collider.GetComponentInParent<PrefabIdentifier>(); |
79 |
| - if (prefabIdentifier != null) |
80 |
| - { |
81 |
| - root = prefabIdentifier.transform; |
82 |
| - } |
83 |
| - else |
84 |
| - { |
85 |
| - var rb = hit.collider.attachedRigidbody; |
86 |
| - if (rb != null) |
87 |
| - { |
88 |
| - root = rb.transform; |
89 |
| - } |
90 |
| - } |
91 |
| - ErrorMessage.AddMessage(string.Format("Showing colliders within object '{0}'.", root.gameObject.name)); |
92 |
| - RenderCollidersInChildren(root); |
93 |
| - } |
94 |
| - else |
95 |
| - { |
96 |
| - ErrorMessage.AddMessage("Raycast failed to hit anything."); |
97 |
| - } |
98 |
| - } |
99 |
| - |
100 |
| - private static IEnumerator ShowCollidersInRangeCoroutine(float maxRange, bool hideMessages = false) |
101 |
| - { |
102 |
| - if (colliderMaterial == null) |
103 |
| - { |
104 |
| - yield return CoroutineHost.StartCoroutine(LoadStasisFieldMaterial()); |
105 |
| - } |
106 |
| - HideColliders(hideMessages); |
107 |
| - var center = SNCameraRoot.main.transform.position; |
108 |
| - var allColliders = Physics.OverlapSphere(center, maxRange); // all colliders |
109 |
| - var colliders = new List<Collider>(); // just colliders we want to use |
110 |
| - var playerRb = Player.main.rigidBody; |
111 |
| - foreach (var collider in allColliders) |
112 |
| - { |
113 |
| - if (collider.attachedRigidbody != playerRb) // don't show player colliders |
114 |
| - { |
115 |
| - colliders.Add(collider); |
116 |
| - } |
117 |
| - } |
118 |
| - if (!hideMessages) ErrorMessage.AddMessage($"Showing all {colliders.Count} colliders within {(int)maxRange} meters."); |
119 |
| - foreach (var collider in colliders) |
120 |
| - { |
121 |
| - RenderCollider(collider); |
122 |
| - } |
123 |
| - if (!hideMessages) ErrorMessage.AddMessage("Use the 'hidecolliders' command to revert these changes. Running the command again will also reset the colliders."); |
124 |
| - } |
125 |
| - |
126 |
| - private static void RenderCollidersInChildren(Transform objectRoot) |
127 |
| - { |
128 |
| - foreach (var collider in objectRoot.GetComponentsInChildren<Collider>()) |
129 |
| - { |
130 |
| - RenderCollider(collider); |
131 |
| - } |
132 |
| - } |
133 |
| - |
134 |
| - private static void RenderCollider(Collider col) |
135 |
| - { |
136 |
| - var colliderType = ColliderType.Collider; |
137 |
| - if (col.attachedRigidbody != null && !col.attachedRigidbody.isKinematic) colliderType = ColliderType.PhysicsCollider; |
138 |
| - if (col.isTrigger) colliderType = ColliderType.Trigger; |
139 |
| - if (col is MeshCollider) colliderType = ColliderType.Mesh; |
140 |
| - var parent = col.transform; |
141 |
| - if (col is SphereCollider sphere) |
142 |
| - { |
143 |
| - var sphereTransform = RenderCollider(parent, GameObject.CreatePrimitive(PrimitiveType.Sphere), Vector3.one * sphere.radius * 2f, sphere.center, colliderType); |
144 |
| - sphereTransform.transform.parent = null; |
145 |
| - sphereTransform.transform.localScale = Vector3.one * sphere.radius * 2f; // spheres are always spherical regardless of parent size! |
146 |
| - sphereTransform.transform.parent = parent; |
147 |
| - } |
148 |
| - if (col is BoxCollider box) |
149 |
| - { |
150 |
| - RenderCollider(parent, GameObject.CreatePrimitive(PrimitiveType.Cube), box.size, box.center, colliderType); |
151 |
| - } |
152 |
| - if (col is CapsuleCollider capsule) |
153 |
| - { |
154 |
| - var capsuleTransform = RenderCollider(parent, GameObject.CreatePrimitive(PrimitiveType.Capsule), new Vector3(capsule.radius * 2f, capsule.height / 2, capsule.radius * 2f), capsule.center, colliderType); |
155 |
| - capsuleTransform.localEulerAngles = AnglesFromCapsuleDirection(capsule.direction); |
156 |
| - } |
157 |
| - if (col is MeshCollider meshCollider) |
158 |
| - { |
159 |
| - var rendererObj = new GameObject("Mesh Collider Object"); |
160 |
| - rendererObj.AddComponent<MeshRenderer>(); |
161 |
| - var mesh = new Mesh(); |
162 |
| - mesh = meshCollider.sharedMesh; |
163 |
| - rendererObj.AddComponent<MeshFilter>().mesh = mesh; |
164 |
| - RenderCollider(parent, rendererObj, Vector3.one, Vector3.zero, colliderType); |
165 |
| - } |
166 |
| - } |
167 |
| - |
168 |
| - private static Transform RenderCollider(Transform parent, GameObject shape, Vector3 localScale, Vector3 center, ColliderType colliderType) |
169 |
| - { |
170 |
| - Object.DestroyImmediate(shape.GetComponent<Collider>()); |
171 |
| - shape.transform.parent = parent; |
172 |
| - shape.transform.localScale = localScale; |
173 |
| - shape.transform.localPosition = center; |
174 |
| - shape.transform.localEulerAngles = Vector3.zero; |
175 |
| - var r = shape.GetComponent<Renderer>(); |
176 |
| - r.material = GetMaterialForColliderType(colliderType); |
177 |
| - colliderRendererObjects.Add(shape); |
178 |
| - return shape.transform; |
179 |
| - } |
180 |
| - |
181 |
| - private static Material GetMaterialForColliderType(ColliderType type) |
182 |
| - { |
183 |
| - switch (type) |
184 |
| - { |
185 |
| - default: return colliderMaterial; ; |
186 |
| - case ColliderType.Trigger: return triggerMaterial; |
187 |
| - case ColliderType.PhysicsCollider: return physicsColliderMaterial; |
188 |
| - case ColliderType.Mesh: return meshColliderMaterial; |
189 |
| - } |
190 |
| - |
191 |
| - } |
192 |
| - |
193 |
| - private static Vector3 AnglesFromCapsuleDirection(int capsuleDirection) |
194 |
| - { |
195 |
| - switch (capsuleDirection) |
196 |
| - { |
197 |
| - default: // x |
198 |
| - return Vector3.forward * 90; |
199 |
| - case 1: // y |
200 |
| - return Vector3.zero; |
201 |
| - case 2: // z |
202 |
| - return Vector3.right * 90f; |
203 |
| - } |
204 |
| - } |
205 |
| - |
206 |
| - private enum ColliderType |
207 |
| - { |
208 |
| - Collider, |
209 |
| - PhysicsCollider, |
210 |
| - Trigger, |
211 |
| - Mesh |
212 |
| - } |
213 |
| - |
214 |
| - [ConsoleCommand("lookingat")] |
215 |
| - public static void LookingAt(bool hitTriggers = false) |
| 28 | + public static void HideColliders(bool hideMessage = false) |
216 | 29 | {
|
217 |
| - Transform scanTransform = MainCameraControl.main.transform; |
218 |
| - if (Physics.Raycast(scanTransform.position + scanTransform.forward, scanTransform.forward, out RaycastHit hit, float.MaxValue, -1, hitTriggers ? QueryTriggerInteraction.Collide : QueryTriggerInteraction.Ignore)) |
219 |
| - { |
220 |
| - var hitGameObject = hit.collider.gameObject; |
221 |
| - var parent = hitGameObject.transform.parent; |
222 |
| - var attachedRb = hit.collider.attachedRigidbody; |
223 |
| - var root = UWE.Utils.GetEntityRoot(hitGameObject); |
224 |
| - ErrorMessage.AddMessage($"Raycast hit collider of name '{hitGameObject.name}'"); |
225 |
| - if (parent != null) |
226 |
| - { |
227 |
| - ErrorMessage.AddMessage($"Collider is direct child of '{parent.name}'"); |
228 |
| - } |
229 |
| - if (attachedRb != null) |
230 |
| - { |
231 |
| - ErrorMessage.AddMessage($"Collider is attached to the RigidBody '{attachedRb.gameObject.name}'"); |
232 |
| - } |
233 |
| - if (root != null) |
234 |
| - { |
235 |
| - ErrorMessage.AddMessage($"Entity root of this collider is '{root.name}'"); |
236 |
| - } |
237 |
| - } |
238 |
| - else |
239 |
| - { |
240 |
| - ErrorMessage.AddMessage("Raycast failed."); |
241 |
| - } |
| 30 | + DebugCollidersManager.main.HideColliders(); |
| 31 | + if (!hideMessage) ErrorMessage.AddMessage($"Colliders are no longer visible."); |
242 | 32 | }
|
| 33 | + /// <summary> |
| 34 | + /// Hides all shown colliders (alias) |
| 35 | + /// </summary> |
| 36 | + [ConsoleCommand("hd_colls")] |
| 37 | + public static void HideColliders2(bool hideMessage = false) => HideColliders(hideMessage); |
243 | 38 | }
|
244 | 39 | }
|
0 commit comments