@@ -30,6 +30,8 @@ public class VRTK_TunnelOverlay : MonoBehaviour
30
30
31
31
[ Tooltip ( "The color to use for the tunnel effect." ) ]
32
32
public Color effectColor = Color . black ;
33
+ [ Tooltip ( "An optional skybox texture to use for the tunnel effect." ) ]
34
+ public Texture effectSkybox ;
33
35
[ Tooltip ( "The initial amount of screen coverage the tunnel to consume without any movement." ) ]
34
36
[ Range ( 0f , 1f ) ]
35
37
public float initialEffectSize = 0f ;
@@ -43,6 +45,7 @@ public class VRTK_TunnelOverlay : MonoBehaviour
43
45
public float smoothingTime = 0.15f ;
44
46
45
47
protected Transform headset ;
48
+ protected Camera headsetCamera ;
46
49
protected Transform playarea ;
47
50
protected VRTK_TunnelEffect cameraEffect ;
48
51
protected float angularVelocity ;
@@ -53,9 +56,11 @@ public class VRTK_TunnelOverlay : MonoBehaviour
53
56
protected int shaderPropertyColor ;
54
57
protected int shaderPropertyAV ;
55
58
protected int shaderPropertyFeather ;
59
+ protected int shaderPropertySkyboxTexture ;
56
60
protected Color originalColor ;
57
61
protected float originalAngularVelocity ;
58
62
protected float originalFeatherSize ;
63
+ protected Texture originalSkyboxTexture ;
59
64
protected float maximumEffectCoverage = 1.15f ;
60
65
61
66
protected virtual void Awake ( )
@@ -64,17 +69,25 @@ protected virtual void Awake()
64
69
shaderPropertyColor = Shader . PropertyToID ( "_Color" ) ;
65
70
shaderPropertyAV = Shader . PropertyToID ( "_AngularVelocity" ) ;
66
71
shaderPropertyFeather = Shader . PropertyToID ( "_FeatherSize" ) ;
72
+ shaderPropertySkyboxTexture = Shader . PropertyToID ( "_SecondarySkyBox" ) ;
67
73
VRTK_SDKManager . instance . AddBehaviourToToggleOnLoadedSetupChange ( this ) ;
68
74
}
69
75
70
76
protected virtual void OnEnable ( )
71
77
{
72
78
headset = VRTK_DeviceFinder . HeadsetCamera ( ) ;
79
+ headsetCamera = headset . GetComponent < Camera > ( ) ;
73
80
playarea = VRTK_DeviceFinder . PlayAreaTransform ( ) ;
74
81
cameraEffect = headset . GetComponent < VRTK_TunnelEffect > ( ) ;
75
82
originalAngularVelocity = matCameraEffect . GetFloat ( shaderPropertyAV ) ;
76
83
originalFeatherSize = matCameraEffect . GetFloat ( shaderPropertyFeather ) ;
77
84
originalColor = matCameraEffect . GetColor ( shaderPropertyColor ) ;
85
+ CheckSkyboxTexture ( ) ;
86
+ if ( effectSkybox != null )
87
+ {
88
+ originalSkyboxTexture = matCameraEffect . GetTexture ( shaderPropertySkyboxTexture ) ;
89
+ matCameraEffect . SetTexture ( "_SecondarySkyBox" , effectSkybox ) ;
90
+ }
78
91
79
92
if ( cameraEffect == null )
80
93
{
@@ -86,10 +99,14 @@ protected virtual void OnEnable()
86
99
protected virtual void OnDisable ( )
87
100
{
88
101
headset = null ;
102
+ headsetCamera = null ;
89
103
playarea = null ;
90
104
91
105
if ( cameraEffect != null )
92
106
{
107
+ matCameraEffect . SetTexture ( "_SecondarySkyBox" , originalSkyboxTexture ) ;
108
+ originalSkyboxTexture = null ;
109
+
93
110
SetShaderFeather ( originalColor , originalAngularVelocity , originalFeatherSize ) ;
94
111
matCameraEffect . SetColor ( shaderPropertyColor , originalColor ) ;
95
112
Destroy ( cameraEffect ) ;
@@ -130,6 +147,25 @@ protected virtual void FixedUpdate()
130
147
131
148
lastForward = fwd ;
132
149
lastPosition = pos ;
150
+
151
+ if ( effectSkybox != null )
152
+ {
153
+ matCameraEffect . SetMatrixArray ( "_EyeToWorld" , new Matrix4x4 [ 2 ]
154
+ {
155
+ headsetCamera . GetStereoViewMatrix ( Camera . StereoscopicEye . Left ) . inverse ,
156
+ headsetCamera . GetStereoViewMatrix ( Camera . StereoscopicEye . Right ) . inverse
157
+ } ) ;
158
+
159
+ Matrix4x4 [ ] eyeProjection = new Matrix4x4 [ 2 ] ;
160
+ eyeProjection [ 0 ] = headsetCamera . GetStereoProjectionMatrix ( Camera . StereoscopicEye . Left ) ;
161
+ eyeProjection [ 1 ] = headsetCamera . GetStereoProjectionMatrix ( Camera . StereoscopicEye . Right ) ;
162
+ eyeProjection [ 0 ] = GL . GetGPUProjectionMatrix ( eyeProjection [ 0 ] , true ) . inverse ;
163
+ eyeProjection [ 1 ] = GL . GetGPUProjectionMatrix ( eyeProjection [ 1 ] , true ) . inverse ;
164
+ eyeProjection [ 0 ] [ 1 , 1 ] *= - 1f ;
165
+ eyeProjection [ 1 ] [ 1 , 1 ] *= - 1f ;
166
+
167
+ matCameraEffect . SetMatrixArray ( "_EyeProjection" , eyeProjection ) ;
168
+ }
133
169
}
134
170
135
171
protected virtual void SetShaderFeather ( Color givenTunnelColor , float givenAngularVelocity , float givenFeatherSize )
@@ -138,5 +174,24 @@ protected virtual void SetShaderFeather(Color givenTunnelColor, float givenAngul
138
174
matCameraEffect . SetFloat ( shaderPropertyAV , givenAngularVelocity ) ;
139
175
matCameraEffect . SetFloat ( shaderPropertyFeather , givenFeatherSize ) ;
140
176
}
177
+
178
+ protected virtual void CheckSkyboxTexture ( )
179
+ {
180
+ if ( effectSkybox == null )
181
+ {
182
+ Cubemap tempTexture = new Cubemap ( 1 , TextureFormat . ARGB32 , false ) ;
183
+ tempTexture . SetPixel ( CubemapFace . NegativeX , 0 , 0 , Color . white ) ;
184
+ tempTexture . SetPixel ( CubemapFace . NegativeY , 0 , 0 , Color . white ) ;
185
+ tempTexture . SetPixel ( CubemapFace . NegativeZ , 0 , 0 , Color . white ) ;
186
+ tempTexture . SetPixel ( CubemapFace . PositiveX , 0 , 0 , Color . white ) ;
187
+ tempTexture . SetPixel ( CubemapFace . PositiveY , 0 , 0 , Color . white ) ;
188
+ tempTexture . SetPixel ( CubemapFace . PositiveZ , 0 , 0 , Color . white ) ;
189
+ effectSkybox = tempTexture ;
190
+ }
191
+ else if ( effectColor . r < 0.15f && effectColor . g < 0.15 && effectColor . b < 0.15 )
192
+ {
193
+ VRTK_Logger . Warn ( "`VRTK_TunnelOverlay` has an `Effect Skybox` texture but the `Effect Color` is too dark which will tint the texture so it is not visible." ) ;
194
+ }
195
+ }
141
196
}
142
197
}
0 commit comments