-
Notifications
You must be signed in to change notification settings - Fork 0
/
gizmo_fov_distortion_fix.cs
43 lines (37 loc) · 1.38 KB
/
gizmo_fov_distortion_fix.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// #author habeebweeb
// #name Gizmo FOV Distortion Fix
// #desc Fixes gizmo size/orientation distortion when camera's fov changes
using System.Collections.Generic;
using System.Reflection.Emit;
using HarmonyLib;
public static class GizmoFOVDistortionFix
{
private static Harmony harmony;
public static void Main() =>
harmony = Harmony.CreateAndPatchAll(typeof(GizmoFOVDistortionFix));
public static void Unload()
{
if (harmony is null)
return;
harmony.UnpatchSelf();
harmony = null;
}
[HarmonyPatch(typeof(GizmoRender), nameof(GizmoRender.RenderGizmos))]
[HarmonyTranspiler]
private static IEnumerable<CodeInstruction> RenderGizmosTranspiler(IEnumerable<CodeInstruction> instructions, ILGenerator ilGenerator) =>
new CodeMatcher(instructions, ilGenerator)
.MatchStartForward(
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(OpCodes.Ldc_R4),
new CodeMatch(OpCodes.Ldc_R4),
new CodeMatch(OpCodes.Call))
.Advance(1)
.SetOperandAndAdvance(2f)
.Advance(4)
.InsertAndAdvance(
new CodeInstruction(OpCodes.Ldc_R4, UnityEngine.Mathf.Deg2Rad),
new CodeInstruction(OpCodes.Mul))
.Advance(4)
.SetOperandAndAdvance(3.5f)
.InstructionEnumeration();
}