Last active
February 1, 2021 06:24
-
-
Save giacomelli/f4aba5c5d7cbeb396ca784dff150e194 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
/// <summary> | |
/// Drawing a Maurer Rose With Unity - http://diegogiacomelli.com.br/drawing-a-maurer-rose-with-unity | |
/// </summary> | |
[RequireComponent(typeof(LineRenderer))] | |
public class MaurerRoseLineRenderer : MonoBehaviour | |
{ | |
const int PointsCount = 361; | |
[SerializeField] | |
float N = 2; | |
[SerializeField] | |
float D = 29; | |
[SerializeField] | |
float Scale = 5; | |
Vector3[] _points; | |
float _k; | |
float _r; | |
float _x; | |
float _y; | |
Vector3 _previousPosition; | |
float _previousN; | |
float _previousD; | |
float _previousSize; | |
LineRenderer _lr; | |
void Start() | |
{ | |
_points = new Vector3[PointsCount]; | |
_lr = GetComponent<LineRenderer>(); | |
_lr.positionCount = PointsCount; | |
} | |
void Update() | |
{ | |
if (_previousPosition != transform.position | |
|| _previousD != D | |
|| _previousN != N | |
|| _previousSize != Scale) | |
{ | |
Draw(); | |
_previousPosition = transform.position; | |
_previousD = D; | |
_previousN = N; | |
_previousSize = Scale; | |
} | |
} | |
void Draw() | |
{ | |
for (int i = 0; i < PointsCount; i++) | |
{ | |
_k = i * (D * Mathf.PI / 180f); | |
_r = Scale * Mathf.Sin(N * _k); | |
_x = _r * Mathf.Cos(_k); | |
_y = _r * Mathf.Sin(_k); | |
_points[i] = transform.position + new Vector3(_x, _y, 0); | |
} | |
_lr.SetPositions(_points); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment