jFx3DTest/src/main/java/com/ThreeDtest/application/Texture3DPreview.java

132 lines
3.3 KiB
Java

package main.java.com.ThreeDtest.application;
import javafx.animation.Interpolator;
import javafx.animation.RotateTransition;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.PointLight;
import javafx.scene.image.Image;
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.transform.Rotate;
import javafx.util.Duration;
public class Texture3DPreview
{
private StackPane previewPane;
public Texture3DPreview(int height, int width)
{
previewPane = new StackPane(); // Stackpane in
// textur3dPreview
previewPane.setMaxWidth(width);
previewPane.setMaxHeight(height);
previewPane
.setStyle("-fx-background-color: #383838;-fx-border-color: red;");
int boxSize = calcBoxSize(height, width);
Box myBox = new Box(boxSize, boxSize, boxSize);
myBox.setCullFace(CullFace.NONE);
final String DIFFUSE_MAP = "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/D%C3%BClmen%2C_Kreuzkapelle_--_2014_--_2731.jpg/640px-D%C3%BClmen%2C_Kreuzkapelle_--_2014_--_2731.jpg";
Image texture = new Image(DIFFUSE_MAP);
PhongMaterial textureMaterial = new PhongMaterial();
textureMaterial.setDiffuseMap(texture);
myBox.setMaterial(textureMaterial);
PointLight pointLightFront = new PointLight(Color.WHITE);
pointLightFront.setTranslateX(100);
pointLightFront.setTranslateY(100);
pointLightFront.setTranslateZ(-300);
pointLightFront.setRotate(90);
PointLight pointLightSky = new PointLight(Color.WHITE);
pointLightSky.setTranslateX(0);
pointLightSky.setTranslateY(-600);
pointLightSky.setTranslateZ(0);
pointLightSky.setRotate(90);
PointLight pointLightGround = new PointLight(Color.WHITE);
pointLightSky.setTranslateX(0);
pointLightSky.setTranslateY(600);
pointLightSky.setTranslateZ(0);
pointLightSky.setRotate(90);
previewPane.getChildren().add(pointLightFront);
previewPane.getChildren().add(pointLightSky);
previewPane.getChildren().add(pointLightGround);
StackPane.setAlignment(pointLightFront, Pos.CENTER);
StackPane.setAlignment(pointLightSky, Pos.CENTER);
StackPane.setAlignment(pointLightGround, Pos.CENTER);
Rotate rxBox = new Rotate(0, 0, 0, 0, Rotate.X_AXIS);
Rotate ryBox = new Rotate(0, 0, 0, 0, Rotate.Y_AXIS);
Rotate rzBox = new Rotate(0, 0, 0, 0, Rotate.Z_AXIS);
rxBox.setAngle(30);
ryBox.setAngle(50);
rzBox.setAngle(30);
myBox.getTransforms().addAll(rxBox, ryBox, rzBox);
StackPane.setAlignment(myBox, Pos.CENTER);
myBox.setTranslateZ(-150);
rotateAroundYAxis(myBox).play();
previewPane.getChildren().add(myBox);
}
public StackPane getPreviewPane()
{
return previewPane;
}
private RotateTransition rotateAroundYAxis(Node node)
{
RotateTransition rotate = new RotateTransition(Duration.seconds(36),
node);
rotate.setAxis(Rotate.Y_AXIS);
rotate.setFromAngle(360);
rotate.setToAngle(0);
rotate.setInterpolator(Interpolator.LINEAR);
rotate.setCycleCount(RotateTransition.INDEFINITE);
return rotate;
}
private int calcBoxSize(int height, int width)
{
if (height <= width) {
// height is smallest dimension
return (int) (height / 2);
} else {
// width is smallest dimension
return (int) (width / 2);
}
}
}