剪裁MeshView scalafx/javafx [英] clipping of MeshView scalafx/javafx

查看:143
本文介绍了剪裁MeshView scalafx/javafx的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下测试代码,我尝试在其中剪切带圆圈的MeshView. 我还尝试将meshView放入一个组,然后对其进行裁剪,但这会导致黑色圆圈.

I have the following test code, where I try to clip a MeshView with a circle. I also tried putting the meshView into a group then clipping that, but this result in a black circle.

是否有一种方法可以剪辑MeshView,最好不要将其分成一组?

Is there a way to clip a MeshView, preferably without putting it into a group?

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
  )

推荐答案

剪切程序实际上对包裹在Group上的MeshView起作用 fine .

The clippling actually works fine over the MeshView wrapped around a Group.

如果您在JavaDoc中检查setClip():

If you check JavaDoc for setClip():

将Clip与3D变换混合存在已知的局限性.剪裁本质上是2D图像操作.在具有3D转换后的子代的Group节点上设置Clip的结果将导致其子代按顺序渲染,而在这些子代之间不应用Z缓冲.

There is a known limitation of mixing Clip with a 3D Transform. Clipping is essentially a 2D image operation. The result of a Clip set on a Group node with 3D transformed children will cause its children to be rendered in order without Z-buffering applied between those children.

结果:

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

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

you will have a 2D image, and it seems Material is not applied. However you can check there's a mesh, by seting this:

meshView.setDrawMode(DrawMode.LINE);

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

So in your case, adjusting dimensions:

@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形状来获得所需的结果.

In the end, clipping doesn't make sense with 3D shapes. For that you can use just 2D shape to get the result you want.

如果要 3D裁剪,请查看CSG操作.检查此问题用于基于JavaFX的解决方案.

If you want 3D clipping have a look at CSG operations. Check this question for a JavaFX based solution.

这篇关于剪裁MeshView scalafx/javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆