コード例 #1
0
    static void GetHit(SceneView sceneView, out GameObject hitObject, out Vector3 hitPoint, out Vector3 hitNormal)
    {
        if (SceneQueryUtility.FindClickWorldIntersection(sceneView.camera, Event.current.mousePosition, out hitObject))
        {
            var intersection = SceneQueryUtility.FindMeshIntersection(sceneView.camera, Event.current.mousePosition);
            hitPoint  = intersection.worldIntersection;
            hitNormal = intersection.worldPlane.normal;
        }
        else
        {
            hitObject = HandleUtility.PickGameObject(Event.current.mousePosition, out int materialIndex);
            hitPoint  = Vector3.zero;
            hitNormal = Vector3.zero;

            if (hitObject)
            {
                var ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                var mf  = hitObject.GetComponentInChildren <MeshFilter>();
                if (mf)
                {
                    EditorHandles_UnityInternal.IntersectRayMesh(ray, mf, out RaycastHit hit);
                    hitPoint  = hit.point;
                    hitNormal = hit.normal;
                }
            }
        }
    }
コード例 #2
0
        public bool Raycast(out Vector3 pos, out Vector3 norm, out int face)
        {
            Ray  mouseRay     = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
            bool hitSomething = false;

            norm = Vector2.zero;
            pos  = Vector3.zero;
            face = -1;

            for (int i = 0; i < activeInstances.Length; i++)
            {
                RaycastHit hit;

                if (EditorHandles_UnityInternal.IntersectRayMesh(mouseRay, activeInstances[i].localMesh, activeInstances[i].transform.localToWorldMatrix, out hit))
                {
                    if (hitSomething)
                    {
                        if (Vector3.Distance(hit.point, mouseRay.origin) > Vector3.Distance(pos, mouseRay.origin))
                        {
                            continue;
                        }
                    }

                    norm         = hit.normal.normalized;
                    pos          = hit.point;
                    face         = hit.triangleIndex;
                    hitSomething = true;
                }
            }

            return(hitSomething);
        }