Skip to content

Instantly share code, notes, and snippets.

@docky
Created April 13, 2023 08:56
Show Gist options
  • Select an option

  • Save docky/bcfa591cf323b7c37b3ff14f7825737a to your computer and use it in GitHub Desktop.

Select an option

Save docky/bcfa591cf323b7c37b3ff14f7825737a to your computer and use it in GitHub Desktop.
code for drawing nice Gizmo Cylinders in Unity
using System;
using UnityEngine;
public class CylinderGizmoDemo : MonoBehaviour
{
public Color _color = Color.cyan;
public float _radius = 0.5f;
public float _height = 2f;
public int _circlePoints = 16;
public int _slices = 2;
public int _upwardLines = 6;
public bool _drawEndLines;
private void OnDrawGizmos()
{
var pos = transform.position;
var rot = transform.rotation;
Gizmos.color = _color;
// basic call
DrawCylinderGizmo(pos, rot, _radius, _height, _circlePoints, _slices, _upwardLines, _drawEndLines);
// simple call
// DrawCylinderGizmo(pos, rot, _radius*2, _height*2);
// even simpler call
// DrawCylinderGizmo(_radius, _height);
}
private void DrawCylinderGizmo(float radius, float height)
{
var pos = transform.position;
var rot = transform.rotation;
;
DrawCylinderGizmo(pos, rot, radius, height);
}
private void DrawCylinderGizmo(Vector3 position, Quaternion rotation, float radius, float height,
int circlePoints = 24, int horizontalSlices = 1, int upwardLines = 6, bool drawEndLines = false)
{
// sanitise the input values silently
radius = Mathf.Clamp(radius, 0.01f, 100f);
height = Mathf.Clamp(height, 0.01f, 100f);
circlePoints = Mathf.Clamp(circlePoints, 3, 48);
upwardLines = Mathf.Clamp(upwardLines, 1, 48);
horizontalSlices = Mathf.Clamp(horizontalSlices, 0, 48);
// set position etc
Gizmos.matrix = Matrix4x4.TRS(position, rotation, Vector3.one);
Vector3[] topCirclePoints = new Vector3[circlePoints];
Vector3[] bottomCirclePoints = new Vector3[circlePoints];
for (int i = 0; i < circlePoints; i++)
{
float angle = (2 * Mathf.PI / circlePoints) * i;
float x = radius * Mathf.Cos(angle);
float z = radius * Mathf.Sin(angle);
topCirclePoints[i] = new Vector3(x, height, z);
bottomCirclePoints[i] = new Vector3(x, 0, z);
}
// draw slices
if (horizontalSlices > 0)
{
float sliceHeightStep = height / (horizontalSlices + 1);
for (int i = 1; i <= horizontalSlices; i++)
{
float sliceHeight = sliceHeightStep * i;
for (int j = 0; j < circlePoints; j++)
{
Vector3 poleStart = new Vector3(bottomCirclePoints[j].x, sliceHeight, bottomCirclePoints[j].z);
Vector3 poleEnd = j < circlePoints - 1
? new Vector3(bottomCirclePoints[j + 1].x, sliceHeight, bottomCirclePoints[j + 1].z)
: new Vector3(bottomCirclePoints[0].x, sliceHeight, bottomCirclePoints[0].z);
Gizmos.DrawLine(poleStart, poleEnd);
}
}
}
upwardLines = Math.Min(circlePoints, upwardLines);
if (upwardLines > 0)
{
int upwardLineStep = circlePoints / upwardLines;
for (int i = 0; i < circlePoints; i++)
{
if (i % upwardLineStep == 0)
{
Gizmos.DrawLine(bottomCirclePoints[i], topCirclePoints[i]);
// Draw a line from the circle center to the edge of each vertical pole
if (drawEndLines)
{
Gizmos.DrawLine(Vector3.zero, bottomCirclePoints[i]);
Gizmos.DrawLine(Vector3.up * height, topCirclePoints[i]);
}
}
if (i < circlePoints - 1)
{
Gizmos.DrawLine(bottomCirclePoints[i], bottomCirclePoints[i + 1]);
Gizmos.DrawLine(topCirclePoints[i], topCirclePoints[i + 1]);
}
else
{
Gizmos.DrawLine(bottomCirclePoints[i], bottomCirclePoints[0]);
Gizmos.DrawLine(topCirclePoints[i], topCirclePoints[0]);
}
}
}
Gizmos.matrix = Matrix4x4.identity;
}
}
@docky

docky commented Apr 13, 2023

Copy link
Copy Markdown
Author

Unity_uqFuLAJbWs
some code to make nice looking cylinders

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment