-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchangeBackdrop.js
482 lines (403 loc) · 17.5 KB
/
changeBackdrop.js
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#pragma strict
var newMat : Material;
var origMat : Material;
var fadeTex : Texture2D;
var mainCamera : GameObject;
var cam : Camera;
var closePlaneTransform : Transform;
static var closePlaneRenderer : Renderer;
var cloudCylinderObj : GameObject;
static var cloudCylinderRenderer : Renderer;
static var startingCloudAlpha : float; // Unity 4 used .39f (99 in RGBA)
var newCloudAlpha : float = .3f;
static var cloudOriginalMaterial : Material;
var oceanCamera : GameObject;
var oceanCameraVR : GameObject;
private var oceanCameraVRHead : GvrHead;
var backdropCameraObj : GameObject;
var backdropCamera : Camera;
var backdropCameraVR : GameObject;
private var backdropCameraVRHead : GvrHead;
var oceanRenderer : Renderer;
var cloudRenderer : Renderer;
var eyeCamerasVR : Array;
var StereoControllerComponent : Component;
//var foo : Material; //set this in the editor
//var bar : Material; //set this in the editor
var oceanLevel : boolean = false;
var ShouldUseOceanCamera : boolean = false;
var ShouldChangeBackdrop : boolean = false;
var FogOnly : boolean = false;
private var origSkybox : Material;
var altSkybox : Material;
var farClipPlaneValueOrig : int;
var farClipPlaneValue : int = 2500;
var fogEndValue : int = 3000;
var farClipPlaneFadeTime : float = 3;
var farClipPlaneValue2 : int = 2800;
var fogEndValue2 : int = 1500;
var farClipPlaneFadeTime2 : float = 2;
function Start () {
if (oceanLevel == true) {
if (!mainCamera) {
mainCamera = Camera.main.gameObject;
// mainCamera = transform.Find("Player-cameras/Camera").gameObject;
}
oceanCamera =
transform.Find("Player-cameras/Camera-for-ocean") ?
transform.Find("Player-cameras/Camera-for-ocean").gameObject : null;
var oceanCameraVRTransform : Transform = mainCamera.transform.Find("Camera-for-ocean-VR");
oceanCameraVR = oceanCameraVRTransform ? oceanCameraVRTransform.gameObject : null;
oceanRenderer = gameObject.Find("sky-water-ocean/Mesh").GetComponent.<Renderer>();
oceanRenderer.enabled = false;
cloudRenderer = gameObject.Find("simple-cloud-plane/Mesh").GetComponent.<Renderer>();
cloudRenderer.enabled = false;
}
if (ShouldChangeBackdrop) {
origSkybox = RenderSettings.skybox;
}
var backdropCameraTransform : Transform = transform.Find("Player-cameras/Camera-for-backdrop");
backdropCameraObj = backdropCameraTransform ? backdropCameraTransform.gameObject : null;
backdropCamera = backdropCameraObj.GetComponent.<Camera>();
var backdropCameraVRTransform : Transform = mainCamera.transform.Find("Camera-for-bg-VR");
backdropCameraVR = backdropCameraVRTransform ? backdropCameraVRTransform.gameObject : null;
cam = mainCamera.GetComponent.<Camera>();
farClipPlaneValueOrig = cam.farClipPlane;
// Warn re. plane-close/Cylinder if they haven't been manually associated via the Inspector:
if (ShouldChangeBackdrop || oceanLevel) {
if (!cloudCylinderObj && Debug.isDebugBuild) {
Debug.Log('Did you forget to link your cloudCylinderObj in the Inspector?');
}
if (cloudCylinderObj) {
cloudCylinderRenderer = cloudCylinderObj.GetComponent.<Renderer>();
cloudOriginalMaterial = cloudCylinderRenderer.material;
startingCloudAlpha = cloudOriginalMaterial.color.a; // Storing for later use.
}
if (!closePlaneTransform && backdropCameraTransform) {
closePlaneTransform = backdropCameraTransform.Find("plane-close");
}
if (closePlaneTransform) {
closePlaneRenderer = closePlaneTransform.GetComponent.<Renderer>();
}
}
// HACK: A coroutine to check for out-of-alignment sub-camera GvrHeads and fix them
// (Ocean and Backdrop cameras). A more robust solution would be to ensure that
// only one master GvrHead is applied to the scene at all, so we don't have to worry
// about manging its rotation and inherited stereo cameras' values.
if (FallingLaunch.isVRMode) {
CheckAndFixSecondaryVRCameras(1.5);
} else {
// if we want to keep the 'legacy' 2D rectangle-based backgrounds...
// SetupNonVRBackground();
}
// For now, try using the same skybox for VR and regular play:
ClearNonVRBackground();
}
function Update () {
// Existence check required for StereoControllerComponent in Update()
// since it takes 1+ seconds to set up everything via GVR plugin:
if (FallingLaunch.isVRMode) {
if (mainCamera && !StereoControllerComponent) {
StereoControllerComponent = mainCamera.GetComponent.<StereoController>();
}
} else {
return;
}
}
function ClearNonVRBackground () {
if (cloudCylinderObj) {
Debug.Log("about to disable cloudCylinderObj " + cloudCylinderObj);
cloudCylinderObj.SetActive(false);
}
if (backdropCameraObj) {
Debug.Log("about to disable backdropCameraObj " + backdropCameraObj);
backdropCameraObj.SetActive(false);
}
}
function SetupNonVRBackground () {
// backdropCamera starts as disabled, so it shouldn't get a stereoController or GvrHead
// auto-applied (and thus shouldn't need manual checking or rotation-fixing):
if (backdropCamera) {backdropCamera.enabled = true;}
if (backdropCameraVR) {backdropCameraVR.SetActive(false);}
}
function CheckAndFixSecondaryVRCameras (interval : float) {
MaintainOceanVRCamera();
MaintainBackdropVRCamera();
while (true && FallingLaunch.isVRMode) {
yield WaitForSeconds(interval);
MaintainOceanVRCamera();
MaintainBackdropVRCamera();
}
}
function MaintainBackdropVRCamera () {
if (!backdropCameraVRHead && backdropCameraVR && backdropCameraVR.GetComponent.<GvrHead>()) {
backdropCameraVRHead = backdropCameraVR.GetComponent.<GvrHead>();
}
if (backdropCameraVRHead) {
if (backdropCameraVRHead.trackRotation ||
backdropCameraVR.transform.localRotation != Quaternion.identity
) {
if (Debug.isDebugBuild) {
Debug.Log("backdropCameraVRHead.trackRotation value " + backdropCameraVRHead.trackRotation);
Debug.Log("backdropCameraVR.localRotation value " + backdropCameraVR.transform.localRotation);
Debug.Log("about to reset backdropCameraVR's rotation");
}
// this GvrHead is already nested within the Player object,
// and the backdrop is meant to be static relative to the player,
// so therefore shouldn't independently track rotation:
backdropCameraVRHead.trackRotation = false;
backdropCameraVR.transform.localRotation = Quaternion.identity;
}
}
}
function MaintainOceanVRCamera () {
if (oceanLevel && oceanCameraVR && !oceanCameraVRHead) {
if (oceanCameraVR.GetComponent.<GvrHead>()) {
// See note in FallingPlayer.SetupVRUI() for explanation:
// oceanCameraVRHead = oceanCameraVR.GetComponent.<StereoController>().Head;
oceanCameraVRHead = oceanCameraVR.GetComponent.<GvrHead>();
// this GvrHead is already nested within the Player camera's
// StereoController, so to stay aligned with its parent,
// it shouldn't independently track rotation:
oceanCameraVRHead.trackRotation = false;
}
}
// Unfortunately necessary, since any object with a GvrHead could potentially get
// an out-of-whack rotation from player input for unknown causes,
// which causes dependent stereo cameras to have a rotational offset that needs zeroing.
// I thought it was caused by trackRotation ever being true (the default) after instantiation,
// but I've caught strange rotations in the Editor without the localRotation conditional
// ever logging as true... due to cached values persisting from GvrHead setup?
// To correct for the potential race condition, we check whether trackRotation ever becomes true
// or if the ocean camera VR component's rotation is ever non-zero:
if (oceanLevel && oceanCameraVR && oceanCameraVRHead) {
if (oceanCameraVRHead.trackRotation ||
oceanCameraVR.transform.localRotation != Quaternion.identity
) {
if (Debug.isDebugBuild) {
Debug.Log("oceanCameraVRHead.trackRotation value " + oceanCameraVRHead.trackRotation);
Debug.Log("oceanCameraVR.localRotation value " + oceanCameraVR.transform.localRotation);
Debug.Log("about to reset oceanCameraVR's rotation");
}
oceanCameraVR.transform.localRotation = Quaternion.identity;
}
}
}
function OnTriggerEnter (other : Collider) {
if (other.gameObject.CompareTag ("changeBackdrop")) {
if (ShouldChangeBackdrop && altSkybox) {
RenderSettings.skybox = altSkybox;
}
if (ShouldChangeBackdrop && closePlaneRenderer) {
closePlaneRenderer.materials = [newMat];
}
if ((ShouldChangeBackdrop || oceanLevel) && cloudOriginalMaterial) {
iTween.ColorTo(cloudCylinderObj,{"a": newCloudAlpha, "time": 1});
}
// Debug.Log("You hit a changeBackdrop trigger! " + other.gameObject);
FadeCameraFarClipPlane (1);
if (FogOnly) { SmoothFogFade (3); }
if (ShouldUseOceanCamera) {
if (FallingLaunch.isVRMode) {
EnableOceanCamera(true);
} else {
EnableOceanCamera(false);
}
SmoothFogFade (1);
}
}
else if (other.gameObject.CompareTag ("changeBackdrop2")) {
// Debug.Log("You hit an alt changeBackdrop trigger!");
FadeCameraFarClipPlane (2);
if (FogOnly) {SmoothFogFade (2);}
if (ShouldUseOceanCamera) {
if (FallingLaunch.isVRMode) {
EnableOceanCamera(true);
} else {
EnableOceanCamera(false);
}
SmoothFogFade (2);
}
}
}
function OnTriggerExit (other : Collider) {
if (other.gameObject.CompareTag ("changeBackdrop") && cloudCylinderObj &&
ShouldChangeBackdrop == true && closePlaneRenderer) {
closePlaneRenderer.materials = [origMat];
// Restore old clouds alpha on death:
if ((ShouldChangeBackdrop || oceanLevel) && cloudOriginalMaterial) {
iTween.ColorTo(cloudCylinderObj,{"a": startingCloudAlpha, "time": .5});
}
}
// Only used by level 1/tutorial ending 'dawn' skybox:
if (other.gameObject.CompareTag ("changeBackdrop") && ShouldChangeBackdrop) {
RenderSettings.skybox = origSkybox;
}
}
// TODO: Switch iTween clip plane transitions to a regular lerp
function FadeCameraFarClipPlane (type : int) {
if (Debug.isDebugBuild) {
Debug.Log("calling FadeCameraFarClipPlane");
}
if (type == 2) {
iTween.ValueTo ( gameObject,
{
"from" : cam.farClipPlane,
"to" : farClipPlaneValue2,
"onstart" : "EnableStereoUpdatesVR",
"onupdate" : "ChangeCameraFarClipPlane",
"oncomplete" : "YieldDisableStereoUpdatesVR",
"time" : farClipPlaneFadeTime2,
"easetype" : "easeInExpo"
});
}
else {
iTween.ValueTo ( gameObject,
{
"from" : cam.farClipPlane,
"to" : farClipPlaneValue,
"onstart" : "EnableStereoUpdatesVR",
"onupdate" : "ChangeCameraFarClipPlane",
"oncomplete" : "YieldDisableStereoUpdatesVR",
"time" : farClipPlaneFadeTime,
"easetype" : "easeInExpo"
});
}
}
function SmoothFogFade (type : int) {
if (type == 1) {
FogLerp(3.0, RenderSettings.fogEndDistance, fogEndValue);
} else if (type == 2) {
FogLerp(farClipPlaneFadeTime2, RenderSettings.fogStartDistance, fogEndValue2);
} else if (type == 3) {
// Effectively zeroes out fog via gentler distance dispersal.
// Assumes that fogEndValue is large enough that halving it
// will still place the fog start far from the camera.
var fogStartType3 : float = fogEndValue * .5;
FogLerp(10.0, fogStartType3, fogEndValue);
}
}
function FogLerp (timer : float, startVal : float, endVal : float) {
var initStart = RenderSettings.fogStartDistance;
var initEnd = RenderSettings.fogEndDistance;
var start = startVal;
var end = endVal;
var i = 0.0;
var step = 1.0/timer;
while (i <= 1.0) {
i += step * Time.deltaTime;
var t : float = i*i * (3f - 2f*i); // smoothstep lerp
RenderSettings.fogStartDistance = Mathf.Lerp(initStart, start, t);
RenderSettings.fogEndDistance = Mathf.Lerp(initEnd, end, t);
// Reset fog if player dies mid-fog-lerp:
if (FallingPlayer.isAlive == 0) {
RenderSettings.fogStartDistance = initStart;
RenderSettings.fogEndDistance = initEnd;
break;
}
yield;
}
}
function ResetCameraClipPlane() {
if (Debug.isDebugBuild) {
Debug.Log("called ResetCameraClipPlane with current clip plane " + cam.farClipPlane);
Debug.Log("...and farClipPlaneValueOrig " + farClipPlaneValueOrig);
}
// This function gets called on first level load (via LatestCheckpointRespawn),
// so we only want to proceed if the current camera clip value doesn't match
// the specified starting value. This should avoid having more than one iTween lerp
// occurring simultaneously and polluting each other.
// TODO: Switch iTween to regular lerps
if (FallingLaunch.isVRMode && farClipPlaneValueOrig != cam.farClipPlane) {
if (Debug.isDebugBuild) {
Debug.Log("resetting main VR camera clip plane to " + farClipPlaneValueOrig);
}
// Set the primary camera's draw distance (far clip plane),
// and enable the keepStereoUpdated boolean on the parent
// stereo controller, so the child eye cameras will update to match.
yield EnableStereoUpdatesVR();
yield ResetFarClipPlane();
yield DisableStereoUpdatesVR(); // Then disable `keepStereoUpdated` for performance
} else if (cam && cam.farClipPlane != farClipPlaneValueOrig) {
cam.farClipPlane = farClipPlaneValueOrig;
}
}
// this gets yielded so we can be sure the new value is set before proceeding:
function ResetFarClipPlane() : IEnumerator {
cam.farClipPlane = farClipPlaneValueOrig;
}
function ChangeCameraFarClipPlane (i : int) {
cam.farClipPlane = i;
}
function EnableStereoUpdatesVR () {
if (Debug.isDebugBuild) {
Debug.Log("called EnableStereoUpdatesVR");
}
// Existence check required for StereoControllerComponent since
// it takes 1+ seconds to instantiate via GVR plugin:
if (FallingLaunch.isVRMode && StereoControllerComponent) {
// Type coercion is required... details in setVRMode.Start():
(StereoControllerComponent as StereoController).keepStereoUpdated = true;
if (Debug.isDebugBuild) {
Debug.Log("keepStereoUpdated is true");
}
}
yield;
}
// needed because iTween can't directly call a function that yields;
function YieldDisableStereoUpdatesVR () {
yield DisableStereoUpdatesVR();
}
function DisableStereoUpdatesVR () {
if (Debug.isDebugBuild) {
Debug.Log("DisableStereoUpdatesVR");
}
// Existence check required for StereoControllerComponent since
// it takes 1+ seconds to instantiate via GVR plugin:
if (FallingLaunch.isVRMode && StereoControllerComponent) {
// Just to make sure the below parent camera alterations
// actually propagate to the relevant stereo cameras:
if ((StereoControllerComponent as StereoController).keepStereoUpdated == false) {
(StereoControllerComponent as StereoController).keepStereoUpdated = true;
}
eyeCamerasVR = getMainChildVRCameras(mainCamera);
for (var camera : Camera in eyeCamerasVR) {
if (Debug.isDebugBuild) {
Debug.Log("Setting camera " + camera + "'s farClipPlane to " + cam.farClipPlane);
}
camera.farClipPlane = cam.farClipPlane;
}
// it takes at least one frame for the GVR plugin to update the relevant cameras
// based on the keepStereoUpdated boolean, so this assumes at least 2fps:
// ...or handle StereoController existence check via a coroutine/callback?
yield WaitForSeconds(.5);
// Type coercion is required... details in setVRMode.Start():
(StereoControllerComponent as StereoController).keepStereoUpdated = false;
}
return;
}
function EnableOceanCamera (isVR: boolean) {
if (!isVR) {
oceanCamera.GetComponent(Camera).enabled = true;
}
oceanRenderer.enabled = true;
}
function getMainChildVRCameras(obj : GameObject) : Array{
var children : Array = new Array();
for (var child : Transform in obj.transform) {
var eyeCam = child.GetComponent.<Camera>();
if (Debug.isDebugBuild) {
Debug.Log("eyeCam is " + eyeCam);
Debug.Log("with name " + child.gameObject.name);
}
// HACK: Brittle attempt to filter by camera name to include only 'main' child cameras,
// and not those of VR UI, Ocean, or Backdrop cameras.
if (eyeCam && (child.gameObject.name == 'Camera Left' || child.gameObject.name == 'Camera Right')) {
if (Debug.isDebugBuild) {
Debug.Log("Adding this eyeCam to results: " + eyeCam);
}
children.Add(eyeCam);
}
}
return children;
}