cleanup
This commit is contained in:
parent
63f03b229c
commit
f2720a559e
|
@ -4,6 +4,7 @@ import javafx.application.Application;
|
|||
import javafx.stage.Stage;
|
||||
import javafx.scene.Scene;
|
||||
import javafx.scene.SceneAntialiasing;
|
||||
import javafx.scene.image.Image;
|
||||
import javafx.scene.layout.BorderPane;
|
||||
|
||||
public class Main extends Application
|
||||
|
@ -19,38 +20,21 @@ public class Main extends Application
|
|||
Scene scene = new Scene(demoPane, 600, 600, true,
|
||||
SceneAntialiasing.BALANCED);
|
||||
|
||||
//Create new Preview
|
||||
Texture3DPreview preview = new Texture3DPreview(500, 500);
|
||||
|
||||
// Create new Preview
|
||||
Texture3DPreview preview = new Texture3DPreview(400, 550);
|
||||
|
||||
//final String DIFFUSE_MAP = "https://bit.ly/2FTajSP";
|
||||
|
||||
//Image texture = new Image(DIFFUSE_MAP);
|
||||
|
||||
//preview.setTexture(texture);
|
||||
|
||||
// Get the generated 3D preview as StackPane
|
||||
demoPane.setCenter(preview.getPreviewPane());
|
||||
|
||||
/*
|
||||
*
|
||||
* Slider resSlider = new Slider();
|
||||
*
|
||||
* resSlider.setMin(0); resSlider.setMax(400); resSlider.setValue(200);
|
||||
* resSlider.setBlockIncrement(10); resSlider.setShowTickLabels(true);
|
||||
* resSlider.setShowTickMarks(true); resSlider.setMajorTickUnit(50);
|
||||
* resSlider.setMinorTickCount(5); resSlider.setBlockIncrement(10);
|
||||
*
|
||||
* // Adding Listener to value property.
|
||||
* resSlider.valueProperty().addListener(new ChangeListener<Number>() {
|
||||
*
|
||||
* @Override public void changed(ObservableValue<? extends Number>
|
||||
* observable, // Number oldValue, Number newValue) {
|
||||
*
|
||||
* System.out.println("New value: " + newValue); } });
|
||||
*
|
||||
* BorderPane.setAlignment(resSlider, Pos.TOP_LEFT);
|
||||
* BorderPane.setMargin(resSlider, new Insets(12, 12, 12, 12));
|
||||
* demoPane.setBottom(resSlider);
|
||||
*
|
||||
*/
|
||||
|
||||
primaryStage.setScene(scene);
|
||||
primaryStage.show();
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package main.java.com.ThreeDtest.application;
|
||||
|
||||
import java.awt.Point;
|
||||
|
||||
import javafx.animation.Interpolator;
|
||||
import javafx.animation.RotateTransition;
|
||||
import javafx.geometry.Point3D;
|
||||
import javafx.geometry.Pos;
|
||||
import javafx.scene.Node;
|
||||
import javafx.scene.PointLight;
|
||||
|
@ -16,37 +19,97 @@ import javafx.util.Duration;
|
|||
|
||||
public class Texture3DPreview
|
||||
{
|
||||
|
||||
private StackPane previewPane;
|
||||
|
||||
private Box myBox;
|
||||
|
||||
private String backgroundColor = "#383838";
|
||||
|
||||
private int previewHeight;
|
||||
|
||||
private int previewWidth;
|
||||
|
||||
PhongMaterial textureMaterial;
|
||||
|
||||
public Texture3DPreview(int height, int width)
|
||||
{
|
||||
previewHeight = height;
|
||||
previewWidth = 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);
|
||||
createPreview();
|
||||
|
||||
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);
|
||||
|
||||
// default texture --> red
|
||||
textureMaterial = new PhongMaterial();
|
||||
textureMaterial.setDiffuseColor(Color.DARKRED);
|
||||
textureMaterial.setSpecularColor(Color.RED);
|
||||
myBox.setMaterial(textureMaterial);
|
||||
|
||||
// light(); // switch on light
|
||||
|
||||
rotateAroundYAxis(myBox).play();
|
||||
previewPane.getChildren().addAll(myBox);
|
||||
|
||||
}
|
||||
|
||||
public StackPane getPreviewPane()
|
||||
{
|
||||
return previewPane;
|
||||
}
|
||||
|
||||
public void setTexture(Image img)
|
||||
{
|
||||
PhongMaterial textureMaterial = new PhongMaterial();
|
||||
textureMaterial.setDiffuseMap(img);
|
||||
myBox.setMaterial(textureMaterial);
|
||||
}
|
||||
|
||||
public void setBackgroundColor(String hexColor)
|
||||
{
|
||||
backgroundColor = hexColor;
|
||||
previewPane.setStyle("-fx-background-color: " + backgroundColor + ";");
|
||||
}
|
||||
|
||||
private RotateTransition rotateAroundYAxis(Node node)
|
||||
{
|
||||
RotateTransition rotate = new RotateTransition(Duration.seconds(18),
|
||||
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()
|
||||
{
|
||||
if (previewHeight <= previewWidth) {
|
||||
// height is smallest dimension
|
||||
return (int) (previewHeight / 2);
|
||||
} else {
|
||||
// width is smallest dimension
|
||||
return (int) (previewWidth / 2);
|
||||
}
|
||||
}
|
||||
|
||||
public void updateSize(int height, int width)
|
||||
{
|
||||
previewHeight = height;
|
||||
previewWidth = width;
|
||||
|
||||
createPreview();
|
||||
light();
|
||||
rotateAroundYAxis(myBox).play();
|
||||
}
|
||||
|
||||
private void light()
|
||||
{
|
||||
PointLight pointLightFront = new PointLight(Color.WHITE);
|
||||
pointLightFront.setTranslateX(100);
|
||||
pointLightFront.setTranslateY(100);
|
||||
|
@ -65,66 +128,38 @@ public class Texture3DPreview
|
|||
pointLightSky.setTranslateZ(0);
|
||||
pointLightSky.setRotate(90);
|
||||
|
||||
previewPane.getChildren().add(pointLightFront);
|
||||
previewPane.getChildren().add(pointLightSky);
|
||||
previewPane.getChildren().add(pointLightGround);
|
||||
previewPane.getChildren().addAll(pointLightFront);
|
||||
previewPane.getChildren().addAll(pointLightSky);
|
||||
previewPane.getChildren().addAll(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()
|
||||
private void createPreview()
|
||||
{
|
||||
|
||||
return previewPane;
|
||||
previewPane.setMaxWidth(previewWidth);
|
||||
previewPane.setMaxHeight(previewHeight);
|
||||
previewPane.setStyle("-fx-background-color: " + backgroundColor + ";");
|
||||
|
||||
}
|
||||
int boxSize = calcBoxSize();
|
||||
myBox = new Box(boxSize, boxSize, boxSize);
|
||||
|
||||
|
||||
|
||||
Rotate rxBox = new Rotate(45, 0, 0, 0, Rotate.X_AXIS);
|
||||
Rotate ryBox = new Rotate(45, 0, 0, 0, Rotate.Y_AXIS);
|
||||
|
||||
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);
|
||||
|
||||
}
|
||||
myBox.getTransforms().addAll(rxBox, ryBox);
|
||||
//StackPane.setAlignment(myBox, Pos.CENTER);
|
||||
myBox.setTranslateZ(((-1) * boxSize) +25);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue