Skip to content

Instantly share code, notes, and snippets.

@romainPechot
Created July 3, 2015 13:49
Show Gist options
  • Save romainPechot/4c5cedc302f07002db2d to your computer and use it in GitHub Desktop.
Save romainPechot/4c5cedc302f07002db2d to your computer and use it in GitHub Desktop.
Geometry Lib
using UnityEngine;
using System.Collections;
[System.Serializable]
public class TwirlLineRenderer
{
[SerializeField] private LineRenderer lineRenderer;
[SerializeField] private float radius = 1f;
[SerializeField] private float lineWidth = 0.25f;
[SerializeField] private float loop = 1f;
private const int stepMin = 8;
private const int stepMax = 64;
[Range(stepMin, stepMax)] [SerializeField] private int step = 1;
private const float twirlMin = -1f;
private const float twirlMax = -1f;
[Range(twirlMin, twirlMax)] [SerializeField] private float twirlXDirection = 1f;
[Range(twirlMin, twirlMax)] [SerializeField] private float twirlZDirection = 1f;
[Range(twirlMin, twirlMax)] [SerializeField] private float twirlYDirection = 1f;
private Vector3 vertPos = Vector3.zero;
private int stepTot = 10;
public void SetLoop(float loop)
{
this.loop = Mathf.Max(loop, 0f);
}// SetLoop()
public void SetLineWidth(float lineWidth)
{
this.lineWidth = lineWidth;
}// SetLineWidth()
public void SetRadius(float radius)
{
this.radius = radius;
}// SetRadius()
public void Refresh()
{
if(lineRenderer == null) return;
step = Mathf.Clamp(step, stepMin, stepMax);
SetLoop(loop);
stepTot = Mathf.FloorToInt(loop * (step + 1));
float iStep = 1f;
iStep /= (float)(stepTot - 1);
lineRenderer.SetVertexCount(stepTot);
lineRenderer.SetWidth(lineWidth, lineWidth);
for(int i = 0; i < stepTot; i++)
{
float _f_ratio = i * iStep;
float _f_sphere = Mathf.Cos((90f + 180f * _f_ratio) * Mathf.Deg2Rad);
vertPos.y = Mathf.Sin((90f + 180f * _f_ratio) * Mathf.Deg2Rad) * radius * twirlYDirection;
vertPos.x = Mathf.Cos(360f * _f_ratio * loop * Mathf.Deg2Rad) * radius * _f_sphere * twirlXDirection;
vertPos.z = Mathf.Sin(360f * _f_ratio * loop * Mathf.Deg2Rad) * radius * _f_sphere * twirlZDirection;
lineRenderer.SetPosition(i, vertPos);
}//for()
}// Refresh()
}// TwirlLineRenderer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment