我目前正在研究 JavaFX 3D 功能,我想对太阳系进行建模。我非常成功地创建了一个 PointLight
类型的光源,并将其坐标设置为绑定(bind)到太阳坐标。现在我想看到太阳以它的光芒作为光源而发光。我该怎么做?
@Component
public class RootPane extends StackPane {
@Inject
protected Scene scene;
@Inject
protected PerspectiveCamera camera;
@Inject
protected LightBase lightBase;
@PostConstruct
public void init() {
setBackground(new Background(new BackgroundFill(
Color.TRANSPARENT, CornerRadii.EMPTY, Insets.EMPTY)));
setAlignment(Pos.CENTER);
setPadding(new Insets(20));
Sphere sunSphere = new Sphere(80);
sunSphere.setEffect(new Glow(5));
Sphere mercurySphere = new Sphere(40);
mercurySphere.translateZProperty().bind(sunSphere.translateZProperty()
.add(200));
Sphere venusSphere = new Sphere(40);
venusSphere.translateXProperty().bind(sunSphere.translateXProperty()
.subtract(300));
venusSphere.translateZProperty().bind(sunSphere.translateZProperty()
.subtract(300));
Sphere earthSphere = new Sphere(40);
earthSphere.translateXProperty().bind(sunSphere.translateXProperty()
.add(300));
earthSphere.translateZProperty().bind(sunSphere.translateZProperty()
.subtract(400));
Rotate rotateX = new Rotate(0, Rotate.X_AXIS);
Rotate rotateY = new Rotate(0, Rotate.Y_AXIS);
Rotate rotateZ = new Rotate(0, Rotate.Z_AXIS);
Translate translate = new Translate(0, 0, -3000);
camera.getTransforms().addAll(rotateX, rotateY, rotateZ, translate);
scene.addEventHandler(
KeyEvent.KEY_PRESSED,
event -> {
if (Objects.equals(event.getCode(), KeyCode.D)) {
rotateY.setAngle(rotateY.getAngle() -
(event.isShiftDown() ? 5 : 0.1));
} else if (Objects.equals(event.getCode(), KeyCode.A)) {
rotateY.setAngle(rotateY.getAngle() +
(event.isShiftDown() ? 5 : 0.1));
}
});
scene.addEventHandler(
KeyEvent.KEY_PRESSED,
event -> {
if (Objects.equals(event.getCode(), KeyCode.W)) {
rotateX.setAngle(rotateX.getAngle() -
(event.isShiftDown() ? 5 : 0.1));
} else if (Objects.equals(event.getCode(), KeyCode.S)) {
rotateX.setAngle(rotateX.getAngle() +
(event.isShiftDown() ? 5 : 0.1));
}
});
lightBase.translateXProperty().bind(sunSphere.translateXProperty());
lightBase.translateYProperty().bind(sunSphere.translateYProperty());
lightBase.translateZProperty().bind(sunSphere.translateZProperty());
getChildren().addAll(sunSphere, mercurySphere, venusSphere,
earthSphere, camera, lightBase);
}
}
最佳答案
如果没有您希望实现的效果示例,则很难提供答案,但这里有两个选项:
使用添加剂混合创建从太阳发射的粒子效果
使用高光贴图创建 Material 。
关于java - JavaFX 3D 中的可见光源,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34470199/