4

要するに、回転する2つの長方形があります。(Y 軸で) 回転している間、それらは互いに重なり合う必要があります。残念ながら、180 度回転しているにもかかわらず、ラクタングルの 1 つが「常に前面」にあります。その動作を修正するにはどうすればよいですか?グループに追加された長方形の順序に関連しているようです。最後に追加されたものが常に前に表示されます。

シーン:

    package drawing.scene;

import javafx.application.Application;
import javafx.geometry.Point3D;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import drawing.objects.Clock;
import drawing.objects.Cube;

public class MyScene extends Application {

    private int sceneEdgeSize = 800;
    private int clolcSize = 400;

    @Override
    public void start(Stage primaryStage) throws Exception {
        Group g = new Group();
        g.setTranslateX((sceneEdgeSize - clolcSize) / 2f);
        g.setTranslateY((sceneEdgeSize - clolcSize) / 2f);

        final Cube c = new Cube(clolcSize);
        g.getChildren().add(c);
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                Rotate r = rotate(0, Rotate.Y_AXIS);
                c.getTransforms().add(r);
                double angle = 0.0;
                while (true) {

                    r.setAngle(angle += 2);
                    try {
                        Thread.sleep(25);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        t.setDaemon(true);
        primaryStage.setScene(new Scene(g, sceneEdgeSize, sceneEdgeSize));
        PerspectiveCamera camera = new PerspectiveCamera();
        primaryStage.getScene().setCamera(camera);
        primaryStage.show();
        t.start();

    }

    public static void main(String[] args) {
        launch(args);
    }

    public Rotate rotate(double angle, Point3D axis) {
        return new Rotate(angle, clolcSize / 2f, clolcSize / 2f, 0, axis);
    }

}

立方体クラス:

package drawing.objects;

import javafx.collections.ObservableList;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.RectangleBuilder;
import javafx.scene.transform.Rotate;

public class Cube extends Group {

    private double edgeLength = 0;

    public Cube(double edgeLength) {
        super();
        this.edgeLength = edgeLength;
        create();
    }

    private void create() {
        final Rotate rx = new Rotate(0, Rotate.X_AXIS);
        final Rotate ry = new Rotate(0, Rotate.Y_AXIS);
        final Rotate rz = new Rotate(0, Rotate.Z_AXIS);

        this.getTransforms().addAll(rx, ry, rz);

        ObservableList<Node> children = this.getChildren();
        //@formatter:off

        Rectangle rect;

        rect = RectangleBuilder // face
                .create()
                .width(edgeLength-20)
                .height(edgeLength-20)
                .translateZ(edgeLength * 0.5)
//                  .translateY(edgeLength * 0.5)
                //  .translateX(-edgeLength * 0.5)
                .fill(Color.LIGHTGREEN)
                .build()
                ;
        children.add(rect);
        rect = RectangleBuilder // face
                .create()
                .width(edgeLength-20)
                .height(edgeLength-20)
                .translateZ(-edgeLength * 0.5)
//              .translateY(-edgeLength * 0.5)
            //  .translateX(-edgeLength * 0.5)
                .fill(Color.DARKGREEN)
                .build()
                ;
        children.add(rect);


        //@formatter:on
    }
}
4

1 に答える 1

13

シーンで深度バッファリングをオンにする必要があります。

この質問のコードはほとんど時代遅れであり、部分的に間違っています:

  1. JavaFX 3D 作業には、 Java 8を使用することをお勧めします。
  2. 立方体を作成するには、ボックスシェイプを使用します。
  3. 他のジオメトリ タイプについては、SphereCylinderまたはMeshViewを使用します。
  4. ポイントライトとアンビエントライトを使用して、シーンを照らします。
  5. マテリアルを 3D オブジェクトに適用してシェーディングします。
  6. モデル インポーターを使用して、複雑なメッシュ モデルをインポートします。
  7. ビルダーは非推奨です。
  8. アニメーションを処理するには、別のスレッドを生成するのではなく、JavaFX アニメーション パッケージを使用することをお勧めします。
  9. 質問の特定のアニメーションについては、RotateTransitionが適切なアニメーションです。
  10. あなたのソリューションはスレッドセーフではありません。アクティブなシーン グラフ内のノードのプロパティ (表示されているノードの変換プロパティなど) をアプリケーション スレッドから変更しないでください (代わりにPlatform.runLaterを使用してください)。
  11. 深度バッファリングが true に設定されたシーンを作成していません。深度バッファリング フラグは、3D オブジェクトに深度ソートとカリングを適用する必要があることを JavaFX に伝えます。

また、システムが JavaFX 3D をサポートしていることも確認してください。

System.out.println(
  "3D supported? " + 
  Platform.isSupported(ConditionalFeature.SCENE3D)
);

回転立方体の Java 8 3D サンプル コード

Java 8 でコーディングされた回転キューブを次に示します。

回転立方体

import javafx.animation.*;
import javafx.application.Application;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class RotatingCube extends Application {

    private static final double SCENE_SIZE = 300;
    private static final double BOX_EDGE_LENGTH = SCENE_SIZE / 2d;

    private static final Color BOX_COLOR     = Color.DARKGREEN;
    private static final Color AMBIENT_COLOR = Color.rgb(30, 30, 30);
    private static final Color LIGHT_COLOR   = Color.WHITE;

    private static final Duration ROTATION_DURATION = Duration.seconds(4.5);

    @Override
    public void start(Stage stage) throws Exception {
        Scene scene = new Scene(
                new Group(
                        new AmbientLight(AMBIENT_COLOR),
                        createPointLight(),
                        createRotatingBox()
                ),
                SCENE_SIZE, SCENE_SIZE,
                true,
                SceneAntialiasing.BALANCED
        );
        scene.setFill(Color.MIDNIGHTBLUE.darker().darker().darker());
        scene.setCamera(new PerspectiveCamera());

        stage.setScene(scene);
        stage.show();
    }

    private PointLight createPointLight() {
        PointLight light = new PointLight(LIGHT_COLOR);
        light.setTranslateX( SCENE_SIZE / 2d);
        light.setTranslateY( SCENE_SIZE / 2d);
        light.setTranslateZ(-SCENE_SIZE);

        return light;
    }

    private Box createRotatingBox() {
        final Box box = new Box(BOX_EDGE_LENGTH, BOX_EDGE_LENGTH, BOX_EDGE_LENGTH);
        box.setTranslateX(SCENE_SIZE / 2d);
        box.setTranslateY(SCENE_SIZE / 2d);
        box.setTranslateZ(BOX_EDGE_LENGTH / 2d);
        box.setMaterial(new PhongMaterial(BOX_COLOR));

        rotateAroundYAxis(box);

        return box;
    }

    private void rotateAroundYAxis(Box box) {
        RotateTransition rotate = new RotateTransition(ROTATION_DURATION, box);
        rotate.setFromAngle(0);
        rotate.setToAngle(360);
        rotate.setAxis(Rotate.Y_AXIS);
        rotate.setCycleCount(RotateTransition.INDEFINITE);
        rotate.setInterpolator(Interpolator.LINEAR);
        rotate.play();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

2 つの回転する長方形の Java 8 3D サンプル コード

import javafx.animation.*;
import javafx.application.*;
import javafx.scene.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class RotatingRectangles extends Application {

    private static final double SCENE_SIZE = 300;
    private static final double EDGE_LENGTH = SCENE_SIZE / 2d;

    private static final Duration ROTATION_DURATION = Duration.seconds(4.5);

    @Override
    public void start(Stage stage) throws Exception {
        System.out.println(
            "3D supported? " +
            Platform.isSupported(ConditionalFeature.SCENE3D)
        );

        Scene scene = new Scene(
                createRotatingShapes(),
                SCENE_SIZE, SCENE_SIZE,
                true,
                SceneAntialiasing.BALANCED
        );
        scene.setFill(Color.MIDNIGHTBLUE.darker().darker().darker());
        scene.setCamera(new PerspectiveCamera());

        stage.setScene(scene);
        stage.show();
    }

    private Group createRotatingShapes() {
        final Rectangle rect1 = new Rectangle(
            EDGE_LENGTH, EDGE_LENGTH,
            Color.LIGHTGREEN
        );
        rect1.setTranslateX(-EDGE_LENGTH / 2d);
        rect1.setTranslateY(-EDGE_LENGTH / 2d);
        rect1.setTranslateZ( EDGE_LENGTH / 2d);

        final Rectangle rect2 = new Rectangle(
            EDGE_LENGTH, EDGE_LENGTH,
            Color.DARKGREEN
        );
        rect2.setTranslateX(-EDGE_LENGTH / 2d);
        rect2.setTranslateY(-EDGE_LENGTH / 2d);
        rect2.setTranslateZ(-EDGE_LENGTH / 2d);

        final Group shapes = new Group(
            rect1, rect2
        );

        shapes.setTranslateX(SCENE_SIZE / 2d);
        shapes.setTranslateY(SCENE_SIZE / 2d);
        shapes.setTranslateZ(EDGE_LENGTH / 2d);

        rotateAroundYAxis(shapes);

        return shapes;
    }

    private void rotateAroundYAxis(Node node) {
        RotateTransition rotate = new RotateTransition(ROTATION_DURATION, node);
        rotate.setFromAngle(0);
        rotate.setToAngle(360);
        rotate.setAxis(Rotate.Y_AXIS);
        rotate.setCycleCount(RotateTransition.INDEFINITE);
        rotate.setInterpolator(Interpolator.LINEAR);
        rotate.play();
    }

    public static void main(String[] args) {
        launch(args);
    }

}

最初の図では、暗い四角形が明るい四角形の前に回転されています。

最初の図では、明るい四角形が暗い四角形の前で回転しています。

したがって、JavaFX システムが深さでソートされたシェイプをシーンに正しく表示していることがわかります。

回転直角ダーク 回転直角ライト

于 2013-10-25T19:44:12.927 に答える