void OnEnable() { NCamPlugin.NCamCreate(); if (targetCamera.Length != 2 || targetCamera[0] == null || targetCamera[1] == null) { Debug.LogError("Missing reference to camera"); enabled = false; } #if !UNITY_EDITOR if (!Config.Load()) { Config.Save(); } #endif if (Ui) { Ui.gameObject.SetActive(enabled); } if (enabled) { Connect(); } }
public void Connect() { if (!NCamPlugin.NCamIsOpen() && !reconnecting) { StartCoroutine(StartNCam()); } }
void Update() { //if (Input.GetKeyDown(KeyCode.F3) && Input.GetKey(KeyCode.LeftControl)) // showGUI = !showGUI; if (!NCamPlugin.NCamIsOpen() && !reconnecting && Config.AutoConnection) { StartCoroutine(StartNCam()); } }
public Status GetStatus() { if (reconnecting) { return(Status.Connecting); } else if (NCamPlugin.NCamIsOpen()) { return(Status.Connected); } else { return(Status.Disconnected); } }
private IEnumerator StartNCam() { reconnecting = true; yield return(new WaitForEndOfFrame()); StopNCam(); NCamPlugin.NCamResetError(); NCamPlugin.NCamSetPacketType(true, true, true); yield return(new WaitForEndOfFrame()); NCamPlugin.NCamSetIpAddress(Config.Ip, Config.Port); GL.IssuePluginEvent(NCamPlugin.GetNCamRenderEventFunc(), (int)NCamRenderEvent.Initialize); yield return(new WaitForEndOfFrame()); reconnecting = false; while (true) { // Wait until all frame rendering is done yield return(new WaitForEndOfFrame()); if (!NCamPlugin.NCamIsOpen()) { yield return(null); } if (distortionMap == null) { yield return(CreateDistortMap()); } GL.IssuePluginEvent(NCamPlugin.GetNCamRenderEventFunc(), (int)NCamRenderEvent.Update); // Check if we got a valid packet if (NCamPlugin.NCamOpticalTimeCode(Optical.Handle.AddrOfPinnedObject()) > 0) { UpdateCameras(); GL.IssuePluginEvent(NCamPlugin.GetNCamRenderEventFunc(), (int)NCamRenderEvent.UpdateDistortion); } } }
private IEnumerator CreateDistortMap() { int w = NCamPlugin.NCamDistortMapWidth(); int h = NCamPlugin.NCamDistortMapHeight(); distortionMapSize = new Vector2(w, h); if (w > 1 && h > 1) // otherwise the reading has not been initialized yet { distortionMap = new RenderTexture(w, h, 8, RenderTextureFormat.RGFloat); distortionMap.name = "NCamDistortMap"; distortionMap.anisoLevel = 0; distortionMap.wrapMode = TextureWrapMode.Clamp; distortionMap.Create(); // Call Create() so it's actually uploaded to the GPU NCamPlugin.NCamSetDistortMapPtr(distortionMap.GetNativeTexturePtr()); } yield return(new WaitForEndOfFrame()); }
void UpdateCameras() { lastOpticalTimeCode = ncamData[1].OpticalTimeCode.Time; for (int i = 0; i < 2; ++i) { FieldIndex = i; NCamPlugin.NCamFieldIndex(FieldIndex); NCamPlugin.NCamGetData(Optical.TimeCode.Handle.AddrOfPinnedObject(), Tracking.TimeCode.Handle.AddrOfPinnedObject(), Encoder.Handle.AddrOfPinnedObject(), Optical.Handle.AddrOfPinnedObject(), Tracking.Handle.AddrOfPinnedObject(), Projection.Handle.AddrOfPinnedObject(), Modelview.Handle.AddrOfPinnedObject(), targetCamera[FieldIndex].nearClipPlane, targetCamera[FieldIndex].farClipPlane); dataBuffer.Enqueue(new NCamData(Tracking.TimeCode, Optical.TimeCode, Optical.FieldOfViewVertical, Optical.ImageAspectRatio, Optical.ImageResolution, Optical.ImageSensorSize, Optical.ProjectionCenter, distortionMapSize, Projection.Matrix, Modelview.Matrix)); if (dataBuffer.Count > FrameDelay) { ncamData[FieldIndex] = dataBuffer.Dequeue() as NCamData; } } if (!Config.UseGLMatrix) { for (int i = 0; i < 2; ++i) { // Reseting matrices in order to have gizmos update into Unity targetCamera[i].ResetProjectionMatrix(); targetCamera[i].ResetWorldToCameraMatrix(); // Projection Setup targetCamera[i].fieldOfView = ProjectionMatrix2Fovy(-ncamData[i].GLProjection.m11); // There is a number precision difference between ImageAspectRatio and the ration between m11 and m00 targetCamera[i].aspect = Mathf.Abs(ncamData[i].GLProjection.m11 / ncamData[i].GLProjection.m00); // ncamData[i].AspectRatio; ApplyCcdShift(targetCamera[i], -ncamData[i].GLProjection.m02, -ncamData[i].GLProjection.m12); // ModelView Setup Matrix4x4 mdv = ncamData[i].GLModelView.inverse; position = mdv.GetColumn(3); eulerAngles = QuaternionFromMatrix(mdv).eulerAngles; targetCamera[i].transform.localPosition = new Vector3(position.x * Config.Scale, position.y * Config.Scale, -position.z * Config.Scale); targetCamera[i].transform.localRotation = Quaternion.Euler(eulerAngles.x, 180.0f - eulerAngles.y, 180.0f - eulerAngles.z); } } else { for (int i = 0; i < 2; ++i) { targetCamera[i].projectionMatrix = ncamData[i].GLProjection; targetCamera[i].worldToCameraMatrix = ncamData[i].GLModelView; // invalid values position = Vector3.zero; eulerAngles = Vector3.zero; } } while (dataBuffer.Count > FrameDelay) { dataBuffer.Dequeue(); } }
private void StopNCam() { reconnecting = false; StopCoroutine(StartNCam()); GL.IssuePluginEvent(NCamPlugin.GetNCamRenderEventFunc(), (int)NCamRenderEvent.Uninitialize); }
void OnDisable() { Disconnect(); NCamPlugin.NCamDestroy(); }