This commit is contained in:
Mister_Nebula 2021-01-05 15:58:12 +00:00
parent 2e0ba3ed27
commit 4e65e500ff
10 changed files with 102 additions and 130 deletions

View File

@ -2,7 +2,6 @@
using QSB.Patches;
using QSB.Player;
using QSB.QuantumSync.WorldObjects;
using QSB.Utility;
using QSB.WorldSync;
using System;
using System.Collections.Generic;

View File

@ -3,7 +3,6 @@ using QSB.Player;
using QSB.QuantumSync.WorldObjects;
using QSB.WorldSync;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;

View File

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
namespace Popcron
@ -21,21 +20,23 @@ namespace Popcron
//find all drawers
if (typeToDrawer == null)
{
typeToDrawer = new Dictionary<Type, Drawer>();
typeToDrawer = new Dictionary<Type, Drawer>
{
//add defaults
typeToDrawer.Add(typeof(CubeDrawer), new CubeDrawer());
typeToDrawer.Add(typeof(LineDrawer), new LineDrawer());
typeToDrawer.Add(typeof(PolygonDrawer), new PolygonDrawer());
typeToDrawer.Add(typeof(SquareDrawer), new SquareDrawer());
typeToDrawer.Add(typeof(FrustumDrawer), new FrustumDrawer());
//add defaults
{ typeof(CubeDrawer), new CubeDrawer() },
{ typeof(LineDrawer), new LineDrawer() },
{ typeof(PolygonDrawer), new PolygonDrawer() },
{ typeof(SquareDrawer), new SquareDrawer() },
{ typeof(FrustumDrawer), new FrustumDrawer() }
};
//find extras
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (Assembly assembly in assemblies)
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
foreach (var assembly in assemblies)
{
Type[] types = assembly.GetTypes();
foreach (Type type in types)
var types = assembly.GetTypes();
foreach (var type in types)
{
if (type.IsAbstract)
{
@ -46,7 +47,7 @@ namespace Popcron
{
try
{
Drawer value = (Drawer)Activator.CreateInstance(type);
var value = (Drawer)Activator.CreateInstance(type);
typeToDrawer[type] = value;
}
catch (Exception e)
@ -58,7 +59,7 @@ namespace Popcron
}
}
if (typeToDrawer.TryGetValue(typeof(T), out Drawer drawer))
if (typeToDrawer.TryGetValue(typeof(T), out var drawer))
{
return drawer;
}

View File

@ -11,21 +11,21 @@ namespace Popcron
public override int Draw(ref Vector3[] buffer, params object[] values)
{
Vector3 position = (Vector3)values[0];
Quaternion rotation = (Quaternion)values[1];
Vector3 size = (Vector3)values[2];
var position = (Vector3)values[0];
var rotation = (Quaternion)values[1];
var size = (Vector3)values[2];
size *= 0.5f;
Vector3 point1 = new Vector3(position.x - size.x, position.y - size.y, position.z - size.z);
Vector3 point2 = new Vector3(position.x + size.x, position.y - size.y, position.z - size.z);
Vector3 point3 = new Vector3(position.x + size.x, position.y + size.y, position.z - size.z);
Vector3 point4 = new Vector3(position.x - size.x, position.y + size.y, position.z - size.z);
var point1 = new Vector3(position.x - size.x, position.y - size.y, position.z - size.z);
var point2 = new Vector3(position.x + size.x, position.y - size.y, position.z - size.z);
var point3 = new Vector3(position.x + size.x, position.y + size.y, position.z - size.z);
var point4 = new Vector3(position.x - size.x, position.y + size.y, position.z - size.z);
Vector3 point5 = new Vector3(position.x - size.x, position.y - size.y, position.z + size.z);
Vector3 point6 = new Vector3(position.x + size.x, position.y - size.y, position.z + size.z);
Vector3 point7 = new Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
Vector3 point8 = new Vector3(position.x - size.x, position.y + size.y, position.z + size.z);
var point5 = new Vector3(position.x - size.x, position.y - size.y, position.z + size.z);
var point6 = new Vector3(position.x + size.x, position.y - size.y, position.z + size.z);
var point7 = new Vector3(position.x + size.x, position.y + size.y, position.z + size.z);
var point8 = new Vector3(position.x - size.x, position.y + size.y, position.z + size.z);
point1 = rotation * (point1 - position);
point1 += position;

View File

@ -3,7 +3,7 @@ using UnityEngine;
namespace Popcron
{
class FrustumDrawer : Drawer
internal class FrustumDrawer : Drawer
{
public FrustumDrawer()
{

View File

@ -11,24 +11,24 @@ namespace Popcron
public override int Draw(ref Vector3[] buffer, params object[] values)
{
Vector3 position = (Vector3)values[0];
int points = (int)values[1];
float radius = (float)values[2];
float offset = (float)values[3];
Quaternion rotation = (Quaternion)values[4];
var position = (Vector3)values[0];
var points = (int)values[1];
var radius = (float)values[2];
var offset = (float)values[3];
var rotation = (Quaternion)values[4];
float step = 360f / points;
var step = 360f / points;
offset *= Mathf.Deg2Rad;
for (int i = 0; i < points; i++)
for (var i = 0; i < points; i++)
{
float cx = Mathf.Cos(Mathf.Deg2Rad * step * i + offset) * radius;
float cy = Mathf.Sin(Mathf.Deg2Rad * step * i + offset) * radius;
Vector3 current = new Vector3(cx, cy);
var cx = Mathf.Cos(Mathf.Deg2Rad * step * i + offset) * radius;
var cy = Mathf.Sin(Mathf.Deg2Rad * step * i + offset) * radius;
var current = new Vector3(cx, cy);
float nx = Mathf.Cos(Mathf.Deg2Rad * step * (i + 1) + offset) * radius;
float ny = Mathf.Sin(Mathf.Deg2Rad * step * (i + 1) + offset) * radius;
Vector3 next = new Vector3(nx, ny);
var nx = Mathf.Cos(Mathf.Deg2Rad * step * (i + 1) + offset) * radius;
var ny = Mathf.Sin(Mathf.Deg2Rad * step * (i + 1) + offset) * radius;
var next = new Vector3(nx, ny);
buffer[i * 2] = position + (rotation * current);
buffer[(i * 2) + 1] = position + (rotation * next);

View File

@ -21,7 +21,7 @@ namespace Popcron
position = p3;
}
Quaternion rotation = (Quaternion)values[1];
var rotation = (Quaternion)values[1];
Vector2 size = default;
if (values[2] is Vector2 s2)

View File

@ -115,14 +115,8 @@ namespace Popcron
[Obsolete("This property is obsolete. Use FrustumCulling instead.", false)]
public static bool Cull
{
get
{
return FrustumCulling;
}
set
{
FrustumCulling = value;
}
get => FrustumCulling;
set => FrustumCulling = value;
}
[Obsolete("This property is obsolete. Subscribe to CameraFilter predicate instead and return true for your custom camera.", false)]
@ -202,12 +196,12 @@ namespace Popcron
const string Delim = ",";
if (_offset == null)
{
string data = PlayerPrefs.GetString($"{PrefsKey}.Offset", 0 + Delim + 0 + Delim + 0);
int indexOf = data.IndexOf(Delim);
int lastIndexOf = data.LastIndexOf(Delim);
var data = PlayerPrefs.GetString($"{PrefsKey}.Offset", 0 + Delim + 0 + Delim + 0);
var indexOf = data.IndexOf(Delim);
var lastIndexOf = data.LastIndexOf(Delim);
if (indexOf + lastIndexOf > 0)
{
string[] arr = data.Split(Delim[0]);
var arr = data.Split(Delim[0]);
_offset = new Vector3(float.Parse(arr[0]), float.Parse(arr[1]), float.Parse(arr[2]));
}
else
@ -239,13 +233,13 @@ namespace Popcron
return;
}
Drawer drawer = Drawer.Get<T>();
var drawer = Drawer.Get<T>();
if (drawer != null)
{
int points = drawer.Draw(ref buffer, args);
var points = drawer.Draw(ref buffer, args);
//copy from buffer and add to the queue
Vector3[] array = new Vector3[points];
var array = new Vector3[points];
Array.Copy(buffer, array, points);
GizmosInstance.Submit(array, color, dashed);
}
@ -267,42 +261,27 @@ namespace Popcron
/// <summary>
/// Draw line in world space.
/// </summary>
public static void Line(Vector3 a, Vector3 b, Color? color = null, bool dashed = false)
{
Draw<LineDrawer>(color, dashed, a, b);
}
public static void Line(Vector3 a, Vector3 b, Color? color = null, bool dashed = false) => Draw<LineDrawer>(color, dashed, a, b);
/// <summary>
/// Draw square in world space.
/// </summary>
public static void Square(Vector2 position, Vector2 size, Color? color = null, bool dashed = false)
{
Square(position, Quaternion.identity, size, color, dashed);
}
public static void Square(Vector2 position, Vector2 size, Color? color = null, bool dashed = false) => Square(position, Quaternion.identity, size, color, dashed);
/// <summary>
/// Draw square in world space with float diameter parameter.
/// </summary>
public static void Square(Vector2 position, float diameter, Color? color = null, bool dashed = false)
{
Square(position, Quaternion.identity, Vector2.one * diameter, color, dashed);
}
public static void Square(Vector2 position, float diameter, Color? color = null, bool dashed = false) => Square(position, Quaternion.identity, Vector2.one * diameter, color, dashed);
/// <summary>
/// Draw square in world space with a rotation parameter.
/// </summary>
public static void Square(Vector2 position, Quaternion rotation, Vector2 size, Color? color = null, bool dashed = false)
{
Draw<SquareDrawer>(color, dashed, position, rotation, size);
}
public static void Square(Vector2 position, Quaternion rotation, Vector2 size, Color? color = null, bool dashed = false) => Draw<SquareDrawer>(color, dashed, position, rotation, size);
/// <summary>
/// Draws a cube in world space.
/// </summary>
public static void Cube(Vector3 position, Quaternion rotation, Vector3 size, Color? color = null, bool dashed = false)
{
Draw<CubeDrawer>(color, dashed, position, rotation, size);
}
public static void Cube(Vector3 position, Quaternion rotation, Vector3 size, Color? color = null, bool dashed = false) => Draw<CubeDrawer>(color, dashed, position, rotation, size);
/// <summary>
/// Draws a rectangle in screen space.
@ -317,10 +296,7 @@ namespace Popcron
/// <summary>
/// Draws a representation of a bounding box.
/// </summary>
public static void Bounds(Bounds bounds, Color? color = null, bool dashed = false)
{
Draw<CubeDrawer>(color, dashed, bounds.center, Quaternion.identity, bounds.size);
}
public static void Bounds(Bounds bounds, Color? color = null, bool dashed = false) => Draw<CubeDrawer>(color, dashed, bounds.center, Quaternion.identity, bounds.size);
/// <summary>
/// Draws a cone similar to the one that spot lights draw.
@ -328,17 +304,17 @@ namespace Popcron
public static void Cone(Vector3 position, Quaternion rotation, float length, float angle, Color? color = null, bool dashed = false, int pointsCount = 16)
{
//draw the end of the cone
float endAngle = Mathf.Tan(angle * 0.5f * Mathf.Deg2Rad) * length;
Vector3 forward = rotation * Vector3.forward;
Vector3 endPosition = position + forward * length;
float offset = 0f;
var endAngle = Mathf.Tan(angle * 0.5f * Mathf.Deg2Rad) * length;
var forward = rotation * Vector3.forward;
var endPosition = position + forward * length;
var offset = 0f;
Draw<PolygonDrawer>(color, dashed, endPosition, pointsCount, endAngle, offset, rotation);
//draw the 4 lines
for (int i = 0; i < 4; i++)
for (var i = 0; i < 4; i++)
{
float a = i * 90f * Mathf.Deg2Rad;
Vector3 point = rotation * new Vector3(Mathf.Cos(a), Mathf.Sin(a)) * endAngle;
var a = i * 90f * Mathf.Deg2Rad;
var point = rotation * new Vector3(Mathf.Cos(a), Mathf.Sin(a)) * endAngle;
Line(position, position + point + forward * length, color, dashed);
}
}
@ -348,7 +324,7 @@ namespace Popcron
/// </summary>
public static void Sphere(Vector3 position, float radius, Color? color = null, bool dashed = false, int pointsCount = 16)
{
float offset = 0f;
var offset = 0f;
Draw<PolygonDrawer>(color, dashed, position, pointsCount, radius, offset, Quaternion.Euler(0f, 0f, 0f));
Draw<PolygonDrawer>(color, dashed, position, pointsCount, radius, offset, Quaternion.Euler(90f, 0f, 0f));
Draw<PolygonDrawer>(color, dashed, position, pointsCount, radius, offset, Quaternion.Euler(0f, 90f, 90f));
@ -359,8 +335,8 @@ namespace Popcron
/// </summary>
public static void Circle(Vector3 position, float radius, Camera camera, Color? color = null, bool dashed = false, int pointsCount = 16)
{
float offset = 0f;
Quaternion rotation = Quaternion.LookRotation(position - camera.transform.position);
var offset = 0f;
var rotation = Quaternion.LookRotation(position - camera.transform.position);
Draw<PolygonDrawer>(color, dashed, position, pointsCount, radius, offset, rotation);
}
@ -369,13 +345,10 @@ namespace Popcron
/// </summary>
public static void Circle(Vector3 position, float radius, Quaternion rotation, Color? color = null, bool dashed = false, int pointsCount = 16)
{
float offset = 0f;
var offset = 0f;
Draw<PolygonDrawer>(color, dashed, position, pointsCount, radius, offset, rotation);
}
public static void Frustum(OWCamera camera, Color? color = null, bool dashed = false)
{
Draw<FrustumDrawer>(color, dashed, camera);
}
public static void Frustum(OWCamera camera, Color? color = null, bool dashed = false) => Draw<FrustumDrawer>(color, dashed, camera);
}
}

View File

@ -44,7 +44,7 @@ namespace Popcron
{
get
{
GizmosInstance inst = GetOrCreate();
var inst = GetOrCreate();
if (inst.overrideMaterial)
{
return inst.overrideMaterial;
@ -54,7 +54,7 @@ namespace Popcron
}
set
{
GizmosInstance inst = GetOrCreate();
var inst = GetOrCreate();
inst.overrideMaterial = value;
}
}
@ -70,7 +70,7 @@ namespace Popcron
{
// Unity has a built-in shader that is useful for drawing
// simple colored things.
Shader shader = Shader.Find("Hidden/Internal-Colored");
var shader = Shader.Find("Hidden/Internal-Colored");
defaultMaterial = new Material(shader)
{
hideFlags = HideFlags.HideAndDontSave
@ -91,9 +91,9 @@ namespace Popcron
{
if (hotReloaded || !instance)
{
bool markDirty = false;
GizmosInstance[] gizmosInstances = FindObjectsOfType<GizmosInstance>();
for (int i = 0; i < gizmosInstances.Length; i++)
var markDirty = false;
var gizmosInstances = FindObjectsOfType<GizmosInstance>();
for (var i = 0; i < gizmosInstances.Length; i++)
{
instance = gizmosInstances[i];
@ -140,7 +140,7 @@ namespace Popcron
{
get
{
float time = 0f;
var time = 0f;
if (Application.isPlaying)
{
time = Time.time;
@ -161,7 +161,7 @@ namespace Popcron
/// </summary>
internal static void Submit(Vector3[] points, Color? color, bool dashed)
{
GizmosInstance inst = GetOrCreate();
var inst = GetOrCreate();
//if new frame, reset index
if (inst.lastFrame != Time.frameCount)
@ -173,8 +173,8 @@ namespace Popcron
//excedeed the length, so make it even bigger
if (inst.queueIndex >= inst.queue.Length)
{
Element[] bigger = new Element[inst.queue.Length + DefaultQueueSize];
for (int i = inst.queue.Length; i < bigger.Length; i++)
var bigger = new Element[inst.queue.Length + DefaultQueueSize];
for (var i = inst.queue.Length; i < bigger.Length; i++)
{
bigger[i] = new Element();
}
@ -194,7 +194,7 @@ namespace Popcron
{
//populate queue with empty elements
queue = new Element[DefaultQueueSize];
for (int i = 0; i < DefaultQueueSize; i++)
for (var i = 0; i < DefaultQueueSize; i++)
{
queue[i] = new Element();
}
@ -231,7 +231,7 @@ namespace Popcron
}
//allow the scene and main camera always
bool isSceneCamera = false;
var isSceneCamera = false;
#if UNITY_EDITOR
SceneView sceneView = SceneView.currentDrawingSceneView;
if (sceneView == null)
@ -266,9 +266,9 @@ namespace Popcron
}
//essentially check if at least 1 point is visible by the camera
for (int i = 0; i < points.points.Length; i++)
for (var i = 0; i < points.points.Length; i++)
{
Vector3 vp = camera.WorldToViewportPoint(points.points[i]);
var vp = camera.WorldToViewportPoint(points.points[i]);
if (vp.x >= 0 && vp.x <= 1 && vp.y >= 0 && vp.y <= 1)
{
return true;
@ -307,19 +307,19 @@ namespace Popcron
return;
}
Vector3 offset = Gizmos.Offset;
var offset = Gizmos.Offset;
GL.PushMatrix();
GL.MultMatrix(Matrix4x4.identity);
GL.Begin(GL.LINES);
bool alt = CurrentTime % 1 > 0.5f;
float dashGap = Mathf.Clamp(Gizmos.DashGap, 0.01f, 32f);
bool frustumCull = Gizmos.FrustumCulling;
List<Vector3> points = new List<Vector3>();
var alt = CurrentTime % 1 > 0.5f;
var dashGap = Mathf.Clamp(Gizmos.DashGap, 0.01f, 32f);
var frustumCull = Gizmos.FrustumCulling;
var points = new List<Vector3>();
//draw le elements
for (int e = 0; e < queueIndex; e++)
for (var e = 0; e < queueIndex; e++)
{
//just in case
if (queue.Length <= e)
@ -327,7 +327,7 @@ namespace Popcron
break;
}
Element element = queue[e];
var element = queue[e];
//dont render this thingy if its not inside the frustum
if (frustumCull)
@ -342,25 +342,25 @@ namespace Popcron
if (element.dashed)
{
//subdivide
for (int i = 0; i < element.points.Length - 1; i++)
for (var i = 0; i < element.points.Length - 1; i++)
{
Vector3 pointA = element.points[i];
Vector3 pointB = element.points[i + 1];
Vector3 direction = pointB - pointA;
var pointA = element.points[i];
var pointB = element.points[i + 1];
var direction = pointB - pointA;
if (direction.sqrMagnitude > dashGap * dashGap * 2f)
{
float magnitude = direction.magnitude;
int amount = Mathf.RoundToInt(magnitude / dashGap);
var magnitude = direction.magnitude;
var amount = Mathf.RoundToInt(magnitude / dashGap);
direction /= magnitude;
for (int p = 0; p < amount - 1; p++)
for (var p = 0; p < amount - 1; p++)
{
if (p % 2 == (alt ? 1 : 0))
{
float startLerp = p / (amount - 1f);
float endLerp = (p + 1) / (amount - 1f);
Vector3 start = Vector3.Lerp(pointA, pointB, startLerp);
Vector3 end = Vector3.Lerp(pointA, pointB, endLerp);
var startLerp = p / (amount - 1f);
var endLerp = (p + 1) / (amount - 1f);
var start = Vector3.Lerp(pointA, pointB, startLerp);
var end = Vector3.Lerp(pointA, pointB, endLerp);
points.Add(start);
points.Add(end);
}
@ -379,7 +379,7 @@ namespace Popcron
}
GL.Color(element.color);
for (int i = 0; i < points.Count; i++)
for (var i = 0; i < points.Count; i++)
{
GL.Vertex(points[i] + offset);
}

View File

@ -109,7 +109,7 @@ namespace QuantumUNET.Transport
{
for (var i = 0; i < count; i++)
{
m_Buffer[(int)checked((IntPtr)unchecked(Position + (ulong)i))] = buffer[i];
m_Buffer[(int)checked(unchecked(Position + (ulong)i))] = buffer[i];
}
}
Position += count;