// Functions
        protected override RaycastResult CalculateRayIntersect()
        {
            RaycastResult result = base.CalculateRayIntersect();

            // Now use the understanding code
            if (SpatialUnderstanding.Instance.AllowSpatialUnderstanding &&
                SpatialUnderstanding.Instance.ScanState == SpatialUnderstanding.ScanStates.Done)
            {
                Vector3 rayPos           = Camera.main.transform.position;
                Vector3 rayVec           = Camera.main.transform.forward * RayCastLength;
                IntPtr  raycastResultPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResultPtr();
                SpatialUnderstandingDll.Imports.PlayspaceRaycast(
                    rayPos.x, rayPos.y, rayPos.z,
                    rayVec.x, rayVec.y, rayVec.z,
                    raycastResultPtr);
                rayCastResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResult();

                float rayCastResultDist = Vector3.Distance(rayPos, rayCastResult.IntersectPoint);
                float resultDist        = Vector3.Distance(rayPos, result.Position);

                // Override
                if (rayCastResult.SurfaceType != SpatialUnderstandingDll.Imports.RaycastResult.SurfaceTypes.Invalid &&
                    rayCastResultDist < resultDist)
                {
                    result.Hit      = true;
                    result.Position = rayCastResult.IntersectPoint;
                    result.Normal   = rayCastResult.IntersectNormal;

                    return(result);
                }
            }

            return(result);
        }
Example #2
0
    public void CheckRayCast(Vector3 targetObjectIndex)
    {
        Debug.Log("Checking raycast for targetObjectIndex..." + targetObjectIndex);
        // Uncomment for HoloLens

        //use for object raycasts
        Vector3 rayPos = transform.position;
        //Vector3 hitdirection = this.transform.TransformDirection(Vector3.forward);
        Vector3 hitdirection = targetObjectIndex - rayPos;

        //Debug.Log("cube position: " + transform.position);
        //Debug.Log("path direction: " + hitdirection);
        // if three is a raycast hit, then do the checks
        RaycastHit hit;

        if (Physics.Raycast(rayPos, hitdirection, out hit, 10))
        {
            // instantiate object where raycast hit an obstacle
            GameObject capsule = Instantiate(GameObject.CreatePrimitive(PrimitiveType.Capsule), hit.point, transform.rotation);
            capsule.transform.localScale = new Vector3(0.3f, 0.3f, 0.3f);

            IntPtr raycastResultPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResultPtr();
            Debug.Log("raycastResultPtr is: " + raycastResultPtr);

            // Uncomment for HoloLens
            SpatialUnderstandingDll.Imports.PlayspaceRaycast(
                rayPos.x, rayPos.y, rayPos.z,
                hitdirection.x, hitdirection.y, hitdirection.z,
                raycastResultPtr);
            rayCastResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResult();
            //Debug.Log("Raycast result is: " + rayCastResult.SurfaceType);

            // calculate distance between movable object and point where raycast hit
            float distance = Vector3.Distance(transform.position, capsule.transform.position);
            Debug.Log("distance is: " + distance);

            visualInfo.text = "Next obstacle at: " + distance + " metres";

            // call move logic whether to move the movable object or not
            MoveLogic(distance, targetObjectIndex);
        }
        // if there are no raycasts hit, then  no obstacles, move the object
        else
        {
            MoveLogic(0, targetObjectIndex);
        }

        Debug.Log("Finished checking raycast");
    }
    // Functions
    protected override RaycastResult CalculateRayIntersect()
    {
        RaycastResult result;

        // Check UI elements - they get precedence
        Vector3 hitPos, hitNormal;
        Button  hitButton;

        if (RayCastUI(out hitPos, out hitNormal, out hitButton))
        {
            result.Hit      = true;
            result.Position = hitPos;
            result.Normal   = hitNormal;

            return(result);
        }

        // Now use the understanding code
        if (SpatialUnderstanding.Instance.AllowSpatialUnderstanding &&
            (SpatialUnderstanding.Instance.ScanState == SpatialUnderstanding.ScanStates.Done))
        {
            Vector3 rayPos           = Camera.main.transform.position;
            Vector3 rayVec           = Camera.main.transform.forward * RayCastLength;
            IntPtr  raycastResultPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResultPtr();
            SpatialUnderstandingDll.Imports.PlayspaceRaycast(
                rayPos.x, rayPos.y, rayPos.z, rayVec.x, rayVec.y, rayVec.z,
                raycastResultPtr);
            rayCastResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResult();

            // Override
            if (rayCastResult.SurfaceType != SpatialUnderstandingDll.Imports.RaycastResult.SurfaceTypes.Invalid)
            {
                result.Hit      = true;
                result.Position = rayCastResult.IntersectPoint;
                result.Normal   = rayCastResult.IntersectNormal;

                return(result);
            }
        }

        // Base
        return(base.CalculateRayIntersect());
    }
Example #4
0
    // Functions
    protected override RaycastResult CalculateRayIntersect()
    {
        RaycastResult result;

        // Check UI elements - they get precedence
        Vector3 hitPos, hitNormal;
        Button hitButton;
        if (RayCastUI(out hitPos, out hitNormal, out hitButton))
        {
            result.Hit = true;
            result.Position = hitPos;
            result.Normal = hitNormal;

            return result;
        }

        // Now use the understanding code
        if (SpatialUnderstanding.Instance.AllowSpatialUnderstanding &&
            (SpatialUnderstanding.Instance.ScanState == SpatialUnderstanding.ScanStates.Done))
        {
            Vector3 rayPos = Camera.main.transform.position;
            Vector3 rayVec = Camera.main.transform.forward * RayCastLength;
            IntPtr raycastResultPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResultPtr();
            SpatialUnderstandingDll.Imports.PlayspaceRaycast(
                rayPos.x, rayPos.y, rayPos.z, rayVec.x, rayVec.y, rayVec.z,
                raycastResultPtr);
            rayCastResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResult();

            // Override
            if (rayCastResult.SurfaceType != SpatialUnderstandingDll.Imports.RaycastResult.SurfaceTypes.Invalid)
            {
                result.Hit = true;
                result.Position = rayCastResult.IntersectPoint;
                result.Normal = rayCastResult.IntersectNormal;

                return result;
            }
        }

        // Base
        return base.CalculateRayIntersect();
    }
Example #5
0
    private void DoRaycast()
    {
        Vector3 rayPos           = Camera.main.transform.position;
        Vector3 rayVec           = Camera.main.transform.forward * 10f;
        IntPtr  raycastResultPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResultPtr();
        int     intersection     = SpatialUnderstandingDll.Imports.PlayspaceRaycast(
            rayPos.x, rayPos.y, rayPos.z, rayVec.x, rayVec.y, rayVec.z,
            raycastResultPtr);

        if (intersection != 0)
        {
            SpatialUnderstandingDll.Imports.RaycastResult rayCastResult = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResult();
            Debug.Log("hit detected=" + rayCastResult.SurfaceType.ToString());
            CreateBulletHole(rayCastResult.IntersectPoint, rayCastResult.IntersectNormal);
        }
        else
        {
            Debug.Log("no hit");
        }
    }
            private void SetupAnimation()
            {
                if (!SpatialUnderstandingManager.Instance.AllowSpatialUnderstanding)
                {
                    return;
                }

                // Calculate the forward distance for the animation start point
                Vector3 rayPos           = CameraCache.Main.transform.position;
                Vector3 rayVec           = CameraCache.Main.transform.forward * InitialPositionForwardMaxDistance;
                IntPtr  raycastResultPtr = SpatialUnderstandingManager.Instance.UnderstandingDLL.GetStaticRaycastResultPtr();

                SpatialUnderstandingDll.Imports.PlayspaceRaycast(
                    rayPos.x, rayPos.y, rayPos.z, rayVec.x, rayVec.y, rayVec.z,
                    raycastResultPtr);
                SpatialUnderstandingDll.Imports.RaycastResult rayCastResult = SpatialUnderstandingManager.Instance.UnderstandingDLL.GetStaticRaycastResult();
                Vector3 animOrigin = (rayCastResult.SurfaceType != SpatialUnderstandingDll.Imports.RaycastResult.SurfaceTypes.Invalid) ?
                                     rayPos + rayVec.normalized * Mathf.Max((rayCastResult.IntersectPoint - rayPos).magnitude - 0.3f, 0.0f) :
                                     rayPos + rayVec * InitialPositionForwardMaxDistance;

                // Create the animation (starting it on the ground in front of the camera
                SpatialUnderstandingDll.Imports.QueryPlayspaceAlignment(SpatialUnderstandingManager.Instance.UnderstandingDLL.GetStaticPlayspaceAlignmentPtr());
                SpatialUnderstandingDll.Imports.PlayspaceAlignment alignment = SpatialUnderstandingManager.Instance.UnderstandingDLL.GetStaticPlayspaceAlignment();
                AnimPosition.AddKey(TimeDelay + 0.0f, new Vector3(animOrigin.x, alignment.FloorYValue, animOrigin.z));
                AnimPosition.AddKey(TimeDelay + AnimationTime * 0.5f, new Vector3(animOrigin.x, alignment.FloorYValue + 1.25f, animOrigin.z));
                AnimPosition.AddKey(TimeDelay + AnimationTime * 0.6f, new Vector3(animOrigin.x, alignment.FloorYValue + 1.0f, animOrigin.z));
                AnimPosition.AddKey(TimeDelay + AnimationTime * 0.95f, Center);
                AnimPosition.AddKey(TimeDelay + AnimationTime * 1.0f, Center);

                AnimScale.AddKey(TimeDelay + 0.0f, 0.0f);
                AnimScale.AddKey(TimeDelay + AnimationTime * 0.5f, 0.5f);
                AnimScale.AddKey(TimeDelay + AnimationTime * 0.8f, 1.0f);
                AnimScale.AddKey(TimeDelay + AnimationTime * 1.0f, 1.0f);

                AnimRotation.AddKey(TimeDelay + 0.0f, -1.5f);
                AnimRotation.AddKey(TimeDelay + AnimationTime * 0.2f, -0.5f);
                AnimRotation.AddKey(TimeDelay + AnimationTime * 0.9f, 0.0f);
                AnimRotation.AddKey(TimeDelay + AnimationTime * 1.0f, 0.0f);

                IsAnimationSetup = true;
            }
 public SpatialUnderstandingDll.Imports.RaycastResult.SurfaceTypes QuerySurfaceType(Vector3 surfacePosition, Vector3 surfaceNormal)
 {
     IntPtr resultPtr = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResultPtr();
     int    success   = SpatialUnderstandingDll.Imports.PlayspaceRaycast(
         // Note that normal points *out* of surface, so this moves us in front of
         // surface. Assuming surface normal is unit length.
         surfacePosition.x + .1f * surfaceNormal.x,
         surfacePosition.y + .1f * surfaceNormal.y,
         surfacePosition.z + .1f * surfaceNormal.z,
         // Length long enough to penetrate the surface
         .2f * surfaceNormal.x,
         .2f * surfaceNormal.y,
         .2f * surfaceNormal.z,
         // Location to write result
         resultPtr);
     //if (success != 0)
     {
         SpatialUnderstandingDll.Imports.RaycastResult result = SpatialUnderstanding.Instance.UnderstandingDLL.GetStaticRaycastResult();
         Debug.Log("surfaceNormal=" + result.IntersectNormal + ", surfacePosition=" + result.IntersectPoint + ", SUCCESS=" + success);
         return(result.SurfaceType);
     }
     //return SpatialUnderstandingDll.Imports.RaycastResult.SurfaceTypes.Invalid;
 }
Example #8
0
    public bool CheckPlacement(SpatialUnderstandingDll.Imports.RaycastResult rayCastResult)
    {
        while (Time.time < nextRefresh)
        {
            //yield null;
        }
        nextRefresh = Time.time + refreshRate;

        IntPtr test = SpatialUnderstanding.Instance.UnderstandingDLL.PinObject(testRes);

        testRes.normal   = rayCastResult.IntersectNormal;
        testRes.position = rayCastResult.IntersectPoint;
        testRes.length   = curretObject.halfLength;
        testRes.width    = curretObject.halfWidth;

        Quaternion quat = Quaternion.LookRotation(CameraCache.Main.transform.forward, testRes.normal);


        switch ((int)rayCastResult.SurfaceType)
        {
        case 0:    //invalid
            break;

        case 1:    //other
            break;

        case 2:    //floor
        case 3:    //floorlike
        case 4:    //platform
            quat = Quaternion.LookRotation(Vector3.Scale(CameraCache.Main.transform.forward, (Vector3.one - testRes.normal)), testRes.normal);
            break;

        case 5:    //ceiling
            quat = Quaternion.LookRotation(Vector3.Scale(CameraCache.Main.transform.forward, (-Vector3.one - testRes.normal)), testRes.normal);
            break;

        case 6:    //wallexternal
        case 7:    //walllike
            quat = Quaternion.LookRotation(Vector3.Scale(CameraCache.Main.transform.forward, (Vector3.one - testRes.normal)), testRes.normal);
            break;

        default:
            break;
        }

        //if (rayCastResult.SurfaceType != SpatialUnderstandingDll.Imports.RaycastResult.SurfaceTypes.Floor)
        {
            Debug.Log(Vector3.Scale(CameraCache.Main.transform.forward, (Vector3.one - testRes.normal)));
            //return;
        }

        Vector3 topLeft     = testRes.position + quat * new Vector3(-curretObject.halfLength, 0f, -curretObject.halfWidth);
        Vector3 topRight    = testRes.position + quat * new Vector3(-curretObject.halfLength, 0f, curretObject.halfWidth);
        Vector3 bottomLeft  = testRes.position + quat * new Vector3(curretObject.halfLength, 0f, -curretObject.halfWidth);
        Vector3 bottomRight = testRes.position + quat * new Vector3(curretObject.halfLength, 0f, curretObject.halfWidth);

        //Debug.Log("ray:" + testRes.position + "\nnorm" + testRes.normal + "\nTL:" + topLeft + "\nTR:" + topRight + "\nBR:" + bottomRight + "\nBL:" + bottomLeft);

        bool isValidRect = SpatialUnderstandingDllTopology.QueryTopology_IsValidRect(topLeft, topRight, bottomLeft, bottomRight, requestType, (int)rayCastResult.SurfaceType, test);

        if (isValidRect)
        {
            objectToPlace.transform.SetPositionAndRotation(testRes.position, quat);
            objectToPlace.SetActive(true);
            isValidLocation = true;
        }
        else
        {
            objectToPlace.SetActive(false);
            isValidLocation = false;
        }

        return(false);
    }