private void FindPoster(int pixelWidth, int pixelHeight) { bool foundPoster = false; try { if (posterSystemAPI == null) { posterSystemAPI = new PosterDetectorAPI(); } // update the poster data to the plugin if (posterTextureDataDirty && posterTextureData != null && posterTextureData.Length > 0) { GCHandle handle = default(GCHandle); try { handle = GCHandle.Alloc(posterTextureData, GCHandleType.Pinned); IntPtr ptr = handle.AddrOfPinnedObject(); posterTextureDataDirty = !posterSystemAPI.SetPoster(ptr, posterTextureDataWidth, posterTextureDataHeight); } finally { if (handle != default(GCHandle)) { handle.Free(); } } } Vector2[] pixelPositions; var result = posterSystemAPI.TryDetectPoster(cameraImagePtrToData, pixelWidth, pixelHeight, out pixelPositions, false); if (result && pixelPositions != null) { foundPoster = true; for (int i = 0; i < 4; i++) { var pixelLocation = new PosterCornerLocation(); pixelLocation.Id = (PosterCornerLocation.CornerLocationId)i; pixelLocation.PixelX = pixelPositions[i].x; pixelLocation.PixelY = pixelPositions[i].y; posterLocationHandler.OnCornerLocated(pixelLocation); } } } catch (Exception ex) { Debug.Log(ex.ToString()); } if (foundPoster) { this.UpdateVersion++; } }
public void OnCornerLocated(PosterCornerLocation location) { Vector2 locationUV = GetLocationUV(location); Ray rayThroughUV = GetLocationRay(locationUV); if ((rayThroughUV.origin == Vector3.zero) || (rayThroughUV.direction == Vector3.zero)) { // wierd position, ignore it return; } Debug.Log("Corner Located! Id=" + location.Id + "\nX=" + location.PixelX + " Y=" + location.PixelY); if (locationCorners[(int)location.Id] == null) { locationCorners[(int)location.Id] = new WorldPosFromRays((int)location.Id); } locationCorners[(int)location.Id].AddRay(rayThroughUV, this.cameraPos, this.cameraProj); }
private Vector2 GetLocationUV(PosterCornerLocation location) { return(new Vector2(location.PixelX / (this.imagePixelWidth - 1), location.PixelY / (this.imagePixelHeight - 1))); }