- Home /
Question by
UnityToMakeMoney · Oct 12, 2020 at 04:24 AM ·
2dlinerendererdrawline renderer
Line Renderer Ending In the Center
Background Info: I am trying to make a drawing app for personal school use since there are some features I want that are either not implemented in other apps or I have to pay to use some of the features I want.
Current Goal: I am trying to simply make a smooth drawn line/curve(s) using the line renderer(but open to other suggestions) through vectors.
Problem: I got it to work perfectly fine, but for some reason, it just ends in the center of the screen (relative to the game camera). See Picture (put a point in each corner from left to right, should have made an upside-down "u").
Here is the script that draws:
using System.Collections;
using System.Collections.Generic;
using Unity.Mathematics;
using UnityEditor;
using UnityEngine;
public class SmoothDrawingScript : MonoBehaviour
{
//current camera
[SerializeField] private Camera myCam;
//the brush
[SerializeField] private GameObject brush;
//our delta since we can't do integrals
[SerializeField] private float changeOfRate = 0.05f;
//holds current points to draw
private List<Vector2> currPoints = new List<Vector2>();
LineRenderer lr;//will point to the line renderer of the brush
private void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0)) {
AddDrawingPoint();
if (currPoints.Count == 4)
Draw();
}
}
/* *
* Draw The Points Using A Bezier Curve
* */
private void Draw() {
GameObject brushInst = Instantiate(brush);
lr = brushInst.GetComponent<LineRenderer>();
List<Vector2> drawingPoints = Curve(currPoints, changeOfRate);
lr.positionCount += drawingPoints.Count;
Debug.Log("First Drawing Point: " + drawingPoints[0]);
//Draw all the points
for (int i = 0; i < drawingPoints.Count; i++) {
lr.SetPosition(i, drawingPoints[i]);
//lr.SetPosition(i+1, drawingPoints[i]);
}
lr = null;//this prevents accidental usage
//remove current points, but keep the last point
Vector2 lastPoint = currPoints[currPoints.Count-1];
Debug.Log("LastPoint: " + lastPoint);
currPoints.Clear();
currPoints.Add(lastPoint);
}
/* *
* Adds current mouse point to list
* */
private void AddDrawingPoint() {
Vector2 p = MouseToVector();//vector to add
Debug.Log("ATTEMPTING TO ADD POINT " + p);
//add if not a duplicate
if (currPoints.Count < 1)
currPoints.Add(p);
else if(currPoints.Count > 0 && currPoints[currPoints.Count-1] != p)
currPoints.Add(p);
}
/* *
* Returns a vector relative to the camera of the mouse position
* */
private Vector2 MouseToVector() {
return myCam.ScreenToWorldPoint(Input.mousePosition);
}
/* *
* Returns a list of vectors that is the curve
*
* @param a list of 4 vectors to create a curve from
* @param the increment from 0 to 1 for equation input
* */
private List<Vector2> Curve(List<Vector2> vec, float delta) {
List<Vector2> curve = new List<Vector2>();//list to output
for (float i = 0f; i <= 1f; i+=delta) {
Vector3 temp = (Mathf.Pow((1 - i), 3f) * vec[0]) +
(3*(Mathf.Pow((1-i),2f))*i*vec[1]) +
(3*(1-i)*Mathf.Pow(i,2f)*vec[2]) +
(Mathf.Pow(i,3)*vec[3]);
curve.Add(temp);//add result to list
}
return curve;
}
}
Thanks in advance for any help!
capture.png
(139.9 kB)
Comment