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