IntersectRayMesh() static private method

static private IntersectRayMesh ( Ray ray, Mesh mesh, Matrix4x4 matrix, RaycastHit &hit ) : bool
ray UnityEngine.Ray
mesh UnityEngine.Mesh
matrix UnityEngine.Matrix4x4
hit UnityEngine.RaycastHit
return bool
        static bool RaycastWorld(Vector2 position, out RaycastHit hit)
        {
            hit = new RaycastHit();
            GameObject picked = HandleUtility.PickGameObject(position, false);

            if (!picked)
            {
                return(false);
            }

            Ray mouseRay = HandleUtility.GUIPointToWorldRay(position);

            // Loop through all meshes and find the RaycastHit closest to the ray origin.
            MeshFilter[] meshFil = picked.GetComponentsInChildren <MeshFilter>();
            float        minT    = Mathf.Infinity;

            foreach (MeshFilter mf in meshFil)
            {
                Mesh mesh = mf.sharedMesh;
                if (!mesh || !mesh.canAccess)
                {
                    continue;
                }

                RaycastHit localHit;
                if (HandleUtility.IntersectRayMesh(mouseRay, mesh, mf.transform.localToWorldMatrix, out localHit))
                {
                    if (localHit.distance < minT)
                    {
                        hit  = localHit;
                        minT = hit.distance;
                    }
                }
            }

            if (minT == Mathf.Infinity)
            {
                // If we didn't find any surface based on meshes, try with colliders.
                Collider[] colliders = picked.GetComponentsInChildren <Collider>();
                foreach (Collider col in colliders)
                {
                    RaycastHit localHit;
                    if (col.Raycast(mouseRay, out localHit, Mathf.Infinity))
                    {
                        if (localHit.distance < minT)
                        {
                            hit  = localHit;
                            minT = hit.distance;
                        }
                    }
                }
            }

            if (minT == Mathf.Infinity)
            {
                // If we didn't hit any mesh or collider surface, then use the transform position projected onto the ray.
                hit.point = Vector3.Project(picked.transform.position - mouseRay.origin, mouseRay.direction) + mouseRay.origin;
            }
            return(true);
        }
Esempio n. 2
0
        private static bool RaycastWorld(Vector2 position, out RaycastHit hit)
        {
            hit = default(RaycastHit);
            GameObject gameObject = HandleUtility.PickGameObject(position, false);

            if (!gameObject)
            {
                return(false);
            }
            Ray ray = HandleUtility.GUIPointToWorldRay(position);

            MeshFilter[] componentsInChildren = gameObject.GetComponentsInChildren <MeshFilter>();
            float        num = float.PositiveInfinity;

            MeshFilter[] array = componentsInChildren;
            for (int i = 0; i < array.Length; i++)
            {
                MeshFilter meshFilter = array[i];
                Mesh       sharedMesh = meshFilter.sharedMesh;
                if (sharedMesh)
                {
                    RaycastHit raycastHit;
                    if (HandleUtility.IntersectRayMesh(ray, sharedMesh, meshFilter.transform.localToWorldMatrix, out raycastHit) && raycastHit.distance < num)
                    {
                        hit = raycastHit;
                        num = hit.distance;
                    }
                }
            }
            if (num == float.PositiveInfinity)
            {
                hit.point = Vector3.Project(gameObject.transform.position - ray.origin, ray.direction) + ray.origin;
            }
            return(true);
        }
Esempio n. 3
0
        private static bool RaycastWorld(Vector2 position, out RaycastHit hit)
        {
            hit = new RaycastHit();
            GameObject obj2 = HandleUtility.PickGameObject(position, false);

            if (obj2 == null)
            {
                return(false);
            }
            Ray ray = HandleUtility.GUIPointToWorldRay(position);

            MeshFilter[] componentsInChildren = obj2.GetComponentsInChildren <MeshFilter>();
            float        positiveInfinity     = float.PositiveInfinity;

            foreach (MeshFilter filter in componentsInChildren)
            {
                RaycastHit hit2;
                Mesh       sharedMesh = filter.sharedMesh;
                if ((sharedMesh != null) && (HandleUtility.IntersectRayMesh(ray, sharedMesh, filter.transform.localToWorldMatrix, out hit2) && (hit2.distance < positiveInfinity)))
                {
                    hit = hit2;
                    positiveInfinity = hit.distance;
                }
            }
            if (positiveInfinity == float.PositiveInfinity)
            {
                Vector3 introduced9 = Vector3.Project(obj2.transform.position - ray.origin, ray.direction);
                hit.point = introduced9 + ray.origin;
            }
            return(true);
        }
        private static bool RaycastWorld(Vector2 position, out RaycastHit hit)
        {
            hit = new RaycastHit();
            GameObject gameObject = HandleUtility.PickGameObject(position, false);

            if (!(bool)((Object)gameObject))
            {
                return(false);
            }
            Ray worldRay = HandleUtility.GUIPointToWorldRay(position);

            MeshFilter[] componentsInChildren = gameObject.GetComponentsInChildren <MeshFilter>();
            float        num = float.PositiveInfinity;

            foreach (MeshFilter meshFilter in componentsInChildren)
            {
                Mesh       sharedMesh = meshFilter.sharedMesh;
                RaycastHit hit1;
                if ((bool)((Object)sharedMesh) && HandleUtility.IntersectRayMesh(worldRay, sharedMesh, meshFilter.transform.localToWorldMatrix, out hit1) && (double)hit1.distance < (double)num)
                {
                    hit = hit1;
                    num = hit.distance;
                }
            }
            if ((double)num == double.PositiveInfinity)
            {
                hit.point = Vector3.Project(gameObject.transform.position - worldRay.origin, worldRay.direction) + worldRay.origin;
            }
            return(true);
        }