// ------------------------------------------------------------------------------------------------------------- void OnDisable() { #if (UNITY_STANDALONE || UNITY_EDITOR || UNITY_WEBGL) RigidFaceTrackingBridge.xzimgReleaseRigidTracking(); myWebCamEngine.ReleaseVideoCapturePlane(); #elif UNITY_ANDROID RigidFaceTrackingBridge.Release(); #elif UNITY_IOS RigidFaceTrackingBridge.xzimgFaceApiReleaseRigidTracking(); #endif DisposeObjects(); }
// ------------------------------------------------------------------------------------------------------------- void Awake() { CheckeParameters(); double fovx = (double)CameraFOVX * 3.1415 / 180.0; if (!useNativeCapture) { if (myWebCamEngine == null) { myWebCamEngine = (xmgWebCamTexture)gameObject.AddComponent(typeof(xmgWebCamTexture)); myWebCamEngine.CaptureWidth = GetVideoCaptureWidth(); myWebCamEngine.CaptureHeight = GetVideoCaptureHeight(); myWebCamEngine.MirrorVideo = MirrorVideo; myWebCamEngine.CameraFOVX = CameraFOVX; myWebCamEngine.UseFrontal = UseFrontal; m_WebcamTexture = myWebCamEngine.CreateVideoCapturePlane(1.0f, videoPlaneFittingMode, videoCaptureIndex); } if (m_WebcamTexture) { int captureWidth = m_WebcamTexture.width, captureHeight = m_WebcamTexture.height; if (captureWidth < 100) { // Unity BUG MACOSX captureWidth = m_WebcamTexture.requestedWidth; captureHeight = m_WebcamTexture.requestedHeight; } // Image has the size of obtained video capture resolution image.m_width = captureWidth; image.m_height = captureHeight; image.m_colorType = 3; image.m_type = 0; image.m_flippedH = true; RigidFaceTrackingBridge.xzimgInitializeRigidTracking(new StringBuilder(), new StringBuilder(), new StringBuilder()); trackingParams.m_size.w = GetProcessingWidth(); trackingParams.m_size.h = GetProcessingHeight(); trackingParams.m_rotate_mode = (int)captureDeviceOrientation; RigidFaceTrackingBridge.xzimgSetCalibration(fovx, GetProcessingWidth(image.m_width), GetProcessingHeight(image.m_height), 0, trackingParams.m_rotate_mode); trackingParams.m_detect_without_eyes = 1; } else { Debug.Log("No camera detected with Unity webcamTexture!"); } } else { // __Prepare Video Capture videoOptions.resolution_mode = videoCaptureMode; videoOptions.frontal = UseFrontal ? 1 : 0; videoOptions.focus_mode = 1; videoOptions.exposure_mode = 1; videoOptions.while_balance_mode = 1; // Create a plane and get back its texture ID PrepareBackgroundPlane(); trackingParams.m_detect_without_eyes = 1; #if UNITY_ANDROID // Create the texture for video stream imgTexture = new Texture2D(GetVideoCaptureWidth(), GetVideoCaptureHeight(), TextureFormat.RGB24, false); gameObject.GetComponent <Renderer>().material.mainTexture = imgTexture; videoOptions.texture_ptr = imgTexture.GetNativeTexturePtr(); int rotateMode = 1; if (Screen.orientation == ScreenOrientation.LandscapeRight) { rotateMode = 2; } else if (Screen.orientation == ScreenOrientation.Portrait) { rotateMode = UseFrontal?1:3; } else if (Screen.orientation == ScreenOrientation.LandscapeLeft) { rotateMode = 0; } else if (Screen.orientation == ScreenOrientation.PortraitUpsideDown) { rotateMode = UseFrontal?3:1; } currentDeviceOrientation = (DeviceOrientation)Screen.orientation; RigidFaceTrackingBridge.StartCameraAndInitialize(videoOptions.resolution_mode, UseFrontal, fovx, rotateMode, false); #elif UNITY_IOS // Create the texture for video stream imgTexture = new Texture2D(GetVideoCaptureWidth(), GetVideoCaptureHeight(), TextureFormat.BGRA32, false); gameObject.GetComponent <Renderer>().material.mainTexture = imgTexture; videoOptions.texture_ptr = imgTexture.GetNativeTexturePtr(); int status = RigidFaceTrackingBridge.xzimgFaceApiInitializeRigidTracking(ref videoOptions); if (status != 1) { Debug.Log("Initialization is impossible!"); } trackingParams.m_size.w = GetProcessingWidth(); trackingParams.m_size.h = GetProcessingHeight(); trackingParams.m_rotate_mode = 0; trackingParams.m_rotate_mode = GetScreenOrientationIndex(); //Debug.Log(trackingParams.m_rotate_mode); RigidFaceTrackingBridge.xzimgSetCalibration(fovx, GetProcessingWidth(), GetProcessingHeight(), trackingParams.m_rotate_mode, trackingParams.m_rotate_mode); trackingParams.m_detect_without_eyes = 1; //trackingParams.m_non_rigid_init_quality = 0; RigidFaceTrackingBridge.xzimgFaceApiReleaseRigidTracking(); status = RigidFaceTrackingBridge.xzimgFaceApiInitializeRigidTracking(ref videoOptions); RigidFaceTrackingBridge.xzimgSetCalibration(fovx, GetProcessingWidth(), GetProcessingHeight(), trackingParams.m_rotate_mode, trackingParams.m_rotate_mode); #endif } }
// ------------------------------------------------------------------------------------------------------------- void Update() { int detectedStatus = 0; if (!useNativeCapture) { if (myWebCamEngine == null || !myWebCamEngine.GetData()) { return; } image.m_imageData = myWebCamEngine.m_PixelsHandle.AddrOfPinnedObject(); detectedStatus = RigidFaceTrackingBridge.xzimgRigidTracking(ref image, ref trackingParams, ref rigidData); myWebCamEngine.ApplyTexture(); } else { #if UNITY_ANDROID float [] vctPose = new float[7]; vctPose = RigidFaceTrackingBridge.xzimgAugmentedFaceDetect(videoOptions.texture_ptr); detectedStatus = (int)vctPose[0]; rigidData.m_position = new Vector3(vctPose[1], vctPose[2], vctPose[3]); rigidData.m_euler = new Vector3(vctPose[4], vctPose[5], vctPose[6]); //PrintDeviceOrientation(); if (detectedStatus == 0) { DeviceOrientation idxOrientation = Input.deviceOrientation; // Debug.Log("Orientation"); if (idxOrientation != currentDeviceOrientation && idxOrientation != DeviceOrientation.Unknown && idxOrientation != DeviceOrientation.FaceUp && idxOrientation != DeviceOrientation.FaceDown) { int rotateMode = 1; if (idxOrientation == DeviceOrientation.Portrait) { rotateMode = UseFrontal?1:3; } else if (idxOrientation == DeviceOrientation.LandscapeRight) { rotateMode = 2; } else if (idxOrientation == DeviceOrientation.LandscapeLeft) { rotateMode = 0; } else if (idxOrientation == DeviceOrientation.PortraitUpsideDown) { rotateMode = UseFrontal?3:1; } RigidFaceTrackingBridge.SetNewDeviceOrientation(rotateMode); currentDeviceOrientation = idxOrientation; } } #elif UNITY_IOS trackingParams.texturePtr = imgTexture.GetNativeTexturePtr(); detectedStatus = RigidFaceTrackingBridge.xzimgFaceApiRigidTracking(ref trackingParams, ref rigidData); // to prevent Unity bug on iOS UpdateBackgroundPlaneOrientation(); if (detectedStatus == 0) { DeviceOrientation idxOrientation = Input.deviceOrientation; // Debug.Log("Orientation"); if (idxOrientation != currentDeviceOrientation && idxOrientation != DeviceOrientation.Unknown && idxOrientation != DeviceOrientation.FaceUp && idxOrientation != DeviceOrientation.FaceDown) { // Debug.Log("****************Orientation changed********************"); if (idxOrientation == DeviceOrientation.LandscapeRight) { trackingParams.m_rotate_mode = 0; // Anticlockwize } else if (idxOrientation == DeviceOrientation.Portrait) { trackingParams.m_rotate_mode = 3; } else if (idxOrientation == DeviceOrientation.LandscapeLeft) { trackingParams.m_rotate_mode = 2; } else if (idxOrientation == DeviceOrientation.PortraitUpsideDown) { trackingParams.m_rotate_mode = 1; } currentDeviceOrientation = idxOrientation; } } #endif } if (detectedStatus > 0) { ShowObject(true); UpdateObjectPosition(); } if (detectedStatus <= 0) { ShowObject(false); } UpdateDebugDisplay(detectedStatus); }