Tanoda
SuperellipsePoints.cs
Go to the documentation of this file.
1
5
6using System.Collections;
7using System.Collections.Generic;
8using UnityEngine;
9
11{
14[ExecuteInEditMode]
15public class SuperellipsePoints : MonoBehaviour
16{
17 public float xLimits = 1f;
18 public float yLimits = 1f;
19 [Range(1f, 96f)]
20 public float superness = 4f;
21
22 private float lastXLim;
23 private float lastYLim;
24 private float lastSuper;
25
26 [Space]
27 [Range(1, 32)]
28 public int levelOfDetail = 4;
29
30 private int lastLoD;
31
32 [Space]
33 public Material material;
34
35 private List<Vector2> pointList = new List<Vector2>();
36
37 void Start()
38 {
39 RecalculateSuperellipse();
40
41 GetComponent<MeshRenderer>().material = material;
42
43 lastXLim = xLimits;
44 lastYLim = yLimits;
45 lastSuper = superness;
46
47 lastLoD = levelOfDetail;
48 }
49
50 void Update()
51 {
52 if (lastXLim != xLimits || lastYLim != yLimits || lastSuper != superness || lastLoD != levelOfDetail)
53 {
54 RecalculateSuperellipse();
55 }
56
57 lastXLim = xLimits;
58 lastYLim = yLimits;
59 lastSuper = superness;
60
61 lastLoD = levelOfDetail;
62 }
63
64 void RecalculateSuperellipse()
65 {
66 pointList.Clear();
67
68 float realLoD = levelOfDetail * 4;
69
70 for (float i = 0; i < xLimits; i += 1 / realLoD)
71 {
72 float y = Superellipse(xLimits, yLimits, i, superness);
73 Vector2 tempVecTwo = new Vector2(i, y);
74 pointList.Add(tempVecTwo);
75 }
76 pointList.Add(new Vector2(xLimits, 0));
77 pointList.Add(Vector2.zero);
78
79 GetComponent<MeshCreator>().CreateMesh(pointList);
80 }
81
82 float Superellipse(float a, float b, float x, float n)
83 {
84 float alpha = Mathf.Pow((x / a), n);
85 float beta = 1 - alpha;
86 float y = Mathf.Pow(beta, 1 / n) * b;
87
88 return y;
89 }
90}
91}
Credit Erdener Gonenc - @PixelEnvision.