Skip to content

Commit 1f08bbb

Browse files
committed
Add Scopes (closes #17)
1 parent d016052 commit 1f08bbb

File tree

6 files changed

+102
-60
lines changed

6 files changed

+102
-60
lines changed

TABGVR/Assets/README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
# Notice
2+
13
You may notice there's no source code here, only a binary file. This is a Unity AssetBundle containing scope shaders and
24
modifications. I cannot legally distribute the source code of this Unity project as it contains copyrighted assets from
35
TABG. You can decompile it if you really want though. There is no executables in this file since AssetBundles don't
4-
support that as a security measure, so there isn't any undocumented source code that would give you a virus or anything.
6+
support that as a security measure, so there isn't any undocumented source code that would give you a virus or anything.
7+
8+
# Asset Credits
9+
**Scope Shader** - [DarkBlade909](https://github.com/DarkBlade909)'s [Scope Shader](https://github.com/DarkBlade909/ScopeShader)

TABGVR/Assets/vr0.tabgvrasset

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:08d474beb202002567c2560bab98196e437d6da5cec91f5b65b34dce2a26e459
3-
size 11097
2+
oid sha256:87191ddda468ccc3b5b9d49d72b653e3268075497dcd73565012c59826189ca4
3+
size 46624

TABGVR/Input/VRControls.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void Start()
4949
interactionHandler.canNotPickUpAction = () => currentPickup = null;
5050

5151
inputHandler.enabled = false;
52-
inputHandler.cameraMovement.ADS = true;
52+
inputHandler.cameraMovement.ADS = false;
5353

5454
menuTransitions = InventoryUI.instance.gameObject.GetComponent<MenuTransitions>();
5555
mapHandler = InventoryUI.instance.gameObject.GetComponentInChildren<MapHandler>();

TABGVR/Patches/Scopes/RedDotPatch.cs renamed to TABGVR/Patches/Scopes/ScopePatch.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ namespace TABGVR.Patches.Scopes;
77

88
[HarmonyPatch(typeof(RedDot))]
99
[VRPatch, FlatscreenPatch]
10-
public static class RedDotPatch
10+
public static class ScopePatch
1111
{
1212
[HarmonyPatch(nameof(RedDot.Start))]
1313
[HarmonyPostfix]
1414
public static void Start(RedDot __instance)
1515
{
1616
__instance.dotTransform.gameObject.SetActive(true);
1717
__instance.enabled = false;
18-
__instance.gameObject.AddComponent<VRRedDot>();
18+
__instance.gameObject.AddComponent<VRScope>();
1919
}
2020
}

TABGVR/Scopes/VRRedDot.cs

Lines changed: 0 additions & 54 deletions
This file was deleted.

TABGVR/Scopes/VRScope.cs

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using DeepSky.Haze;
4+
using Unity.XR.CoreUtils;
5+
using UnityEngine;
6+
using Object = System.Object;
7+
8+
namespace TABGVR.Scopes;
9+
10+
public class VRScope : MonoBehaviour
11+
{
12+
private RedDot redDot;
13+
private bool success;
14+
private const float Multiplier = 1 / (0.4f * 3.5f * 0.12f * 0.7f);
15+
16+
// scope sizes (magnification -> fov)
17+
private static readonly Dictionary<double, int> FOV = new()
18+
{
19+
{ 8, 15 },
20+
{ 4, 27 },
21+
{ 2, 40 },
22+
{ 0.5, 125 }
23+
};
24+
25+
private void Start()
26+
{
27+
redDot = GetComponent<RedDot>();
28+
29+
Plugin.Logger.LogInfo(transform.parent.name);
30+
if (!transform.parent.name.StartsWith("RedDot"))
31+
{
32+
var rt = new RenderTexture(768, 768, 32, RenderTextureFormat.ARGB32);
33+
rt.Create();
34+
35+
var stringMagnification = transform.parent.name.Split("x")[0];
36+
var doubleMagnification = double.Parse(stringMagnification);
37+
38+
var diff = Instantiate(
39+
AssetBundle.Bundle0.LoadAsset<GameObject>($"{stringMagnification}xDiff"), transform);
40+
41+
diff.transform.localPosition = Vector3.zero;
42+
diff.transform.localRotation = Quaternion.identity;
43+
diff.transform.localScale = Vector3.one;
44+
45+
var scopeMaterial = diff.transform.Find("ScopeLens").GetComponent<MeshRenderer>().material;
46+
var cam = diff.transform.Find("Camera").GetComponent<Camera>();
47+
48+
scopeMaterial.SetTexture("_RenderTexture", rt);
49+
50+
cam.targetTexture = rt;
51+
cam.gameObject.AddComponent<FlareLayer>();
52+
cam.gameObject.AddComponent<DS_HazeView>();
53+
cam.fieldOfView = FOV[doubleMagnification];
54+
55+
success = true;
56+
return;
57+
}
58+
59+
var lens = transform.Find("GameObject").Find("Model").Find("W_RedDot").Find("W_RedDot_Glass").gameObject;
60+
lens.SetActive(true); // enable lens glass
61+
62+
lens.GetComponent<MeshRenderer>().material =
63+
AssetBundle.Bundle0.LoadAsset<Material>("Assets/TABGVR/Scopes/RedDot/Glass.mat");
64+
redDot.dotTransform.GetComponent<MeshRenderer>().material =
65+
AssetBundle.Bundle0.LoadAsset<Material>("Assets/TABGVR/Scopes/RedDot/RedDot.mat");
66+
}
67+
68+
private void Update()
69+
{
70+
// this method just bruteforces the positioning until it doesn't error :|
71+
if (success) return;
72+
73+
try
74+
{
75+
// ReSharper disable once Unity.PerformanceCriticalCodeInvocation
76+
var shootPosition = redDot.camMove.weaponHandler.m_holding.heldObject.rig.GetComponent<Gun>()
77+
.shootPositions;
78+
79+
redDot.dotTransform.localPosition =
80+
Vector3.forward * (redDot.transform.InverseTransformPoint(shootPosition[0].position).z * Multiplier);
81+
82+
redDot.dotTransform.localScale = Vector3.one * (0.006f * redDot.dotTransform.localPosition.z);
83+
84+
success = true;
85+
}
86+
catch
87+
{
88+
// ignored
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)