void Start() { faceDetectionRects = new FaceDetectionRects(); if (CompanionDeviceManager == null) { Logger.E(TAG, "CompanionDeviceManager doesn't exist!"); } else { Logger.E(TAG, "CompanionDeviceManager appears to be OK!"); } ensureBarcodeCamera(); /* Local HTTP client */ httpClient = new HttpClient(); httpClient.Timeout = new TimeSpan(0, 0, 0, 0, (int)DEFAULT_REST_TIMEOUT_MS); encoded = new Texture2D(256, 256); CompanionDeviceHost = "http://www.mobiledgex.com"; shouldEncodeNow = true; screenRect = new Rect(0, 0, Screen.width, Screen.height); OnEnable(); qrThread = new Thread(CameraFeedHandler); qrThread.Start(); // The CompanionDeviceManager GameObject separately updates itself to get // the AppInst for this App/Scene. }
// Update is called once per frame void Update() { Logger.D(TAG, "Update()..."); FaceDetectionRects faceDetectionRects = ComputerVisionFaceDetection.faceDetectionRects; // If no new rects, just leave the current ones up floating in world space (don't update). if (!RectsUpdated) { Logger.D(TAG, "No new rects notified."); return; } RectsUpdated = false; // Singleton field from camera detections var faceRects = faceDetectionRects.GetRects(); Logger.D(TAG, "Pre: Num Rects to draw: " + faceRects.Capacity); if (faceRects.Capacity == 0) { // Disable for Update. foreach (GameObject decorator in detectionUIInsts) { decorator.SetActive(false); } Logger.D(TAG, "No detection rectangles."); return; } Logger.D(TAG, "In FaceDetectionUI rects size: " + faceRects.Capacity); ensureDecoratorInstanceCapacity(faceRects.Capacity); if (parentCam == null) { Logger.D(TAG, "ParentCam or transform is missing!"); return; } int i = 0; // Sort of hacks dependent on the sub-image cut out and image dimensions. float xOffset = 0; // If the camera cut sub-image is heuristically centered, it's 0. float yOffset = imageH / 5; foreach (Rect r in faceRects) { Logger.D(TAG, $"Rect: {r.x}, {r.y}, {r.width}, {r.height}"); GameObject decorator = detectionUIInsts[i++]; if (decorator == null) { Logger.D(TAG, "No decorator, odd: " + decorator); continue; } decorator.SetActive(true); // Resize rect: decorator.transform.localScale = new Vector3(r.width / imageW, r.height / imageH, 1); // Set decorator position to camera position decorator.transform.position = parentCam.transform.position; // Move decorator in front of camera decorator.transform.Translate(unitCamForward); // Rotate to match camera rotation: Quaternion parentRot = parentCam.transform.rotation; Vector3 parentEuler = parentRot.eulerAngles; decorator.transform.rotation = Quaternion.Euler(parentEuler.x, parentEuler.y, 0); // no rotation around z-axis // Reposition r.x and r.y (origin is at top left of screen) in terms of unity's coordinate system (origin is at "center" of screen) // This offset depends entirely on the webcam physical position. The whole thing is sent. Vector3 v3pos = new Vector3( (float)(-.5 * widthAtDepth + ((r.x + r.width / 2 + xOffset) / imageW * widthAtDepth)), (float)(.5 * heightAtDepth - ((r.y + r.height / 2 + yOffset) / imageH * heightAtDepth)), 0); // left hand coordinate system decorator.transform.Translate(v3pos); // Translation is relative to own space } // Disable remainder: for (; i < faceRects.Capacity; i++) { detectionUIInsts[i].SetActive(false); } }