【问题标题】:clipping of MeshView scalafx/javafxMeshView scalafx/javafx 的剪裁
【发布时间】:2015-10-10 09:06:09
【问题描述】:

我有以下测试代码,我尝试用圆圈剪辑 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
  )

【问题讨论】:

    标签: javafx scalafx javafx-3d


    【解决方案1】:

    裁剪实际上很好MeshView 包裹在Group 上。

    如果您检查 JavaDoc 是否有 setClip()

    将 Clip 与 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();
    }
    

    会给这个:

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

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

    【讨论】:

    • 我会看看 CSG 的操作。如果我正确理解了您的回答,您的意思是无法完成。
    • 3D裁剪将是一个CSG布尔运算,所以可以做到……我没说不能。在我提到的问题中,您有一个使用 JCSGFXyz 库的 3D 形状之间的布尔运算示例。
    • 我还在研究 CSG 布尔运算,我已经在使用 FXyz 并且不了解 CSG。两个库都很棒。由于网格上的 clip() 在不破坏材质的情况下无法工作,因此我想出了一个解决方法来使用 Shape.subtract 进行剪裁。它几乎可以工作,但有一个问题。如何依次获取网格的外边界点。我不了解像 Cloth 这样的复杂网格中点的通用顺序,因此无法使用公式来选择创建多边形所需的点。 gist.github.com/anonymous/49977e526a501905d1fb
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    相关资源
    最近更新 更多