Ejemplo n.º 1
0
    /// <summary>
    /// Checks nearby for valid places for the drone to move.
    /// Valid places must be in front of the player, and not intersect any objects within a reasonable tolerance.
    /// Use radiusCheckRate and percentageThreshold to tweak what counts as a valid location.
    /// </summary>
    /// <param name="newpos"></param>
    /// <returns></returns>
    private bool FindNewMovePosition(out Vector3 newpos)
    {
        //We can't move if the ZED isn't initialized.
        if (!zedManager.IsZEDReady)
        {
            newpos = transform.position;
            return(false);
        }

        Vector3 randomPosition;

        // Look Around For a New Position
        //If the Drone is on the screen, search around a smaller radius.
        //Note that we only check the primary ZEDManager because we only want the drone to spawn in front of the player anyway.
        if (ZEDSupportFunctions.CheckScreenView(transform.position, zedManager.GetMainCamera()))
        {
            randomPosition = UnityEngine.Random.onUnitSphere * UnityEngine.Random.Range(2f, 3f) + transform.position;
        }
        else //if the drone is outside, look around a bigger radius to find a position which is inside the screen.
        {
            randomPosition = UnityEngine.Random.onUnitSphere * UnityEngine.Random.Range(4f, 5f) + transform.position;
        }

        // Look For Any Collisions Through The ZED
        bool hit = ZEDSupportFunctions.HitTestAtPoint(zedManager.zedCamera, zedManager.GetMainCamera(), randomPosition);

        if (!hit)
        {
            newpos = transform.position;
            return(false);
        }

        //If we spawn the drone at that world point, it'll spawn inside a wall. Bring it closer by a distance of ClearRadius.
        Quaternion directiontoDrone = Quaternion.LookRotation(zedManager.GetMainCameraTransform().position - randomPosition, Vector3.up);
        Vector3    newPosition      = randomPosition + directiontoDrone * Vector3.forward * ClearRadius;

        //Check the new position isn't too close from the camera.
        float dist = Vector3.Distance(zedManager.GetMainCamera().transform.position, randomPosition);

        if (dist < 1f)
        {
            newpos = transform.position;
            return(false);
        }

        //Also check nearby points in a sphere of radius to make sure the whole drone has a clear space.
        if (ZEDSupportFunctions.HitTestOnSphere(zedManager.zedCamera, zedManager.GetMainCamera(), newPosition, 1f, radiusCheckRate, percentageThreshold))
        {
            newpos = transform.position;
            return(false);
        }

        //Return true if it's made it this far and out the location we chose.
        newpos = newPosition;
        return(true);
    }
 // Use this for initialization
 void Start()
 {
     zedManager = FindObjectOfType <ZEDManager> ();
     //Set the countdown Timer;
     respawncountdown = respawnTimer;
     //Get the ZED camera
     cam = zedManager.GetMainCamera();
 }
    // Use this for initialization
    void Awake()
    {
        //zedManager = gameObject.transform.parent.GetComponentInChildren<ZEDManager>();
        zedManager = ZEDManager.GetInstance(sl.ZED_CAMERA_ID.CAMERA_ID_01);
        cam        = zedManager.GetMainCamera();

        Cursor.visible = true; //Make sure cursor is visible so we can click on the world accurately.
    }
    /// <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);
    }
Ejemplo n.º 5
0
    private void Awake()
    {
        if (zedManager == null)
        {
            zedManager = FindObjectOfType <ZEDManager>();
            //If this happenend when only using a primary ZED for collisions but there are multiple ZEDs, collisions will be
            //calculated using an arbitrary camera. Warn the user.
            if (testCollisionsUsingAllZEDs == false && ZEDManager.GetInstances().Count > 1)
            {
                Debug.LogWarning("Warning: ZEDProjectile's zedManager value was not specified, resulting in assigning to first available " +
                                 " camera, but there are multiple cameras in the scene. This can cause strange collision test behavior.");
            }
        }

        if (!cam)
        {
            cam = zedManager.GetMainCamera();
        }
    }
Ejemplo n.º 6
0
    /// <summary>
    /// Tests the depth of both the real and virtual in the center of the screen, and returns the world position of the closest one.
    /// </summary>
    /// <param name="crosshairpoint">Where the crosshair should be rendered.</param>
    /// <param name="collisionnormal">The normal vector of the surface aimed at, for rotating the crosshair accordingly if desired.</param>
    /// <returns>False if there is no valid object, real or virtual, on which to place the crosshair. </returns>
    private bool FindCrosshairPosition(out Vector3 crosshairpoint, out Vector3 collisionnormal)
    {
        //Find the distance to the real world. The bool will be false if there is an error reading the depth at the center of the screen.
        Vector3 realpoint         = Vector3.zero;
        float   realdistance      = 20f; //Arbitrary distance to put the crosshair if it hits nothing at all. Chosen by ZED's max range.
        bool    foundrealdistance = false;

        if (ZEDSupportFunctions.HitTestOnRay(zedManager.zedCamera, zedManager.GetMainCamera(), laserPointerBeadHolder.transform.position,
                                             laserPointerBeadHolder.transform.rotation, 5f, 0.01f, out realpoint))
        {
            realdistance      = Vector3.Distance(laserPointerBeadHolder.transform.position, realpoint);
            foundrealdistance = true;
        }


        //Find the distance to the virtual. The bool will be false if there are no colliders ahead of you.
        RaycastHit hitinfo;
        bool       foundvirtualdistance = Physics.Raycast(laserPointerBeadHolder.transform.position, laserPointerBeadHolder.transform.rotation * Vector3.forward, out hitinfo);

        //If we didn't find either, return false so the laser and bead can be disabled.
        if (!foundrealdistance && !foundvirtualdistance)
        {
            crosshairpoint  = Vector3.zero;
            collisionnormal = Vector3.zero;
            return(false);
        }

        //Decide if we use the real or virtual distance
        if (!foundvirtualdistance || realdistance < hitinfo.distance)
        {
            //The real world is closer. Give the position of the real world pixel and return true.
            crosshairpoint = realpoint;
            ZEDSupportFunctions.GetNormalAtWorldLocation(zedManager.zedCamera, realpoint, sl.REFERENCE_FRAME.WORLD, zedManager.GetMainCamera(), out collisionnormal);
            return(true);
        }
        else
        {
            //The virtual world is closer, or they're tied. Return the world posiiton where the raycast hit the virtual collider.
            crosshairpoint  = hitinfo.point;
            collisionnormal = hitinfo.normal;
            return(true);
        }
    }
Ejemplo n.º 7
0
    void Awake()
    {
        if (!zedManager)
        {
            zedManager = FindObjectOfType <ZEDManager>();
        }

        //Find the left camera object if we didn't assign it at start.
        if (!cam)
        {
            cam = zedManager.GetMainCamera();
        }

        //Check if there is a Object Tracker on this object for VR controls.
        var tracker = GetComponent <ZEDControllerTracker>();

        if (tracker != null)
        {
            //Get the parent object of the baseball bat.
            if (transform.childCount > 1)
            {
                baseballBat = transform.GetChild(1).gameObject;
                baseballBat.SetActive(false); //Hide it by default. It'll get revealed once we place a bunny.
            }
        }
        //Instantiate the pointer prefab and assign it to our variables.
        if (pointerPrefab != null)
        {
            pointer     = Instantiate(pointerPrefab) as GameObject; //Get the Anchor/root of the pointerBead.
            placeholder = pointer.transform.GetChild(0).gameObject; //Get the laser's pointerBead.
        }
        //If we didn't set a transform for the pointer's origin position...
        if (rayOrigin == null)
        {
            rayOrigin = transform; //...then take our local position.
        }

        //Set the PlaneManager's reference to our placeholder.
        GetComponent <BunnyPlacement>().SetPlaceholder(placeholder.transform);
    }
    /// <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.
    /// <para>Note that this can't be called the first frame the ZED is ready. Use a coroutine to wait one frame,
    /// or until ZEDManager.isZEDReady is true.</para>
    /// </summary>
    /// <returns><c>true</c>, if floor plane was detected, <c>false</c> otherwise.</returns>
    public bool DetectFloorPlane(ZEDManager manager, bool auto)
    {
        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.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(cam.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);
                }

                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  = cam.transform.position;                     //Add the camera's world position
                floorPlaneGO.transform.position += cam.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(cam, plane, worldPlaneVertices, worldPlaneTriangles, 0, overrideMaterial);
                    }
                    else
                    {
                        floorPlane.Create(cam, 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);
    }