Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
12
Question by ensomniac · Feb 03, 2013 at 03:52 AM · mathline rendererinterpolationmathf.lerpmathf.sin

Line drawing: How can I interpolate between points to create a smooth arc?

This image probably illustrates my issue best

I'm using the line renderer to create a line on screen based off user input. Think finger drawing.

The problem is that when a user draws too quickly, there isn't enough fidelity captured per frame to create a smooth line - it looks jaggy. I have an array of x,y point values in Javascript.

I'd like to figure out a simple way to take my array and smooth it out before sending the values to the line renderer by interpolating between the points I have. Any ideas?

Here is an example array that I'm working with based off user input:

 var user_touch_input = [(16.6, 7.4, 0.0),(15.5, 7.2, 0.0),(14.4, 7.1, 0.0),(13.5, 7.1, 0.0),(12.4, 7.7, 0.0)];

interp.png (121.8 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fattie · Jul 01, 2018 at 07:04 PM 0
Share

in general, what you're looking for is called a bezier. it is a very basic problem in computer science, and used constantly in Unity and almost all computer system.

4 Replies

· Add your reply
  • Sort: 
avatar image
18
Best Answer

Answer by Codetastic · Feb 03, 2013 at 08:41 AM

Thanks to this site, I was able to create the following script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Curver : MonoBehaviour {
     //arrayToCurve is original Vector3 array, smoothness is the number of interpolations. 
     public static Vector3[] MakeSmoothCurve(Vector3[] arrayToCurve,float smoothness){
         List<Vector3> points;
         List<Vector3> curvedPoints;
         int pointsLength = 0;
         int curvedLength = 0;
         
         if(smoothness < 1.0f) smoothness = 1.0f;
         
         pointsLength = arrayToCurve.Length;
         
         curvedLength = (pointsLength*Mathf.RoundToInt(smoothness))-1;
         curvedPoints = new List<Vector3>(curvedLength);
         
         float t = 0.0f;
         for(int pointInTimeOnCurve = 0;pointInTimeOnCurve < curvedLength+1;pointInTimeOnCurve++){
             t = Mathf.InverseLerp(0,curvedLength,pointInTimeOnCurve);
             
             points = new List<Vector3>(arrayToCurve);
             
             for(int j = pointsLength-1; j > 0; j--){
                 for (int i = 0; i < j; i++){
                     points[i] = (1-t)*points[i] + t*points[i+1];
                 }
             }
             
             curvedPoints.Add(points[0]);
         }
         
         return(curvedPoints.ToArray());
     }
 }


To use this just call Curver.MakeSmoothCurve(theArrayYouWantToCurve,numberOfInterpolations). Example:

 //javascript/unityscript example
 #pragma strict
 var points : Vector3[];
 
 var lineRenderer : LineRenderer;
 var c1 : Color = Color.yellow;
 var c2 : Color = Color.red;
 
 function Start () {
     points = Curver.MakeSmoothCurve(points,3.0);
     
     lineRenderer.SetColors(c1, c2);
     lineRenderer.SetWidth(0.5,0.5);
     lineRenderer.SetVertexCount(points.Length);
     var counter : int = 0;
     for(var i : Vector3 in points){
         lineRenderer.SetPosition(counter, i);
         ++counter;
     }
 }

Notes:

  1. Place in a folder that will be compiled earlier than your scripts if you are using unityscript/javascript. A "Plugins" folder will probably work.

  2. smoothness is rounded.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ensomniac · Feb 04, 2013 at 07:05 PM 1
Share

Wow, such an amazingly detailed & thoughtful answer. This code is flawless. Thanks to your help, my lines are smooth at last. Can't thank you enough for providing such a great answer.

avatar image Codetastic · Feb 04, 2013 at 09:06 PM 1
Share

Well I figured a detailed question deserved a decent answer. But the real thanks should go to Evgeny Demidov and Paul de Casteljau. All I did was put it into code.

avatar image J_P_ · May 22, 2013 at 08:09 PM 1
Share

While this is nice code, it doesn't seem to behave exactly like the question describes above. In his illustration, the smoothed path includes the original points -- this code doesn't produce smooth paths that do this

avatar image NinjaISV · Apr 06, 2017 at 09:04 PM 1
Share

Thank you! I have been trying to get this for a very long time and this works 100% great!

avatar image iruinedyourday NinjaISV · Apr 10, 2017 at 01:26 AM 0
Share

I am trying to figure out how to use this, is this c# or javastcript?

avatar image Aduci · Oct 11, 2017 at 11:14 AM 0
Share

Nice script, but it's performance is very weak, over 70 points i get fps lag, can yoz help pls?

avatar image
2

Answer by robertbu · Feb 03, 2013 at 06:25 AM

There is a lot of spline algorithms and spline code on the net you could adapt. If you are looking for something easier, iTween comes with smoothed Path that you could use to calculate the positions for additional points. There are also packages at the Asset store. Vectrosity will handle both the smoothing and line drawing. I've never used it, but I've read some good things about SuperSplines.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image podperson · Apr 30, 2015 at 02:57 PM 1
Share

This is a much better answer than the checked solution. iTween (now?) also includes a command for simply doing what you want. In essence there's iTween.PointOnPath(Vector3[], float) which returns a point along the smoothed path formed by the positions (there's also a version that takes an array of transforms and does the same thing).

avatar image ensomniac · Apr 30, 2015 at 03:03 PM 1
Share

I'm not sure I agree that adding an entire library to a project in order to accomplish one relatively simple task is a better solution than 40 lines of specific, tailored code.

avatar image vasilverdouw · Apr 21, 2020 at 02:10 PM 0
Share

Hey, sorry to come back on an old thread but could you maybe explain a little bit more how we would use this?

avatar image
0

Answer by ercion · Feb 17, 2017 at 09:12 AM

Codetastic's answer provides Bezier curves which don't reach middle points. Using iTween.PointOnPath gives you Catmull-Rom spline which is better in some situations.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by surajdico · Jun 07, 2018 at 05:29 AM

Am new to line renderer and is it possible I can use this code and assign to the device's x y z motion to draw like a graph thing. @ensomniac. thanks in advance.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help me out with a simple smoothing function for accelerometer data? 1 Answer

Lerp that wraps around a variable x 1 Answer

Mathf.Lerp is just jumping to maximum, no 5.0 interpolation. 1 Answer

How can I get a point position in circle line 1 Answer

Mathf.Lerp like User Define Function 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges