许多 3d 程序在选择 3d 对象时使用轮廓来提示用户。
有没有办法在 javafx 中模仿这种行为?
最佳答案
带有已剔除正面的缩放 3d 对象的轮廓
在这个方法中。一个 3d 对象被实例化为类类型 PickResult
节点,当鼠标在 Group
节点上输入时,该对象具有与所选对象相同的大小和相同的平移(在本例中只是 x 轴)但略微缩放 (1.1)。所以,3d 对象更大,它在同一个位置,但不与下面的 shape3d 重叠,因为任何朝向相机的脸都被 Culface.FRONT 剔除了。 Shape3d.setCullFace()
中的枚举。最后,当鼠标退出 pickresult 节点时,3d 对象将从组的子节点中移除,从而允许下一次迭代。这是您可以尝试的单类功能性 javafx 应用
应用程序.java
public class App extends Application {
private Shape3D outLine;
@Override
public void start(Stage stage) {
Shape3D sphere = new Sphere(0.45);
PhongMaterial sMaterial = new PhongMaterial(Color.CORAL);
sphere.setMaterial(sMaterial);
sphere.setTranslateX(-1.1);
Shape3D box = new Box(0.9, 0.9, 0.9);
box.setTranslateX(1.1);
box.setMaterial(new PhongMaterial(Color.PALEGREEN));
Shape3D cylinder = new Cylinder(0.45, 0.8);
cylinder.setMaterial(new PhongMaterial(Color.PALEVIOLETRED));
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setTranslateZ(-5.5);
Group group3d = new Group(camera, box, sphere, cylinder);
Scene scene = new Scene(group3d, 640, 480, true, SceneAntialiasing.BALANCED);
group3d.setOnMouseExited((t) -> {
group3d.getChildren().remove(outLine);
});
group3d.setOnMouseEntered((t) -> {
PickResult pickResult = t.getPickResult();
Node intersectedNode = pickResult.getIntersectedNode();
if (intersectedNode instanceof Sphere) {
outLine = new Sphere(((Sphere) intersectedNode).getRadius());
outLine.setTranslateX(intersectedNode.getTranslateX());
group3d.getChildren().add(outLine);
outLine.setCullFace(CullFace.FRONT);
outLine.setScaleX(1.1);
outLine.setScaleY(1.1);
outLine.setScaleZ(1.1);
}
if (intersectedNode instanceof Cylinder) {
Cylinder c = (Cylinder) intersectedNode;
outLine = new Cylinder(c.getRadius(), c.getHeight(), c.getDivisions());
outLine.setTranslateX(c.getTranslateX());
group3d.getChildren().add(outLine);
outLine.setCullFace(CullFace.FRONT);
outLine.setScaleX(1.1);
outLine.setScaleY(1.1);
outLine.setScaleZ(1.1);
}
if (intersectedNode instanceof Box) {
Box b = (Box) intersectedNode;
outLine = new Box(b.getWidth(), b.getHeight(), b.getDepth());
outLine.setTranslateX(b.getTranslateX());
group3d.getChildren().add(outLine);
outLine.setCullFace(CullFace.FRONT);
outLine.setScaleX(1.1);
outLine.setScaleY(1.1);
outLine.setScaleZ(1.1);
}
});
scene.setCamera(camera);
scene.setFill(Color.AQUA);
stage.setScene(scene);
stage.setTitle("outline javafx");
stage.show();
}
public static void main(String[] args) {
launch();
}
}
注意 Shape3d 对象从其中心开始缩放。如果此方法将使用自定义 MeshView 对象实现,则这些网格也需要在其中心缩放
关于java - 使用鼠标事件勾画 Shape3d javafx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72313843/