Search Unity

Question Floating Original - Fixed but not fixed?

Discussion in 'Editor & General Support' started by Aviation_Simmer, Nov 16, 2022.

  1. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    I am working on a flight simulator. which ig going to have a large world. Now I am using a floating original script, which acctually works very well. there is no glittering going on. but... here's the catch. The glittering stopped but the transforms (positions) are getting unprecised. Is there any way to solve that? thanks, Jonas

    https://drive.google.com/file/d/1-DTHoAF9is-EtiwkJbuMNwlHYYKUenkq/view?usp=share_link

    https://drive.google.com/file/d/1-ApoRvl8gBXNV9X3nRjuqkLcNfsbTnSq/view?usp=share_link
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    These two statements:

    Make me suspect you did not actually correctly implement floating origin.

    Fortunately it is trivial to see: when something starts "getting unprecised," print out the position of it.

    If any axis of the position is greater than 1000 (or whatever upper quanta you chose) units from (0,0,0) in any axis, you're not doing floating origin.
     
  3. Aviation_Simmer

    Aviation_Simmer

    Joined:
    Aug 30, 2021
    Posts:
    110
    I mean... I have to move the world origin every 1024m. as you can see in the videos. You have to move the floating origin position every x meters, to stop the shaking. Maybe I don't get it?

    Code (CSharp):
    1. // Based on the Unity Wiki FloatingOrigin script by Peter Stirling
    2. // URL: http://wiki.unity3d.com/index.php/Floating_Origin
    3.  
    4. using UnityEngine;
    5. using UnityEngine.SceneManagement;
    6. using UnityEngine.VFX;
    7. using UnityEngine.Experimental.VFX;
    8.  
    9. public class FloatingOrginal : MonoBehaviour
    10. {
    11.     [Tooltip("Point of reference from which to check the distance to origin.")]
    12.  
    13.     public Transform ReferenceObject = null;
    14.  
    15.  
    16.  
    17.     [Tooltip("Distance from the origin the reference object must be in order to trigger an origin shift.")]
    18.     public float Threshold = 0f;
    19.  
    20.     [Header("Options")]
    21.     [Tooltip("When true, origin shifts are considered only from the horizontal distance to orign.")]
    22.     public bool Use2DDistance = false;
    23.  
    24.     [Tooltip("When true, updates ALL open scenes. When false, updates only the active scene.")]
    25.     public bool UpdateAllScenes = true;
    26.  
    27.     [Tooltip("Should ParticleSystems be moved with an origin shift.")]
    28.     public bool UpdateParticles = true;
    29.  
    30.     [Tooltip("Should TrailRenderers be moved with an origin shift.")]
    31.     public bool UpdateTrailRenderers = true;
    32.  
    33.     [Tooltip("Should LineRenderers be moved with an origin shift.")]
    34.     public bool UpdateLineRenderers = true;
    35.  
    36.     private ParticleSystem.Particle[] parts = null;
    37.  
    38.    
    39.  
    40.  
    41.     void Update()
    42.     {
    43.         Vector3 referencePosition = ReferenceObject.position;
    44.  
    45.         if (Use2DDistance)
    46.             referencePosition.y = 100f;
    47.  
    48.         if (referencePosition.magnitude > Threshold)
    49.         {
    50.             MoveRootTransforms(referencePosition);
    51.  
    52.             if (UpdateParticles)
    53.                 MoveParticles(referencePosition);
    54.  
    55.             if (UpdateTrailRenderers)
    56.                 MoveTrailRenderers(referencePosition);
    57.  
    58.             if (UpdateLineRenderers)
    59.                 MoveLineRenderers(referencePosition);
    60.         }
    61.     }
    62.  
    63.     private void MoveRootTransforms(Vector3 offset)
    64.     {
    65.         if (UpdateAllScenes)
    66.         {
    67.             for (int z = 0; z < SceneManager.sceneCount; z++)
    68.             {
    69.                 foreach (GameObject g in SceneManager.GetSceneAt(z).GetRootGameObjects())
    70.                     g.transform.position -= offset;
    71.             }
    72.         }
    73.         else
    74.         {
    75.             foreach (GameObject g in SceneManager.GetActiveScene().GetRootGameObjects())
    76.                 g.transform.position -= offset;
    77.         }
    78.     }
    79.  
    80.     private void MoveTrailRenderers(Vector3 offset)
    81.     {
    82.         var trails = FindObjectsOfType<TrailRenderer>() as TrailRenderer[];
    83.         foreach (var trail in trails)
    84.         {
    85.             Vector3[] positions = new Vector3[trail.positionCount];
    86.  
    87.             int positionCount = trail.GetPositions(positions);
    88.             for (int i = 0; i < positionCount; ++i)
    89.                 positions[i] -= offset;
    90.  
    91.             trail.SetPositions(positions);
    92.         }
    93.     }
    94.  
    95.     private void MoveLineRenderers(Vector3 offset)
    96.     {
    97.         var lines = FindObjectsOfType<LineRenderer>() as LineRenderer[];
    98.         foreach (var line in lines)
    99.         {
    100.             Vector3[] positions = new Vector3[line.positionCount];
    101.  
    102.             int positionCount = line.GetPositions(positions);
    103.             for (int i = 0; i < positionCount; ++i)
    104.                 positions[i] -= offset;
    105.  
    106.             line.SetPositions(positions);
    107.         }
    108.     }
    109.  
    110.     private void MoveParticles(Vector3 offset)
    111.     {
    112.         var particles = FindObjectsOfType<ParticleSystem>() as ParticleSystem[];
    113.         foreach (ParticleSystem system in particles)
    114.         {
    115.             if (system.main.simulationSpace != ParticleSystemSimulationSpace.World)
    116.                 continue;
    117.  
    118.             int particlesNeeded = system.main.maxParticles;
    119.  
    120.             if (particlesNeeded <= 0)
    121.                 continue;
    122.  
    123.             bool wasPaused = system.isPaused;
    124.             bool wasPlaying = system.isPlaying;
    125.  
    126.             if (!wasPaused)
    127.                 system.Pause();
    128.  
    129.             // ensure a sufficiently large array in which to store the particles
    130.             if (parts == null || parts.Length < particlesNeeded)
    131.             {
    132.                 parts = new ParticleSystem.Particle[particlesNeeded];
    133.             }
    134.  
    135.             // now get the particles
    136.             int num = system.GetParticles(parts);
    137.  
    138.             for (int i = 0; i < num; i++)
    139.             {
    140.                 parts[i].position -= offset;
    141.             }
    142.  
    143.             system.SetParticles(parts, num);
    144.  
    145.             if (wasPlaying)
    146.                 system.Play();
    147.         }
    148.  
    149.         var particles2 = FindObjectsOfType<VisualEffect>() as VisualEffect[];
    150.         foreach (VisualEffect system in particles2)
    151.         {
    152.             int particlesNeeded = system.aliveParticleCount;
    153.  
    154.             if (particlesNeeded <= 0)
    155.                 continue;
    156.  
    157.             bool wasPaused = !system.isActiveAndEnabled;
    158.             bool wasPlaying = system.isActiveAndEnabled;
    159.  
    160.             if (!wasPaused)
    161.                 system.Stop();
    162.         }
    163.     }
    164. }