Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Splines, large dataset of knots runs way too slow.

Discussion in 'Scripting' started by bdharmon, Oct 25, 2023.

  1. bdharmon

    bdharmon

    Joined:
    Aug 18, 2023
    Posts:
    4
    I am trying to draw a road using Unity Splines (v2.4.0). However, I am using a real life dataset and assigning the coordinates of each element to a new knot and adding that newly created knot to the spline container. The spline is being drawn out fine, however, the performance is HORRENDOUS. I am passing in a JSON file using TextAsset and then reading the JSON file using Unity's native JsonUtility.FromJson<>(). It's ~2500 items in the dataset, therefore producing ~2500 knots on a single spline. I have a simple 3D capsule game object animated on the spline. When I run Play Mode, it feels like I'm running it on a toaster. The editor itself is extremely laggy, responds to mouse clicks and scrolls a few seconds later. The capsule animates along the spline in extreme laggy, stuttering fashion. It feels like I'm playing a game at 5 fps and I obviously need it to run like a smooth 60fps game. How can I improve performance by a extremely significant margin? Do I need to cache? Do I need to parse the JSON file in a Coroutine? Below, I have attached my spline controller script and a text file of the dataset I am using. Thank you for any and all help.

    Screenshot 2023-10-25 164829.png

    Screenshot 2023-10-25 164915.png
     

    Attached Files:

  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,310
    Is this an assumption? I have never used the spline package but if there is some way to confirm via code or Inspector that it's indeed 2500 nodes it would help. I can imagine it may actually be a lot more.

    Then you seem to be testing this in the editor. If the Inspector shows any spline-related stuff or even includes an array with 2500 items in it, then rendering the Inspector GUI is likely what's slow. Try testing performance in a build. It may also help to deselect any object in the hierarchy, or just don't select the one with the spline.

    If you have a large dataset like this, you will want to take control over every step including rendering the splines. I wouldn't leave that to an existing codebase to do the right thing for you. It is unlikely to have been designed for this many knots, more than likely the use-case is for splines of a couple dozen to one or two hundred knots at most, given common game development use case scenarios.
     
  3. bdharmon

    bdharmon

    Joined:
    Aug 18, 2023
    Posts:
    4
    No assumption. When I expand the Spline component in the inspector, it is indeed an large array with 2500 knots in it. I've also tried building and testing (with a small and large dataset), the performance remains terrible. But you're also saying that if I use Unity's spline package, I will also need to do some modifications and custom code?
     
  4. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,129
    It feels like, or it is? What is the profiler telling you? What are the actual numbers you're getting?

    2500 points is pretty damn large for something that is quite mathematically and performance intensive, speaking from having build my own spline . But first you need to actually profile and figure out where the performance issue lies.
     
    CodeSmile likes this.
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    4,310
    And we don‘t know if you have one spline, or one hundred times 2500 knots.

    I‘m just thinking that the spline package was intended to do some camera flyby or something, typically you need maybe 10-50 knots. That I expect to be the typical use case. An Inspector array with thousands of items will cause slowdowns, no matter the component or array contents. Another point why I think the spline use case is for far fewer knots.

    I would say you need to look into a specialized solution, either an existing road generator asset or your custom implementation. With this many knots you may not even need a spline but merely a line renderer of some sort.
     
  6. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,047
    try also by NOT having the spline selected, having inspector with large public array or list selected often kills editor.

    i tested the original 2.0 splines release, was pretty slow if you added hundreds of objects moving in the spline,
    and i think in the splines forum thread people offered some tips to improve performance (but maybe those are already in)

    could test precalculating those values, like if you are going from 0-1 in time and evaluating spline pos,
    get those values into lookup-table and use from there.