下面列出了怎么用javafx.scene.paint.PhongMaterial的API类实例代码及写法,或者点击链接到github查看源代码。
private void buildAxes(Group world) {
PhongMaterial redMaterial = new PhongMaterial();
redMaterial.setDiffuseColor(Color.DARKRED);
redMaterial.setSpecularColor(Color.RED);
PhongMaterial greenMaterial = new PhongMaterial();
greenMaterial.setDiffuseColor(Color.DARKGREEN);
greenMaterial.setSpecularColor(Color.GREEN);
PhongMaterial blueMaterial = new PhongMaterial();
blueMaterial.setDiffuseColor(Color.DARKBLUE);
blueMaterial.setSpecularColor(Color.BLUE);
Box xAxis = new Box(AXES_SIZE, 1, 1);
xAxis.setMaterial(redMaterial);
Box yAxis = new Box(1, AXES_SIZE, 1);
yAxis.setMaterial(greenMaterial);
Box zAxis = new Box(1, 1, AXES_SIZE);
zAxis.setMaterial(blueMaterial);
Group group = new Group();
group.getChildren().addAll(xAxis, yAxis, zAxis);
group.setVisible(true);
world.getChildren().addAll(group);
group.visibleProperty().bind(checkBoxAxes.selectedProperty());
}
private static MeshView makeMeshView(final PainteraTriangleMesh verticesAndNormals)
{
final float[] vertices = verticesAndNormals.getVertices();
final float[] normals = verticesAndNormals.getNormals();
final TriangleMesh mesh = new TriangleMesh();
mesh.getPoints().addAll(vertices);
mesh.getNormals().addAll(normals);
mesh.getTexCoords().addAll(0, 0);
mesh.setVertexFormat(VertexFormat.POINT_NORMAL_TEXCOORD);
final int[] faceIndices = new int[vertices.length];
for (int i = 0, k = 0; i < faceIndices.length; i += 3, ++k)
{
faceIndices[i + 0] = k;
faceIndices[i + 1] = k;
faceIndices[i + 2] = 0;
}
mesh.getFaces().addAll(faceIndices);
final PhongMaterial material = Meshes.painteraPhongMaterial();
final MeshView mv = new MeshView(mesh);
mv.setOpacity(1.0);
mv.setCullFace(CullFace.FRONT);
mv.setMaterial(material);
mv.setDrawMode(DrawMode.FILL);
return mv;
}
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);
}
public void setXYZData(List<Double> xData, List<Double> yData, List<Double> zData, List<Color> colors) {
xAxisData = xData;
yAxisData = yData;
zAxisData = zData;
scatterDataGroup.getChildren().clear();
//for now we will always default to x axis
//later we could maybe dynamically determine the smallest axis and then
//uses 0's for the other axes that are larger.
for(int i=0;i<xAxisData.size();i++) {
final Shape3D dataSphere = createDefaultNode(nodeRadius);
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);
dataSphere.setMaterial(new PhongMaterial(colors.get(i)));
scatterDataGroup.getChildren().add(dataSphere);
}
}
public void setSelectedTile(Tile3D tile) {
// Simulate SV behavior
if (currentSelectedNode != null) {
((PhongMaterial) currentSelectedNode.getMaterial()).setDiffuseColor(Color.WHITE);
final Node currentSelectedNodeToUnselect = currentSelectedNode;
// Wait for principal UI to be updated (the selection)
Platform.runLater(() -> {
((Tile3D) currentSelectedNodeToUnselect).snapshot(); // In case of changes on 2D node after edit
});
}
currentSelectedNode = tile;
((PhongMaterial) tile.getMaterial()).setDiffuseColor(Color.YELLOW); // Simulate SV selection color
}
private Node createBlockShape(final ShapeKey<T> key)
{
final Interval keyInterval = key.interval();
final double[] worldMin = new double[3], worldMax = new double[3];
Arrays.setAll(worldMin, d -> keyInterval.min(d));
Arrays.setAll(worldMax, d -> keyInterval.min(d) + keyInterval.dimension(d));
unshiftedWorldTransforms.apply(key.scaleIndex()).apply(worldMin, worldMin);
unshiftedWorldTransforms.apply(key.scaleIndex()).apply(worldMax, worldMax);
final RealInterval blockWorldInterval = new FinalRealInterval(worldMin, worldMax);
final double[] blockWorldSize = new double[blockWorldInterval.numDimensions()];
Arrays.setAll(blockWorldSize, d -> blockWorldInterval.realMax(d) - blockWorldInterval.realMin(d));
// the standard Box primitive is made up of triangles, so the unwanted diagonals are visible when using DrawMode.Line
// final Box box = new Box(
// blockWorldSize[0],
// blockWorldSize[1],
// blockWorldSize[2]
// );
final PolygonMeshView box = new PolygonMeshView(Meshes.createQuadrilateralMesh(
(float) blockWorldSize[0],
(float) blockWorldSize[1],
(float) blockWorldSize[2]
));
final double[] blockWorldTranslation = new double[blockWorldInterval.numDimensions()];
Arrays.setAll(blockWorldTranslation, d -> blockWorldInterval.realMin(d) + blockWorldSize[d] * 0.5);
box.setTranslateX(blockWorldTranslation[0]);
box.setTranslateY(blockWorldTranslation[1]);
box.setTranslateZ(blockWorldTranslation[2]);
final PhongMaterial material = Meshes.painteraPhongMaterial();
box.setCullFace(CullFace.NONE);
box.setMaterial(material);
box.setDrawMode(DrawMode.LINE);
return box;
}
private Xform buildAxes()
{
Xform axes = new Xform();
final PhongMaterial red = new PhongMaterial();
red.setDiffuseColor(Color.RED);
red.setSpecularColor(Color.DARKRED);
final PhongMaterial green = new PhongMaterial();
green.setDiffuseColor(Color.GREEN);
green.setSpecularColor(Color.DARKGREEN);
final PhongMaterial blue = new PhongMaterial();
blue.setDiffuseColor(Color.BLUE);
blue.setSpecularColor(Color.DARKBLUE);
final Box xAxis = new Box(AXIS_LENGTH, 1, 1);
final Box yAxis = new Box(1, AXIS_LENGTH, 1);
final Box zAxis = new Box(1, 1, AXIS_LENGTH);
xAxis.setTranslateX(AXIS_LENGTH/2 + 0.5);
yAxis.setTranslateY(AXIS_LENGTH/2 + 0.5);
zAxis.setTranslateZ(AXIS_LENGTH/2 + 0.5);
xAxis.setMaterial(red);
yAxis.setMaterial(green);
zAxis.setMaterial(blue);
axes.getChildren().addAll(xAxis, yAxis, zAxis);
return axes;
}
@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);
}
public static PhongMaterial getMaterial(String face){
PhongMaterial arrowMat = new PhongMaterial();
arrowMat.setSpecularColor(Color.WHITESMOKE);
Color color = Color.WHITE;
switch(face){
case "F":
case "Fi": color = Color.BLUE.brighter();
break;
case "B":
case "Bi": color = Color.BLUE.brighter();
break;
case "R":
case "Ri": color = Color.RED.brighter();
break;
case "L":
case "Li": color = Color.RED.brighter();
break;
case "U":
case "Ui": color = Color.FORESTGREEN.brighter();
break;
case "D":
case "Di": color = Color.FORESTGREEN.brighter();
break;
case "Z":
case "Zi": color = Color.BLUE.brighter();
break;
case "X":
case "Xi": color = Color.RED.brighter();
break;
case "Y":
case "Yi": color = Color.FORESTGREEN.brighter();
break;
}
arrowMat.setDiffuseColor(color);
return arrowMat;
}
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);
}
public ShapeContainer(T shape) {
this.shape = shape;
this.material = new PhongMaterial();
this.emissive = new PointLight();
this.selfIllumination = new AmbientLight();
this.selfIllumination.getScope().add(ShapeContainer.this);
initialize();
}
public Material getMaterialWithColor(Color color, String image){
PhongMaterial mat = new PhongMaterial(color);
if(image!=null && !image.isEmpty()){
Image img = new Image(image);
mat.setDiffuseMap(img);
NormalMap normal = new NormalMap(img);
// normal.setIntensity(10);
// normal.setIntensityScale(2);
mat.setBumpMap(normal);
}
mat.setSpecularPower(32);
mat.setSpecularColor(Color.WHITE);
return mat;
}
private void clearMaterialAndSetDiffMap(PhongMaterial mat, Image diff){
mat.setBumpMap(null);
mat.setSpecularMap(null);
mat.setSelfIlluminationMap(null);
mat.setDiffuseColor(DEFAULT_DIFFUSE_COLOR);
mat.setSpecularColor(DEFAULT_SPECULAR_COLOR);
mat.setDiffuseMap(diff);
}
private void clearMaterialAndSetColor(PhongMaterial mat, Color col){
mat.setBumpMap(null);
mat.setSpecularMap(null);
mat.setSelfIlluminationMap(null);
mat.setDiffuseMap(null);
mat.setDiffuseColor(col);
}
public Axes(double scale) {
axisX.getTransforms().addAll(new Rotate(90, Rotate.Z_AXIS), new Translate(0, 30, 0));
axisX.setMaterial(new PhongMaterial(Color.RED));
axisY.getTransforms().add(new Translate(0, 30, 0));
axisY.setMaterial(new PhongMaterial(Color.GREEN));
axisZ.setMaterial(new PhongMaterial(Color.BLUE));
axisZ.getTransforms().addAll(new Rotate(90, Rotate.X_AXIS), new Translate(0, 30, 0));
getChildren().addAll(axisX, axisY, axisZ);
getTransforms().add(new Scale(scale, scale, scale));
}
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 static PhongMaterial getMaterial(String face){
PhongMaterial arrowMat = new PhongMaterial();
arrowMat.setSpecularColor(Color.WHITESMOKE);
Color color=Color.WHITE;
switch(face){
case "F":
case "Fi": color=Color.BLUE.brighter();
break;
case "B":
case "Bi": color=Color.BLUE.brighter();
break;
case "R":
case "Ri": color=Color.RED.brighter();
break;
case "L":
case "Li": color=Color.RED.brighter();
break;
case "U":
case "Ui": color=Color.FORESTGREEN.brighter();
break;
case "D":
case "Di": color=Color.FORESTGREEN.brighter();
break;
case "Z":
case "Zi": color=Color.BLUE.brighter();
break;
case "X":
case "Xi": color=Color.RED.brighter();
break;
case "Y":
case "Yi": color=Color.FORESTGREEN.brighter();
break;
}
arrowMat.setDiffuseColor(color);
return arrowMat;
}
@Override
public void setNodeColor(Color featureColor) {
PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(featureColor);
featureBox.setMaterial(material);
}
public void clearSelection() {
if (currentSelectedNode != null) {
((PhongMaterial) currentSelectedNode.getMaterial()).setDiffuseColor(Color.WHITE);
((Tile3D) currentSelectedNode).snapshot(); // In case of changes
}
}
public Tile3D(SVNode currentRoot2D, double factor2d3d, SVNode node2D, double depth, double thickness, ITile3DListener l, IThreeDOM i) {
this.depth = depth;
this.currentRoot2D = currentRoot2D;
this.factor2d3d = factor2d3d;
this.iTile3DListener = l;
this.iThreeDOM = i;
node2d = node2D;
material = new PhongMaterial();
material.setDiffuseColor(Color.WHITE);
material.setSpecularColor(Color.TRANSPARENT);
Bounds bounds2D = localetoRoot(node2D);
//
Bounds bounds3D = new BoundingBox(bounds2D.getMinX() * factor2d3d,
bounds2D.getMinY() * factor2d3d,
bounds2D.getWidth() * factor2d3d,
bounds2D.getHeight() * factor2d3d);
super.setDepth(thickness);
super.setWidth(bounds3D.getWidth());
super.setHeight(bounds3D.getHeight());
// Place object as 0,0 that is curently in the middle of 3D universe
getTransforms().add(new Translate(bounds3D.getMinX() + bounds3D.getWidth() / 2,
bounds3D.getMinY() + bounds3D.getHeight() / 2,
0));
setMaterial(material);
snapshot();
super.setOnMouseMoved((MouseEvent me) -> {
String mouseOverTileText = node2D.getImpl().getClass().getSimpleName();
iTile3DListener.onMouseMovedOnTile(mouseOverTileText);
});
// super.setOnMouseClicked((MouseEvent event) -> {
super.setOnMousePressed((MouseEvent event) -> {
// Selection
iTile3DListener.onMouseClickedOnTile(Tile3D.this);
if(event.isSecondaryButtonDown()){
iTile3DListener.onMouseRightClickedOnTile(event);
}
});
}
public PhongMaterial getMaterial()
{
return material;
}
public static PhongMaterial painteraPhongMaterial() {
return painteraPhongMaterial(DEFAULT_MESH_COLOR);
}
public static PhongMaterial painteraPhongMaterial(final Color color) {
final PhongMaterial material = new PhongMaterial();
material.setSpecularColor(color);
material.setSpecularPower(50);
return material;
}
private void createQubit() {
PerspectiveCamera camera = new PerspectiveCamera(true);
camera.setNearClip(0.1);
camera.setFarClip(10000.0);
camera.getTransforms().addAll(rotateX, rotateY, new Translate(0, 0, -200));
FrustumMesh plane = new FrustumMesh(50, 50, 1, 1, new Point3D(0, -0.5f, 0), new Point3D(0, 0.5f, 0));
plane.setMaterial(new PhongMaterial(Color.web("#ccdd3320")));
SegmentedSphereMesh innerSphere = new SegmentedSphereMesh(40, 0, 0, 50, new Point3D(0, 0, 0));
innerSphere.setMaterial(new PhongMaterial(Color.web("#ff800080")));
SegmentedSphereMesh frameSphere = new SegmentedSphereMesh(20, 0, 0, 50, new Point3D(0, 0, 0));
frameSphere.setMaterial(new PhongMaterial(Color.BLACK));
frameSphere.setDrawMode(DrawMode.LINE);
FrustumMesh rod = new FrustumMesh(2, 2, 1, 1, new Point3D(0, 0, 0), new Point3D(50, 0, 0));
rod.setMaterial(new PhongMaterial(Color.web("#0080ff")));
SegmentedSphereMesh smallSphere = new SegmentedSphereMesh(20, 0, 0, 4, new Point3D(50, 0, 0));
smallSphere.setMaterial(new PhongMaterial(Color.web("#0080ff")));
rodSphere = new Group(smallSphere, rod);
Group group = new Group(plane, rodSphere, innerSphere, frameSphere, new AmbientLight(Color.BISQUE));
SubScene subScene = new SubScene(group, 100, 100, true, SceneAntialiasing.BALANCED);
subScene.setCamera(camera);
subScene.setOnMousePressed(event -> {
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
subScene.setOnMouseDragged(event -> {
rotateX.setAngle(rotateX.getAngle() - (event.getSceneY() - mouseOldY));
rotateY.setAngle(rotateY.getAngle() + (event.getSceneX() - mouseOldX));
mouseOldX = event.getSceneX();
mouseOldY = event.getSceneY();
});
getChildren().add(subScene);
rodSphere.getTransforms().setAll(myRotate);
}
private static PhongMaterial getMaterial(final Scanner scanner)
{
final PhongMaterial material = new PhongMaterial();
material.setDiffuseColor(getColor(scanner));
return material;
}
public void setStroke(Color color) {
Platform.runLater(() -> setMaterial(new PhongMaterial(color)));
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setResizable(false);
Scene scene = new Scene(root, 1024, 800, true);
// Create and position camera
Camera camera = new PerspectiveCamera();
camera.getTransforms().addAll(
new Rotate(0, Rotate.Y_AXIS),
new Rotate(0, Rotate.X_AXIS),
new Translate(-500, -425, 1200));
scene.setCamera(camera);
scene.setFill(Paint.valueOf(Color.BLACK.toString()));
// Box
testObject = new Cylinder(10, 50);
testObject.setMaterial(new PhongMaterial(Color.RED));
testObject.getTransforms().addAll(new Translate(50, 0, 0));
TdsModelImporter model_importer = new TdsModelImporter();
model_importer.read(getClass().getResource("/models/SpaceLaunchSystem.3DS"));
Node[] nodes = model_importer.getImport();
model_importer.close();
Group rocket = new Group(nodes);
rocket.getTransforms().addAll(new Translate(0, 25, 0));
// Build the Scene Graph
root.getChildren().addAll(testObject, rocket);
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.out.println(event);
if (event.getEventType().equals(WindowEvent.WINDOW_CLOSE_REQUEST)) {
System.exit(0);
}
}
});
mqttClient.subscribe(MQTT_TOPIC_IMU + "/#");
}
private void addMesh(String key) {
if (facesStart >= faces.size()) {
// we're only interested in faces
smoothingGroupsStart = smoothingGroups.size();
return;
}
Map<Integer, Integer> vertexMap = new HashMap<>(vertexes.size() / 2);
Map<Integer, Integer> uvMap = new HashMap<>(uvs.size() / 2);
Map<Integer, Integer> normalMap = new HashMap<>(normals.size() / 2);
FloatArrayList newVertexes = new FloatArrayList(vertexes.size() / 2);
FloatArrayList newUVs = new FloatArrayList(uvs.size() / 2);
FloatArrayList newNormals = new FloatArrayList(normals.size() / 2);
boolean useNormals = true;
for (int i = facesStart; i < faces.size(); i += 2) {
int vi = faces.get(i);
Integer nvi = vertexMap.get(vi);
if (nvi == null) {
nvi = newVertexes.size() / 3;
vertexMap.put(vi, nvi);
newVertexes.add(vertexes.get(vi * 3));
newVertexes.add(vertexes.get(vi * 3 + 1));
newVertexes.add(vertexes.get(vi * 3 + 2));
}
faces.set(i, nvi);
int uvi = faces.get(i + 1);
Integer nuvi = uvMap.get(uvi);
if (nuvi == null) {
nuvi = newUVs.size() / 2;
uvMap.put(uvi, nuvi);
if (uvi >= 0) {
newUVs.add(uvs.get(uvi * 2));
newUVs.add(uvs.get(uvi * 2 + 1));
} else {
newUVs.add(0f);
newUVs.add(0f);
}
}
faces.set(i + 1, nuvi);
if (useNormals) {
int ni = faceNormals.get(i/2);
Integer nni = normalMap.get(ni);
if (nni == null) {
nni = newNormals.size() / 3;
normalMap.put(ni, nni);
if (ni >= 0 && normals.size() >= (ni+1)*3) {
newNormals.add(normals.get(ni * 3));
newNormals.add(normals.get(ni * 3 + 1));
newNormals.add(normals.get(ni * 3 + 2));
} else {
useNormals = false;
newNormals.add(0f);
newNormals.add(0f);
newNormals.add(0f);
}
}
faceNormals.set(i/2, nni);
}
}
TriangleMesh mesh = new TriangleMesh();
mesh.getPoints().setAll(newVertexes.toFloatArray());
mesh.getTexCoords().setAll(newUVs.toFloatArray());
mesh.getFaces().setAll(((IntegerArrayList) faces.subList(facesStart, faces.size())).toIntArray());
// Use normals if they are provided
if (useNormals) {
int[] newFaces = ((IntegerArrayList) faces.subList(facesStart, faces.size())).toIntArray();
int[] newFaceNormals = ((IntegerArrayList) faceNormals.subList(facesNormalStart, faceNormals.size())).toIntArray();
int[] smGroups = SmoothingGroups.calcSmoothGroups(mesh, newFaces, newFaceNormals, newNormals.toFloatArray());
mesh.getFaceSmoothingGroups().setAll(smGroups);
} else {
mesh.getFaceSmoothingGroups().setAll(((IntegerArrayList) smoothingGroups.subList(smoothingGroupsStart, smoothingGroups.size())).toIntArray());
}
int keyIndex = 2;
String keyBase = key;
while (meshes.get(key) != null) {
key = keyBase + " (" + keyIndex++ + ")";
}
meshes.put(key, mesh);
materials.put(key, material);
log(
"Added mesh '" + key + "' of " + mesh.getPoints().size() / mesh.getPointElementSize() + " vertexes, "
+ mesh.getTexCoords().size() / mesh.getTexCoordElementSize() + " uvs, "
+ mesh.getFaces().size() / mesh.getFaceElementSize() + " faces, "
+ mesh.getFaceSmoothingGroups().size() + " smoothing groups.");
log("material diffuse color = " + ((PhongMaterial) material).getDiffuseColor());
log("material diffuse map = " + ((PhongMaterial) material).getDiffuseMap());
facesStart = faces.size();
facesNormalStart = faceNormals.size();
smoothingGroupsStart = smoothingGroups.size();
}