Пример #1
0
    /// <summary>
    /// Raycast from mouse Position in scene view for future mouseClick object
    /// </summary>
    public static HitSceneView SetCurrentOverObject(HitSceneView newHit, bool setNullIfNot = true)
    {
        RaycastHit _saveRaycastHit;
        Ray        worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);

        //first a test only for point
        //if (Physics.Raycast(worldRay, out saveRaycastHit, Mathf.Infinity, 1 << LayerMask.NameToLayer(gravityAttractorEditor.layerPoint), QueryTriggerInteraction.Ignore))
        if (Physics.Raycast(worldRay, out _saveRaycastHit, Mathf.Infinity))
        {
            if (_saveRaycastHit.collider.gameObject != null)
            {
                newHit.objHit   = _saveRaycastHit.collider.gameObject;
                newHit.pointHit = _saveRaycastHit.point;
                newHit.normal   = _saveRaycastHit.normal;
            }
        }
        else
        {
            if (setNullIfNot)
            {
                newHit.objHit   = null;
                newHit.pointHit = ExtVector3.GetNullVector();
            }
        }
        return(newHit);
    }
    /// <summary>
    /// take into acount unidirectinnal option, return null if not found
    /// </summary>
    /// <returns></returns>
    private Vector3 GetGoodPointUnidirectionnal(Vector3 p, Vector3 foundPosition)
    {
        //Vector3 projectedOnPlane = TriPlane.Project(EdgeAb.A, TriNorm.normalized, p);
        Vector3 dirPlayer = p - foundPosition;

        float dotPlanePlayer = ExtVector3.DotProduct(dirPlayer.normalized, TriNorm.normalized);

        if ((dotPlanePlayer < 0 && !inverseDirection) || dotPlanePlayer > 0 && inverseDirection)
        {
            return(foundPosition);
        }
        else
        {
            Debug.DrawRay(p, dirPlayer, Color.yellow, 5f);
            Debug.DrawRay(p, TriNorm.normalized, Color.black, 5f);
            return(ExtVector3.GetNullVector());
        }
    }
Пример #3
0
    /// <summary>
    /// get closest point from an array of points
    /// </summary>
    public static Vector3 GetClosestPoint(Vector3 posEntity, Vector3[] arrayPos, ref int indexFound)
    {
        float sqrDist = 0;

        indexFound = -1;

        int firstIndex = 0;

        for (int i = 0; i < arrayPos.Length; i++)
        {
            if (ExtVector3.IsNullVector(arrayPos[i]))
            {
                continue;
            }

            float dist = (posEntity - arrayPos[i]).sqrMagnitude;
            if (firstIndex == 0)
            {
                indexFound = i;
                sqrDist    = dist;
            }
            else if (dist < sqrDist)
            {
                sqrDist    = dist;
                indexFound = i;
            }
            firstIndex++;
        }

        if (indexFound == -1)
        {
            //Debug.LogWarning("nothing found");
            return(ExtVector3.GetNullVector());
        }
        return(arrayPos[indexFound]);
    }
    public Vector3 ClosestPointTo(Vector3 p)
    {
        // Find the projection of the point onto the edge

        var  uab       = EdgeAb.Project(p);
        var  uca       = EdgeCa.Project(p);
        bool isInPlane = false;

        Vector3 rightPositionIfOutsidePlane = CalculateCornerAndLine(p, uab, uca, ref isInPlane);

        if (!infinitePlane)
        {
            //ici on est dans un plan fini, si on dis de ne pas prendre
            //en compte les borders, juste tomber !
            if (noGravityBorders && !isInPlane)
            {
                //Debug.Log("ici on est PAS dans le plane, juste tomber !");
                return(ExtVector3.GetNullVector());
            }
            else if (noGravityBorders && isInPlane)
            {
                //Debug.Log("ici on est DANS le plane, ET en noGravityBorder: tester la normal ensuite !");
                if (unidirectionnal)
                {
                    return(GetGoodPointUnidirectionnal(p, TriPlane.Project(EdgeAb.A, TriNorm.normalized, p)));  //get the good point (or null) in a plane unidirectionnal
                }
                else
                {
                    //ici en gravityBorder, et on est dans le plan, et c'esst multi directionnel, alors OK dac !
                    return(TriPlane.Project(EdgeAb.A, TriNorm.normalized, p));
                }
            }
            else if (!noGravityBorders && unidirectionnal)
            {
                //here not infinite, and WITH borders AND unidirectionnal
                if (isInPlane)
                {
                    return(GetGoodPointUnidirectionnal(p, TriPlane.Project(EdgeAb.A, TriNorm.normalized, p)));  //get the good point (or null) in a plane unidirectionnal
                }
                else
                {
                    return(GetGoodPointUnidirectionnal(p, rightPositionIfOutsidePlane));
                }
            }
            else
            {
                //here Not infinite, WITH borders, NO unidirectionnal
                if (isInPlane)
                {
                    return(TriPlane.Project(EdgeAb.A, TriNorm.normalized, p));
                }
                else
                {
                    return(rightPositionIfOutsidePlane);
                }
            }
        }
        else
        {
            if (unidirectionnal)
            {
                return(GetGoodPointUnidirectionnal(p, TriPlane.Project(EdgeAb.A, TriNorm.normalized, p)));  //get the good point (or null) in a plane unidirectionnal
            }
        }



        //ici le plan est infini, OU fini mais on est dedant


        // The closest point is in the triangle so
        // project to the plane to find it
        //Vector3 projectedPoint = TriPlane.Project(EdgeAb.A, TriNorm.normalized, p);
        return(TriPlane.Project(EdgeAb.A, TriNorm.normalized, p));
    }
Пример #5
0
    /// <summary>
    /// return the middle of X points (POINTS, NOT vector)
    /// </summary>
    public static Vector3 GetMiddleOfXPoint(Vector3[] arrayVect, bool barycenter = true)
    {
        if (arrayVect.Length == 0)
        {
            return(ExtVector3.GetNullVector());
        }

        if (!barycenter)
        {
            Vector3 sum = Vector3.zero;
            for (int i = 0; i < arrayVect.Length; i++)
            {
                sum += arrayVect[i];
            }
            return(sum / arrayVect.Length);
        }
        else
        {
            if (arrayVect.Length == 1)
            {
                return(arrayVect[0]);
            }

            float xMin = arrayVect[0].x;
            float yMin = arrayVect[0].y;
            float zMin = arrayVect[0].z;
            float xMax = arrayVect[0].x;
            float yMax = arrayVect[0].y;
            float zMax = arrayVect[0].z;

            for (int i = 1; i < arrayVect.Length; i++)
            {
                if (arrayVect[i].x < xMin)
                {
                    xMin = arrayVect[i].x;
                }
                if (arrayVect[i].x > xMax)
                {
                    xMax = arrayVect[i].x;
                }

                if (arrayVect[i].y < yMin)
                {
                    yMin = arrayVect[i].y;
                }
                if (arrayVect[i].y > yMax)
                {
                    yMax = arrayVect[i].y;
                }

                if (arrayVect[i].z < zMin)
                {
                    zMin = arrayVect[i].z;
                }
                if (arrayVect[i].z > zMax)
                {
                    zMax = arrayVect[i].z;
                }
            }
            Vector3 lastMiddle = new Vector3((xMin + xMax) / 2, (yMin + yMax) / 2, (zMin + zMax) / 2);
            return(lastMiddle);
        }
    }