@@ -14,7 +14,7 @@ namespace MarkerLessARExample
1414 /// <summary>
1515 /// Pattern capture.
1616 /// </summary>
17- [ RequireComponent ( typeof ( WebCamTextureToMatHelper ) ) ]
17+ [ RequireComponent ( typeof ( MultiSource2MatHelper ) ) ]
1818 public class CapturePattern : MonoBehaviour
1919 {
2020 /// <summary>
@@ -28,9 +28,9 @@ public class CapturePattern : MonoBehaviour
2828 Texture2D texture ;
2929
3030 /// <summary>
31- /// The webcam texture to mat helper.
31+ /// The multi source to mat helper.
3232 /// </summary>
33- WebCamTextureToMatHelper webCamTextureToMatHelper ;
33+ MultiSource2MatHelper multiSource2MatHelper ;
3434
3535 /// <summary>
3636 /// The pattern rect.
@@ -57,6 +57,11 @@ public class CapturePattern : MonoBehaviour
5757 /// </summary>
5858 MatOfKeyPoint keypoints ;
5959
60+ /// <summary>
61+ /// The FPS monitor.
62+ /// </summary>
63+ FpsMonitor fpsMonitor ;
64+
6065 // Use this for initialization
6166 void Start ( )
6267 {
@@ -83,37 +88,39 @@ void Start()
8388 }
8489 }
8590
86- webCamTextureToMatHelper = gameObject . GetComponent < WebCamTextureToMatHelper > ( ) ;
87-
88- webCamTextureToMatHelper . Initialize ( ) ;
91+ multiSource2MatHelper = gameObject . GetComponent < MultiSource2MatHelper > ( ) ;
92+ multiSource2MatHelper . outputColorFormat = Source2MatHelperColorFormat . RGBA ;
93+ multiSource2MatHelper . Initialize ( ) ;
8994
9095 detector = ORB . create ( ) ;
9196 detector . setMaxFeatures ( 1000 ) ;
9297 keypoints = new MatOfKeyPoint ( ) ;
9398 }
9499
95100 /// <summary>
96- /// Raises the web cam texture to mat helper initialized event.
101+ /// Raises the source to mat helper initialized event.
97102 /// </summary>
98- public void OnWebCamTextureToMatHelperInitialized ( )
103+ public void OnSourceToMatHelperInitialized ( )
99104 {
100- Debug . Log ( "OnWebCamTextureToMatHelperInitialized " ) ;
105+ Debug . Log ( "OnSourceToMatHelperInitialized " ) ;
101106
102107
103- Mat webCamTextureMat = webCamTextureToMatHelper . GetMat ( ) ;
108+ Mat rgbaMat = multiSource2MatHelper . GetMat ( ) ;
104109
105- texture = new Texture2D ( webCamTextureMat . width ( ) , webCamTextureMat . height ( ) , TextureFormat . RGB24 , false ) ;
106- rgbMat = new Mat ( webCamTextureMat . rows ( ) , webCamTextureMat . cols ( ) , CvType . CV_8UC3 ) ;
107- outputMat = new Mat ( webCamTextureMat . rows ( ) , webCamTextureMat . cols ( ) , CvType . CV_8UC3 ) ;
110+ texture = new Texture2D ( rgbaMat . width ( ) , rgbaMat . height ( ) , TextureFormat . RGB24 , false ) ;
111+ rgbMat = new Mat ( rgbaMat . rows ( ) , rgbaMat . cols ( ) , CvType . CV_8UC3 ) ;
112+ outputMat = new Mat ( rgbaMat . rows ( ) , rgbaMat . cols ( ) , CvType . CV_8UC3 ) ;
108113
109- gameObject . transform . localScale = new Vector3 ( webCamTextureMat . width ( ) , webCamTextureMat . height ( ) , 1 ) ;
114+ // Set the Texture2D as the main texture of the Renderer component attached to the game object
115+ gameObject . GetComponent < Renderer > ( ) . material . mainTexture = texture ;
110116
117+ // Adjust the scale of the game object to match the dimensions of the texture
118+ gameObject . transform . localScale = new Vector3 ( rgbaMat . width ( ) , rgbaMat . height ( ) , 1 ) ;
111119 Debug . Log ( "Screen.width " + Screen . width + " Screen.height " + Screen . height + " Screen.orientation " + Screen . orientation ) ;
112120
113-
114- float width = webCamTextureMat . width ( ) ;
115- float height = webCamTextureMat . height ( ) ;
116-
121+ // Adjust the orthographic size of the main Camera to fit the aspect ratio of the image
122+ float width = rgbaMat . width ( ) ;
123+ float height = rgbaMat . height ( ) ;
117124 float widthScale = ( float ) Screen . width / width ;
118125 float heightScale = ( float ) Screen . height / height ;
119126 if ( widthScale < heightScale )
@@ -125,27 +132,23 @@ public void OnWebCamTextureToMatHelperInitialized()
125132 Camera . main . orthographicSize = height / 2 ;
126133 }
127134
128- gameObject . GetComponent < Renderer > ( ) . material . mainTexture = texture ;
129-
130135
131- //if WebCamera is frontFaceing,flip Mat.
132- if ( webCamTextureToMatHelper . GetWebCamDevice ( ) . isFrontFacing )
133- {
134- webCamTextureToMatHelper . flipHorizontal = true ;
135- }
136+ // If the WebCam is front facing, flip the Mat horizontally. Required for successful detection.
137+ if ( multiSource2MatHelper . source2MatHelper is WebCamTexture2MatHelper webCamHelper )
138+ webCamHelper . flipHorizontal = webCamHelper . IsFrontFacing ( ) ;
136139
137140
138- int patternWidth = ( int ) ( Mathf . Min ( webCamTextureMat . width ( ) , webCamTextureMat . height ( ) ) * 0.8f ) ;
141+ int patternWidth = ( int ) ( Mathf . Min ( rgbaMat . width ( ) , rgbaMat . height ( ) ) * 0.8f ) ;
139142
140- patternRect = new OpenCVForUnity . CoreModule . Rect ( webCamTextureMat . width ( ) / 2 - patternWidth / 2 , webCamTextureMat . height ( ) / 2 - patternWidth / 2 , patternWidth , patternWidth ) ;
143+ patternRect = new OpenCVForUnity . CoreModule . Rect ( rgbaMat . width ( ) / 2 - patternWidth / 2 , rgbaMat . height ( ) / 2 - patternWidth / 2 , patternWidth , patternWidth ) ;
141144 }
142145
143146 /// <summary>
144- /// Raises the web cam texture to mat helper disposed event.
147+ /// Raises the source to mat helper disposed event.
145148 /// </summary>
146- public void OnWebCamTextureToMatHelperDisposed ( )
149+ public void OnSourceToMatHelperDisposed ( )
147150 {
148- Debug . Log ( "OnWebCamTextureToMatHelperDisposed " ) ;
151+ Debug . Log ( "OnSourceToMatHelperDisposed " ) ;
149152
150153 if ( rgbMat != null )
151154 {
@@ -158,20 +161,26 @@ public void OnWebCamTextureToMatHelperDisposed()
158161 }
159162
160163 /// <summary>
161- /// Raises the web cam texture to mat helper error occurred event.
164+ /// Raises the source to mat helper error occurred event.
162165 /// </summary>
163166 /// <param name="errorCode">Error code.</param>
164- public void OnWebCamTextureToMatHelperErrorOccurred ( WebCamTextureToMatHelper . ErrorCode errorCode )
167+ /// <param name="message">Message.</param>
168+ public void OnSourceToMatHelperErrorOccurred ( Source2MatHelperErrorCode errorCode , string message )
165169 {
166- Debug . Log ( "OnWebCamTextureToMatHelperErrorOccurred " + errorCode ) ;
170+ Debug . Log ( "OnSourceToMatHelperErrorOccurred " + errorCode + ":" + message ) ;
171+
172+ if ( fpsMonitor != null )
173+ {
174+ fpsMonitor . consoleText = "ErrorCode: " + errorCode + ":" + message ;
175+ }
167176 }
168177
169178 // Update is called once per frame
170179 void Update ( )
171180 {
172- if ( webCamTextureToMatHelper . IsPlaying ( ) && webCamTextureToMatHelper . DidUpdateThisFrame ( ) )
181+ if ( multiSource2MatHelper . IsPlaying ( ) && multiSource2MatHelper . DidUpdateThisFrame ( ) )
173182 {
174- Mat rgbaMat = webCamTextureToMatHelper . GetMat ( ) ;
183+ Mat rgbaMat = multiSource2MatHelper . GetMat ( ) ;
175184
176185 Imgproc . cvtColor ( rgbaMat , rgbMat , Imgproc . COLOR_RGBA2RGB ) ;
177186 Imgproc . cvtColor ( rgbaMat , outputMat , Imgproc . COLOR_RGBA2RGB ) ;
@@ -183,7 +192,7 @@ void Update()
183192
184193 Imgproc . rectangle ( rgbMat , patternRect . tl ( ) , patternRect . br ( ) , new Scalar ( 255 , 0 , 0 , 255 ) , 5 ) ;
185194
186- Utils . fastMatToTexture2D ( rgbMat , texture ) ;
195+ Utils . matToTexture2D ( rgbMat , texture ) ;
187196 }
188197 }
189198
@@ -192,7 +201,7 @@ void Update()
192201 /// </summary>
193202 void OnDestroy ( )
194203 {
195- webCamTextureToMatHelper . Dispose ( ) ;
204+ multiSource2MatHelper . Dispose ( ) ;
196205
197206 detector . Dispose ( ) ;
198207 if ( keypoints != null )
@@ -214,31 +223,31 @@ public void OnBackButtonClick()
214223 /// </summary>
215224 public void OnPlayButtonClick ( )
216225 {
217- webCamTextureToMatHelper . Play ( ) ;
226+ multiSource2MatHelper . Play ( ) ;
218227 }
219228
220229 /// <summary>
221230 /// Raises the pause button click event.
222231 /// </summary>
223232 public void OnPauseButtonClick ( )
224233 {
225- webCamTextureToMatHelper . Pause ( ) ;
234+ multiSource2MatHelper . Pause ( ) ;
226235 }
227236
228237 /// <summary>
229238 /// Raises the stop button click event.
230239 /// </summary>
231240 public void OnStopButtonClick ( )
232241 {
233- webCamTextureToMatHelper . Stop ( ) ;
242+ multiSource2MatHelper . Stop ( ) ;
234243 }
235244
236245 /// <summary>
237246 /// Raises the change camera button click event.
238247 /// </summary>
239248 public void OnChangeCameraButtonClick ( )
240249 {
241- webCamTextureToMatHelper . requestedIsFrontFacing = ! webCamTextureToMatHelper . IsFrontFacing ( ) ;
250+ multiSource2MatHelper . requestedIsFrontFacing = ! multiSource2MatHelper . requestedIsFrontFacing ;
242251 }
243252
244253 /// <summary>
@@ -283,11 +292,11 @@ public void OnSaveButtonClick()
283292
284293 if ( GraphicsSettings . defaultRenderPipeline == null )
285294 {
286- SceneManager . LoadScene ( "WebCamTextureMarkerLessARExample_Built -in" ) ;
295+ SceneManager . LoadScene ( "MultiSourceMarkerLessARExample_Built -in" ) ;
287296 }
288297 else
289298 {
290- SceneManager . LoadScene ( "WebCamTextureMarkerLessARExample_SRP " ) ;
299+ SceneManager . LoadScene ( "MultiSourceMarkerLessARExample_SRP " ) ;
291300 }
292301 }
293302 }
0 commit comments