JavaFX 3D深度渲染不正确

时间:2016-05-21 12:41:19

标签: java javafx-8

设定: 下面的代码从Z方向偏移-156个单位的摄像机渲染具有位于原点的可见坐标轴的3D场景。此外,相机的Z位置是鼠标滚动的功能,因此向上/向下滚动会使相机远离原点移动。

问题: 在程序初始启动时,红色和绿色轴在原点处/附近渲染,当在物理世界中时,从当前摄像机视图中看不到它们。 (蓝轴阻挡它们)。此外,当您向后和向前滚动时,您可以看到毛刺/闪烁,其中红色/绿色轴在蓝色轴后面可见,这不应该出现。

结果截图(我手动添加问题说明): initial_screenshot

问题: 1)这是我的设置问题吗?还是JavaFX? 2)如果这是我的设置问题,那么有人可以解释一下我可以做些什么来解决这个问题吗?

代码:

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package testproblemjavafx01;

/**
 *
 * @author ad
 */

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.EventHandler;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.input.ScrollEvent;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.stage.Stage;

public class TestProblemJavaFX01 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Group root = new Group();
        buildAxes(root);
        Scene scene = new Scene(root, 600, 400, true, SceneAntialiasing.BALANCED);
        PerspectiveCamera camera = new PerspectiveCamera(true);       

        scene.setFill(Color.WHITE);
        camera.setNearClip(0);
        camera.setFarClip(1000.0);

        camera.setTranslateX(0);
        camera.setTranslateY(0);
        camera.setTranslateZ(-156); 

        scene.setCamera(camera);
        setMouseEvents(scene);

        primaryStage.setResizable(false);
        primaryStage.setTitle("TestProblemJavaFX01");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

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

    private void buildAxes(Group root) {
        final PhongMaterial redMaterial = new PhongMaterial();
        redMaterial.setDiffuseColor(Color.DARKRED);
        redMaterial.setSpecularColor(Color.RED);

        final PhongMaterial greenMaterial = new PhongMaterial();
        greenMaterial.setDiffuseColor(Color.DARKGREEN);
        greenMaterial.setSpecularColor(Color.GREEN);

        final PhongMaterial blueMaterial = new PhongMaterial();
        blueMaterial.setDiffuseColor(Color.DARKBLUE);
        blueMaterial.setSpecularColor(Color.BLUE);

        final Box xAxis = new Box(240.0, 1, 1);
        final Box yAxis = new Box(1, 240.0, 1);
        final Box zAxis = new Box(1, 1, 240.0);

        xAxis.setMaterial(redMaterial);
        yAxis.setMaterial(greenMaterial);
        zAxis.setMaterial(blueMaterial);

        root.getChildren().addAll(xAxis, yAxis, zAxis);
    }

    private void setMouseEvents(final Scene scene) {
    scene.setOnScroll(
            new EventHandler<ScrollEvent>() {
              @Override
              public void handle(ScrollEvent event) {
                double deltaY = event.getDeltaY();

                Camera camera = scene.getCamera();
                camera.setTranslateZ(camera.getTranslateZ() + deltaY);
                event.consume();
              }
            });
  }
}

1 个答案:

答案 0 :(得分:2)

我认为问题在camera.setNearClip(0);

行内

来自setNearClip的文档:

  

指定距离近视剪裁平面的眼睛的距离   相机在眼睛坐标空间。物体比眼睛更近   nearClip未绘制。 nearClip指定为大于的值   零即可。小于或等于零的值被视为非常小   正数。

尝试将值设置为其默认值0.1。或者只是删除该行。

相关问题