/// <summary> /// Create the plane as a gameobject and fills the internal structure planeData for future access /// </summary> /// <param name="holder">Holder.</param> /// <param name="plane">PlaneData fills by findXXXPlane().</param> /// <param name="vertices">Vertices of the mesh</param> /// <param name="triangles">Triangles of the mesh</param> public void Create(Transform holder, ZEDPlaneGameObject.PlaneData plane, Vector3[] vertices, int[] triangles, int opt_count, Material rendermaterial) { planeData.ErrorCode = plane.ErrorCode; planeData.Type = plane.Type; planeData.PlaneNormal = plane.PlaneNormal; planeData.PlaneCenter = plane.PlaneCenter; planeData.PlaneTransformPosition = plane.PlaneTransformPosition; planeData.PlaneTransformOrientation = plane.PlaneTransformOrientation; planeData.PlaneEquation = plane.PlaneEquation; planeData.Extents = plane.Extents; ///Create the GameObject as Primitive Plane if (gameObject == null) { gameObject = new GameObject(); } gameObject.AddComponent <MeshCollider>().sharedMesh = null; if (plane.Type == PLANE_TYPE.FLOOR) { gameObject.name = "Floor Plane"; } else if (plane.Type == PLANE_TYPE.HIT) { gameObject.name = "Hit Plane " + opt_count; } gameObject.transform.SetParent(holder); gameObject.layer = sl.ZEDCamera.TagOneObject; SetComponents(plane, vertices, triangles, rendermaterial); isCreated = true; }
/// <summary> /// Detects the floor plane. Replaces the current floor plane, if there is one, unlike DetectPlaneAtHit. /// </summary> /// <returns><c>true</c>, if floor plane was detected, <c>false</c> otherwise.</returns> public bool DetectFloorPlane(bool auto) { if (!isReady) { return(false); } ZEDPlaneGameObject.PlaneData plane = new ZEDPlaneGameObject.PlaneData(); if (zedCam.findFloorPlane(ref plane, out estimatedPlayerHeight, Quaternion.identity, Vector3.zero) == sl.ERROR_CODE.SUCCESS) { int numVertices, numTriangles = 0; zedCam.convertFloorPlaneToMesh(planeMeshVertices, planeMeshTriangles, out numVertices, out numTriangles); if (numVertices > 0 && numTriangles > 0) { Vector3[] worldPlaneVertices = new Vector3[numVertices]; int[] worldPlaneTriangles = new int[numTriangles]; TransformCameraToLocalMesh(LeftCamera.transform, planeMeshVertices, planeMeshTriangles, worldPlaneVertices, worldPlaneTriangles, numVertices, numTriangles, plane.PlaneCenter); hasDetectedFloor = true; if (!floorPlaneGO) { floorPlaneGO = new GameObject("Floor Plane"); floorPlaneGO.transform.SetParent(holder.transform); } //Move the gameobject to the center of the plane. Note that the plane data's center is relative to the camera. floorPlaneGO.transform.position = LeftCamera.transform.position; //Add the camera's world position floorPlaneGO.transform.position += LeftCamera.transform.rotation * plane.PlaneCenter; //Add the center of the plane if (!floorPlane) { floorPlane = floorPlaneGO.AddComponent <ZEDPlaneGameObject>(); } if (!floorPlane.IsCreated) { if (overrideMaterial != null) { floorPlane.Create(plane, worldPlaneVertices, worldPlaneTriangles, 0, overrideMaterial); } else { floorPlane.Create(plane, worldPlaneVertices, worldPlaneTriangles, 0); } floorPlane.SetPhysics(addPhysicsOption); } else { floorPlane.UpdateFloorPlane(!auto, plane, worldPlaneVertices, worldPlaneTriangles, overrideMaterial); floorPlane.SetPhysics(addPhysicsOption); } return(true); } } return(false); }
/// <summary> /// Detects the floor plane. Replaces the current floor plane, if there is one, unlike DetectPlaneAtHit(). /// If a floor is detected, also assigns the user's height from the floor to estimatedPlayerHeight. /// </summary> /// <returns><c>true</c>, if floor plane was detected, <c>false</c> otherwise.</returns> public bool DetectFloorPlane(bool auto) { if (!IsReady) { return(false); //Do nothing if the ZED isn't finished initializing. } ZEDPlaneGameObject.PlaneData plane = new ZEDPlaneGameObject.PlaneData(); if (zedCam.findFloorPlane(ref plane, out estimatedPlayerHeight, Quaternion.identity, Vector3.zero) == sl.ERROR_CODE.SUCCESS) //We found a plane. { int numVertices, numTriangles = 0; zedCam.convertFloorPlaneToMesh(planeMeshVertices, planeMeshTriangles, out numVertices, out numTriangles); if (numVertices > 0 && numTriangles > 0) { Vector3[] worldPlaneVertices = new Vector3[numVertices]; int[] worldPlaneTriangles = new int[numTriangles]; TransformCameraToLocalMesh(LeftCamera.transform, planeMeshVertices, planeMeshTriangles, worldPlaneVertices, worldPlaneTriangles, numVertices, numTriangles, plane.PlaneCenter); hasDetectedFloor = true; if (!floorPlaneGO) //Make the GameObject. { floorPlaneGO = new GameObject("Floor Plane"); floorPlaneGO.transform.SetParent(holder.transform); } //Move the GameObject to the center of the plane. Note that the plane data's center is relative to the camera. floorPlaneGO.transform.position = LeftCamera.transform.position; //Add the camera's world position floorPlaneGO.transform.position += LeftCamera.transform.rotation * plane.PlaneCenter; //Add the center of the plane if (!floorPlane) //Add a new ZEDPlaneGameObject to the floor plane if it doesn't already exist. { floorPlane = floorPlaneGO.AddComponent <ZEDPlaneGameObject>(); } if (!floorPlane.IsCreated) //Call ZEDPlaneGameObject.Create() on the floor ZEDPlaneGameObject if it hasn't yet been run. { if (overrideMaterial != null) { floorPlane.Create(plane, worldPlaneVertices, worldPlaneTriangles, 0, overrideMaterial); } else { floorPlane.Create(plane, worldPlaneVertices, worldPlaneTriangles, 0); } floorPlane.SetPhysics(addPhysicsOption); } else //Update the ZEDPlaneGameObject with the new plane's data. { floorPlane.UpdateFloorPlane(!auto, plane, worldPlaneVertices, worldPlaneTriangles, overrideMaterial); floorPlane.SetPhysics(addPhysicsOption); } return(true); } } return(false); }
/// <summary> /// Updates the floor plane with new plane data, if /// </summary> /// <returns><c>true</c>, if floor plane was updated, <c>false</c> otherwise.</returns> /// <param name="force">If set to <c>true</c> force the update. Is set to false, update only if new plane/mesh is bigger or contains the old one</param> /// <param name="plane">PlaneData returned from the ZED SDK.</param> /// <param name="vertices">Vertices of the new plane mesh.</param> /// <param name="triangles">Triangles of the new plane mesh.</param> public bool UpdateFloorPlane(bool force, ZEDPlaneGameObject.PlaneData plane, Vector3[] vertices, int[] triangles, Material rendermaterial = null) { bool need_update = false; if (!force) //Not force mode. Check if the new mesh contains or is larger than the old one. { if (!gameObject.GetComponent <MeshRenderer>().isVisible) { need_update = true; } else { Mesh tmp = new Mesh(); tmp.vertices = vertices; tmp.SetTriangles(triangles, 0, false); tmp.RecalculateBounds(); Bounds tmpNBnds = tmp.bounds; MeshFilter mf = gameObject.GetComponent <MeshFilter>(); if ((tmpNBnds.Contains(mf.mesh.bounds.min) && tmpNBnds.Contains(mf.mesh.bounds.max)) || (tmpNBnds.size.x * tmpNBnds.size.y > 1.1 * mf.mesh.bounds.size.x * mf.mesh.bounds.size.y)) { need_update = true; } tmp.Clear(); } } else //Force mode. Update the mesh regardless of the existing mesh. { need_update = true; } if (need_update) { SetComponents(plane, vertices, triangles, gameObject.GetComponent <MeshRenderer>().material); } MeshRenderer mr = gameObject.GetComponent <MeshRenderer>(); if (mr != null) { if (rendermaterial != null) { mr.material = rendermaterial; } else { mr.material = GetDefaultMaterial(plane.Type); } } return(need_update); }
/// <summary> /// Detects the plane around screen-space coordinates specified. /// </summary> /// <returns><c>true</c>, if plane at hit was detected, <c>false</c> otherwise.</returns> /// <param name="screenPos">Position of the pixel in screen space (2D).</param> public bool DetectPlaneAtHit(ZEDManager manager, Vector2 screenPos) { if (!manager.IsZEDReady) { return(false); //Do nothing if the ZED isn't finished initializing. } sl.ZEDCamera zedcam = manager.zedCamera; Camera cam = manager.GetMainCamera(); ZEDPlaneGameObject.PlaneData plane = new ZEDPlaneGameObject.PlaneData(); if (zedcam.findPlaneAtHit(ref plane, screenPos) == sl.ERROR_CODE.SUCCESS) //We found a plane. { int numVertices, numTriangles = 0; zedcam.convertHitPlaneToMesh(planeMeshVertices, planeMeshTriangles, out numVertices, out numTriangles); if (numVertices > 0 && numTriangles > 0) { GameObject newhitGO = new GameObject(); //Make a new GameObject to hold the new plane. newhitGO.transform.SetParent(holder.transform); Vector3[] worldPlaneVertices = new Vector3[numVertices]; int[] worldPlaneTriangles = new int[numTriangles]; TransformCameraToLocalMesh(cam.transform, planeMeshVertices, planeMeshTriangles, worldPlaneVertices, worldPlaneTriangles, numVertices, numTriangles, plane.PlaneCenter); //Move the GameObject to the center of the plane. Note that the plane data's center is relative to the camera. newhitGO.transform.position = cam.transform.position; //Add the camera's world position newhitGO.transform.position += cam.transform.rotation * plane.PlaneCenter; //Add the center of the plane ZEDPlaneGameObject hitPlane = newhitGO.AddComponent <ZEDPlaneGameObject>(); if (overrideMaterial != null) { hitPlane.Create(cam, plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1, overrideMaterial); } else { hitPlane.Create(cam, plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1); } hitPlane.SetPhysics(addPhysicsOption); //hitPlane.SetVisible(isVisibleInSceneOption); hitPlaneList.Add(hitPlane); planeHitCount++; return(true); } } return(false); }
/// <summary> /// Detects the plane around screen-space coordinates specified. /// </summary> /// <returns><c>true</c>, if plane at hit was detected, <c>false</c> otherwise.</returns> /// <param name="screenPos">position of the pixel in screen space (2D)</param> public bool DetectPlaneAtHit(Vector2 screenPos) { if (!isReady) { return(false); } ZEDPlaneGameObject.PlaneData plane = new ZEDPlaneGameObject.PlaneData(); if (zedCam.findPlaneAtHit(ref plane, screenPos) == sl.ERROR_CODE.SUCCESS) { int numVertices, numTriangles = 0; zedCam.convertHitPlaneToMesh(planeMeshVertices, planeMeshTriangles, out numVertices, out numTriangles); if (numVertices > 0 && numTriangles > 0) { GameObject newhitGO = new GameObject(); //TODO: Move to proper location. (Need to rework how the mesh works first, though) newhitGO.transform.SetParent(holder.transform); Vector3[] worldPlaneVertices = new Vector3[numVertices]; int[] worldPlaneTriangles = new int[numTriangles]; TransformCameraToLocalMesh(LeftCamera.transform, planeMeshVertices, planeMeshTriangles, worldPlaneVertices, worldPlaneTriangles, numVertices, numTriangles, plane.PlaneCenter); //Move the gameobject to the center of the plane. Note that the plane data's center is relative to the camera. newhitGO.transform.position = LeftCamera.transform.position; //Add the camera's world position newhitGO.transform.position += LeftCamera.transform.rotation * plane.PlaneCenter; //Add the center of the plane ZEDPlaneGameObject hitPlane = newhitGO.AddComponent <ZEDPlaneGameObject>(); if (overrideMaterial != null) { hitPlane.Create(plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1, overrideMaterial); } else { hitPlane.Create(plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1); } hitPlane.SetPhysics(addPhysicsOption); hitPlane.SetVisible(isVisibleInSceneOption); hitPlaneList.Add(hitPlane); planeHitCount++; return(true); } } return(false); }
/// <summary> /// Detects the floor plane. /// </summary> /// <returns><c>true</c>, if floor plane was detected, <c>false</c> otherwise.</returns> public bool DetectFloorPlane(bool auto) { if (!isReady) { return(false); } ZEDPlaneGameObject.PlaneData plane = new ZEDPlaneGameObject.PlaneData(); if (zedCam.findFloorPlane(ref plane, out estimatedPlayerHeight, Quaternion.identity, Vector3.zero) == sl.ERROR_CODE.SUCCESS) { int numVertices, numTriangles = 0; zedCam.convertFloorPlaneToMesh(planeMeshVertices, planeMeshTriangles, out numVertices, out numTriangles); if (numVertices > 0 && numTriangles > 0) { Vector3[] worldPlaneVertices = new Vector3[numVertices]; int[] worldPlaneTriangles = new int[numTriangles]; TransformLocalToWorldMesh(LeftCamera.transform, planeMeshVertices, planeMeshTriangles, worldPlaneVertices, worldPlaneTriangles, numVertices, numTriangles); hasDetectedFloor = true; if (!floorPlaneGO.IsCreated) { if (overrideMaterial != null) { floorPlaneGO.Create(holder.transform, plane, worldPlaneVertices, worldPlaneTriangles, 0, overrideMaterial); } else { floorPlaneGO.Create(holder.transform, plane, worldPlaneVertices, worldPlaneTriangles, 0); } floorPlaneGO.SetPhysics(addPhysicsOption); } else { floorPlaneGO.UpdateFloorPlane(!auto, plane, worldPlaneVertices, worldPlaneTriangles, overrideMaterial); floorPlaneGO.SetPhysics(addPhysicsOption); } return(true); } } return(false); }
/// <summary> /// Detects the plane around pixel hit /// </summary> /// <returns><c>true</c>, if plane at hit was detected, <c>false</c> otherwise.</returns> /// <param name="imagePixel">Image pixel.</param> public bool DetectPlaneAtHit(Vector2 imagePixel) { if (!isReady) { return(false); } ZEDPlaneGameObject.PlaneData plane = new ZEDPlaneGameObject.PlaneData(); if (zedCam.findPlaneAtHit(ref plane, imagePixel) == sl.ERROR_CODE.SUCCESS) { int numVertices, numTriangles = 0; zedCam.convertHitPlaneToMesh(planeMeshVertices, planeMeshTriangles, out numVertices, out numTriangles); if (numVertices > 0 && numTriangles > 0) { Vector3[] worldPlaneVertices = new Vector3[numVertices]; int[] worldPlaneTriangles = new int[numTriangles]; TransformLocalToWorldMesh(LeftCamera.transform, planeMeshVertices, planeMeshTriangles, worldPlaneVertices, worldPlaneTriangles, numVertices, numTriangles); ZEDPlaneGameObject hitPlane = new ZEDPlaneGameObject(); if (overrideMaterial != null) { hitPlane.Create(holder.transform, plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1, overrideMaterial); } else { hitPlane.Create(holder.transform, plane, worldPlaneVertices, worldPlaneTriangles, planeHitCount + 1); } hitPlane.SetPhysics(addPhysicsOption); hitPlane.SetVisible(isVisibleInSceneOption); hitPlaneGOList.Add(hitPlane); planeHitCount++; return(true); } } return(false); }
/// <summary> /// Updates the floor plane using "force" mode /// </summary> /// <returns><c>true</c>, if floor plane was updated, <c>false</c> otherwise.</returns> /// <param name="force">If set to <c>true</c> force the update. Is set to false, update only if new plane/mesh is bigger or contains the old one</param> /// <param name="plane">Plane.</param> /// <param name="vertices">Vertices.</param> /// <param name="triangles">Triangles.</param> public bool UpdateFloorPlane(bool force, ZEDPlaneGameObject.PlaneData plane, Vector3[] vertices, int[] triangles, Material rendermaterial = null) { //Needs to be created if (gameObject == null) { return(false); } bool need_update = false; //Check mesh if (!force) { if (!gameObject.GetComponent <MeshRenderer>().isVisible) { need_update = true; } else { Mesh tmp = new Mesh(); tmp.vertices = vertices; tmp.SetTriangles(triangles, 0, false); tmp.RecalculateBounds(); Bounds tmpNBnds = tmp.bounds; MeshFilter mf = gameObject.GetComponent <MeshFilter>(); if ((tmpNBnds.Contains(mf.mesh.bounds.min) && tmpNBnds.Contains(mf.mesh.bounds.max)) || (tmpNBnds.size.x * tmpNBnds.size.y > 1.1 * mf.mesh.bounds.size.x * mf.mesh.bounds.size.y)) { need_update = true; } tmp.Clear(); } } else { need_update = true; } if (need_update) { SetComponents(plane, vertices, triangles, gameObject.GetComponent <MeshRenderer>().material); } MeshRenderer mr = gameObject.GetComponent <MeshRenderer>(); if (mr != null) { if (rendermaterial != null) { mr.material = rendermaterial; } else { mr.material = GetDefaultMaterial(plane.Type); } } return(need_update); }
/// <summary> /// Create the plane as a gameobject and fills the internal structure planeData for future access /// </summary> /// <param name="holder">Holder.</param> /// <param name="plane">PlaneData fills by findXXXPlane().</param> /// <param name="vertices">Vertices of the mesh</param> /// <param name="triangles">Triangles of the mesh</param> public void Create(Transform holder, ZEDPlaneGameObject.PlaneData plane, Vector3[] vertices, int[] triangles, int opt_count) { Create(holder, plane, vertices, triangles, opt_count, GetDefaultMaterial(plane.Type)); }