首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MeshView scalafx/javafx的裁剪

MeshView scalafx/javafx的裁剪
EN

Stack Overflow用户
提问于 2015-07-21 11:58:34
回答 1查看 544关注 0票数 1

我有下面的测试代码,我试着用一个圆来裁剪一个MeshView。我也试着把meshView放到一个组中,然后裁剪它,但结果是出现了一个黑色的圆圈。

有没有一种方法可以裁剪一个MeshView,最好不把它放到一个组中?

代码语言:javascript
复制
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
  )
EN

回答 1

Stack Overflow用户

发布于 2015-07-21 20:51:47

裁剪实际上在包装GroupMeshView上工作得很好。

如果您检查setClip()的JavaDoc

将剪辑与3D变换混合存在已知限制。裁剪本质上是一种2D图像操作。在具有3D变换子节点的组节点上设置剪辑的结果将导致按顺序渲染子节点,而不在这些子节点之间应用Z缓冲。

其结果是:

代码语言:javascript
复制
Group meshGroup = new Group(meshView);
meshGroup.setClip(clipCircle);

您将有一个2D图像,并且似乎没有应用Material。但是,您可以通过设置以下内容来检查是否有网格:

代码语言:javascript
复制
meshView.setDrawMode(DrawMode.LINE);

因此,在您的情况下,调整尺寸:

代码语言:javascript
复制
@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 shape来获得您想要的结果。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31530110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档