|
| 1 | +using HarmonyLib; |
| 2 | +using KSPCommunityFixes.Library; |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Reflection; |
| 6 | +using System.Reflection.Emit; |
| 7 | +using UnityEngine; |
| 8 | +using Vectrosity; |
| 9 | + |
| 10 | +namespace KSPCommunityFixes.Performance |
| 11 | +{ |
| 12 | + public class OptimisedVectorLines : BasePatch |
| 13 | + { |
| 14 | + protected override Version VersionMin => new Version(1, 12, 0); |
| 15 | + |
| 16 | + protected override void ApplyPatches() |
| 17 | + { |
| 18 | + AddPatch(PatchType.Transpiler, typeof(VectorLine), nameof(VectorLine.Line3D)); |
| 19 | + |
| 20 | + AddPatch(PatchType.Transpiler, typeof(VectorLine), nameof(VectorLine.BehindCamera)); |
| 21 | + AddPatch(PatchType.Transpiler, typeof(VectorLine), nameof(VectorLine.IntersectAndDoSkip)); |
| 22 | + |
| 23 | + AddPatch(PatchType.Transpiler, typeof(VectorLine), nameof(VectorLine.Draw3D)); |
| 24 | + } |
| 25 | + |
| 26 | + #region VectorLine Patches |
| 27 | + |
| 28 | + static IEnumerable<CodeInstruction> VectorLine_Line3D_Transpiler(IEnumerable<CodeInstruction> instructions) => |
| 29 | + ReplaceWorldToScreenPoint(instructions, 2); |
| 30 | + |
| 31 | + static IEnumerable<CodeInstruction> VectorLine_BehindCamera_Transpiler(IEnumerable<CodeInstruction> instructions) => |
| 32 | + ReplaceWorldToViewportPoint(instructions, 2); |
| 33 | + |
| 34 | + static IEnumerable<CodeInstruction> VectorLine_IntersectAndDoSkip_Transpiler(IEnumerable<CodeInstruction> instructions) => |
| 35 | + ReplaceWorldToScreenPoint(instructions, 2); |
| 36 | + |
| 37 | + static IEnumerable<CodeInstruction> VectorLine_Draw3D_Transpiler(IEnumerable<CodeInstruction> instructions) |
| 38 | + { |
| 39 | + instructions = ReplaceWorldToScreenPoint(instructions, 2); |
| 40 | + instructions = ReplaceScreenToWorldPoint(instructions, 4); |
| 41 | + |
| 42 | + return instructions; |
| 43 | + } |
| 44 | + |
| 45 | + static IEnumerable<CodeInstruction> VectorLine_SetIntersectionPoint3D_Transpiler(IEnumerable<CodeInstruction> instructions) |
| 46 | + { |
| 47 | + return ReplaceScreenToWorldPoint(instructions, 2); |
| 48 | + } |
| 49 | + |
| 50 | + private static IEnumerable<CodeInstruction> ReplaceCall(IEnumerable<CodeInstruction> instructions, MethodInfo original, MethodInfo replacement, int count = 1) |
| 51 | + { |
| 52 | + List<CodeInstruction> code = new List<CodeInstruction>(instructions); |
| 53 | + int counter = 0; |
| 54 | + |
| 55 | + for (int i = 0; i < code.Count; i++) |
| 56 | + { |
| 57 | + if (code[i].opcode == OpCodes.Callvirt && code[i].Calls(original)) |
| 58 | + { |
| 59 | + code[i].opcode = OpCodes.Call; |
| 60 | + code[i].operand = replacement; |
| 61 | + |
| 62 | + if (++counter == count) |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + return code; |
| 68 | + } |
| 69 | + |
| 70 | + private static IEnumerable<CodeInstruction> ReplaceWorldToViewportPoint(IEnumerable<CodeInstruction> instructions, int count) |
| 71 | + { |
| 72 | + MethodInfo Camera_WorldToViewportPoint = AccessTools.Method(typeof(Camera), nameof(Camera.WorldToViewportPoint), new Type[] { typeof(Vector3) }); |
| 73 | + MethodInfo VectorLineOptimisation_WorldToViewportPoint = AccessTools.Method(typeof(VectorLineCameraProjection), nameof(VectorLineCameraProjection.WorldToViewportPoint)); |
| 74 | + |
| 75 | + return ReplaceCall(instructions, Camera_WorldToViewportPoint, VectorLineOptimisation_WorldToViewportPoint, count); |
| 76 | + } |
| 77 | + |
| 78 | + private static IEnumerable<CodeInstruction> ReplaceWorldToScreenPoint(IEnumerable<CodeInstruction> instructions, int count) |
| 79 | + { |
| 80 | + MethodInfo Camera_WorldToScreenPoint = AccessTools.Method(typeof(Camera), nameof(Camera.WorldToScreenPoint), new Type[] { typeof(Vector3) }); |
| 81 | + MethodInfo VectorLineOptimisation_WorldToScreenPoint = AccessTools.Method(typeof(VectorLineCameraProjection), nameof(VectorLineCameraProjection.WorldToScreenPoint)); |
| 82 | + |
| 83 | + return ReplaceCall(instructions, Camera_WorldToScreenPoint, VectorLineOptimisation_WorldToScreenPoint, count); |
| 84 | + } |
| 85 | + |
| 86 | + private static IEnumerable<CodeInstruction> ReplaceScreenToWorldPoint(IEnumerable<CodeInstruction> instructions, int count) |
| 87 | + { |
| 88 | + MethodInfo Camera_ScreenToWorldPoint = AccessTools.Method(typeof(Camera), nameof(Camera.ScreenToWorldPoint), new Type[] { typeof(Vector3) }); |
| 89 | + MethodInfo VectorLineOptimisation_ScreenToWorldPoint = AccessTools.Method(typeof(VectorLineCameraProjection), nameof(VectorLineCameraProjection.ScreenToWorldPoint)); |
| 90 | + |
| 91 | + return ReplaceCall(instructions, Camera_ScreenToWorldPoint, VectorLineOptimisation_ScreenToWorldPoint, count); |
| 92 | + } |
| 93 | + |
| 94 | + private static IEnumerable<CodeInstruction> ReplaceScreenToViewportPoint(IEnumerable<CodeInstruction> instructions, int count) |
| 95 | + { |
| 96 | + MethodInfo Camera_ScreenToViewportPoint = AccessTools.Method(typeof(Camera), nameof(Camera.ScreenToViewportPoint), new Type[] { typeof(Vector3) }); |
| 97 | + MethodInfo VectorLineOptimisation_ScreenToViewportPoint = AccessTools.Method(typeof(VectorLineCameraProjection), nameof(VectorLineCameraProjection.ScreenToViewportPoint)); |
| 98 | + |
| 99 | + return ReplaceCall(instructions, Camera_ScreenToViewportPoint, VectorLineOptimisation_ScreenToViewportPoint, count); |
| 100 | + } |
| 101 | + |
| 102 | + #endregion |
| 103 | + } |
| 104 | + |
| 105 | + public static class VectorLineCameraProjection |
| 106 | + { |
| 107 | + // Based on CameraProjectionCache from UnityCsReference. |
| 108 | + // https://github.com/Unity-Technologies/UnityCsReference/blob/2019.4/Editor/Mono/Camera/CameraProjectionCache.cs |
| 109 | + |
| 110 | + public static bool patchEnabled = true; |
| 111 | + public static long lastCachedFrame; |
| 112 | + |
| 113 | + private static TransformMatrix worldToClip; |
| 114 | + private static TransformMatrix clipToWorld; |
| 115 | + |
| 116 | + // Storing viewport info instead of using Rect properties grants us a few extra frames. |
| 117 | + public struct ViewportInfo |
| 118 | + { |
| 119 | + public double halfWidth; |
| 120 | + public double halfHeight; |
| 121 | + public double width; |
| 122 | + public double height; |
| 123 | + public double x; |
| 124 | + public double y; |
| 125 | + |
| 126 | + public ViewportInfo(Rect viewport) |
| 127 | + { |
| 128 | + width = viewport.width; |
| 129 | + height = viewport.height; |
| 130 | + halfWidth = width * 0.5; |
| 131 | + halfHeight = height * 0.5; |
| 132 | + x = viewport.x; |
| 133 | + y = viewport.y; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static ViewportInfo viewport; |
| 138 | + |
| 139 | + private static void UpdateCache() |
| 140 | + { |
| 141 | + lastCachedFrame = KSPCommunityFixes.UpdateCount; |
| 142 | + Camera camera = VectorLine.cam3D; |
| 143 | + |
| 144 | + viewport = new ViewportInfo(camera.pixelRect); |
| 145 | + |
| 146 | + Matrix4x4 worldToClip = camera.projectionMatrix * camera.worldToCameraMatrix; |
| 147 | + VectorLineCameraProjection.worldToClip = new TransformMatrix(ref worldToClip); |
| 148 | + |
| 149 | + Matrix4x4 worldToCameraInv = camera.worldToCameraMatrix.inverse; |
| 150 | + Matrix4x4 projectionInv = camera.projectionMatrix.inverse; |
| 151 | + projectionInv.m02 += projectionInv.m03; |
| 152 | + projectionInv.m12 += projectionInv.m13; |
| 153 | + projectionInv.m22 += projectionInv.m23; |
| 154 | + |
| 155 | + Matrix4x4 clipToWorld = worldToCameraInv * projectionInv; |
| 156 | + VectorLineCameraProjection.clipToWorld = new TransformMatrix(clipToWorld.m00, clipToWorld.m01, clipToWorld.m02, camera.worldToCameraMatrix.inverse.m03, |
| 157 | + clipToWorld.m10, clipToWorld.m11, clipToWorld.m12, camera.worldToCameraMatrix.inverse.m13, |
| 158 | + clipToWorld.m20, clipToWorld.m21, clipToWorld.m22, camera.worldToCameraMatrix.inverse.m23); |
| 159 | + } |
| 160 | + |
| 161 | + #region World to Clip |
| 162 | + |
| 163 | + public static Vector3 WorldToScreenPoint(Camera camera, Vector3 worldPosition) |
| 164 | + { |
| 165 | + // These patchEnabled checks are commented out atm in case they affect performance. |
| 166 | + // For testing they can be re-enabled and patchEnabled edited in UnityExplorer. |
| 167 | + |
| 168 | + //if (!patchEnabled) |
| 169 | + // return camera.WorldToScreenPoint(worldPosition); |
| 170 | + |
| 171 | + if (lastCachedFrame != KSPCommunityFixes.UpdateCount) |
| 172 | + UpdateCache(); |
| 173 | + |
| 174 | + double x = worldPosition.x; |
| 175 | + double y = worldPosition.y; |
| 176 | + double z = worldPosition.z; |
| 177 | + |
| 178 | + worldToClip.MutateMultiplyPoint3x4(ref x, ref y, ref z); |
| 179 | + |
| 180 | + double num = 0.5 / z; |
| 181 | + x = (0.5 + num * x) * viewport.width + viewport.x; |
| 182 | + y = (0.5 + num * y) * viewport.height + viewport.y; |
| 183 | + |
| 184 | + return new Vector3((float)x, (float)y, (float)z); |
| 185 | + } |
| 186 | + |
| 187 | + public static Vector3 WorldToViewportPoint(Camera camera, Vector3 worldPosition) |
| 188 | + { |
| 189 | + //if (!patchEnabled) |
| 190 | + // return camera.WorldToViewportPoint(worldPosition); |
| 191 | + |
| 192 | + if (lastCachedFrame != KSPCommunityFixes.UpdateCount) |
| 193 | + UpdateCache(); |
| 194 | + |
| 195 | + double x = worldPosition.x; |
| 196 | + double y = worldPosition.y; |
| 197 | + double z = worldPosition.z; |
| 198 | + |
| 199 | + worldToClip.MutateMultiplyPoint3x4(ref x, ref y, ref z); |
| 200 | + |
| 201 | + double num = 0.5 / z; |
| 202 | + x = 0.5 + num * x; |
| 203 | + y = 0.5 + num * y; |
| 204 | + |
| 205 | + return new Vector3((float)x, (float)y, (float)z); |
| 206 | + } |
| 207 | + |
| 208 | + #endregion |
| 209 | + |
| 210 | + #region Clip to World |
| 211 | + |
| 212 | + public static Vector3 ScreenToWorldPoint(Camera camera, Vector3 screenPosition) |
| 213 | + { |
| 214 | + //if (!patchEnabled) |
| 215 | + // return camera.ScreenToWorldPoint(screenPosition); |
| 216 | + |
| 217 | + if (lastCachedFrame != KSPCommunityFixes.UpdateCount) |
| 218 | + UpdateCache(); |
| 219 | + |
| 220 | + double x = screenPosition.x; |
| 221 | + double y = screenPosition.y; |
| 222 | + double z = screenPosition.z; |
| 223 | + |
| 224 | + x = z * ((x - viewport.x) / viewport.halfWidth - 1); |
| 225 | + y = z * ((y - viewport.y) / viewport.halfHeight - 1); |
| 226 | + |
| 227 | + clipToWorld.MutateMultiplyPoint3x4(ref x, ref y, ref z); |
| 228 | + |
| 229 | + return new Vector3((float)x, (float)y, (float)z); |
| 230 | + } |
| 231 | + |
| 232 | + public static Vector3 ScreenToViewportPoint(Camera camera, Vector3 position) |
| 233 | + { |
| 234 | + //if (!patchEnabled) |
| 235 | + // return camera.ScreenToViewportPoint(position); |
| 236 | + |
| 237 | + //if (lastCachedFrame != KSPCommunityFixes.frameCount) |
| 238 | + // UpdateCache(); |
| 239 | + |
| 240 | + // Not used by VectorLine. |
| 241 | + throw new NotImplementedException(); |
| 242 | + } |
| 243 | + |
| 244 | + #endregion |
| 245 | + } |
| 246 | +} |
0 commit comments