public static void notifyMeshReady() { if (StructureARPlugin.StructureEvent != null) { StructureARPlugin.StructureEvent(new object(), new StructureARPluginEventArgs(StructureARPluginEvent.ScannedMeshReady)); } }
public static void notifyCameraAccessRequired() { if (StructureARPlugin.StructureEvent != null) { StructureARPlugin.StructureEvent(new object(), new StructureARPluginEventArgs(StructureARPluginEvent.CameraAccessRequired)); } }
public static void setCameraProjectionMatrix(IntPtr pointer) { float[] matrix = new float[16]; Marshal.Copy(pointer, matrix, 0, 16); projectionMatrix.m00 = matrix[0]; projectionMatrix.m10 = matrix[1]; projectionMatrix.m20 = matrix[2]; projectionMatrix.m30 = matrix[3]; projectionMatrix.m01 = matrix[4]; projectionMatrix.m11 = matrix[5]; projectionMatrix.m21 = matrix[6]; projectionMatrix.m31 = matrix[7]; projectionMatrix.m02 = matrix[8]; projectionMatrix.m12 = matrix[9]; projectionMatrix.m22 = matrix[10]; projectionMatrix.m32 = matrix[11]; projectionMatrix.m03 = matrix[12]; projectionMatrix.m13 = matrix[13]; projectionMatrix.m23 = matrix[14]; projectionMatrix.m33 = matrix[15]; if (StructureARPlugin.StructureEvent != null) { StructureARPlugin.StructureEvent(new object(), new StructureARPluginEventArgs(StructureARPluginEvent.UpdateProjectionMatrix)); } }
public static void notifySensorNeedsCharging() { if (StructureARPlugin.StructureEvent != null) { StructureARPlugin.StructureEvent(new object(), new StructureARPluginEventArgs(StructureARPluginEvent.SensorNeedsCharging)); } }
public void HandleHoverCatButtonClickedEvent(object sender, HoverCatButtonEventArgs buttonArgs) { switch (this.gameState) { case SensorState.DeviceReady: this.MainCamera.GetComponent <PinchToScale>().enablePinchToScale = false; StructureARPlugin.startScanning(); //tell StructurePlugin to start scanning this.gameState = SensorState.Scanning; break; case SensorState.Scanning: StructureARPlugin.doneScanning(); //tell StructurePlugin to finish scanning. this.gameState = SensorState.WaitingForMesh; break; case SensorState.WaitingForMesh: //ignore buttons. break; case SensorState.Playing: if (buttonArgs.catButtonEvent == HoverCatButtonEvent.ResetGame) { this.MainCamera.GetComponent <PinchToScale>().enablePinchToScale = true; structureObjectLoader.ClearMesh(this.scanObject); StructureARPlugin.resetScanning(); //tell StructurePlugin to reset the scanned data. this.gameState = SensorState.DeviceReady; } break; default: GameLog.Log(this.ToString() + " -- unhandled game state for button" + buttonArgs.catButtonEvent); break; } }
public static void notifySensorDisConnected() { if (StructureARPlugin.StructureEvent != null) { StructureARPlugin.StructureEvent(new object(), new StructureARPluginEventArgs(StructureARPluginEvent.SensorDisconnected)); } }
protected void OnPostRender() { //this tells the StructureAR plugin that //we have finished showing the rendering #if UNITY_IPHONE StructureARPlugin.CallPostRenderEvent(); GL.InvalidateState(); #endif }
protected void OnPreRender() { //this tells the StructureAR plugin to //prep the render buffer #if UNITY_IPHONE StructureARPlugin.CallPreRenderEvent(); GL.InvalidateState(); #endif }
public static void notifyTrackingStatus(bool trackingOK) { if (StructureARPlugin.StructureEvent != null) { if (trackingOK) { StructureARPlugin.StructureEvent(new object(), new StructureARPluginEventArgs(StructureARPluginEvent.TrackingFound)); } else { StructureARPlugin.StructureEvent(new object(), new StructureARPluginEventArgs(StructureARPluginEvent.TrackingLost)); } } }
public CameraViewScript(Camera camera, float distance) { this.CameraObject = new GameObject("OrthoCamera"); float fWidth = (float)(1 * camera.aspect); float fHeight = (float)(1); Vector3 a = new Vector3(fWidth, fHeight, distance); Vector3 b = new Vector3(fWidth, -fHeight, distance); Vector3 c = new Vector3(-fWidth, -fHeight, distance); Vector3 d = new Vector3(-fWidth, fHeight, distance); //add mesh filter MeshFilter meshFilter = this.CameraObject.gameObject.AddComponent <MeshFilter>() as MeshFilter; meshFilter.mesh = makeQuad(a, b, c, d); this.CameraTexture = new Texture2D(640, 480, TextureFormat.RGBA32, false); StructureARPlugin.SetCameraTexture(this.CameraTexture.GetNativeTexturePtr()); MeshRenderer meshRenderer = this.CameraObject.gameObject.AddComponent <MeshRenderer>() as MeshRenderer; Material material = new Material(Shader.Find("CameraVideo")); material.color = Color.white; meshRenderer.material = material; meshRenderer.material.mainTexture = this.CameraTexture; // this camera render the iPad camera, we don't want it interferes with other Unity objects, so we move the position very far away this.CameraObject.AddComponent <Camera>(); this.CameraObject.transform.position = new Vector3(0, -1000, 0); this.CameraObject.transform.localScale = new Vector3(1, -1, 1); this.CameraObject.GetComponent <Camera>().depth = -1; this.CameraObject.GetComponent <Camera>().clearFlags = CameraClearFlags.Skybox; this.CameraObject.GetComponent <Camera>().enabled = true; this.CameraObject.GetComponent <Camera>().orthographic = true; this.CameraObject.GetComponent <Camera>().orthographicSize = 1.0f; this.CameraObject.GetComponent <Camera>().farClipPlane = 1.1f; this.CameraObject.GetComponent <Camera>().nearClipPlane = 0.9f; this.CameraObject.AddComponent <OnPreRenderBehaviour>(); }
public IEnumerator LoadObject(GameObject gameObject) { //clear the old mesh and replace it with a new one this.objectMesh = new Mesh(); // The LoadObject method is called from the manager // and we ask the plugin to update a gameObject's mesh StructureARPlugin.getMeshObj(ref this.objectMesh); if (this.objectMesh == null) { yield return(null); } else { GameLog.Log("getting mesh object from plugin..."); // check if there is allready a MeshFilter present, if not add one MeshFilter meshFilter = (MeshFilter)gameObject.GetComponent <MeshFilter>() as MeshFilter; //if we were not able to get the meshFilter component of this game object then //we shuld add one here if (meshFilter == null) { //adding the mesh filter here meshFilter = gameObject.AddComponent <MeshFilter>() as MeshFilter; } //make sure that the mesh on the object's mesh filter is this one. meshFilter.mesh = this.objectMesh; //check if there is allready a MeshRenderer present, if not add one MeshRenderer meshRenderer = (MeshRenderer)gameObject.GetComponent <MeshRenderer>() as MeshRenderer; if (meshRenderer == null) { meshRenderer = gameObject.AddComponent <MeshRenderer>() as MeshRenderer; Material mat = this.AssignMaterial(@"Materials/TransparentInvisible"); if (mat != null) { meshRenderer.material = mat; } } //more of the same with the collider. MeshCollider meshCollider = (MeshCollider)gameObject.GetComponent <MeshCollider>() as MeshCollider; if (meshCollider == null) { meshCollider = gameObject.AddComponent <MeshCollider>() as MeshCollider; meshCollider.sharedMesh = meshFilter.mesh; } else { meshCollider.sharedMesh = this.objectMesh; } this.objectMesh.RecalculateBounds(); this.objectMesh.RecalculateNormals(); yield return(new WaitForFixedUpdate()); } }
// Update is called once per frame void Update() { if (!this.enablePinchToScale) { return; } int touchesCount = 0; foreach (Touch touch in Input.touches) { if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) { touchesCount++; } } if (this.PrevTouchCount < 2 && touchesCount == 2) { this.HandlePinchStarted(); } else if (this.PrevTouchCount == 2 && touchesCount < 2) { this.HandlePinchEnded(); } else if (touchesCount == 2 && this.PrevTouchCount == 2)// If there are two touches on the device... { // Store both touches. Touch touchZero = Input.GetTouch(0); Touch touchOne = Input.GetTouch(1); if (touchZero.phase == TouchPhase.Moved && touchOne.phase == TouchPhase.Moved) { Touch touchCurr0, touchCurr1; if (touchZero.fingerId == TouchStart0.fingerId) { touchCurr0 = touchZero; touchCurr1 = touchOne; } else { touchCurr0 = touchOne; touchCurr1 = touchZero; } Vector2 v0 = TouchStart0.position - TouchStart1.position; Vector2 v1 = touchCurr0.position - touchCurr1.position; this.DeltaScale = Mathf.Sqrt(v1.magnitude / v0.magnitude); this.CurrentScale = this.InitialScale * this.DeltaScale; } this.CurrentScale = Mathf.Max(Mathf.Min(this.MaxScale, this.CurrentScale), this.MinScale); // Send pinch events StructureARPlugin.handlePinchScale(this.CurrentScale); if (TouchEvent != null) { TouchEvent(new ScaleEventArgs(this.CurrentScale)); } } this.PrevTouchCount = touchesCount; }
/// <summary> /// Raises the structure AR event event. /// </summary> /// <param name="sender">Sender.</param> /// <param name="structureArgs">Structure arguments.</param> protected virtual void HandleStructureEvent(object sender, StructureARPluginEventArgs structureArgs) { switch (structureArgs.eventType) { case StructureARPluginEvent.SensorConnected: if (this.gameState == SensorState.DeviceNotReady) { if (this.MainCamera.GetComponent <PinchToScale>() != null) { this.MainCamera.GetComponent <PinchToScale>().enablePinchToScale = true; } //clear the scanned mesh this.ClearScannedMesh(); StructureARPlugin.resetScanning(); //tell StructurePlugin to reset the scanned data. } this.gameState = SensorState.DeviceReady; break; case StructureARPluginEvent.CameraAccessRequired: this.gameState = SensorState.CameraAccessRequired; if (StructureARGameEvent != null) { StructureARGameEvent(this, new GameEventArgs(this.gameState, true)); } break; case StructureARPluginEvent.SensorNeedsCharging: this.gameState = SensorState.DeviceNeedsCharging; break; case StructureARPluginEvent.SensorDisconnected: this.gameState = SensorState.DeviceNotReady; break; case StructureARPluginEvent.UpdateProjectionMatrix: if (this.MainCamera != null) { this.MainCamera.projectionMatrix = StructureARPlugin.projectionMatrix; } break; case StructureARPluginEvent.ScannedMeshReady: // this constructs the mesh from the scanned data // and will be called once we have a mesh ready to build this.BuildScannedMesh(); this.gameState = SensorState.Playing; break; case StructureARPluginEvent.TrackingLost: if (this.trackingIsGood == true) { if (StructureARGameEvent != null) { StructureARGameEvent(this, new GameEventArgs(this.gameState, false)); } this.trackingIsGood = false; } break; case StructureARPluginEvent.TrackingFound: if (this.trackingIsGood == false) { if (StructureARGameEvent != null) { StructureARGameEvent(this, new GameEventArgs(this.gameState, true)); } this.trackingIsGood = true; } break; default: break; } }
/// <summary> /// Raises the button click event. /// </summary> ///*********************** IMPORTANT *********************** /// this is the method that sends the start, done and reset /// commands to the plug-in! public virtual void HandleButtonClickedEvent(object sender, ButtonEventArgs buttonArgs) { //we already know what state we're in, and what state we //need to go to. The local property will update the button's //when it's changed. switch (this.gameState) { case SensorState.DeviceReady: // DeviceReady -> Scanning if (this.MainCamera.GetComponent <PinchToScale>() != null) { this.MainCamera.GetComponent <PinchToScale>().enablePinchToScale = false; } //tell StructurePlugin to start scanning StructureARPlugin.startScanning(); this.gameState = SensorState.Scanning; break; case SensorState.Scanning: // Scanning -> WaitingForMesh if (this.trackingIsGood) { //tell StructurePlugin to finish scanning. //this finishes up the scanning session, when the //mesh is finished, it's sent to the wireframe //object where it's copied over. //the HandleStructureEvent gets ScannedMeshReady where //the construction of the mesh is completed. StructureARPlugin.doneScanning(); this.gameState = SensorState.WaitingForMesh; } else { if (this.MainCamera.GetComponent <PinchToScale>() != null) { this.MainCamera.GetComponent <PinchToScale>().enablePinchToScale = true; } //clear scanned mesh this.ClearScannedMesh(); StructureARPlugin.resetScanning(); //tell StructurePlugin to reset the scanned data. this.gameState = SensorState.DeviceReady; } break; case SensorState.WaitingForMesh: // WaitingForMesh -> Playing UploadScannedMesh(); break; case SensorState.Playing: // Playing -> DeviceReady if (this.MainCamera.GetComponent <PinchToScale>() != null) { this.MainCamera.GetComponent <PinchToScale>().enablePinchToScale = true; } //clear scanned mesh this.ClearScannedMesh(); StructureARPlugin.resetScanning(); //tell StructurePlugin to reset the scanned data. this.gameState = SensorState.DeviceReady; break; default: GameLog.Log(this.ToString() + " -- unhandled game state for button" + buttonArgs.toState); break; } }
public virtual void Start() { Manager._structureManager = this; if (this.MainCamera != null) { Manager._MainCamera = this.MainCamera; } //check if we need to make a GameLog. if (this.ShowDebugLog && this.GetComponent <GameLog>() == null) { this.gameObject.AddComponent <GameLog>(); GameLog.ShowGameLog = this.ShowDebugLog; } //tell the game to run at 30 fps rather than at max. //otherwise the game might be running faster //than the camera. Application.targetFrameRate = 30; //check Main Camera for StructurePOV if (!FreezePOV) { POV sPOV = this.MainCamera.GetComponent <POV>(); if (sPOV == null) { //assign one if it doesn't already have one. this.structurePOV = this.MainCamera.gameObject.AddComponent <POV>(); } else { //get a reference to the component if it does have one. this.structurePOV = sPOV; } } //initialize this game manager to recieve events //from the plug-in. Anything else you want to have listen to //the plugin can be done through here. StructureARPlugin.StructureEvent += this.HandleStructureEvent; StructureARPlugin.setCallbacks(); //object loader to build the mesh in the game world. this.structureObjectLoader = new ObjectLoader(); GameLog.Log(this.ToString() + this.structureObjectLoader); //provide the scanned mesh a place to live in the game world. this.scanObject = new GameObject("ScanObject"); Manager._ScanObject = this.scanObject; Wireframe wf = this.scanObject.AddComponent <Wireframe>(); wf.lineColor = this.ScanMeshWireColor; GameLog.Log(this.ToString() + this.scanObject); //Make color camera back plane this.MainCamera.clearFlags = CameraClearFlags.Depth; this.cameraView = new CameraViewScript(this.MainCamera, 1f); this.cameraView.CameraObject.transform.parent = this.MainCamera.transform; //should also set the camera FOV to 45 this.MainCamera.fieldOfView = 45.0f; //setup the POV camera in game to match the projection //of the rear camera of the device StructureARPlugin.initStructureAR(this.MainCamera.projectionMatrix); //do minor setup here, hide things, change UI etc. if (this.gameState == SensorState.CameraAccessRequired) { return; } // We need to update our status based on whether or not // the sensor is connected since we don't want to scan // without the sensor attached. if (StructureARPlugin.isStructureConnected()) { this.gameState = SensorState.DeviceReady; } else { this.gameState = SensorState.DeviceNotReady; } //tracking is always false in DeviceReady and DeviceNotReady states this.trackingIsGood = false; }