Skip to content

Instantly share code, notes, and snippets.

@JohannesMP
Last active March 6, 2021 12:52
Show Gist options
  • Save JohannesMP/9b5906c034a8797691339548b44e1320 to your computer and use it in GitHub Desktop.
Save JohannesMP/9b5906c034a8797691339548b44e1320 to your computer and use it in GitHub Desktop.
using System;
using UnityEngine;
using Shapes;
using UnityEditor;
using UnityEngine.Rendering;
using UnityEngine.UIElements;
[ExecuteAlways]
public class CloseThicknessExample : ImmediateModeShapeDrawer {
[Min(0)]
public float pixelThickness = 100;
public Transform pointA;
public Transform pointB;
public bool volumetric = true;
public bool flipOrder = false;
private Color refColor = new Color(1, 1, 1, 0.25f);
private Color[] Colors => new [] {Color.red, Color.yellow};
private Vector3[] Points => new[] {pointA.position, pointB.position};
private int Index0 => flipOrder ? 1 : 0;
private int Index1 => flipOrder ? 0 : 1;
public override void DrawShapes(Camera cam) {
if (pointA == null || pointB == null) return;
using (Draw.Command(cam)) {
Draw.ResetAllDrawStates();
Draw.BlendMode = ShapesBlendMode.Opaque;
Draw.LineThicknessSpace = ThicknessSpace.Pixels;
Draw.LineGeometry = volumetric ? LineGeometry.Volumetric3D : LineGeometry.Billboard;
Draw.Line(Points[Index0], Points[Index1],
pixelThickness, Colors[Index0], Colors[Index1]);
// Reference Spheres (show expected thickness)
Draw.BlendMode = ShapesBlendMode.Transparent;
Draw.SphereRadiusSpace = ThicknessSpace.Pixels;
for (int i = 0; i < Points.Length; ++i) {
Draw.Sphere(Points[i], pixelThickness / 2, refColor);
}
}
}
#if UNITY_EDITOR
// Stuff to help debug draw view frustums and on-screen debug text
public override void OnEnable() {
base.OnEnable();
EditorApplication.update += RedrawAllSceneViews;
}
public override void OnDisable() {
base.OnDisable();
EditorApplication.update -= RedrawAllSceneViews;
}
private static void RedrawAllSceneViews() {
foreach (SceneView view in SceneView.sceneViews) {
view.Repaint();
}
}
private void OnDrawGizmos() {
Camera curCam = Camera.current;
Color frustumColor = Color.cyan;
frustumColor.a = 0.5f;
var sceneCams = SceneView.GetAllSceneCameras();
for (int i = 0; i < sceneCams.Length; ++i) {
var sceneCam = sceneCams[i];
if (sceneCam == curCam) continue;
float param01 = (float)i / Mathf.Max(sceneCams.Length, 1);
Color color = Color.HSVToRGB(param01, 0.6f, 1);
color.a = 0.4f;
Gizmos.color = color;
Gizmos.matrix = sceneCam.transform.localToWorldMatrix;
Gizmos.DrawFrustum(Vector3.zero, sceneCam.fieldOfView,
1, sceneCam.nearClipPlane, sceneCam.aspect);
}
if (pointA == null || pointB == null) return;
Vector3 viewPoint0 = curCam.WorldToViewportPoint(Points[Index0]);
DrawScreenText(curCam, new Vector2(10, 10),
$"Point0: {viewPoint0.x:0.00}, {viewPoint0.y:0.00}, {viewPoint0.z:0.00}", Colors[Index0]);
Vector3 viewPoint1 = curCam.WorldToViewportPoint(Points[Index1]);
DrawScreenText(curCam, new Vector2(10, 25),
$"Point1: {viewPoint1.x:0.00}, {viewPoint1.y:0.00}, {viewPoint1.z:0.00}", Colors[Index1]);
}
private static void DrawScreenText(Camera cam, Vector2 screenPos, string text, Color color) {
GUIStyle style = new GUIStyle { normal = { textColor = color } };
Vector3 topLeft = new Vector3(screenPos.x, cam.pixelHeight - screenPos.y, 1);
Handles.Label(cam.ScreenToWorldPoint(topLeft), text, style);
}
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment