|
| 1 | +using KSPCommunityFixes.Library; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using Unity.Collections; |
| 5 | +using UnityEngine; |
| 6 | + |
| 7 | +namespace KSPCommunityFixes.Performance |
| 8 | +{ |
| 9 | + internal class FloatingOriginPerf : BasePatch |
| 10 | + { |
| 11 | + protected override Version VersionMin => new Version(1, 12, 3); |
| 12 | + |
| 13 | + protected override void ApplyPatches() |
| 14 | + { |
| 15 | + AddPatch(PatchType.Override, typeof(FloatingOrigin), nameof(FloatingOrigin.setOffset)); |
| 16 | + } |
| 17 | + |
| 18 | + private static HashSet<int> activePS = new HashSet<int>(200); |
| 19 | + |
| 20 | + private static void FloatingOrigin_setOffset_Override(FloatingOrigin fo, Vector3d refPos, Vector3d nonFrame) |
| 21 | + { |
| 22 | + if (refPos.IsInvalid()) |
| 23 | + return; |
| 24 | + |
| 25 | + if (double.IsInfinity(refPos.sqrMagnitude)) |
| 26 | + return; |
| 27 | + |
| 28 | + fo.SetOffsetThisFrame = true; |
| 29 | + fo.offset = refPos; |
| 30 | + fo.reverseoffset = new Vector3d(0.0 - refPos.x, 0.0 - refPos.y, 0.0 - refPos.z); |
| 31 | + fo.offsetNonKrakensbane = fo.offset + nonFrame; |
| 32 | + |
| 33 | + Vector3 offsetF = fo.offset; |
| 34 | + Vector3 offsetNonKrakensbaneF = fo.offsetNonKrakensbane; |
| 35 | + float deltaTime = Time.deltaTime; |
| 36 | + Vector3 frameVelocity = Krakensbane.GetFrameVelocity(); |
| 37 | + |
| 38 | + activePS.Clear(); |
| 39 | + |
| 40 | + List<CelestialBody> bodies = FlightGlobals.Bodies; |
| 41 | + for (int i = bodies.Count; i-- > 0;) |
| 42 | + bodies[i].position -= fo.offsetNonKrakensbane; |
| 43 | + |
| 44 | + bool needCoMRecalc = fo.offset.sqrMagnitude > fo.CoMRecalcOffsetMaxSqr; |
| 45 | + List<Vessel> vessels = FlightGlobals.Vessels; |
| 46 | + |
| 47 | + for (int i = vessels.Count; i-- > 0;) |
| 48 | + { |
| 49 | + Vessel vessel = vessels[i]; |
| 50 | + |
| 51 | + if (vessel.state == Vessel.State.DEAD) |
| 52 | + continue; |
| 53 | + |
| 54 | + Vector3d vesselOffset = (!vessel.loaded || vessel.packed || vessel.LandedOrSplashed) ? fo.offsetNonKrakensbane : fo.offset; |
| 55 | + vessel.SetPosition((Vector3d)vessel.transform.position - vesselOffset); |
| 56 | + |
| 57 | + if (needCoMRecalc && vessel.packed) |
| 58 | + { |
| 59 | + vessel.precalc.CalculatePhysicsStats(); |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + vessel.CoMD -= vesselOffset; |
| 64 | + vessel.CoM = vessel.CoMD; |
| 65 | + } |
| 66 | + |
| 67 | + // Update legacy (?) particle system |
| 68 | + for (int j = vessel.parts.Count; j-- > 0;) |
| 69 | + { |
| 70 | + Part part = vessel.parts[j]; |
| 71 | + |
| 72 | + if (part.fxGroups.Count == 0) |
| 73 | + continue; |
| 74 | + |
| 75 | + bool partDataComputed = false; |
| 76 | + bool hasRigidbody = false; |
| 77 | + Vector3 partVelocity = Vector3.zero; |
| 78 | + |
| 79 | + for (int k = part.fxGroups.Count; k-- > 0;) |
| 80 | + { |
| 81 | + FXGroup fXGroup = part.fxGroups[k]; |
| 82 | + for (int l = fXGroup.fxEmittersNewSystem.Count; l-- > 0;) |
| 83 | + { |
| 84 | + ParticleSystem particleSystem = fXGroup.fxEmittersNewSystem[l]; |
| 85 | + |
| 86 | + if (particleSystem.IsNullOrDestroyed()) |
| 87 | + continue; |
| 88 | + |
| 89 | + int particleCount = particleSystem.particleCount; |
| 90 | + if (particleCount == 0 || particleSystem.main.simulationSpace != ParticleSystemSimulationSpace.World) |
| 91 | + continue; |
| 92 | + |
| 93 | + activePS.Add(particleSystem.GetInstanceIDFast()); |
| 94 | + |
| 95 | + if (!partDataComputed) |
| 96 | + { |
| 97 | + partDataComputed = true; |
| 98 | + Rigidbody partRB = part.Rigidbody; |
| 99 | + if (partRB.IsNotNullOrDestroyed()) |
| 100 | + { |
| 101 | + hasRigidbody = true; |
| 102 | + partVelocity = partRB.velocity + frameVelocity; |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + NativeArray<ParticleSystem.Particle> particleBuffer = particleSystem.GetParticlesNativeArray(ref particleCount); |
| 107 | + for (int pIdx = particleCount; pIdx-- > 0;) |
| 108 | + { |
| 109 | + Vector3 particlePos = particleBuffer.GetParticlePosition(pIdx); |
| 110 | + |
| 111 | + if (hasRigidbody) |
| 112 | + { |
| 113 | + float scalar = UnityEngine.Random.value * deltaTime; |
| 114 | + particlePos.Substract(partVelocity.x * scalar, partVelocity.y * scalar, partVelocity.z * scalar); |
| 115 | + } |
| 116 | + |
| 117 | + particlePos.Substract(offsetNonKrakensbaneF); |
| 118 | + particleBuffer.SetParticlePosition(pIdx, particlePos); |
| 119 | + } |
| 120 | + particleSystem.SetParticles(particleBuffer, particleCount); |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + // update "new" (but just as shitty) particle system (this replicate a call to EffectBehaviour.OffsetParticles()) |
| 127 | + Vector3 systemVelocity = Vector3.zero; |
| 128 | + |
| 129 | + List<ParticleSystem> pSystems = EffectBehaviour.emitters; |
| 130 | + for (int i = pSystems.Count; i-- > 0;) |
| 131 | + { |
| 132 | + ParticleSystem particleSystem = pSystems[i]; |
| 133 | + |
| 134 | + if (particleSystem.IsNullOrDestroyed()) |
| 135 | + { |
| 136 | + pSystems.RemoveAt(i); |
| 137 | + continue; |
| 138 | + } |
| 139 | + |
| 140 | + int particleCount = particleSystem.particleCount; |
| 141 | + if (particleCount == 0 || particleSystem.main.simulationSpace != ParticleSystemSimulationSpace.World) |
| 142 | + continue; |
| 143 | + |
| 144 | + activePS.Add(particleSystem.GetInstanceIDFast()); |
| 145 | + |
| 146 | + bool hasRigidbody = false; |
| 147 | + Rigidbody rb = particleSystem.GetComponentInParent<Rigidbody>(); |
| 148 | + if (rb.IsNotNullRef()) |
| 149 | + { |
| 150 | + hasRigidbody = true; |
| 151 | + systemVelocity = rb.velocity + frameVelocity; |
| 152 | + } |
| 153 | + |
| 154 | + NativeArray<ParticleSystem.Particle> particleBuffer = particleSystem.GetParticlesNativeArray(ref particleCount); |
| 155 | + for (int pIdx = particleCount; pIdx-- > 0;) |
| 156 | + { |
| 157 | + Vector3 particlePos = particleBuffer.GetParticlePosition(pIdx); |
| 158 | + |
| 159 | + if (hasRigidbody) |
| 160 | + { |
| 161 | + float scalar = UnityEngine.Random.value * deltaTime; |
| 162 | + particlePos.Substract(systemVelocity.x * scalar, systemVelocity.y * scalar, systemVelocity.z * scalar); |
| 163 | + } |
| 164 | + |
| 165 | + particlePos.Substract(offsetNonKrakensbaneF); |
| 166 | + particleBuffer.SetParticlePosition(pIdx, particlePos); |
| 167 | + } |
| 168 | + particleSystem.SetParticles(particleBuffer, particleCount); |
| 169 | + } |
| 170 | + |
| 171 | + List<KSPParticleEmitter> pSystemsKSP = EffectBehaviour.kspEmitters; |
| 172 | + for (int i = pSystemsKSP.Count; i-- > 0;) |
| 173 | + { |
| 174 | + KSPParticleEmitter particleSystemKSP = pSystemsKSP[i]; |
| 175 | + |
| 176 | + if (particleSystemKSP.IsNullOrDestroyed()) |
| 177 | + { |
| 178 | + pSystemsKSP.RemoveAt(i); |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + int particleCount = particleSystemKSP.ps.particleCount; |
| 183 | + if (particleCount == 0 || !particleSystemKSP.useWorldSpace) |
| 184 | + continue; |
| 185 | + |
| 186 | + activePS.Add(particleSystemKSP.ps.GetInstanceIDFast()); |
| 187 | + |
| 188 | + bool hasRigidbody = false; |
| 189 | + Rigidbody rb = particleSystemKSP.GetComponentInParent<Rigidbody>(); |
| 190 | + if (rb.IsNotNullRef()) |
| 191 | + { |
| 192 | + hasRigidbody = true; |
| 193 | + systemVelocity = rb.velocity + frameVelocity; |
| 194 | + } |
| 195 | + |
| 196 | + NativeArray<ParticleSystem.Particle> particleBuffer = particleSystemKSP.ps.GetParticlesNativeArray(ref particleCount); |
| 197 | + for (int pIdx = particleCount; pIdx-- > 0;) |
| 198 | + { |
| 199 | + Vector3 particlePos = particleBuffer.GetParticlePosition(pIdx); |
| 200 | + |
| 201 | + if (hasRigidbody) |
| 202 | + { |
| 203 | + float scalar = UnityEngine.Random.value * deltaTime; |
| 204 | + particlePos.Substract(systemVelocity.x * scalar, systemVelocity.y * scalar, systemVelocity.z * scalar); |
| 205 | + } |
| 206 | + |
| 207 | + particlePos.Substract(offsetNonKrakensbaneF); |
| 208 | + particleBuffer.SetParticlePosition(pIdx, particlePos); |
| 209 | + } |
| 210 | + particleSystemKSP.ps.SetParticles(particleBuffer, particleCount); |
| 211 | + } |
| 212 | + |
| 213 | + // Just have another handling of the same stuff, sometimes overlapping, sometimes not, because why not ? |
| 214 | + for (int i = fo.particleSystems.Count; i-- > 0;) |
| 215 | + { |
| 216 | + ParticleSystem particleSystem = fo.particleSystems[i]; |
| 217 | + if (particleSystem.IsNullOrDestroyed() || activePS.Contains(particleSystem.GetInstanceIDFast())) |
| 218 | + { |
| 219 | + fo.particleSystems.RemoveAt(i); |
| 220 | + continue; |
| 221 | + } |
| 222 | + |
| 223 | + int particleCount = particleSystem.particleCount; |
| 224 | + if (particleCount == 0) |
| 225 | + continue; |
| 226 | + |
| 227 | + if (particleSystem.main.simulationSpace != ParticleSystemSimulationSpace.World) |
| 228 | + continue; |
| 229 | + |
| 230 | + if (activePS.Contains(particleSystem.GetInstanceIDFast())) |
| 231 | + { |
| 232 | + fo.particleSystems.RemoveAt(i); |
| 233 | + continue; |
| 234 | + } |
| 235 | + |
| 236 | + NativeArray<ParticleSystem.Particle> particleBuffer = particleSystem.GetParticlesNativeArray(ref particleCount); |
| 237 | + for (int pIdx = particleCount; pIdx-- > 0;) |
| 238 | + { |
| 239 | + Vector3 particlePos = particleBuffer.GetParticlePosition(pIdx); |
| 240 | + particlePos.Substract(offsetNonKrakensbaneF); |
| 241 | + particleBuffer.SetParticlePosition(pIdx, particlePos); |
| 242 | + } |
| 243 | + |
| 244 | + particleSystem.SetParticles(particleBuffer, particleCount); |
| 245 | + } |
| 246 | + |
| 247 | + // more particle system (explosions, fireworks...) moving in here, but this is getting silly, I don't care anymore... |
| 248 | + if (FlightGlobals.ActiveVessel != null && FlightGlobals.ActiveVessel.radarAltitude < fo.altToStopMovingExplosions) |
| 249 | + FXMonger.OffsetPositions(-fo.offsetNonKrakensbane); |
| 250 | + |
| 251 | + for (int i = FlightGlobals.physicalObjects.Count; i-- > 0;) |
| 252 | + { |
| 253 | + physicalObject physicalObject = FlightGlobals.physicalObjects[i]; |
| 254 | + if (physicalObject.IsNotNullOrDestroyed()) |
| 255 | + { |
| 256 | + Transform obj = physicalObject.transform; |
| 257 | + obj.position -= offsetF; |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + FloatingOrigin.TerrainShaderOffset += fo.offsetNonKrakensbane; |
| 262 | + GameEvents.onFloatingOriginShift.Fire(fo.offset, nonFrame); |
| 263 | + } |
| 264 | + } |
| 265 | +} |
0 commit comments