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 are updating our Terms of Service for all Unity subscription plans, effective October 13, 2022, to create a more streamlined, user-friendly set of terms. Please review them here:
    Dismiss Notice
  3. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

Polygon deformation

Discussion in 'Scripting' started by virmaygames, Aug 15, 2022.

  1. virmaygames


    Jan 18, 2022
    Hi there!

    I'm writing my own polygon collider, and i did script, where the polygon follows object transform states, including scaling deformation things.

    Code (CSharp):
    1. public class Polygon
    2.     {
    3.         //bunch of points
    4.         public Vector2[] vertexes;
    5.         //calculated points
    6.         [HideInInspector] public Vector2[] trueVertexes;
    8.         //t - transform component of object
    9.         public void Transform(Transform t)
    10.         {
    11.             trueVertexes = (Vector2[])vertexes.Clone();
    12.             Transform iterTransform = t;
    13.             Vector2 dir;
    15.             while(iterTransform != null)
    16.             {
    17.                 for (int i = 0; i < trueVertexes.Length; i++)
    18.                 {
    19.                     //scale
    20.                     if (iterTransform.localScale != trueVertexes[i] *= iterTransform.localScale;
    21.                     //rotate
    22.                     if (iterTransform.localEulerAngles.z != 0)
    23.                     {
    24.                         dir = trueVertexes[i];
    25.                         dir = Quaternion.Euler(0, 0, iterTransform.localEulerAngles.z) * dir;
    26.                         trueVertexes[i] = dir;
    27.                     }
    28.                 }
    29.                 //go to the next parent
    30.                 iterTransform = iterTransform.parent;
    31.             }
    32.             for (int i = 0; i < trueVertexes.Length; i++)
    33.                 //translate
    34.                 trueVertexes[i] += (Vector2)t.position;
    35.         }
    36.     }
    It works correctly, but.. Are there any ways to simplify this? Because I'm afraid that executing a "while" cycle every frame is not a good idea.

  2. lordofduct


    Oct 3, 2011
    First and foremost you usually should optimize if it's a problem. Have you ran the profiler to see the performance in game?

    With that said...

    Some optimizations I would do.

    1) Not use the array clone method if this is happening frequently. If this happens like every frame like you suggest in your comments, consider creating a cached array in your Awake/Start and copying the state of 'vertexes' if you need a reset every frame. This way you're not creating a new array and tossing it every frame.

    2) Instead of while looping over each parent and then looping every vertex... meaning you're getting vertexcount * parentcount number of total loops (note I'm ignoring the final translation loop for now).

    You could instead loop the parents, build the complete scale vector and quaternion (ensuring to respect the same order of operations to get an identical output). THEN loop the vertices and apply those vectors/quats to each vertex. Resulting in vertexcount + parentcount number of loops.

    3) If you followed #2, you could actually wrap the final loop of the translation loop into that #2 loop structure. Removing yet another full loop of the vertices.

    4) you could also read the t.position only once and put it in a variable, rather than reading it vertexcount times and casting it to Vector2.