下面列出了怎么用javafx.scene.shape.MeshView的API类实例代码及写法,或者点击链接到github查看源代码。
public void setTexCoords(final RealLocalizable texCoordMin, final RealLocalizable texCoordMax)
{
final List<RealPoint> texCoordsPoints = calculateTexCoords(texCoordMin, texCoordMax);
for (int row = 0; row < 2; ++row) {
for (int col = 0; col < 2; ++col) {
final MeshView meshView = meshViews.get(2 * row + col);
if (meshView == null)
continue;
final TriangleMesh mesh = (TriangleMesh) meshView.getMesh();
mesh.getTexCoords().clear();
final int[] pointIndices = getPointIndicesForQuadrant(row, col);
for (final int ptIndex : pointIndices) {
texCoordsPoints.get(ptIndex).localize(buf2D);
mesh.getTexCoords().addAll(buf2D);
}
}
}
}
public MeshGeneratorJobManager(
final int numScaleLevels,
final T identifier,
final ObservableMap<ShapeKey<T>, Pair<MeshView, Node>> meshesAndBlocks,
final Pair<Group, Group> meshesAndBlocksGroups,
final MeshViewUpdateQueue<T> meshViewUpdateQueue,
final GetBlockListFor<T> getBlockLists,
final GetMeshFor<T> getMeshes,
final IntFunction<AffineTransform3D> unshiftedWorldTransforms,
final ExecutorService managers,
final HashPriorityQueueBasedTaskExecutor<MeshWorkerPriority> workers,
final IndividualMeshProgress meshProgress)
{
this.identifier = identifier;
this.meshesAndBlocks = meshesAndBlocks;
this.meshesAndBlocksGroups = meshesAndBlocksGroups;
this.meshViewUpdateQueue = meshViewUpdateQueue;
this.getBlockLists = getBlockLists;
this.getMeshes = getMeshes;
this.unshiftedWorldTransforms = unshiftedWorldTransforms;
this.managers = managers;
this.workers = workers;
this.numScaleLevels = numScaleLevels;
this.meshesAndBlocks.addListener(this::handleMeshListChange);
this.meshProgress = meshProgress;
}
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;
}
/**
* Places a request to add a mesh onto the scene into the queue.
* The request will be executed at some point later on FX application thread, and {@code onCompleted} will be called after that.
*
* @param key
* @param meshAndBlockToAdd
* @param meshAndBlockGroup
* @param onCompleted
*/
public synchronized void addToQueue(
final ShapeKey<T> key,
final Pair<MeshView, Node> meshAndBlockToAdd,
final Pair<Group, Group> meshAndBlockGroup,
final Runnable onCompleted,
final MeshWorkerPriority priority)
{
final MeshViewQueueEntry entry = new MeshViewQueueEntry(meshAndBlockToAdd, meshAndBlockGroup, onCompleted);
keysToEntries.put(key, entry);
final boolean queueWasEmpty = priorityQueue.isEmpty();
priorityQueue.addOrUpdate(priority, key);
if (queueWasEmpty)
scheduleTask();
}
/**
* Removes the object.
*
* @param previousCsg
* the previous
*/
public void removeObject(CSG previousCsg) {
// for(Polygon poly:previousCsg.getPolygons())
// sumVert-=(poly.vertices.size());
// System.err.println("Total Verts = "+sumVert);
// System.out.println(" Removing a CSG from file: "+previousCsg+" from
// file "+csgSourceFile.get(previousCsg));
MeshView previous = getCsgMap().get(previousCsg);
if (previous != null) {
lookGroup.getChildren().remove(previous);
lookGroup.getChildren().remove(axisMap.get(previous));
axisMap.remove(previous);
}
getCsgMap().remove(previousCsg);
csgSourceFile.remove(previousCsg);
}
public void setSelectedCsg(List<CSG> selectedCsg) {
// System.err.println("Selecting group");
setSelectedCsg(selectedCsg.get(selectedCsg.size()-1));
try {
for (int in = 0; in < selectedCsg.size()-1; in++) {
int i = in;
MeshView mesh = getCsgMap().get(selectedCsg.get(i));
if (mesh != null)
Platform.runLater(() -> {
try {
mesh.setMaterial(new PhongMaterial(Color.GOLD));
}catch(Exception ex) {}
});
}
} catch (java.lang.NullPointerException ex0) {
} // if a selection is called before the limb is loaded
resetMouseTime();
}
public void updateArrow(String face, boolean hover){
boolean bFaceArrow = !(face.startsWith("X") || face.startsWith("Y") || face.startsWith("Z"));
MeshView arrow = bFaceArrow ? faceArrow : axisArrow;
if (hover && onRotation.get()) {
return;
}
arrow.getTransforms().clear();
if (hover) {
double d0 = arrow.getBoundsInParent().getHeight() / 2d;
Affine aff = Utils.getAffine(dimCube, d0, bFaceArrow, face);
arrow.getTransforms().setAll(aff);
arrow.setMaterial(Utils.getMaterial(face));
if (previewFace.get().isEmpty()) {
previewFace.set(face);
onPreview.set(true);
rotateFace(face, true, false);
}
} else if (previewFace.get().equals(face)) {
rotateFace(Utils.reverseRotation(face), true, true);
} else if (previewFace.get().equals("V")) {
previewFace.set("");
onPreview.set(false);
}
}
public void updateArrow(String face, boolean hover){
boolean bFaceArrow=!(face.startsWith("X")||face.startsWith("Y")||face.startsWith("Z"));
MeshView arrow=bFaceArrow?faceArrow:axisArrow;
if(hover && onRotation.get()){
return;
}
arrow.getTransforms().clear();
if(hover){
double d0=arrow.getBoundsInParent().getHeight()/2d;
Affine aff=Utils.getAffine(dimCube, d0, bFaceArrow, face);
arrow.getTransforms().setAll(aff);
arrow.setMaterial(Utils.getMaterial(face));
if(previewFace.get().isEmpty()) {
previewFace.set(face);
onPreview.set(true);
rotateFace(face,true,false);
}
} else if(previewFace.get().equals(face)){
rotateFace(Utils.reverseRotation(face),true,true);
} else if(previewFace.get().equals("V")){
previewFace.set("");
onPreview.set(false);
}
}
private void setMeshVisibility(final Pair<MeshView, Node> meshAndBlock, final boolean isVisible)
{
if (meshAndBlock.getA() != null)
meshAndBlock.getA().setVisible(isVisible);
if (meshAndBlock.getB() != null)
meshAndBlock.getB().setVisible(isVisible);
}
private MeshViewQueueEntry(
final Pair<MeshView, Node> meshAndBlockToAdd,
final Pair<Group, Group> meshAndBlockGroup,
final Runnable onCompleted)
{
this.meshAndBlockToAdd = meshAndBlockToAdd;
this.meshAndBlockGroup = meshAndBlockGroup;
this.onCompleted = onCompleted;
}
public void doSequence(String list){
onScrambling.set(true);
sequence = Utils.unifyNotation(list);
/*
This is the way to perform several rotations from a list, waiting till each of
them ends properly. A listener is added to onRotation, so only when the last rotation finishes
a new rotation is performed. The end of the list is used to stop the listener, by adding
a new listener to the index property. Note the size+1, to allow for the last rotation to end.
*/
IntegerProperty index = new SimpleIntegerProperty(1);
ChangeListener<Boolean> lis = (ov, b, b1) -> {
if (!b1) {
if (index.get()<sequence.size()) {
rotateFace(sequence.get(index.get()));
} else {
// save transforms
for (Map.Entry<String, MeshView> entry : mapMeshes.entrySet()) {
mapTransformsScramble.put(entry.getKey(), entry.getValue().getTransforms().get(0));
}
orderScramble = new ArrayList<>();
for (Integer i : reorder) {
orderScramble.add(i);
}
}
index.set(index.get()+1);
}
};
index.addListener((ov, v, v1) -> {
if (v1.intValue() == sequence.size()+1) {
onScrambling.set(false);
onRotation.removeListener(lis);
count.set(-1);
}
});
onRotation.addListener(lis);
rotateFace(sequence.get(0));
}
public void doReset(){
// System.out.println("Reset!");
content.resetCam();
for (Map.Entry<String, MeshView> entry : mapMeshes.entrySet()) {
entry.getValue().getTransforms().setAll(mapTransformsOriginal.get(entry.getKey()));
}
order = new ArrayList<>();
for (Integer i : orderOriginal) {
order.add(i);
}
rot.setCube(order);
count.set(-1);
}
private static Point3D getMeshNormal(MeshView mesh){
TriangleMesh tm = (TriangleMesh) mesh.getMesh();
float[] fPoints = new float[tm.getPoints().size()];
tm.getPoints().toArray(fPoints);
Point3D BA = new Point3D(fPoints[3] - fPoints[0], fPoints[4] - fPoints[1], fPoints[5] - fPoints[2]);
Point3D CA = new Point3D(fPoints[6] - fPoints[0], fPoints[7] - fPoints[1], fPoints[8] - fPoints[2]);
Point3D normal = BA.crossProduct(CA);
Affine a = new Affine(mesh.getTransforms().get(0));
return a.transform(normal.normalize());
}
public MeshView buildMeshView(String key) {
MeshView meshView = new MeshView();
meshView.setId(key);
meshView.setMaterial(materials.get(key));
meshView.setMesh(meshes.get(key));
meshView.setCullFace(CullFace.NONE);
return meshView;
}
private static Point3D getMeshNormal(MeshView mesh){
TriangleMesh tm=(TriangleMesh)mesh.getMesh();
float[] fPoints=new float[tm.getPoints().size()];
tm.getPoints().toArray(fPoints);
Point3D BA=new Point3D(fPoints[3]-fPoints[0],fPoints[4]-fPoints[1],fPoints[5]-fPoints[2]);
Point3D CA=new Point3D(fPoints[6]-fPoints[0],fPoints[7]-fPoints[1],fPoints[8]-fPoints[2]);
Point3D normal=BA.crossProduct(CA);
Affine a=new Affine(mesh.getTransforms().get(0));
return a.transform(normal.normalize());
}
/**
* @return
* list of 4 {@link MeshView}s representing quadrants of the orthoslice
*/
public List<MeshView> getMeshViews()
{
return meshViews;
}
private synchronized void handleMeshListChange(final MapChangeListener.Change<? extends ShapeKey<T>, ? extends Pair<MeshView, Node>> change)
{
final ShapeKey<T> key = change.getKey();
assert change.wasAdded() != change.wasRemoved() : "Mesh is only supposed to be added or removed at any time but not replaced: " + key;
if (change.wasAdded())
{
assert tasks.containsKey(key) : "Mesh was rendered but its task does not exist: " + key;
final long tag = tasks.get(key).tag;
final Runnable onMeshAdded = () -> {
if (!managers.isShutdown())
managers.submit(withErrorPrinting(() -> onMeshAdded(key, tag)));
};
if (change.getValueAdded().getA() != null || change.getValueAdded().getB() != null)
{
// add to the queue, call onMeshAdded() when complete
final MeshWorkerPriority priority = tasks.get(key).priority;
meshViewUpdateQueue.addToQueue(
key,
change.getValueAdded(),
meshesAndBlocksGroups,
onMeshAdded,
priority
);
}
else
{
// nothing to add, invoke the callback immediately
onMeshAdded.run();
}
}
if (change.wasRemoved() && (change.getValueRemoved().getA() != null || change.getValueRemoved().getB() != null))
{
// try to remove the request from the queue in case the mesh has not been added to the scene yet
if (!meshViewUpdateQueue.removeFromQueue(key))
{
// was not in the queue, remove it from the scene
InvokeOnJavaFXApplicationThread.invoke(() -> {
meshesAndBlocksGroups.getA().getChildren().remove(change.getValueRemoved().getA());
meshesAndBlocksGroups.getB().getChildren().remove(change.getValueRemoved().getB());
});
}
}
}
public static void main(String[] args) throws IOException {
PlatformImpl.startup(() -> {});
final Path path = Paths.get(System.getProperty("user.home"), "Downloads", "catmaid-meshes", "Block3.json");
final TriangleMesh mesh = new CatmaidJsonLoader().loadMesh(path);
final double[] min = {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY};
final double[] max = {Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY};
for (int i = 0; i < mesh.getPoints().size(); i += 3) {
for (int d = 0; d < 3; ++d) {
min[d] = Math.min(min[d], mesh.getPoints().get(i + d));
max[d] = Math.max(max[d], mesh.getPoints().get(i + d));
}
}
System.out.print(Arrays.toString(min) +" " + Arrays.toString(max));
final Interval interval = Intervals.smallestContainingInterval(new FinalRealInterval(min, max));
final MeshView mv = new MeshView(mesh);
mv.setMaterial(Meshes.painteraPhongMaterial(Color.WHITE));
mv.setDrawMode(DrawMode.FILL);
mv.setCullFace(CullFace.BACK);
final Viewer3DFX viewer = new Viewer3DFX(800, 600);
viewer.meshesEnabledProperty().set(true);
mv.setOpacity(1.0);
viewer.setInitialTransformToInterval(interval);
final MeshView mv2 = new MeshView(mesh);
mv.setMaterial(Meshes.painteraPhongMaterial());
mv.setDrawMode(DrawMode.FILL);
mv.setCullFace(CullFace.BACK);
mv2.setTranslateX(100);
viewer.meshesGroup().getChildren().addAll(mv, mv2);
Platform.setImplicitExit(true);
Platform.runLater(() -> {
final Scene scene = new Scene(viewer);
final Stage stage = new Stage();
stage.setScene(scene);
stage.setWidth(800);
stage.setHeight(600);
stage.show();
});
// final String mesh = "<IndexedTriangleSet index='0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35'><Coordinate point='440474 99212 136120 440474 119212 136120 460474 99212 136120 460474 99212 136120 440474 119212 136120 460474 119212 136120 440474 99212 136120 460474 99212 136120 460474 99212 156120 440474 99212 136120 460474 99212 156120 440474 99212 156120 440474 119212 136120 440474 119212 156120 460474 119212 156120 440474 119212 136120 460474 119212 156120 460474 119212 136120 440474 99212 156120 460474 119212 156120 440474 119212 156120 440474 99212 156120 460474 99212 156120 460474 119212 156120 440474 99212 136120 440474 119212 156120 440474 119212 136120 440474 99212 136120 440474 99212 156120 440474 119212 156120 460474 99212 136120 460474 119212 136120 460474 99212 156120 460474 119212 136120 460474 119212 156120 460474 99212 156120'/></IndexedTriangleSet>";
// final Document doc = Jsoup.parse(mesh);
// System.out.println(doc);
// System.out.println(doc.select("IndexedTriangleSet").attr("index"));
}
public static void main(String[] args) throws IOException {
PlatformImpl.startup(() -> {});
// https://people.sc.fsu.edu/~jburkardt/data/obj/obj.html
// final String objFile = "al.obj";
// final String objFile = "diamond.obj";
final String objFile = "alfa147.obj";
final Path path = Paths.get(System.getProperty("user.home"), "Downloads", objFile);
final TriangleMesh mesh = new ObjLoader().loadMesh(path);
final double[] min = {Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY};
final double[] max = {Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY};
for (int i = 0; i < mesh.getPoints().size(); i += 3) {
for (int d = 0; d < 3; ++d) {
min[d] = Math.min(min[d], mesh.getPoints().get(i + d));
max[d] = Math.max(max[d], mesh.getPoints().get(i + d));
}
}
final Interval interval = Intervals.smallestContainingInterval(new FinalRealInterval(min, max));
final MeshView mv = new MeshView(mesh);
mv.setMaterial(Meshes.painteraPhongMaterial(Color.WHITE));
mv.setDrawMode(DrawMode.FILL);
mv.setCullFace(CullFace.BACK);
final Viewer3DFX viewer = new Viewer3DFX(800, 600);
viewer.meshesEnabledProperty().set(true);
mv.setOpacity(1.0);
viewer.setInitialTransformToInterval(interval);
final MeshView mv2 = new MeshView(mesh);
mv.setMaterial(Meshes.painteraPhongMaterial());
mv.setDrawMode(DrawMode.FILL);
mv.setCullFace(CullFace.BACK);
mv2.setTranslateX(100);
viewer.meshesGroup().getChildren().addAll(mv, mv2);
// final double factor = 1;
// final double w = 1*factor, h = 2*factor, d = 3*factor;
// final Box box = new Box(w, h, d);
// box.setCullFace(CullFace.NONE);
// box.setOpacity(1.0);
// box.setMaterial(Meshes.painteraPhongMaterial(Color.RED));
// viewer.meshesGroup().getChildren().add(box);
Platform.setImplicitExit(true);
Platform.runLater(() -> {
final Scene scene = new Scene(viewer);
final Stage stage = new Stage();
stage.setScene(scene);
stage.setWidth(800);
stage.setHeight(600);
stage.show();
});
}
public void showAxis() {
Platform.runLater(() -> axisGroup.getChildren().add(gridGroup));
for (MeshView a : axisMap.keySet()) {
axisMap.get(a).show();
}
}
public void hideAxis() {
Platform.runLater(() -> axisGroup.getChildren().remove(gridGroup));
for (MeshView a : axisMap.keySet()) {
axisMap.get(a).hide();
}
}
public HashMap<CSG, MeshView> getCsgMap() {
return csgMap;
}
public void setCsgMap(HashMap<CSG, MeshView> csgMap) {
this.csgMap = csgMap;
}
public Map<String, MeshView> getMapMeshes() {
return mapMeshes;
}
public MeshView getFaceArrow() {
return faceArrow;
}
public MeshView getAxisArrow() {
return axisArrow;
}
public void doReplay(List<Move> moves){
if (moves.isEmpty()) {
return;
}
content.resetCam();
//restore scramble
if (mapTransformsScramble.size() > 0) {
// System.out.println("Restoring scramble");
for (Map.Entry<String, MeshView> entry : mapMeshes.entrySet()) {
entry.getValue().getTransforms().setAll(mapTransformsScramble.get(entry.getKey()));
}
order = new ArrayList<>();
for (Integer i : orderScramble) {
order.add(i);
}
rot.setCube(order);
count.set(-1);
} else {
// restore original
doReset();
}
onReplaying.set(true);
IntegerProperty index = new SimpleIntegerProperty(1);
ChangeListener<Boolean> lis = (ov, v, v1) -> {
if (!v1 && moves.size() > 1) {
if (index.get() < moves.size()) {
timestamp.set(moves.get(index.get()).getTimestamp());
rotateFace(moves.get(index.get()).getFace());
}
index.set(index.get() + 1);
}
};
index.addListener((ov, v, v1) -> {
if (v1.intValue() == moves.size() + 1) {
onReplaying.set(false);
onRotation.removeListener(lis);
}
});
onRotation.addListener(lis);
timestamp.set(moves.get(0).getTimestamp());
rotateFace(moves.get(0).getFace());
}
public static String getPickedRotation(int cubie, MeshView mesh){
Point3D normal = getMeshNormal(mesh);
String rots = ""; // Rx-Ry
switch(cubie){
case 0: rots = (normal.getZ() > 0.99) ? "Ui-Li" : ((normal.getX() < -0.99) ? "Ui-F" : ((normal.getY() > 0.99) ? "Ui-Li" : ""));
break;
case 1: rots = (normal.getZ() > 0.99) ? "F-Mi" : ((normal.getY() > 0.99) ? "Ui-Mi" : ""); // between L and R, as L
break;
case 2: rots = (normal.getZ() > 0.99) ? "Ui-R" : ((normal.getX() > 0.99) ? "Ui-Fi" : ((normal.getY() > 0.99) ? "Ui-R" : ""));
break;
case 3: rots = (normal.getZ() > 0.99) ? "E-F" : ((normal.getX() < -0.99) ? "E-Li" : ""); // between U and D, as D
break;
case 4: rots = (normal.getZ() > 0.99) ? "Yi-X" : "";
break;
case 5: rots = (normal.getZ() > 0.99) ? "E-Fi" : ((normal.getX() > 0.99) ? "E-R" : ""); // between U and D, as D
break;
case 6: rots = (normal.getZ() > 0.99) ? "D-Li" : ((normal.getX() < -0.99) ? "D-F" : ((normal.getY() < -0.99) ? "D-Li" : ""));
break;
case 7: rots = (normal.getZ() > 0.99) ? "Fi-Mi" : ((normal.getY() < -0.99) ? "Fi-Mi" : ""); // between L and R, as L
break;
case 8: rots = (normal.getZ() > 0.99) ? "D-R" : ((normal.getX() > 0.99) ? "D-Fi" : ((normal.getY() < -0.99) ? "D-R" : ""));
break;
case 9: rots = (normal.getY() > 0.99) ? "S-U" : ((normal.getX() < -0.99) ? "L-S" : ""); // between U and D, as D
break;
case 10: rots = (normal.getY() > 0.99) ? "Z-X" : "";
break;
case 11: rots = (normal.getY() > 0.99) ? "S-Ui" : ((normal.getX() > 0.99) ? "R-Si" : ""); // between U and D, as D
break;
case 12: rots = (normal.getX() < -0.99) ? "Yi-Z" : "";
break;
case 14: rots = (normal.getX() > 0.99) ? "Yi-Zi" : "";
break;
case 15: rots = (normal.getY() < -0.99) ? "D-S" : ((normal.getX() < -0.99) ? "Li-S" : ""); // between U and D, as D
break;
case 16: rots = (normal.getY() < -0.99) ? "Zi-X" : "";
break;
case 17: rots = (normal.getY() < -0.99) ? "D-S" : ((normal.getX() > 0.99) ? "Ri-Si" : ""); // between U and D, as D
break;
case 18: rots = (normal.getZ() < -0.99) ? "Ui-L" : ((normal.getX() < -0.99) ? "Ui-Bi" : ((normal.getY() > 0.99) ? "Ui-L" : ""));
break;
case 19: rots = (normal.getZ() < -0.99) ? "B-M" : ((normal.getY() > 0.99) ? "U-M" : ""); // between L and R, as L
break;
case 20: rots = (normal.getZ() < -0.99) ? "Ui-Ri" : ((normal.getX() > 0.99) ? "Ui-B" : ((normal.getY() > 0.99) ? "Ui-Ri" : ""));
break;
case 21: rots = (normal.getZ() < -0.99) ? "E-Bi" : ((normal.getX() < -0.99) ? "E-L" : ""); // between U and D, as D
break;
case 22: rots = (normal.getZ() < -0.99) ? "Yi-Xi" : "";
break;
case 23: rots = (normal.getZ() < -0.99) ? "E-B" : ((normal.getX() > 0.99) ? "E-Ri" : ""); // between U and D, as D
break;
case 24: rots = (normal.getZ() < -0.99) ? "D-L" : ((normal.getX() < -0.99) ? "D-Bi" : ((normal.getY() < -0.99) ? "D-L" : ""));
break;
case 25: rots = (normal.getZ() < -0.99) ? "Bi-M" : ((normal.getY() < -0.99) ? "Bi-M" : ""); // between L and R, as L
break;
case 26: rots = (normal.getZ() < -0.99) ? "D-Ri" : ((normal.getX() > 0.99) ? "D-B" : ((normal.getY() < -0.99) ? "D-B" : ""));
break;
}
return rots;
}
public void setHeightData(float[][] arrayY, int spacing, Color color, boolean ambient, boolean fill) {
material = new PhongMaterial();
material.setSpecularColor(color);
material.setDiffuseColor(color);
mesh = new TriangleMesh();
// Fill Points
for (int x = 0; x < arrayY.length; x++) {
for (int z = 0; z < arrayY[0].length; z++) {
mesh.getPoints().addAll(x * spacing, arrayY[x][z], z * spacing);
}
}
//for now we'll just make an empty texCoordinate group
mesh.getTexCoords().addAll(0, 0);
int total = arrayY.length * arrayY.length;
int nextRow = arrayY.length;
//Add the faces "winding" the points generally counter clock wise
for (int i = 0; i < total - nextRow -1; i++) {
//Top upper left triangle
mesh.getFaces().addAll(i,0,i+nextRow,0,i+1,0);
//Top lower right triangle
mesh.getFaces().addAll(i+nextRow,0,i+nextRow + 1,0,i+1,0);
//Bottom
}
//Create a viewable MeshView to be added to the scene
//To add a TriangleMesh to a 3D scene you need a MeshView container object
meshView = new MeshView(mesh);
//The MeshView allows you to control how the TriangleMesh is rendered
if(fill) {
meshView.setDrawMode(DrawMode.FILL);
} else {
meshView.setDrawMode(DrawMode.LINE); //show lines only by default
}
meshView.setCullFace(CullFace.BACK); //Removing culling to show back lines
getChildren().add(meshView);
meshView.setMaterial(material);
if (ambient) {
selfLight.getScope().add(meshView);
if(!getChildren().contains(selfLight))
getChildren().add(selfLight);
}
else if(getChildren().contains(selfLight))
getChildren().remove(selfLight);
setDepthTest(DepthTest.ENABLE);
}
public PolyLine3D(List<Point3D> points, int width, Color color) {
this.points = points;
this.width = width;
this.color = color;
setDepthTest(DepthTest.ENABLE);
mesh = new TriangleMesh();
//add each point. For each point add another point shifted on Z axis by width
//This extra point allows us to build triangles later
for(Point3D point: points) {
mesh.getPoints().addAll(point.x,point.y,point.z);
mesh.getPoints().addAll(point.x,point.y,point.z+width);
}
//add dummy Texture Coordinate
mesh.getTexCoords().addAll(0,0);
//Now generate trianglestrips for each line segment
for(int i=2;i<points.size()*2;i+=2) { //add each segment
//Vertices wound counter-clockwise which is the default front face of any Triange
//These triangles live on the frontside of the line facing the camera
mesh.getFaces().addAll(i,0,i-2,0,i+1,0); //add primary face
mesh.getFaces().addAll(i+1,0,i-2,0,i-1,0); //add secondary Width face
//Add the same faces but wind them clockwise so that the color looks correct when camera is rotated
//These triangles live on the backside of the line facing away from initial the camera
mesh.getFaces().addAll(i+1,0,i-2,0,i,0); //add primary face
mesh.getFaces().addAll(i-1,0,i-2,0,i+1,0); //add secondary Width face
}
//Need to add the mesh to a MeshView before adding to our 3D scene
meshView = new MeshView(mesh);
meshView.setDrawMode(DrawMode.FILL); //Fill so that the line shows width
material = new PhongMaterial(color);
material.setDiffuseColor(color);
material.setSpecularColor(color);
meshView.setMaterial(material);
//Make sure you Cull the Back so that no black shows through
meshView.setCullFace(CullFace.BACK);
//Add some ambient light so folks can see it
AmbientLight light = new AmbientLight(Color.WHITE);
light.getScope().add(meshView);
getChildren().add(light);
getChildren().add(meshView);
}