Beispiel #1
0
    void OnSceneGUI(SceneView sceneView)
    {
        RaycastHit hit;
        Vector3 mousePosition = Event.current.mousePosition;

        // ScreenPointToRayで期待しているyの位置を補正する。
        mousePosition.y = sceneView.camera.pixelHeight - mousePosition.y;
        var ray = sceneView.camera.ScreenPointToRay(mousePosition);

        if (Event.current.isMouse && Event.current.type == EventType.mouseDown)
        {
            if (!init)
            {
                setup();
                init = true;
            }

            // 現状meshが一個しかないので判定を省きつつ、meshに対してrayを打つ。
            if (RXLookingGlass.IntersectRayMesh(ray, mesh, transform.localToWorldMatrix, out hit))
            {
                // hit.pointはこの場合meshでのローカル座標になる。
                Debug.LogFormat("hit at {0}(local position)", hit.point);
                // あとは頂点を塗るだけ
                paint(hit.point);
            }
        }
    }
Beispiel #2
0
    private void OnSceneGUI(SceneView sceneView)
    {
        // 当前屏幕坐标,左上角是(0,0)右下角(camera.pixelWidth,camera.pixelHeight)
        Vector2 mousePosition = Event.current.mousePosition;

        // Retina 屏幕需要拉伸值
        float mult = 1;

#if UNITY_5_4_OR_NEWER
        mult = EditorGUIUtility.pixelsPerPoint;
#endif

        // 转换成摄像机可接受的屏幕坐标,左下角是(0,0,0)右上角是(camera.pixelWidth,camera.pixelHeight,0)
        mousePosition.y  = sceneView.camera.pixelHeight - mousePosition.y * mult;
        mousePosition.x *= mult;

        // 近平面往里一些,才能看得到摄像机里的位置
        Vector3 fakePoint = mousePosition;
        fakePoint.z = 20;
        Vector3 point = sceneView.camera.ScreenToWorldPoint(fakePoint);

        Ray          ray = sceneView.camera.ScreenPointToRay(mousePosition);
        MeshFilter[] componentsInChildren = GameObject.FindObjectsOfType <MeshFilter>();
        float        num = float.PositiveInfinity;
        foreach (MeshFilter meshFilter in componentsInChildren)
        {
            Mesh       sharedMesh = meshFilter.sharedMesh;
            RaycastHit hit;
            if (sharedMesh &&
                RXLookingGlass.IntersectRayMesh(ray, sharedMesh, meshFilter.transform.localToWorldMatrix, out hit) &&
                hit.distance < num)
            {
                point = hit.point;
                num   = hit.distance;
            }
        }

        //Handles.SphereCap(0, point, Quaternion.identity, 2);
        SphereCapPos(point);

        // 刷新界面,才能让球一直跟随
        sceneView.Repaint();
        HandleUtility.Repaint();
    }