我有下面的测试代码,我试着用一个圆来裁剪一个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
)发布于 2015-07-21 20:51:47
裁剪实际上在包装Group的MeshView上工作得很好。
如果您检查setClip()的JavaDoc
将剪辑与3D变换混合存在已知限制。裁剪本质上是一种2D图像操作。在具有3D变换子节点的组节点上设置剪辑的结果将导致按顺序渲染子节点,而不在这些子节点之间应用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 shape来获得您想要的结果。
如果你想要3D裁剪,可以看看CSG操作。检查此question以获取基于JavaFX的解决方案。
https://stackoverflow.com/questions/31530110
复制相似问题