ドキュメント



JavaFX: JavaFXグラフィックスの操作

7 ピッキング

この章では、JavaFX 3Dグラフィックス機能に含まれるPickResult APIについて説明します。

PickResult APIは、すでに2Dプリミティブで透視投影カメラによって使用できました。ただし、デプス・バッファとともに使用された場合の既存の制限により、PickResultクラスはjavafx.scene.inputパッケージに追加されています。これは、選択イベントの結果を格納するコンテナ・オブジェクトです。

ユーザーの選択に関する情報が返されるように、PickResult引数がMouseEventMouseDragEventDragEventGestureEventContextMenuEventおよびTouchPointクラスのすべてのコンストラクタに追加されています。これらのクラスに新しく追加されたgetPickResult()メソッドによって、選択に関する情報を含む新しいPickResultオブジェクトが返されます。追加されたもう1つのメソッドはgetZ()であり、MouseEventのソースの原点に相対的な、イベントの深度位置が返されます。

PickResultオブジェクトの作成

例7-1に示すように、JavaFX APIには、アプリケーションでPickResultオブジェクトの新しいインスタンスを作成するための3つのコンストラクタがあります。

例7-1 PickResultのコンストラクタ

// Creates a pick result for a 2D case where no additional information
// is needed. Converts the given scene coordinates to the target's local
// coordinate space andstores the value as the intersected point. Sets 
// intersected node to the giventarget, distance to 1.0, face to 
// FACE_UNDEFINED and texCoord to null
PickResult(EventTarget target, double sceneX, double sceneY)

// Creates a new instance of PickResult for a non-3d-shape target. Sets face
// to FACE_UNDEFINED and texCoord to null.
PickResult(Node node, Point3D point, double distance)

// Creates a new instance of PickResult
PickResult(Node node, Point3D point, double distance, int face, 
           Point2D texCoord)

PickResultオブジェクトのメソッド

コードでPickResultオブジェクトを作成すると、次のメソッドを使用して、イベントを処理するクラスから渡された情報を使用できます。

例7-2 PickResultのメソッド

// Returns the intersected node. Returns null if there was no intersection
// with any node and the scene was picked.
public final Node getIntersectedNode()

// Returns the intersected point in local coordinate of the picked Node. If
// no node was picked, it returns the intersected point with the ion 
// plane.
public final Point3D getIntersectedPoint()

// Returns the intersected distance between camera position and the 
// intersected point
public final double getIntersectedDistance()

// Returns the intersected face of the picked Node, FACE_UNDEFINED if the
//  node doesn't have user-specified faces or was picked on bounds.
public final int getIntersectedFace()
 
// Return the intersected texture coordinates of the picked 3d shape. If the
// picked target is not Shape3D or has pickOnBounds==true, it returns null. 
// Returns new Point2D presenting the intersected TexCoord
public final Point2D getIntersectedTexCoord()

PickResultの使用例

例7-3に、PickResultオブジェクトおよびメソッドの使用方法を示します。これらのコード・スニペットは、PickMesh3DSampleアプリケーションの一部であり、PickResultオブジェクト内の情報にアクセスする方法を示しています。PickMesh3DSample.zip NetBeansプロジェクト・ファイルをダウンロードし、サンプルを実行します。メッシュ上にマウスを置くと、マウス位置に関する情報がオーバーレイに表示されます。[L]キーを押して描画モードを塗りつぶしと線の間で切り替え、メッシュを構成するそれぞれの面を確認できます。

例7-3 PickResultを使用するコード・サンプル

...
EventHandler<MouseEvent> moveHandler = new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                PickResult res = event.getPickResult();
                setState(res);
                event.consume();
            }
...
...
final void setState(PickResult result) {
        if (result.getIntersectedNode() == null) {
            data.setText("Scene\n\n"
                    + point3DToString(result.getIntersectedPoint()) + "\n"
                    + point2DToString(result.getIntersectedTexCoord()) + "\n"
                    + result.getIntersectedFace() + "\n"
                    + String.format("%.1f", result.getIntersectedDistance()));
        } else {
            data.setText(result.getIntersectedNode().getId() + "\n"
                    + getCullFace(result.getIntersectedNode()) + "\n"
                    + point3DToString(result.getIntersectedPoint()) + "\n"
                    + point2DToString(result.getIntersectedTexCoord()) + "\n"
                    + result.getIntersectedFace() + "\n"
                    + String.format("%.1f", result.getIntersectedDistance()));
        }
    }
...
...
ウィンドウを閉じる

目次

JavaFX: JavaFXグラフィックスの操作

展開 | 縮小