Skip to content

Instantly share code, notes, and snippets.

@Birdasaur
Created June 1, 2022 02:40
Show Gist options
  • Select an option

  • Save Birdasaur/bf8ad77d052f42aaf943933fdaf1496e to your computer and use it in GitHub Desktop.

Select an option

Save Birdasaur/bf8ad77d052f42aaf943933fdaf1496e to your computer and use it in GitHub Desktop.
Quick JavaFX 18 test of adding multiple 3D SpotLight objects to the scene.
package edu.jhuapl.trinity.javafx;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.SpotLight;
import javafx.scene.SubScene;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.DrawMode;
import javafx.stage.Stage;
import org.fxyz3d.geometry.MathUtils;
import org.fxyz3d.utils.CameraTransformer;
public class SpotLightTest extends Application {
protected Box box;
PerspectiveCamera camera = new PerspectiveCamera(true);
public Group sceneRoot = new Group();
public SubScene subScene;
public CameraTransformer cameraTransform = new CameraTransformer();
private double cameraDistance = -4000;
private final double sceneWidth = 10000;
private final double sceneHeight = 4000;
private double mousePosX;
private double mousePosY;
private double mouseOldX;
private double mouseOldY;
private double mouseDeltaX;
private double mouseDeltaY;
@Override
public void start(Stage primaryStage) throws Exception {
subScene = new SubScene(sceneRoot, sceneWidth, sceneHeight, true, SceneAntialiasing.BALANCED);
//Start Tracking mouse movements only when a button is pressed
subScene.setOnMouseDragged((MouseEvent me) -> mouseDragCamera(me));
subScene.setOnScroll((ScrollEvent event) -> {
double modifier = 50.0;
double modifierFactor = 0.1;
if (event.isControlDown()) {
modifier = 1;
}
if (event.isShiftDown()) {
modifier = 100.0;
}
double z = camera.getTranslateZ();
double newZ = z + event.getDeltaY() * modifierFactor * modifier;
camera.setTranslateZ(newZ);
});
StackPane stackPane = new StackPane(subScene);
subScene.widthProperty().bind(stackPane.widthProperty());
subScene.heightProperty().bind(stackPane.heightProperty());
subScene.setFill(Color.LIGHTGREY);
camera = new PerspectiveCamera(true);
//setup camera transform for rotational support
cameraTransform.setTranslate(0, 0, 0);
cameraTransform.getChildren().add(camera);
camera.setNearClip(0.1);
camera.setFarClip(100000.0);
camera.setTranslateZ(cameraDistance);
cameraTransform.ry.setAngle(-45.0);
cameraTransform.rx.setAngle(-10.0);
subScene.setCamera(camera);
//create multiple spot lights
List<SpotLight> lights = new ArrayList<>();
lights.add(new SpotLight(Color.WHITE));
lights.add(new SpotLight(Color.GREEN));
lights.add(new SpotLight(Color.RED));
for(int i=0; i< lights.size(); i++) {
SpotLight spotLight = lights.get(i);
spotLight.setDirection(new Point3D(0, 1, 0));
spotLight.setInnerAngle(120);
spotLight.setOuterAngle(30);
spotLight.setFalloff(-0.4);
spotLight.setTranslateZ(2000 * i);
spotLight.setTranslateY(-200);
}
box = new Box(sceneWidth, 50, sceneWidth);
box.setDrawMode(DrawMode.FILL);
box.setCullFace(CullFace.BACK);
box.setMaterial(new PhongMaterial(Color.CYAN));
sceneRoot.getChildren().addAll(cameraTransform, box);
sceneRoot.getChildren().addAll(lights);
BorderPane bpOilSpill = new BorderPane(subScene);
stackPane.getChildren().clear();
stackPane.getChildren().addAll(bpOilSpill);
stackPane.setPadding(new Insets(10));
stackPane.setBackground(new Background(new BackgroundFill(Color.rgb(255, 255, 255), CornerRadii.EMPTY, Insets.EMPTY)));
Scene scene = new Scene(stackPane, 1000, 1000);
scene.setOnMouseEntered(event -> subScene.requestFocus());
primaryStage.setTitle("Multiple SpotLight Test");
primaryStage.setScene(scene);
primaryStage.show();
}
private void mouseDragCamera(MouseEvent me) {
mouseOldX = mousePosX;
mouseOldY = mousePosY;
mousePosX = me.getSceneX();
mousePosY = me.getSceneY();
mouseDeltaX = (mousePosX - mouseOldX);
mouseDeltaY = (mousePosY - mouseOldY);
double modifier = 10.0;
double modifierFactor = 0.1;
if (me.isControlDown()) {
modifier = 1;
}
if (me.isShiftDown()) {
modifier = 50.0;
}
if (me.isPrimaryButtonDown()) {
if(me.isAltDown()) { //roll
cameraTransform.rz.setAngle(((cameraTransform.rz.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
} else {
cameraTransform.ry.setAngle(((cameraTransform.ry.getAngle() + mouseDeltaX * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180); // +
cameraTransform.rx.setAngle(
MathUtils.clamp(-60,
(((cameraTransform.rx.getAngle() - mouseDeltaY * modifierFactor * modifier * 2.0) % 360 + 540) % 360 - 180),
60)); // -
}
} else if (me.isMiddleButtonDown() ) {
cameraTransform.t.setX(cameraTransform.t.getX() + mouseDeltaX * modifierFactor * modifier * 0.3); // -
cameraTransform.t.setY(cameraTransform.t.getY() + mouseDeltaY * modifierFactor * modifier * 0.3); // -
}
}
public static void main(String[] args){launch(args);}
}
@Birdasaur
Copy link
Copy Markdown
Author

oooo different colors could put spin and velocity effects on the 3D shapes. Oh this could be really cool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment