下面列出了javafx.scene.AmbientLight#javafx.scene.shape.Sphere 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void buildSphereGroup() {
final PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(Color.WHITE);//TRANSPARENT);//BROWN);
material.diffuseMapProperty().bind(Bindings.when(diffuseMap).then(dImage).otherwise((Image) null));
material.bumpMapProperty().bind(Bindings.when(bumpMap).then(nImage).otherwise((Image) null));
material.setSpecularColor(Color.LIGHTGRAY);
//material.selfIlluminationMapProperty().bind(Bindings.when(selfIlluminationMap).then(siImage).otherwise((Image) null));
Xform marsXform = new Xform();
Sphere mars = new Sphere(300.0);
mars.setMaterial(material);
marsXform.getChildren().add(mars);
sphereGroup.getChildren().add(marsXform);
world.getChildren().addAll(sphereGroup);//, ambientXform);
}
/**
* @param data the xAxisData to set
*/
public void setxAxisData(ArrayList<Double> data) {
xAxisData = data;
scatterDataGroup.getChildren().clear();
for(int i=0;i<xAxisData.size();i++) {
final Sphere dataSphere = new Sphere(scatterRadius);
// final Box dataSphere = new Box(getScatterRadius(), getScatterRadius(), getScatterRadius());
double translateY = 0.0;
double translateZ = 0.0;
if(!yAxisData.isEmpty() && yAxisData.size() > i)
translateY = yAxisData.get(i);
if(!zAxisData.isEmpty() && zAxisData.size() > i)
translateZ = zAxisData.get(i);
dataSphere.setTranslateX(xAxisData.get(i));
dataSphere.setTranslateY(translateY);
dataSphere.setTranslateZ(translateZ);
scatterDataGroup.getChildren().add(dataSphere);
}
}
/**
* @param data the yAxisData to set
*/
public void setyAxisData(ArrayList<Double> data) {
yAxisData = data;
scatterDataGroup.getChildren().clear();
for(int i=0;i<yAxisData.size();i++) {
final Sphere dataSphere = new Sphere(scatterRadius);
// final Box dataSphere = new Box(getScatterRadius(), getScatterRadius(), getScatterRadius());
double translateX = 0.0;
double translateZ = 0.0;
if(!xAxisData.isEmpty() && xAxisData.size() > i)
translateX = xAxisData.get(i);
if(!zAxisData.isEmpty() && zAxisData.size() > i)
translateZ = zAxisData.get(i);
dataSphere.setTranslateX(translateX);
dataSphere.setTranslateY(yAxisData.get(i));
dataSphere.setTranslateZ(translateZ);
scatterDataGroup.getChildren().add(dataSphere);
}
}
/**
* @param data the zAxisData to set
*/
public void setzAxisData(ArrayList<Double> data) {
zAxisData = data;
scatterDataGroup.getChildren().clear();
for(int i=0;i<zAxisData.size();i++) {
final Sphere dataSphere = new Sphere(scatterRadius);
// final Box dataSphere = new Box(getScatterRadius(), getScatterRadius(), getScatterRadius());
double translateX = 0.0;
double translateY = 0.0;
if(!xAxisData.isEmpty() && xAxisData.size() > i)
translateX = xAxisData.get(i);
if(!yAxisData.isEmpty() && yAxisData.size() > i)
translateY = yAxisData.get(i);
dataSphere.setTranslateX(translateX);
dataSphere.setTranslateY(translateY);
dataSphere.setTranslateZ(zAxisData.get(i));
scatterDataGroup.getChildren().add(dataSphere);
}
}
@Test
public void buildStructure_goodInputWithComment_returnNewStructure() throws Exception
{
String inputWithComment = "sphere(10, 20, 30, 10.0, 150, 160, 170, 1.0, \"This is round.\")";
Xform struct = Viewer3d.buildStructure(new ByteArrayInputStream(inputWithComment.getBytes()));
Sphere sphere = (Sphere) struct.getChildren().get(0);
/* Check that the transforms are correct. */
assertEquals(10, sphere.getTranslateX(), 0);
assertEquals(20, sphere.getTranslateY(), 0);
assertEquals(30, sphere.getTranslateZ(), 0);
/* Check that the size is correct. */
assertEquals(10.0, sphere.getRadius(), 0);
/* Check that the color is correct. */
PhongMaterial material = (PhongMaterial) sphere.getMaterial();
Color color = material.getDiffuseColor();
assertEquals(150/255.0, color.getRed(), 0.001);
assertEquals(160/255.0, color.getGreen(), 0.001);
assertEquals(170/255.0, color.getBlue(), 0.001);
assertEquals(1.0, color.getOpacity(), 0.0);
}
@Test
public void buildStructure_goodInput_returnNewStructure() throws Exception
{
String input = "sphere(25, 20, 35, 108.0, 155, 24, 43, 0.565)";
Xform struct = Viewer3d.buildStructure(new ByteArrayInputStream(input.getBytes()));
Sphere sphere = (Sphere) struct.getChildren().get(0);
/* Check that the transforms are correct. */
assertEquals(25, sphere.getTranslateX(), 0);
assertEquals(20, sphere.getTranslateY(), 0);
assertEquals(35, sphere.getTranslateZ(), 0);
/* Check that the size is correct. */
assertEquals(108.0, sphere.getRadius(), 0);
/* Check that the color is correct. */
PhongMaterial material = (PhongMaterial) sphere.getMaterial();
Color color = material.getDiffuseColor();
assertEquals(155/255.0, color.getRed(), 0.001);
assertEquals(24/255.0, color.getGreen(), 0.001);
assertEquals(43/255.0, color.getBlue(), 0.001);
assertEquals(0.565, color.getOpacity(), 0.001);
}
private void buildAxes() {
double length = 2d * dimModel;
double width = dimModel / 100d;
double radius = 2d * dimModel / 100d;
final PhongMaterial redMaterial = new PhongMaterial();
redMaterial.setDiffuseColor(Color.DARKRED);
redMaterial.setSpecularColor(Color.RED);
final PhongMaterial greenMaterial = new PhongMaterial();
greenMaterial.setDiffuseColor(Color.DARKGREEN);
greenMaterial.setSpecularColor(Color.GREEN);
final PhongMaterial blueMaterial = new PhongMaterial();
blueMaterial.setDiffuseColor(Color.DARKBLUE);
blueMaterial.setSpecularColor(Color.BLUE);
Sphere xSphere = new Sphere(radius);
Sphere ySphere = new Sphere(radius);
Sphere zSphere = new Sphere(radius);
xSphere.setMaterial(redMaterial);
ySphere.setMaterial(greenMaterial);
zSphere.setMaterial(blueMaterial);
xSphere.setTranslateX(dimModel);
ySphere.setTranslateY(dimModel);
zSphere.setTranslateZ(dimModel);
Box xAxis = new Box(length, width, width);
Box yAxis = new Box(width, length, width);
Box zAxis = new Box(width, width, length);
xAxis.setMaterial(redMaterial);
yAxis.setMaterial(greenMaterial);
zAxis.setMaterial(blueMaterial);
autoScalingGroup.getChildren().addAll(xAxis, yAxis, zAxis);
autoScalingGroup.getChildren().addAll(xSphere, ySphere, zSphere);
}
private Group buildScene() {
Sphere mars = new Sphere(MARS_RADIUS);
mars.setTranslateX(VIEWPORT_SIZE / 2d);
mars.setTranslateY(VIEWPORT_SIZE / 2d);
PhongMaterial material = new PhongMaterial();
material.setDiffuseMap(
new Image(this.getClass().getResource(DIFFUSE_MAP).toExternalForm(),
MAP_WIDTH,
MAP_HEIGHT,
true,
true
)
);
material.setBumpMap(
new Image(this.getClass().getResource(NORMAL_MAP).toExternalForm(),
MAP_WIDTH,
MAP_HEIGHT,
true,
true
)
);
/*
material.setSpecularMap(
new Image(this.getClass().getResource(SPECULAR_MAP).toExternalForm(),
MAP_WIDTH,
MAP_HEIGHT,
true,
true
)
);
*/
mars.setMaterial(
material
);
return new Group(mars);
}
private Group buildScene() {
Sphere earth = new Sphere(EARTH_RADIUS);
earth.setTranslateX(VIEWPORT_SIZE / 2d);
earth.setTranslateY(VIEWPORT_SIZE / 2d);
PhongMaterial earthMaterial = new PhongMaterial();
earthMaterial.setDiffuseMap(
new Image(
DIFFUSE_MAP,
MAP_WIDTH,
MAP_HEIGHT,
true,
true
)
);
earthMaterial.setBumpMap(
new Image(
NORMAL_MAP,
MAP_WIDTH,
MAP_HEIGHT,
true,
true
)
);
earthMaterial.setSpecularMap(
new Image(
SPECULAR_MAP,
MAP_WIDTH,
MAP_HEIGHT,
true,
true
)
);
earth.setMaterial(
earthMaterial
);
return new Group(earth);
}
private Shape3D createDefaultNode(double radius) {
switch(defaultNodeType) {
case SPHERE: return new Sphere(radius);
case CUBE: return new Box(radius, radius, radius);
default: return new Box(radius, radius, radius);
}
}
@Override
public void start(Stage stage) {
Box floor = new Box(1000, 10, 1000);
floor.setTranslateY(150);
root.getChildren().add(floor);
Sphere s = new Sphere(150);
s.setTranslateX(5);
s.setPickOnBounds(true);
root.getChildren().add(s);
showStage(stage);
}
public static void initFx(JFXPanel fxPanel) {
sphere = new Sphere(100);
sphere.setLayoutX(200);
sphere.setLayoutY(200);
Group root = new Group(sphere);
Scene scene = new Scene(root, 900, 600);
fxPanel.setScene(scene);
fxPanel.setSize(900, 600);
}
private void buildAxes() {
double length = 2d*dimModel;
double width = dimModel/100d;
double radius = 2d*dimModel/100d;
final PhongMaterial redMaterial = new PhongMaterial();
redMaterial.setDiffuseColor(Color.DARKRED);
redMaterial.setSpecularColor(Color.RED);
final PhongMaterial greenMaterial = new PhongMaterial();
greenMaterial.setDiffuseColor(Color.DARKGREEN);
greenMaterial.setSpecularColor(Color.GREEN);
final PhongMaterial blueMaterial = new PhongMaterial();
blueMaterial.setDiffuseColor(Color.DARKBLUE);
blueMaterial.setSpecularColor(Color.BLUE);
Sphere xSphere = new Sphere(radius);
Sphere ySphere = new Sphere(radius);
Sphere zSphere = new Sphere(radius);
xSphere.setMaterial(redMaterial);
ySphere.setMaterial(greenMaterial);
zSphere.setMaterial(blueMaterial);
xSphere.setTranslateX(dimModel);
ySphere.setTranslateY(dimModel);
zSphere.setTranslateZ(dimModel);
Box xAxis = new Box(length, width, width);
Box yAxis = new Box(width, length, width);
Box zAxis = new Box(width, width, length);
xAxis.setMaterial(redMaterial);
yAxis.setMaterial(greenMaterial);
zAxis.setMaterial(blueMaterial);
autoScalingGroup.getChildren().addAll(xAxis, yAxis, zAxis);
autoScalingGroup.getChildren().addAll(xSphere, ySphere, zSphere);
}
public Parent createContent() throws Exception {
Image dImage = new Image(Simple3DSphereApp.class.getResource("/maps/earth-d.jpg").toExternalForm());
Image nImage = new Image(Simple3DSphereApp.class.getResource("/maps/earth-n.jpg").toExternalForm());
Image sImage = new Image(Simple3DSphereApp.class.getResource("/maps/earth-s.jpg").toExternalForm());
Image siImage = new Image(Simple3DSphereApp.class.getResource("/maps/earth-l.jpg").toExternalForm());
material = new PhongMaterial();
material.setDiffuseColor(Color.WHITE);
material.diffuseMapProperty().bind(
Bindings.when(diffuseMap).then(dImage).otherwise((Image) null));
material.setSpecularColor(Color.TRANSPARENT);
material.specularMapProperty().bind(
Bindings.when(specularMap).then(sImage).otherwise((Image) null));
material.bumpMapProperty().bind(
Bindings.when(bumpMap).then(nImage).otherwise((Image) null));
material.selfIlluminationMapProperty().bind(
Bindings.when(selfIlluminationMap).then(siImage).otherwise((Image) null));
earth = new Sphere(5);
earth.setMaterial(material);
earth.setRotationAxis(Rotate.Y_AXIS);
// Create and position camera
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.getTransforms().addAll(
new Rotate(-20, Rotate.Y_AXIS),
new Rotate(-20, Rotate.X_AXIS),
new Translate(0, 0, -20));
sun = new PointLight(Color.rgb(255, 243, 234));
sun.translateXProperty().bind(sunDistance.multiply(-0.82));
sun.translateYProperty().bind(sunDistance.multiply(-0.41));
sun.translateZProperty().bind(sunDistance.multiply(-0.41));
sun.lightOnProperty().bind(sunLight);
AmbientLight ambient = new AmbientLight(Color.rgb(1, 1, 1));
// Build the Scene Graph
Group root = new Group();
root.getChildren().add(camera);
root.getChildren().add(earth);
root.getChildren().add(sun);
root.getChildren().add(ambient);
RotateTransition rt = new RotateTransition(Duration.seconds(24), earth);
rt.setByAngle(360);
rt.setInterpolator(Interpolator.LINEAR);
rt.setCycleCount(Animation.INDEFINITE);
rt.play();
// Use a SubScene
SubScene subScene = new SubScene(root, 400, 300, true, SceneAntialiasing.BALANCED);
subScene.setFill(Color.TRANSPARENT);
subScene.setCamera(camera);
return new Group(subScene);
}