@@ -16,6 +16,11 @@ public class WebCamTextureMarkerBasedARSample : MonoBehaviour
1616 /// </summary>
1717 WebCamTexture webCamTexture ;
1818
19+ /// <summary>
20+ /// The web cam device.
21+ /// </summary>
22+ WebCamDevice webCamDevice ;
23+
1924 /// <summary>
2025 /// The colors.
2126 /// </summary>
@@ -98,13 +103,9 @@ public class WebCamTextureMarkerBasedARSample : MonoBehaviour
98103 // Use this for initialization
99104 void Start ( )
100105 {
101-
102-
103106
104107 StartCoroutine ( init ( ) ) ;
105-
106-
107-
108+
108109 }
109110
110111 /// <summary>
@@ -127,9 +128,10 @@ private IEnumerator init ()
127128
128129
129130 Debug . Log ( cameraIndex + " name " + WebCamTexture . devices [ cameraIndex ] . name + " isFrontFacing " + WebCamTexture . devices [ cameraIndex ] . isFrontFacing ) ;
130-
131+
132+ webCamDevice = WebCamTexture . devices [ cameraIndex ] ;
131133
132- webCamTexture = new WebCamTexture ( WebCamTexture . devices [ cameraIndex ] . name , width , height ) ;
134+ webCamTexture = new WebCamTexture ( webCamDevice . name , width , height ) ;
133135
134136
135137 break ;
@@ -139,7 +141,8 @@ private IEnumerator init ()
139141 }
140142
141143 if ( webCamTexture == null ) {
142- webCamTexture = new WebCamTexture ( width , height ) ;
144+ webCamDevice = WebCamTexture . devices [ 0 ] ;
145+ webCamTexture = new WebCamTexture ( webCamDevice . name , width , height ) ;
143146 }
144147
145148 Debug . Log ( "width " + webCamTexture . width + " height " + webCamTexture . height + " fps " + webCamTexture . requestedFPS ) ;
@@ -151,9 +154,13 @@ private IEnumerator init ()
151154
152155 while ( true ) {
153156 //If you want to use webcamTexture.width and webcamTexture.height on iOS, you have to wait until webcamTexture.didUpdateThisFrame == 1, otherwise these two values will be equal to 16. (http://forum.unity3d.com/threads/webcamtexture-and-error-0x0502.123922/)
154- if ( webCamTexture . width > 16 && webCamTexture . height > 16 ) {
157+ #if UNITY_IPHONE && ! UNITY_EDITOR
158+ if ( webCamTexture . width > 16 && webCamTexture . height > 16 ) {
159+ #else
160+ if ( webCamTexture . didUpdateThisFrame ) {
161+ #endif
155162 Debug . Log ( "width " + webCamTexture . width + " height " + webCamTexture . height + " fps " + webCamTexture . requestedFPS ) ;
156- Debug . Log ( "videoRotationAngle " + webCamTexture . videoRotationAngle + " videoVerticallyMirrored " + webCamTexture . videoVerticallyMirrored ) ;
163+ Debug . Log ( "videoRotationAngle " + webCamTexture . videoRotationAngle + " videoVerticallyMirrored " + webCamTexture . videoVerticallyMirrored + " isFrongFacing " + webCamDevice . isFrontFacing ) ;
157164
158165
159166 colors = new Color32 [ webCamTexture . width * webCamTexture . height ] ;
@@ -261,18 +268,44 @@ void Update ()
261268 if ( ! initDone )
262269 return ;
263270
264- if ( webCamTexture . width > 16 && webCamTexture . height > 16 ) {
271+ #if UNITY_IPHONE && ! UNITY_EDITOR
272+ if ( webCamTexture . width > 16 && webCamTexture . height > 16 ) {
273+ #else
274+ if ( webCamTexture . didUpdateThisFrame ) {
275+ #endif
265276
266277
267278 Utils . webCamTextureToMat ( webCamTexture , rgbaMat , colors ) ;
268279
269280 //flip to correct direction.
270- if ( webCamTexture . videoRotationAngle == 180 && webCamTexture . videoVerticallyMirrored ) {
271- Core . flip ( rgbaMat , rgbaMat , 1 ) ;
272- } else if ( webCamTexture . videoRotationAngle == 180 ) {
273- Core . flip ( rgbaMat , rgbaMat , - 1 ) ;
274- } else if ( webCamTexture . videoVerticallyMirrored ) {
275- Core . flip ( rgbaMat , rgbaMat , 0 ) ;
281+ if ( webCamTexture . videoVerticallyMirrored ) {
282+ if ( webCamDevice . isFrontFacing ) {
283+ if ( webCamTexture . videoRotationAngle == 0 ) {
284+ Core . flip ( rgbaMat , rgbaMat , - 1 ) ;
285+ } else if ( webCamTexture . videoRotationAngle == 180 ) {
286+
287+ }
288+ } else {
289+ if ( webCamTexture . videoRotationAngle == 0 ) {
290+ Core . flip ( rgbaMat , rgbaMat , 0 ) ;
291+ } else if ( webCamTexture . videoRotationAngle == 180 ) {
292+ Core . flip ( rgbaMat , rgbaMat , 1 ) ;
293+ }
294+ }
295+ } else {
296+ if ( webCamDevice . isFrontFacing ) {
297+ if ( webCamTexture . videoRotationAngle == 0 ) {
298+ Core . flip ( rgbaMat , rgbaMat , 1 ) ;
299+ } else if ( webCamTexture . videoRotationAngle == 180 ) {
300+ Core . flip ( rgbaMat , rgbaMat , 0 ) ;
301+ }
302+ } else {
303+ if ( webCamTexture . videoRotationAngle == 0 ) {
304+
305+ } else if ( webCamTexture . videoRotationAngle == 180 ) {
306+ Core . flip ( rgbaMat , rgbaMat , - 1 ) ;
307+ }
308+ }
276309 }
277310
278311 markerDetector . processFrame ( rgbaMat , 1 ) ;
0 commit comments