修剪MeshView scalafx / javafx

时间:2015-07-21 03:58:34

标签: javafx scalafx javafx-3d

我有以下测试代码,我尝试用圆圈剪辑MeshView。   我也尝试将meshView放入一个组然后剪切它,但这会导致黑色圆圈。

有没有办法剪辑MeshView,最好不要把它放到一个组中?

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.scene.image.Image
import scalafx.scene.paint.{Color, PhongMaterial}
import scalafx.scene.shape.{TriangleMesh, Circle, MeshView}
import scalafx.scene.{Group, PerspectiveCamera, Scene, SceneAntialiasing}

object Test4 extends JFXApp {
  stage = new PrimaryStage {
    scene = new Scene(500, 500, true, SceneAntialiasing.Balanced) {
      fill = Color.LightGray
      val clipCircle = Circle(150.0)
      val meshView = new MeshView(new RectangleMesh(500,500)) {
        // takes a while to load
        material = new PhongMaterial(Color.White, new Image("https://peach.blender.org/wp-content/uploads/bbb-splash.png"), null, null, null)
      }
    //  val meshGroup = new Group(meshView)
      meshView.setClip(clipCircle)
      root = new Group {children = meshView; translateX = 250.0; translateY = 250.0; translateZ = 560.0}
      camera = new PerspectiveCamera(false)
    }
  }
}

class RectangleMesh(Width: Float, Height: Float) extends TriangleMesh {
  points = Array(
    -Width / 2, Height / 2, 0,
    -Width / 2, -Height / 2, 0,
    Width / 2, Height / 2, 0,
    Width / 2, -Height / 2, 0
  )
  texCoords = Array(
    1, 1,
    1, 0,
    0, 1,
    0, 0
  )
  faces = Array(
    2, 2, 1, 1, 0, 0,
    2, 2, 3, 3, 1, 1
  )

1 个答案:

答案 0 :(得分:0)

实际上,在MeshView缠绕的Group上,

如果您检查setClip()的JavaDoc:

  

将剪辑与3D变换混合存在已知的限制。剪切本质上是2D图像操作。在具有3D转换子节点的Group节点上设置Clip的结果将导致其子节点按顺序呈现,而不会在这些子节点之间应用Z缓冲。

因此:

Group meshGroup = new Group(meshView);
meshGroup.setClip(clipCircle);

您将拥有2D图像,并且似乎未应用Material。但是你可以通过设置这个来检查网格:

meshView.setDrawMode(DrawMode.LINE);

所以在你的情况下,调整尺寸:

@Override
public void start(Stage primaryStage) {
    Circle clipCircle = new Circle(220.0);
    MeshView meshView = new MeshView(new RectangleMesh(400,400));
    meshView.setDrawMode(DrawMode.LINE);
    Group meshGroup = new Group(meshView);
    meshGroup.setClip(clipCircle);
    PerspectiveCamera camera = new PerspectiveCamera(false);

    StackPane root = new StackPane();
    final Circle circle = new Circle(220.0);
    circle.setFill(Color.TRANSPARENT);
    circle.setStroke(Color.RED);
    root.getChildren().addAll(meshGroup, circle);

    Scene scene = new Scene(root, 500, 500, true, SceneAntialiasing.BALANCED);
    scene.setCamera(camera);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
}

会给出这个:

clip

最后,裁剪对3D形状没有意义。为此,您可以仅使用2D形状来获得所需的结果。

如果您想要 3D剪辑,请查看CSG操作。检查此question以获取基于JavaFX的解决方案。