Skip to content
This repository was archived by the owner on Nov 8, 2019. It is now read-only.

Commit 3a9f4cb

Browse files
committed
Version 0.4.10
1 parent fb08bdd commit 3a9f4cb

File tree

8 files changed

+121
-86
lines changed

8 files changed

+121
-86
lines changed

CHANGELOG

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,9 @@ v0.4.9 (3/27/2015):
4747
- Demo scene now uses uGUI instead of OnGUI.
4848
- Original demo scene using CardboardGUI moved to Legacy folder.
4949

50+
v0.4.10 (4/16/2015):
51+
- StereoController now caches the Eyes array. Call InvalidateEyes() to reset the cache.
52+
- Moved coroutine management in Cardboard and StereoController to OnEnable()/OnDisable().
53+
- Fixed CardboardGUI's Head property in the Legacy demo scene.
54+
- Fixed the neck model Z offset in the Editor.
55+
- Fixed a bug in CardboardEditor which failed to save the Editor-only settings.

Cardboard/Editor/CardboardEditor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ public override void OnInspectorGUI() {
102102
cardboard.BackButtonExitsApp =
103103
EditorGUILayout.Toggle(backButtonExitsAppLabel, cardboard.BackButtonExitsApp);
104104

105-
if (GUI.changed) {
106-
EditorUtility.SetDirty(cardboard);
107-
}
108-
109105
EditorGUILayout.Separator();
110106

111107
EditorGUILayout.LabelField(editorSettingsLabel);
@@ -122,6 +118,10 @@ public override void OnInspectorGUI() {
122118
cardboard.deviceType = (CardboardProfile.DeviceTypes)
123119
EditorGUILayout.EnumPopup(deviceTypeLabel, cardboard.deviceType);
124120

121+
if (GUI.changed) {
122+
EditorUtility.SetDirty(cardboard);
123+
}
124+
125125
if (EditorApplication.isPlaying) {
126126
bool newInCardboard = EditorGUILayout.Toggle("Is In Cardboard", cardboard.InCardboard);
127127
if (newInCardboard != cardboard.InCardboard) {

Cardboard/Legacy/DemoScene/DemoScene.unity

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ Prefab:
249249
- target: {fileID: 11400004, guid: dcd8898a5b8c54c59a1b18bcf72ebd37, type: 2}
250250
propertyPath: head
251251
value:
252-
objectReference: {fileID: 0}
252+
objectReference: {fileID: 299255874}
253253
- target: {fileID: 11400004, guid: dcd8898a5b8c54c59a1b18bcf72ebd37, type: 2}
254254
propertyPath: pointerSize.x
255255
value: 8
@@ -340,6 +340,8 @@ MonoBehaviour:
340340
m_Script: {fileID: 11500000, guid: f1578549c7fcc4f6fa1b063992091672, type: 3}
341341
m_Name:
342342
m_EditorClassIdentifier:
343+
trackRotation: 1
344+
trackPosition: 1
343345
target: {fileID: 0}
344346
updateEarly: 0
345347
--- !u!1 &334714608
@@ -1952,7 +1954,7 @@ MonoBehaviour:
19521954
m_Script: {fileID: 11500000, guid: f06724eec91fb4704a525c71a8e8deea, type: 3}
19531955
m_Name:
19541956
m_EditorClassIdentifier:
1955-
head: {fileID: 0}
1957+
head: {fileID: 299255874}
19561958
pointerImage: {fileID: 2800000, guid: 53a9e781f887f46e1817489bff4b661d, type: 3}
19571959
pointerSize: {x: 8, y: 8}
19581960
pointerSpot: {x: 4, y: 4}

Cardboard/Scripts/Cardboard.cs

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ public void SetInCardboard(bool value) {
198198
public RenderTexture StereoScreen {
199199
get {
200200
// Don't need it except for distortion correction.
201-
if (!nativeDistortionCorrection || !vrModeEnabled) {
201+
if (!nativeDistortionCorrection || !vrModeEnabled || captureFramebuffer) {
202202
return null;
203203
}
204204
if (stereoScreen == null) {
@@ -209,6 +209,7 @@ public RenderTexture StereoScreen {
209209
}
210210

211211
private RenderTexture stereoScreen;
212+
private bool captureFramebuffer;
212213

213214
// Describes the current device, including phone screen.
214215
public CardboardProfile Profile { get; private set; }
@@ -317,11 +318,6 @@ public void initialize() {
317318
supportsAndroidRenderEvent = isAtLeastUnity4_5 && isAndroid;
318319
}
319320

320-
public bool canApplyDistortionCorrection() {
321-
return supportsRenderTextures && supportsAndroidRenderEvent
322-
&& canAccessActivity;
323-
}
324-
325321
public string getDistortionCorrectionDiagnostic() {
326322
List<string> causes = new List<string>();
327323
if (!isAndroid) {
@@ -373,8 +369,21 @@ void Awake() {
373369
SetInCardboard(true);
374370
}
375371
#endif
372+
}
373+
374+
void OnEnable() {
375+
StartCoroutine("EndOfFrame");
376+
}
377+
378+
void OnDisable() {
379+
StopCoroutine("EndOfFrame");
380+
}
376381

377-
StartCoroutine("EndOfFrame");
382+
void OnApplicationPause(bool paused) {
383+
if (!paused) {
384+
// Device configuration may have changed.
385+
UpdateScreenData();
386+
}
378387
}
379388

380389
void Update() {
@@ -426,24 +435,27 @@ public void UpdateScreenData() {
426435
}
427436

428437
public void CreateStereoScreen(int x, int y) {
429-
if (config.canApplyDistortionCorrection()) {
430-
Debug.Log("Creating new cardboard screen texture.");
431-
if (stereoScreen != null) {
432-
stereoScreen.Release();
433-
}
434-
stereoScreen = new RenderTexture(x, y, 16, RenderTextureFormat.RGB565);
435-
stereoScreen.Create();
436-
} else {
438+
if (stereoScreen != null) {
439+
stereoScreen.Release();
437440
stereoScreen = null;
438-
if (!Application.isEditor) {
439-
nativeDistortionCorrection = false;
440-
Debug.LogWarning("Lens distortion-correction disabled. Causes: [" +
441-
config.getDistortionCorrectionDiagnostic() + "]");
441+
}
442+
captureFramebuffer = false;
443+
if (config.canAccessActivity && config.supportsAndroidRenderEvent) {
444+
if (config.supportsRenderTextures) {
445+
Debug.Log("Creating new cardboard screen texture.");
446+
stereoScreen = new RenderTexture(x, y, 16, RenderTextureFormat.RGB565);
447+
stereoScreen.Create();
448+
} else {
449+
captureFramebuffer = true;
442450
}
451+
} else if (!Application.isEditor) {
452+
nativeDistortionCorrection = false;
453+
Debug.LogWarning("Lens distortion-correction disabled. Causes: [" +
454+
config.getDistortionCorrectionDiagnostic() + "]");
443455
}
444456
#if ANDROID_DEVICE
445-
if (stereoScreen != null) {
446-
InitFromUnity(stereoScreen.GetNativeTextureID());
457+
if (stereoScreen != null || captureFramebuffer) {
458+
InitFromUnity(stereoScreen != null ? stereoScreen.GetNativeTextureID() : 0);
447459
}
448460
#endif
449461
}
@@ -486,7 +498,6 @@ void OnCardboardTriggerInternal() {
486498
}
487499

488500
void OnDestroy() {
489-
StopCoroutine("EndOfFrame");
490501
if (sdk == this) {
491502
sdk = null;
492503
}
@@ -501,7 +512,7 @@ IEnumerator EndOfFrame() {
501512
yield return new WaitForEndOfFrame();
502513
if (UpdateState() && vrModeEnabled && !Application.isEditor) {
503514
GL.InvalidateState(); // necessary for Windows, but not Mac.
504-
if (stereoScreen != null && nativeDistortionCorrection) {
515+
if (nativeDistortionCorrection) {
505516
GL.IssuePluginEvent(kPerformDistortionCorrection);
506517
}
507518
if (enableSettingsButton || enableAlignmentMarker) {
@@ -783,7 +794,7 @@ IEnumerator DoAndroidScreenTap(int x, int y) {
783794
public bool simulateDistortionCorrection = true;
784795

785796
// Simulated neck model.
786-
private static readonly Vector3 neckOffset = new Vector3(0, 0.075f, -0.08f);
797+
private static readonly Vector3 neckOffset = new Vector3(0, 0.075f, 0.08f);
787798

788799
// Use mouse to emulate head in the editor.
789800
private float mouseX = 0;
@@ -849,39 +860,4 @@ private void SimulateInput() {
849860
}
850861
}
851862
#endif
852-
853-
private GUIStyle style;
854-
855-
// The amount of time (in seconds) to show error message on GUI.
856-
private const float GUI_ERROR_MSG_DURATION = 10;
857-
858-
private const string warning =
859-
@"Distortion correction is disabled.
860-
Requires Unity Pro v4.5+.
861-
See log for additional details.";
862-
863-
#if !UNITY_EDITOR
864-
void OnGUI() {
865-
if (Debug.isDebugBuild && !config.canApplyDistortionCorrection() &&
866-
Time.realtimeSinceStartup <= GUI_ERROR_MSG_DURATION) {
867-
DisplayDistortionCorrectionDisabledWarning();
868-
}
869-
}
870-
#endif
871-
872-
private void DisplayDistortionCorrectionDisabledWarning() {
873-
if (style == null) {
874-
style = new GUIStyle();
875-
style.normal.textColor = Color.red;
876-
style.alignment = TextAnchor.LowerCenter;
877-
}
878-
if (VRModeEnabled) {
879-
style.fontSize = (int)(50 * Screen.width / 1920f / 2);
880-
GUI.Label(new Rect(0, 0, Screen.width / 2, Screen.height), warning, style);
881-
GUI.Label(new Rect(Screen.width / 2, 0, Screen.width / 2, Screen.height), warning, style);
882-
} else {
883-
style.fontSize = (int)(50 * Screen.width / 1920f);
884-
GUI.Label(new Rect(0, 0, Screen.width, Screen.height), warning, style);
885-
}
886-
}
887863
}

Cardboard/Scripts/CardboardEye.cs

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
// projection based on the head-tracking data from the Cardboard.SDK object.
2121
// To enable a stereo camera pair, enable the parent mono camera and set
2222
// Cardboard.SDK.VRModeEnabled = true.
23+
// NOTE: If you programmatically change the set of CardboardEyes belonging to a
24+
// StereoController, be sure to call InvalidateEyes() on it in order to reset its
25+
// cache.
2326
[RequireComponent(typeof(Camera))]
2427
public class CardboardEye : MonoBehaviour {
2528
// Whether this is the left eye or the right eye.
@@ -51,6 +54,16 @@ public CardboardHead Head {
5154
}
5255
}
5356

57+
#if UNITY_5
58+
// For backwards source code compatibility, since we refer to the camera component A LOT in
59+
// this script.
60+
new private Camera camera;
61+
62+
void Awake() {
63+
camera = GetComponent<Camera>();
64+
}
65+
#endif
66+
5467
void Start() {
5568
var ctlr = Controller;
5669
if (ctlr == null) {
@@ -73,7 +86,6 @@ void Start() {
7386
private void FixProjection(ref Matrix4x4 proj, float near, float far, float ipdScale) {
7487
// Adjust for non-fullscreen camera. Cardboard SDK assumes fullscreen,
7588
// so the aspect ratio might not match.
76-
Camera camera = GetComponent<Camera>();
7789
float aspectFix = camera.rect.height / camera.rect.width / 2;
7890
proj[0, 0] *= aspectFix;
7991

@@ -89,13 +101,12 @@ private void FixProjection(ref Matrix4x4 proj, float near, float far, float ipdS
89101
proj[2, 3] = 2 * near * far / (near - far);
90102
}
91103

92-
public void Render() {
104+
private void Setup() {
93105
// Shouldn't happen because of the check in Start(), but just in case...
94106
if (controller == null) {
95107
return;
96108
}
97109

98-
var camera = GetComponent<Camera>();
99110
var monoCamera = controller.GetComponent<Camera>();
100111
Matrix4x4 proj = Cardboard.SDK.Projection(eye);
101112

@@ -155,11 +166,7 @@ public void Render() {
155166
new Vector4(p.device.distortion.k1, p.device.distortion.k2));
156167
}
157168

158-
RenderTexture stereoScreen = controller.StereoScreen;
159-
int screenWidth = stereoScreen ? stereoScreen.width : Screen.width;
160-
int screenHeight = stereoScreen ? stereoScreen.height : Screen.height;
161-
162-
if (stereoScreen == null) {
169+
if (controller.StereoScreen == null) {
163170
Rect rect = camera.rect;
164171
if (!Cardboard.SDK.nativeDistortionCorrection || Application.isEditor) {
165172
// We are rendering straight to the screen. Use the reported rect that is visible
@@ -175,7 +182,7 @@ public void Render() {
175182
}
176183
if (Application.isEditor) {
177184
// The Game window's aspect ratio may not match the fake device parameters.
178-
float realAspect = (float)screenWidth / screenHeight;
185+
float realAspect = (float)Screen.width / Screen.height;
179186
float fakeAspect = Cardboard.SDK.Profile.screen.width / Cardboard.SDK.Profile.screen.height;
180187
float aspectComparison = fakeAspect / realAspect;
181188
if (aspectComparison < 1) {
@@ -188,21 +195,30 @@ public void Render() {
188195
}
189196
camera.rect = rect;
190197
}
198+
}
199+
200+
// Called by StereoController to run the whole render pipeline for this camera.
201+
public void Render()
202+
{
203+
Setup();
191204

192205
// Use the "fast" or "slow" method. Fast means the camera draws right into one half of
193206
// the stereo screen. Slow means it draws first to a side buffer, and then the buffer
194207
// is written to the screen. The slow method is provided because a lot of Image Effects
195208
// don't work if you draw to only part of the window.
196209
if (controller.directRender) {
197210
// Redirect to our stereo screen.
198-
camera.targetTexture = stereoScreen;
211+
camera.targetTexture = controller.StereoScreen;
199212
// Draw!
200213
camera.Render();
201214
} else {
202215
// Save the viewport rectangle and reset to "full screen".
203216
Rect pixRect = camera.pixelRect;
204217
camera.rect = new Rect (0, 0, 1, 1);
205218
// Redirect to a temporary texture. The defaults are supposedly Android-friendly.
219+
RenderTexture stereoScreen = controller.StereoScreen;
220+
int screenWidth = stereoScreen ? stereoScreen.width : Screen.width;
221+
int screenHeight = stereoScreen ? stereoScreen.height : Screen.height;
206222
int depth = stereoScreen ? stereoScreen.depth : 16;
207223
RenderTextureFormat format = stereoScreen ? stereoScreen.format : RenderTextureFormat.RGB565;
208224
camera.targetTexture = RenderTexture.GetTemporary((int)pixRect.width, (int)pixRect.height,
@@ -228,15 +244,26 @@ public void Render() {
228244
camera.targetTexture = null;
229245
}
230246

247+
// Alternate means of rendering stereo, when you don't plan to switch in and out of VR mode:
248+
// In the editor, disable the MainCamera's camera component. Enable the two stereo eye
249+
// camera components. Note: due to a quirk of Unity, there must be at least one camera
250+
// not rendering to a texture, preferably last in order. If necessary, add a dummy camera
251+
// to the scene with a high depth value, it's clear flags set to "Don't Clear", and culling mask
252+
// set to Nothing.
253+
void OnPreCull() {
254+
if (camera.enabled) {
255+
Setup();
256+
camera.targetTexture = controller.StereoScreen;
257+
}
258+
}
259+
231260
// Helper to copy camera settings from the controller's mono camera.
232261
// Used in OnPreCull and the custom editor for StereoController.
233262
// The parameters parx and pary, if not left at default, should come from a
234263
// projection matrix returned by the SDK.
235264
// They affect the apparent depth of the camera's window. See OnPreCull().
236265
public void CopyCameraAndMakeSideBySide(StereoController controller,
237266
float parx = 0, float pary = 0) {
238-
var camera = GetComponent<Camera>();
239-
240267
// Sync the camera properties.
241268
camera.CopyFrom(controller.GetComponent<Camera>());
242269
camera.cullingMask ^= toggleCullingMask.value;

Cardboard/Scripts/RadialUndistortionEffect.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
[RequireComponent(typeof(Camera))]
2323
public class RadialUndistortionEffect : MonoBehaviour {
2424

25+
#if UNITY_EDITOR
2526
private StereoController controller;
27+
#endif
2628
private Material material;
2729

2830
void Awake() {
@@ -38,12 +40,14 @@ void Awake() {
3840
material = new Material(shader);
3941
}
4042

43+
#if UNITY_EDITOR
4144
void Start() {
4245
var eye = GetComponent<CardboardEye>();
4346
if (eye != null) {
4447
controller = eye.Controller;
4548
}
4649
}
50+
#endif
4751

4852
void OnRenderImage(RenderTexture source, RenderTexture dest) {
4953
// Check if we found our shader, and that native distortion correction is OFF (except maybe in

0 commit comments

Comments
 (0)