Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug Netcode 1.1.0-exp.1 Prediction Smoothing Action backup / previous values can be invalid.

Discussion in 'NetCode for ECS' started by desertGhost_, Oct 21, 2023.

  1. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    259
    Hi,
    I have noticed that invalid values can sometimes be applied to the previous/backup value in a Prediction Smoothing Action. To create a simple reproduction, I added the following system to the Asteroids Sample:

    Code (CSharp):
    1. using Unity.Burst;
    2. using Unity.Entities;
    3. using Unity.NetCode;
    4. using Unity.Transforms;
    5. using System;
    6. using Unity.Collections.LowLevel.Unsafe;
    7. using Unity.Mathematics;
    8.  
    9. namespace Samples.Asteroids.Client.Systems
    10. {
    11.     [BurstCompile]
    12.     [WorldSystemFilter(WorldSystemFilterFlags.ClientSimulation)]
    13.     [CreateAfter(typeof(GhostPredictionSmoothingSystem))]
    14.     public partial struct RegisterPredictionSmoothingSystem : ISystem
    15.     {
    16.         public void OnCreate(ref SystemState state)
    17.         {
    18.             // ReSharper disable once Unity.Entities.SingletonMustBeRequested
    19.             var ghostPredictionSmoothing = SystemAPI.GetSingleton<GhostPredictionSmoothing>();
    20.  
    21.             ghostPredictionSmoothing.RegisterSmoothingAction<LocalTransform>(state.EntityManager,
    22.                 MyTranslationSmoothingAction.Action);
    23.         }
    24.     }
    25.    
    26.     /// <summary>
    27.     /// The default prediction error <see cref="SmoothingAction"/> function for the <see cref="Translation"/> component.
    28.     /// Supports the user data that lets you customize the clamping and snapping of the translation component (any time the translation prediction error is too large).
    29.     /// </summary>
    30.     [BurstCompile]
    31.     public unsafe struct MyTranslationSmoothingAction
    32.     {
    33.         /// <summary>
    34.         /// The default value for the <see cref="DefaultSmoothingActionUserParams"/> if the no user data is passed to the function.
    35.         /// Position is corrected if the prediction error is at least 1 unit (usually mt) and less than 10 unit (usually mt)
    36.         /// </summary>
    37.         public sealed class DefaultStaticUserParams
    38.         {
    39.             internal static readonly SharedStatic<float> maxDist =
    40.                 SharedStatic<float>.GetOrCreate<DefaultStaticUserParams, MaxDistKey>();
    41.  
    42.             internal static readonly SharedStatic<float> delta =
    43.                 SharedStatic<float>.GetOrCreate<DefaultStaticUserParams, DeltaKey>();
    44.  
    45.             static DefaultStaticUserParams()
    46.             {
    47.                 maxDist.Data = 10;
    48.                 delta.Data = 1;
    49.             }
    50.  
    51.             class MaxDistKey
    52.             {
    53.             }
    54.  
    55.             class DeltaKey
    56.             {
    57.             }
    58.         }
    59.  
    60.         /// <summary>
    61.         /// Return a the burst compatible function pointer that can be used to register the smoothing action to the
    62.         /// <see cref="GhostPredictionSmoothing"/> singleton.
    63.         /// </summary>
    64.         public static readonly PortableFunctionPointer<GhostPredictionSmoothing.SmoothingActionDelegate> Action =
    65.             new PortableFunctionPointer<GhostPredictionSmoothing.SmoothingActionDelegate>(SmoothingAction);
    66.  
    67.         [BurstCompile(DisableDirectCall = true)]
    68.         [AOT.MonoPInvokeCallback(typeof(GhostPredictionSmoothing.SmoothingActionDelegate))]
    69.         private static void SmoothingAction(IntPtr currentData, IntPtr previousData, IntPtr usrData)
    70.         {
    71.             ref var trans = ref UnsafeUtility.AsRef<LocalTransform>((void*) currentData);
    72.             ref var backup = ref UnsafeUtility.AsRef<LocalTransform>((void*) previousData);
    73.  
    74.             float maxDist = DefaultStaticUserParams.maxDist.Data;
    75.             float delta = DefaultStaticUserParams.delta.Data;
    76.  
    77.             if (usrData.ToPointer() != null)
    78.             {
    79.                 ref var userParam = ref UnsafeUtility.AsRef<DefaultSmoothingActionUserParams>(usrData.ToPointer());
    80.                 maxDist = userParam.maxDist;
    81.                 delta = userParam.delta;
    82.             }
    83.  
    84.             var dist = math.distance(trans.Position, backup.Position);
    85.            
    86.            
    87.             UnityEngine.Debug.Log($"cp: {trans.Position} bp: {backup.Position}");
    88.            
    89.             if (dist < maxDist && dist > delta && dist > 0)
    90.             {
    91.                 trans.Position = backup.Position + (trans.Position - backup.Position) * delta / dist;
    92.             }
    93.         }
    94.     }
    95. }
    The MyTranslationSmoothingAction is a copy of the DefaultTranslationSmoothingAction with a debug added. The debug statement will sometimes generate the following result:
    Erroneous NaN and near zero values are sometimes loaded into the backup / previous LocalTransform.
     
  2. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    873
    Hey! mmm this is indeed interesting. We will check this with high priority! Super thanks for reporting this.
    Would you mind to open a case for it so we can track the issue properly?
     
  3. desertGhost_

    desertGhost_

    Joined:
    Apr 12, 2018
    Posts:
    259
    Hi,

    I have filed a bug report for this issue: CASE IN-58319.

    Thank you.
     
    Last edited: Oct 23, 2023
    CMarastoni likes this.
  4. Jawsarn

    Jawsarn

    Joined:
    Jan 12, 2017
    Posts:
    230
    Was this fixed for you with the [1.1.0-pre.3]?
    I am having the same issue on [1.0.15] (cherry picking the fixes did not seem to solve it).
     
    Last edited: Dec 1, 2023
  5. CMarastoni

    CMarastoni

    Unity Technologies

    Joined:
    Mar 18, 2020
    Posts:
    873
    IT should have been fixed in 1.1. I will double check just in case.
     
    Jawsarn likes this.