/* Use this for initialization */ void ViewSetup() { #if UNITY_EDITOR if (!m_ignoreConnectionSceneInEditor) #endif { // Get Connected Sphero Sphero[] spheros = SpheroProvider.GetSharedProvider().GetConnectedSpheros(); if (spheros.Length == 0) { Application.LoadLevel("SpheroConnectionScene"); return; } else { spheros[0].SetRGBLED(1, 1, 1); spheros[0].Roll(0, 0f); //spheros[0].SetHeading(0); } } if (!ARUNBridge._ARUNBridgeStartVisionEngine()) { Debug.LogError("Vision Failed to Start :("); } }
// Use this for initialization void Start() { if (ARUNBridge._ARUNBridgeVisionIsInitialized()) { SetupARUNVideo(); } }
void Update() { if (!m_videoGenerated && ARUNBridge._ARUNBridgeVisionIsInitialized()) { SetupARUNVideo(); } }
// Update is called once per frame void Update() { if (!ARUNBridge._ARUNBridgeVisionIsInitialized()) { return; } ARUNBridge.UpdateArResults(); ARUNBridge.FindSpheroState newState = ARUNBridge.CurrentARResult.SpheroTrackingState; if (m_lastTrackingState != newState) { if (OnTrackingStateChanged != null) { OnTrackingStateChanged(newState); } m_lastTrackingState = newState; } if (OnVisionWillUpdate != null) { OnVisionWillUpdate(); } if (OnVisionUpdate != null) { OnVisionUpdate(); } if (OnVisionUpdateComplete != null) { OnVisionUpdateComplete(); } }
/*! * @brief sets the camera to match the given configuration * @param config the configuration to match * @param cam the camera whose projection paramters will be modified */ private void SetCameraToConfig(ref ARUNBridge.AuPlatformConfiguration config, Camera cam) { cam.orthographic = false; cam.nearClipPlane = config.projFrustNear; cam.projectionMatrix = ARUNMath.PerspectiveProjectionFromCameraGeometry( ref config, cam.farClipPlane); }
// Use this for initialization void Start() { if (!s_visionInitialized) { ARUNBridge._ARUNBridgeInitializeVisionEngine(ARUNBridge.CameraMotionMode.StaticHeight); s_visionInitialized = true; } ViewSetup(); }
/*! * @brief determines if the provided @a config structure is valid * @param config the config structure to check * @return true if @a config is true */ private bool ConfigurationValid(ref ARUNBridge.AuPlatformConfiguration config) { return config.projFrustLeft != 0 && config.projFrustTop != 0 && config.projFrustNear != 0 && config.vidRectLeft != 0 && config.vidRectTop != 0 && config.vidRectNear != 0; }
/*! * @brief configures the video plane geometry (vertex positions) * @param config the AuPlatformConfiguration describing the plane geometry * @param targetMesh the mesh to write into */ private void ConfigurePlaneGeometry(ref ARUNBridge.AuPlatformConfiguration config, Mesh targetMesh) { Vector2 offsets = OffsetsFromConfiguration(ref config); Vector3[] positions = { new Vector3(-offsets.x, -offsets.y, 0.5f), // 0 new Vector3(-offsets.x, offsets.y, 0.5f), // 1 new Vector3( offsets.x, offsets.y, 0.5f), // 2 new Vector3( offsets.x, -offsets.y, 0.5f) // 3 }; targetMesh.vertices = positions; }
/* This is called when the application returns from background or entered from NoSpheroConnectionScene */ void OnApplicationPause(bool pause) { if (pause) { // Initialize the device messenger which sets up the callback SpheroProvider.GetSharedProvider().DisconnectSpheros(); ARUNBridge._ARUNBridgeQuitVisionEngine(); } else { ViewSetup(); } }
/* This is called when the application returns from background or entered from NoSpheroConnectionScene */ void OnApplicationPause(bool pause) { #if !UNITY_EDITOR if (pause) { ARUNBridge._ARUNBridgePauseVisionEngine(); } else { ARUNBridge._ARUNBridgeStartVisionEngine(); SpheroProvider.GetSharedProvider().Connect(0); StartCoroutine(DelaySpheroConnection()); } #endif }
/*! * @brief determines if the camera geometry must be updated from @a config * @param config configuration struct describing the current camera gometry * @return true if the camera proje tion needs updating */ private bool ConfigurationNeedsUpdating(ref ARUNBridge.AuPlatformConfiguration config) { return config.projFrustTop != m_lastTop || config.projFrustLeft != m_lastLeft; }
/*! * @brief generates the video plane from a configuration struct * @param config the AuPlatformConfiguration describing the configuration * @return the generated video plane */ private GameObject GenerateVideoPlane(ref ARUNBridge.AuPlatformConfiguration config) { Vector2[] uvs = { new Vector2(0,1), new Vector2(0,0), new Vector2(1,0), new Vector2(1,1) }; int[] indices = { 0, 1, 2, 0, 2, 3 }; GameObject videoObj = new GameObject(); videoObj.transform.parent = transform; videoObj.transform.localPosition = Vector3.zero; videoObj.layer = gameObject.layer; videoObj.name = name + "_Plane"; MeshFilter vidMeshFilter = videoObj.AddComponent<MeshFilter>(); Mesh vidMesh = vidMeshFilter.mesh; ConfigurePlaneGeometry(ref config, vidMesh); vidMesh.uv = uvs; vidMesh.triangles = indices; MeshRenderer vidRenderer = videoObj.AddComponent<MeshRenderer>(); vidRenderer.material = VideoMaterial; #if !UNITY_EDITOR ARUNVideoTexture auVidTexture = videoObj.AddComponent<ARUNVideoTexture>(); #endif videoObj.transform.eulerAngles = new Vector3(0f, 0f, -90f); return videoObj; }
/*! * @brief regenerates the camera and plane geometry from the given @a config struct * @param config the configuration for which to generate a camera and plane */ private void RegenerateGeometry(ref ARUNBridge.AuPlatformConfiguration config) { ConfigureCameraGeometry(m_camera.camera); ConfigurePlaneGeometry(ref config, m_plane.GetComponent<MeshFilter>().mesh); }
Vector2 OffsetsFromConfiguration(ref ARUNBridge.AuPlatformConfiguration config) { float cols = ARUNBridge.CurrentARResult.BackgroundVideoSize.width; float rows = ARUNBridge.CurrentARResult.BackgroundVideoSize.height; Vector2 result; if (cols > rows) { result.x = cols / rows; result.y = 1f; } else { result.x = 1f; result.y = rows / cols; } return result; }
/*! * @brief retrieves the AuPlatformConfiguration, setting @a config in the process * @param config the AuPlatformConfiguration struct to hold the current configuration */ private void GetPlatformConfiguration(out ARUNBridge.AuPlatformConfiguration config) { #if UNITY_EDITOR // simulate waiting a few frames for the camera geometry if (queryCount > 5) { config = new ARUNBridge.AuPlatformConfiguration(); config.projFrustLeft = -0.03f; config.projFrustTop = -0.05325f; config.projFrustNear = 0.1f; config.vidRectLeft = -360.0f; config.vidRectTop = -640.0f; config.vidRectNear = 1200.0f; } else { config = new ARUNBridge.AuPlatformConfiguration(); config.projFrustLeft = 0.0f; config.projFrustTop = 0.0f; config.projFrustNear = 0.0f; config.vidRectLeft = 0.0f; config.vidRectTop = 0.0f; config.vidRectNear = 0.0f; } queryCount ++; #else config = ARUNBridge.CurrentARResult.CameraConfigurationSettings; #endif }
/*! * @brief generates a perspective projection matrix from the given platform configuration * @param config the platform configuration describing the camera * @param near the near plane * @param far the far plane * @return a new perspective projection matrix described by the given @a config */ public static Matrix4x4 PerspectiveProjectionFromCameraGeometry(ref ARUNBridge.AuPlatformConfiguration config, float far) { return GeneratePerspective( config.projFrustTop, -config.projFrustTop, -config.projFrustLeft, config.projFrustLeft, config.projFrustNear, far); }