2015-07-21 24 views
1

我有以下测试代码,我尝试用圆圈剪切MeshView。 我也尝试将meshView放入一个组中,然后剪裁,但这导致了一个黑色的圆圈。剪切MeshView scalafx/javafx

有没有办法剪切一个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 
) 

回答

0

的clippling实际上在MeshView一个Group缠工作罚款

如果检查的JavaDoc setClip()

有一个带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(); 
} 

会给这样的:

clip

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

如果你想要3D剪辑看看CSG的操作。对于基于JavaFX的解决方案,请检查question

+0

我来看看CSG的运营情况。如果我理解你的答案是正确的,那么你说这是无法完成的。 – workingdog

+0

三维裁剪将是一个CSG布尔操作,所以可以完成...我没有说它不能。在我提到的问题中,您有使用[JCSG](https://github.com/miho/JCSG)和[FXyz](https://github.com/Birdasaur/FXyz)的3D形状之间的布尔操作示例。库。 –

+0

我仍在看CSG布尔运算,我已经在使用FXyz,并且不知道CSG。 这两个图书馆都很棒。由于网格上的clip()在不破坏材质的情况下不起作用,我想出了一个解决方法来使用Shape.subtract进行剪切。 它几乎工作,但有一个问题。如何依次获取网格的外边界点。 我不明白像Cloth这样的复杂网格中的点的通用排序,因此无法制定公式来挑选我需要创建多边形的点。 https://gist.github.com/anonymous/49977e526a501905d1fb – workingdog