Created
April 3, 2017 20:04
-
-
Save midoruton/32d5cf3b4e813301ba2756324dd93ea8 to your computer and use it in GitHub Desktop.
This file contains 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; | |
using System.Collections; | |
[ExecuteInEditMode] | |
public class Bezir3 : MonoBehaviour { | |
public GameObject StartObject; | |
public GameObject StartControlObject; | |
public GameObject EndObject; | |
public GameObject EndControlObject; | |
public int PointsNum = 100;//Number of points between start pos and end | |
public Color StartColor= Color.black; | |
public Color EndColor =Color.black; | |
private Vector3 StartPosition; | |
private Vector3 EndPosition; | |
private Vector3 StartControlPosition; | |
private Vector3 EndControlPosition; | |
private LineRenderer lineRenderer; | |
private Vector3[] positions; | |
// Use this for initialization | |
void Start () { | |
} | |
// Update is called once per frame | |
void Update () { | |
if (!this.GetComponent<LineRenderer> ()) { | |
lineRenderer = this.gameObject.AddComponent<LineRenderer> (); | |
lineRenderer.material = new Material (Shader.Find("Particles/Additive")); | |
} | |
lineRenderer.SetVertexCount (PointsNum+2); | |
lineRenderer.SetColors (StartColor, EndColor); | |
SetEnds (ref StartObject,ref StartControlObject, "Start","StartControl"); | |
SetEnds (ref EndObject, ref EndControlObject,"End","EndControl"); | |
StartPosition = StartObject.transform.position; | |
EndPosition = EndObject.transform.position; | |
StartControlPosition = StartControlObject.transform.position; | |
EndControlPosition = EndControlObject.transform.position; | |
for(int i = 0 ;i<=PointsNum+1;i++){ | |
lineRenderer.SetPosition (i,CalculatePosition((float)i / (float)(PointsNum+1))); | |
} | |
} | |
private Vector3 CalculatePosition(float t){ | |
return Mathf.Pow (1 - t, 3) * StartPosition | |
+ 3 * Mathf.Pow (1 - t, 2) * t * StartControlPosition | |
+ 3 * (1 - t) * t * t * EndControlPosition | |
+ t * t * t * EndPosition; | |
} | |
private void SetEnds(ref GameObject obj,ref GameObject controlObj ,string objName,string controlObjName){ | |
if (!obj) { | |
obj = GameObject.CreatePrimitive (PrimitiveType.Sphere); | |
obj.name = objName; | |
obj.transform.parent = this.transform; | |
obj.transform.localPosition = Vector3.zero; | |
} | |
if (!controlObj) { | |
controlObj = GameObject.CreatePrimitive (PrimitiveType.Sphere); | |
controlObj.name = controlObjName; | |
controlObj.transform.parent = obj.transform; | |
controlObj.transform.localPosition = Vector3.zero; | |
} | |
LineRenderer lineRenderer = obj.GetComponent<LineRenderer> (); | |
if (!lineRenderer) { | |
lineRenderer = obj.AddComponent<LineRenderer> (); | |
lineRenderer.material = new Material (Shader.Find("Particles/Additive")); | |
lineRenderer.SetWidth (0.1f, 0.1f); | |
} | |
lineRenderer.SetPosition (0,obj.transform.position); | |
lineRenderer.SetPosition (1,controlObj.transform.position); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment