/// <summary> /// This method checks if we are using OpenGL ES 2.0 or later. /// </summary> private bool IsOpenGLES2() { // in play mode on a desktop machine, always return true if (QCARRuntimeUtilities.IsPlayMode()) { return(true); } string graphicsDeviceVersion = SystemInfo.graphicsDeviceVersion; Debug.Log("Sample using " + graphicsDeviceVersion); int oglStringIdx = graphicsDeviceVersion.IndexOf(CHECK_STRING, StringComparison.Ordinal); if (oglStringIdx >= 0) { // it's open gl es, parse the version number float esVersion; if (float.TryParse(graphicsDeviceVersion.Substring(oglStringIdx + CHECK_STRING.Length + 1, 3), out esVersion)) { return(esVersion >= 2.0f); } } return(false); }
private void Awake() { if (QCARRuntimeUtilities.IsQCAREnabled()) { if (QCARRuntimeUtilities.IsPlayMode()) { QCARUnity.CheckInitializationError(); } bool flag = false; QCARAbstractBehaviour behaviour = (QCARAbstractBehaviour)UnityEngine.Object.FindObjectOfType(typeof(QCARAbstractBehaviour)); if ((behaviour != null) && behaviour.enabled) { behaviour.enabled = false; flag = true; } if (TrackerManager.Instance.GetTracker <TextTracker>() == null) { TrackerManager.Instance.InitTracker <TextTracker>(); } if (flag) { behaviour.enabled = true; } } }
public override bool SetRegionOfInterest(Rect detectionRegion, Rect trackingRegion) { Vector2 vector; Vector2 vector2; Vector2 vector3; Vector2 vector4; QCARAbstractBehaviour behaviour = (QCARAbstractBehaviour)UnityEngine.Object.FindObjectOfType(typeof(QCARAbstractBehaviour)); if (behaviour == null) { Debug.LogError("QCAR Behaviour could not be found"); return(false); } Rect viewportRectangle = behaviour.GetViewportRectangle(); bool videoBackGroundMirrored = behaviour.VideoBackGroundMirrored; CameraDevice.VideoModeData videoMode = CameraDevice.Instance.GetVideoMode(behaviour.CameraDeviceMode); QCARRuntimeUtilities.SelectRectTopLeftAndBottomRightForLandscapeLeft(detectionRegion, videoBackGroundMirrored, out vector, out vector2); QCARRuntimeUtilities.SelectRectTopLeftAndBottomRightForLandscapeLeft(trackingRegion, videoBackGroundMirrored, out vector3, out vector4); QCARRenderer.Vec2I veci = QCARRuntimeUtilities.ScreenSpaceToCameraFrameCoordinates(vector, viewportRectangle, videoBackGroundMirrored, videoMode); QCARRenderer.Vec2I veci2 = QCARRuntimeUtilities.ScreenSpaceToCameraFrameCoordinates(vector2, viewportRectangle, videoBackGroundMirrored, videoMode); QCARRenderer.Vec2I veci3 = QCARRuntimeUtilities.ScreenSpaceToCameraFrameCoordinates(vector3, viewportRectangle, videoBackGroundMirrored, videoMode); QCARRenderer.Vec2I veci4 = QCARRuntimeUtilities.ScreenSpaceToCameraFrameCoordinates(vector4, viewportRectangle, videoBackGroundMirrored, videoMode); if (QCARWrapper.Instance.TextTrackerSetRegionOfInterest(veci.x, veci.y, veci2.x, veci2.y, veci3.x, veci3.y, veci4.x, veci4.y, (int)this.CurrentUpDirection) == 0) { Debug.LogError(string.Format("Could not set region of interest: ({0}, {1}, {2}, {3}) - ({4}, {5}, {6}, {7})", new object[] { detectionRegion.x, detectionRegion.y, detectionRegion.width, detectionRegion.height, trackingRegion.x, trackingRegion.y, trackingRegion.width, trackingRegion.height })); return(false); } return(true); }
public void UpdateUI(bool tf) { if (!tf) { return; } if (QCARRuntimeUtilities.IsPlayMode() && (mWindowsPlayModeTexture != null)) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mWindowsPlayModeTexture); } else { #if (UNITY_IPHONE || UNITY_IOS) float textureWidth = m_SplashStyle.normal.background.width; float textureHeight = m_SplashStyle.normal.background.height; float height = Screen.height; float width = (height * textureWidth) / textureHeight; float x = (Screen.width - width) / 2; GUI.Box(new Rect(x, 0, width, height), "", m_SplashStyle); #else if (mAndroidPotrait != null) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mAndroidPotrait, ScaleMode.ScaleAndCrop); } #endif } }
public override void ClearVideoBackgroundConfig() { if (QCARRuntimeUtilities.IsPlayMode()) { this.mVideoBGConfigSet = false; } }
private void Update() { if (QCARRuntimeUtilities.IsPlayMode() && (this.mWebCamImpl != null)) { this.mWebCamImpl.Update(); } }
private void Update() { if (QCARManager.Instance.Initialized) { if (this.mAndroidUnityPlayer != null) { this.mAndroidUnityPlayer.Update(); } ScreenOrientation surfaceOrientation = (ScreenOrientation)QCARWrapper.Instance.GetSurfaceOrientation(); CameraDeviceImpl instance = (CameraDeviceImpl)CameraDevice.Instance; if (instance.CameraReady && (QCARUnity.IsRendererDirty() || (this.mProjectionOrientation != surfaceOrientation))) { this.ConfigureVideoBackground(false); this.UpdateProjection(surfaceOrientation); instance.ResetDirtyFlag(); } this.mClearMaterial.SetPass(0); if (((QCARManagerImpl)QCARManager.Instance).Update(this.mProjectionOrientation, this.CameraDeviceMode, ref this.mVideoMode)) { this.UpdateCameraClearFlags(); foreach (ITrackerEventHandler handler in this.mTrackerEventHandlers) { handler.OnTrackablesUpdated(); } } } else if (QCARRuntimeUtilities.IsPlayMode()) { Debug.LogWarning("Scripts have been recompiled during Play mode, need to restart!"); QCARWrapper.Create(); PlayModeEditorUtility.Instance.RestartPlayMode(); } }
void Start() { if (QCARRuntimeUtilities.IsQCAREnabled()) { this.renderer.sharedMaterial = maskMaterial; } }
/// <summary> /// Returns the axis-aligned bounding boxes for all letters of the word. These are defined in the range of [0, 1] which corresponds to the whole bounding box of the word. /// </summary> public RectangleData[] GetLetterBoundingBoxes() { if (!QCARRuntimeUtilities.IsQCAREnabled()) { return(new RectangleData[0]); } if (mLetterBoundingBoxes == null) { var length = mText.Length; mLetterBoundingBoxes = new RectangleData[length]; var rectPtr = Marshal.AllocHGlobal(length * Marshal.SizeOf( typeof(RectangleData))); QCARWrapper.Instance.WordGetLetterBoundingBoxes(ID, rectPtr); var c = new IntPtr(rectPtr.ToInt32()); for (var i = 0; i < length; i++) { mLetterBoundingBoxes[i] = (RectangleData)Marshal.PtrToStructure(c, typeof(RectangleData)); c = new IntPtr(c.ToInt32() + Marshal.SizeOf( typeof(RectangleData))); } Marshal.FreeHGlobal(rectPtr); } return(mLetterBoundingBoxes); }
// Initializes the camera. public override bool Init(CameraDirection cameraDirection) { // play mode emulates back facing camera mode only for now if (QCARRuntimeUtilities.IsPlayMode()) { cameraDirection = CameraDirection.CAMERA_BACK; } if (InitCameraDevice((int)cameraDirection) == 0) { return(false); } mCameraReady = true; // in play mode, CameraReady does not only evaluate the above flag, but also the web cam state if (this.CameraReady) { QCARBehaviour qcarBehaviour = (QCARBehaviour)Object.FindObjectOfType(typeof(QCARBehaviour)); if (qcarBehaviour) { // resets the number of frames for which the color buffer needs to be cleared // to avoid rendering artifacts while the camera is being initialized qcarBehaviour.ResetClearBuffers(); // configure the videobackground, set initial reflection setting qcarBehaviour.ConfigureVideoBackground(true); } } return(true); }
private void UpdateWordResultPoses(Camera arCamera, IEnumerable <QCARManagerImpl.WordResultData> wordResults) { QCARAbstractBehaviour behaviour = (QCARAbstractBehaviour)UnityEngine.Object.FindObjectOfType(typeof(QCARAbstractBehaviour)); if (behaviour == null) { Debug.LogError("QCAR Behaviour could not be found"); } else { Rect viewportRectangle = behaviour.GetViewportRectangle(); bool videoBackGroundMirrored = behaviour.VideoBackGroundMirrored; CameraDevice.VideoModeData videoMode = behaviour.GetVideoMode(); foreach (QCARManagerImpl.WordResultData data2 in wordResults) { WordResultImpl impl = (WordResultImpl)this.mTrackedWords[data2.id]; Vector3 position = arCamera.transform.TransformPoint(data2.pose.position); Quaternion orientation = data2.pose.orientation; Quaternion quaternion2 = (arCamera.transform.rotation * orientation) * Quaternion.AngleAxis(270f, Vector3.left); impl.SetPose(position, quaternion2); impl.SetStatus(data2.status); OrientedBoundingBox cameraFrameObb = new OrientedBoundingBox(data2.orientedBoundingBox.center, data2.orientedBoundingBox.halfExtents, data2.orientedBoundingBox.rotation); impl.SetObb(QCARRuntimeUtilities.CameraFrameToScreenSpaceCoordinates(cameraFrameObb, viewportRectangle, videoBackGroundMirrored, videoMode)); } if (this.mWordPrefabCreationMode == WordPrefabCreationMode.DUPLICATE) { this.UpdateWordBehaviourPoses(); } } }
// Initializes the tracker of the given type // Initializing a tracker must not be done when the CameraDevice // is initialized or started. This function will return null if the // CameraDevice is currently initialized. public override Tracker InitTracker(Tracker.Type trackerType) { if (!QCARRuntimeUtilities.IsQCAREnabled()) { return(null); } if (QCARWrapper.Instance.TrackerManagerInitTracker((int)trackerType) == 0) { Debug.LogError("Could not initialize the tracker."); return(null); } if (trackerType == Tracker.Type.IMAGE_TRACKER) { if (mImageTracker == null) { mImageTracker = new ImageTrackerImpl(); } return(mImageTracker); } else if (trackerType == Tracker.Type.MARKER_TRACKER) { if (mMarkerTracker == null) { mMarkerTracker = new MarkerTrackerImpl(); } return(mMarkerTracker); } else { Debug.LogError("Could not initialize tracker. Unknown tracker type."); return(null); } }
// Update the camera projection matrix to match QCAR settings private void UpdateProjection(ScreenOrientation orientation) { if (!QCARRuntimeUtilities.IsQCAREnabled()) { // Skip this when running in the free editor version // that does not support native plugins return; } // This member stores the screen orientation used for the last projection matrix // update. It is used to counter rotate the poses later. mProjectionOrientation = orientation; // cache the current surface orientation: QCARRuntimeUtilities.CacheSurfaceOrientation(orientation); Matrix4x4 projectionMatrix = QCARUnity.GetProjectionGL(camera.nearClipPlane, camera.farClipPlane, mProjectionOrientation); if (mViewportRect.width != Screen.width) { float viewportDistort = mViewportRect.width / Screen.width; projectionMatrix[0] *= viewportDistort; } if (mViewportRect.height != Screen.height) { float viewportDistort = mViewportRect.height / Screen.height; projectionMatrix[5] *= viewportDistort; } this.camera.projectionMatrix = projectionMatrix; }
public void UpdateUI(bool tf) { if (!tf) { return; } if (QCARRuntimeUtilities.IsPlayMode()) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mWindowsPlayModeTexture); } else { #if UNITY_IPHONE if (iPhone.generation == iPhoneGeneration.iPhone5) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mPotraitTextureIPhone5); } else if (iPhone.generation == iPhoneGeneration.iPhone) { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mPotraitTextureIPhone, ScaleMode.ScaleAndCrop); } else { GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mPotraitTextureIPad, ScaleMode.ScaleAndCrop); } #else GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), mAndroidPotrait, ScaleMode.ScaleAndCrop); #endif } }
void Awake() { if (!QCARRuntimeUtilities.IsQCAREnabled()) { return; } if (QCARRuntimeUtilities.IsPlayMode()) { // initialize QCAR QCARUnity.CheckInitializationError(); } // In Unity 4.x, if the QCARBehaviour is already active from a previous scene, // we need to deactivate it briefly to be able to initialize the text tracker bool enableQCARBehaviourAgain = false; QCARBehaviour qcarBehaviour = (QCARBehaviour)FindObjectOfType(typeof(QCARBehaviour)); if (qcarBehaviour && qcarBehaviour.enabled) { qcarBehaviour.enabled = false; enableQCARBehaviourAgain = true; } if (TrackerManager.Instance.GetTracker(Tracker.Type.TEXT_TRACKER) == null) { TrackerManager.Instance.InitTracker(Tracker.Type.TEXT_TRACKER); } // restart the QCARBehaviour if it was stopped again if (enableQCARBehaviourAgain) { qcarBehaviour.enabled = true; } }
// Update the camera clear flags and background color in response // to QCAR settings private void UpdateCameraClearFlags() { // Specifically handle when running in the free editor version // that does not support native plugins if (!QCARRuntimeUtilities.IsQCAREnabled()) { mCameraState = CameraState.UNINITED; return; } // Update camera clear flags if necessary switch (mCameraState) { case CameraState.UNINITED: mCameraState = CameraState.DEVICE_INITED; break; case CameraState.DEVICE_INITED: // Check whether QCAR requires a transparent clear color if (QCARUnity.RequiresAlpha()) { // Camera clears both depth and color buffer, // We set the clear color to transparent black as // required by QCAR. this.camera.clearFlags = CameraClearFlags.SolidColor; this.camera.backgroundColor = new Color(0, 0, 0, 0); Debug.Log("Setting camera clear flags to transparent black"); } else { // Check whether QCAR is rendering the video in native if (mCachedDrawVideoBackground) { // Clear only depth this.camera.clearFlags = CameraClearFlags.Depth; Debug.Log("Setting camera clear flags to depth only"); } else { // Restore clear settings to Inspector values this.camera.clearFlags = mCachedCameraClearFlags; this.camera.backgroundColor = mCachedCameraBackgroundColor; Debug.Log("Setting camera clear flags to Inspector values"); } } mCameraState = CameraState.RENDERING_INITED; break; case CameraState.RENDERING_INITED: // Check if the DrawVideoBackground setting has changed bool drawVideoBackground = QCARManager.Instance.DrawVideoBackground; if (drawVideoBackground != mCachedDrawVideoBackground) { mCameraState = CameraState.DEVICE_INITED; mCachedDrawVideoBackground = drawVideoBackground; } break; } }
private /*unsafe*/ void UpdateProjection(ScreenOrientation orientation) { if (QCARRuntimeUtilities.IsQCAREnabled()) { this.mProjectionOrientation = orientation; QCARRuntimeUtilities.CacheSurfaceOrientation(orientation); Matrix4x4 matrixx = QCARUnity.GetProjectionGL(base.camera.nearClipPlane, base.camera.farClipPlane, this.mProjectionOrientation); if (this.mViewportRect.width != Screen.width) { //ref Matrix4x4 matrixxRef; //Matrix4x4 *matrixxRef = &matrixx; float num = (this.mViewportRect.width / ((float)Screen.width)) * matrixx[0]; //(matrixxRef = (Matrix4x4) &matrixx)[0] = matrixxRef[0] * num; //(*matrixxRef)[0] = (*matrixxRef)[0] * num; matrixx[0] = num; } if (this.mViewportRect.height != Screen.height) { //ref Matrix4x4 matrixxRef2; //Matrix4x4 *matrixxRef2 = (Matrix4x4 *) &matrixx; float num2 = (this.mViewportRect.height / ((float)Screen.height)) * matrixx[5]; //(matrixxRef2 = (Matrix4x4) &matrixx)[5] = matrixxRef2[5] * num2; //(*matrixxRef2)[5] = (*matrixxRef2)[5] * num2; matrixx[5] = num2; } base.camera.projectionMatrix = matrixx; } }
/// <summary> /// In Unity 4, WebCamTexture is stopped when a new level is loaded. /// We manually restart the webcam again if it has been enabled before. /// </summary> void OnLevelWasLoaded() { if (QCARRuntimeUtilities.IsPlayMode() && mWebCamImpl != null) { mWebCamImpl.ResetPlaying(); } }
public void OnInitialized() { if (QCARRuntimeUtilities.IsQCAREnabled()) { foreach (string str in this.mDataSetsToLoad) { if (!DataSet.Exists(str)) { Debug.LogError("Data set " + str + " does not exist."); } else { ImageTracker tracker = TrackerManager.Instance.GetTracker <ImageTracker>(); DataSet dataSet = tracker.CreateDataSet(); if (!dataSet.Load(str)) { Debug.LogError("Failed to load data set " + str + "."); } else if (this.mDataSetsToActivate.Contains(str)) { tracker.ActivateDataSet(dataSet); } } } } }
private void OnLevelWasLoaded() { if (QCARRuntimeUtilities.IsPlayMode() && (this.mWebCamImpl != null)) { this.mWebCamImpl.ResetPlaying(); } }
private Rect ScreenSpaceRectFromCamSpaceRectData(RectangleIntData camSpaceRectData, Rect bgTextureViewPortRect, bool isTextureMirrored, CameraDevice.VideoModeData videoModeData) { Vector2 topLeft = QCARRuntimeUtilities.CameraFrameToScreenSpaceCoordinates(new Vector2((float)camSpaceRectData.leftTopX, (float)camSpaceRectData.leftTopY), bgTextureViewPortRect, isTextureMirrored, videoModeData); Vector2 bottomRight = QCARRuntimeUtilities.CameraFrameToScreenSpaceCoordinates(new Vector2((float)camSpaceRectData.rightBottomX, (float)camSpaceRectData.rightBottomY), bgTextureViewPortRect, isTextureMirrored, videoModeData); return(QCARRuntimeUtilities.CalculateRectFromLandscapeLeftCorners(topLeft, bottomRight, isTextureMirrored)); }
internal void Update() { if (QCARRuntimeUtilities.IsPlayMode()) { if (!this.IsTextureSizeAvailable && this.mWebCamTexture.DidUpdateThisFrame) { QCARRenderer.Vec2I resampledTextureSize = this.mWebCamProfile.ResampledTextureSize; CameraDevice.VideoModeData data = new CameraDevice.VideoModeData { width = resampledTextureSize.x, height = resampledTextureSize.y, frameRate = this.mWebCamProfile.RequestedFPS }; this.mVideoModeData = data; QCARRenderer.VideoTextureInfo info = new QCARRenderer.VideoTextureInfo { imageSize = resampledTextureSize, textureSize = resampledTextureSize }; this.mVideoTextureInfo = info; this.mTextureRenderer = new TextureRenderer(this.mWebCamTexture.Texture, this.mRenderTextureLayer, resampledTextureSize); this.mBufferReadTexture = new Texture2D(resampledTextureSize.x, resampledTextureSize.y); this.mReadPixelsRect = new Rect(0f, 0f, (float)resampledTextureSize.x, (float)resampledTextureSize.y); this.IsTextureSizeAvailable = true; } this.mBgRenderingTexBehaviour.CheckAndSetActive(QCARRenderer.Instance.DrawVideoBackground); } }
public override bool Load(string path, DataSet.StorageType storageType) { if (this.mDataSetPtr == IntPtr.Zero) { Debug.LogError("Called Load without a data set object"); return(false); } string relativePath = path; if ((storageType == DataSet.StorageType.STORAGE_APPRESOURCE) && QCARRuntimeUtilities.IsPlayMode()) { relativePath = "Assets/StreamingAssets/" + relativePath; } if (QCARWrapper.Instance.DataSetLoad(relativePath, (int)storageType, this.mDataSetPtr) == 0) { Debug.LogError("Did not load: " + path); return(false); } this.mPath = path; this.mStorageType = storageType; this.CreateImageTargets(); this.CreateMultiTargets(); this.CreateCylinderTargets(); this.CreateRigidBodyTargets(); ((StateManagerImpl)TrackerManager.Instance.GetStateManager()).AssociateTrackableBehavioursForDataSet(this); return(true); }
public override bool LoadWordListFile(string path, DataSet.StorageType storageType) { if ((storageType == DataSet.StorageType.STORAGE_APPRESOURCE) && QCARRuntimeUtilities.IsPlayMode()) { path = "Assets/StreamingAssets/" + path; } return(QCARWrapper.Instance.WordListLoadWordList(path, (int)storageType) == 1); }
internal static bool ExistsImpl(string path, DataSet.StorageType storageType) { if ((storageType == DataSet.StorageType.STORAGE_APPRESOURCE) && QCARRuntimeUtilities.IsPlayMode()) { path = "Assets/StreamingAssets/" + path; } return(QCARWrapper.Instance.DataSetExists(path, (int)storageType) == 1); }
private void OnDestroy() { if (QCARRuntimeUtilities.IsPlayMode() && (this.mWebCamImpl != null)) { this.mWebCamImpl.OnDestroy(); UnityEngine.Object.Destroy(this.mBackgroundCameraInstance); } }
public override bool IsVideoBackgroundInfoAvailable() { if (QCARRuntimeUtilities.IsPlayMode()) { return(this.mVideoBGConfigSet); } return(QCARWrapper.Instance.RendererIsVideoBackgroundTextureInfoAvailable() != 0); }
// clears the config of the videobackground when QCAR is stopped. public override void ClearVideoBackgroundConfig() { if (QCARRuntimeUtilities.IsPlayMode()) { // remember the config has been cleared (checking for null not possible for structs) mVideoBGConfigSet = false; } }
// Checks if a data set exists at the given path. // Storage type is used to correctly interpret the given path. public static bool ExistsImpl(String path, StorageType storageType) { if (QCARRuntimeUtilities.IsPlayMode()) { path = "Assets/StreamingAssets/" + path; } return(QCARWrapper.Instance.DataSetExists(path, (int)storageType) == 1); }
// Deinitialize QCAR and trackers when QCARBehaviour is destroyed. void OnDestroy() { // clear all trackable results in the StateManager StateManagerImpl stateManager = (StateManagerImpl)TrackerManager.Instance.GetStateManager(); stateManager.ClearTrackableBehaviours(); // Destroy all the datasets ImageTracker imageTracker = (ImageTracker)TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER); if (imageTracker != null) { imageTracker.DestroyAllDataSets(false); imageTracker.Stop(); } // Destroy all the markers MarkerTracker markerTracker = (MarkerTracker)TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER); if (markerTracker != null) { markerTracker.DestroyAllMarkers(false); markerTracker.Stop(); } TextTracker textTracker = (TextTracker)TrackerManager.Instance.GetTracker(Tracker.Type.TEXT_TRACKER); if (textTracker != null) { textTracker.Stop(); } // Deinit the QCARManager QCARManager.Instance.Deinit(); // Deinit the trackers if (TrackerManager.Instance.GetTracker(Tracker.Type.MARKER_TRACKER) != null) { TrackerManager.Instance.DeinitTracker(Tracker.Type.MARKER_TRACKER); } if (TrackerManager.Instance.GetTracker(Tracker.Type.IMAGE_TRACKER) != null) { TrackerManager.Instance.DeinitTracker(Tracker.Type.IMAGE_TRACKER); } if (TrackerManager.Instance.GetTracker(Tracker.Type.TEXT_TRACKER) != null) { TrackerManager.Instance.DeinitTracker(Tracker.Type.TEXT_TRACKER); } if (QCARRuntimeUtilities.IsPlayMode()) { // deinit explicitly if running in the emulator QCARWrapper.Instance.QcarDeinit(); } }