Search in sources :

Example 1 with MeshView

use of javafx.scene.shape.MeshView in project FXyzLib by Birdasaur.

the class ScatterPlotMesh method setXYZData.

public void setXYZData(ArrayList<Double> xData, ArrayList<Double> yData, ArrayList<Double> zData) {
    xAxisData = xData;
    yAxisData = yData;
    zAxisData = zData;
    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.
    ArrayList<Point3D> point3DList = new ArrayList<>();
    for (int i = 0; i < xAxisData.size(); i++) {
        //some safety checks for array sizes
        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);
        setTranslateX(xAxisData.get(i));
        //Convert to Floats and build list of adjusted points
        point3DList.add(new Point3D(new Float(xAxisData.get(i)), new Float(translateY), new Float(translateZ)));
        float width = 1;
        final TriangleMesh mesh = new TriangleMesh();
        //This extra point allows us to build triangles later
        for (Point3D point : point3DList) {
            //Rear points
            //top right rear point
            mesh.getPoints().addAll(point.x + width, point.y + width, point.z + width);
            //top left rear point
            mesh.getPoints().addAll(point.x - width, point.y + width, point.z + width);
            //bottom right rear point
            mesh.getPoints().addAll(point.x + width, point.y - width, point.z + width);
            //bottom left rear point
            mesh.getPoints().addAll(point.x - width, point.y - width, point.z + width);
            //Front points
            //top right front point
            mesh.getPoints().addAll(point.x + width, point.y + width, point.z - width);
            //top left front point
            mesh.getPoints().addAll(point.x - width, point.y + width, point.z - width);
            //bottom right front point
            mesh.getPoints().addAll(point.x + width, point.y - width, point.z - width);
            //bottom left front point
            mesh.getPoints().addAll(point.x - width, point.y - width, point.z - width);
        }
        //add dummy Texture Coordinate
        mesh.getTexCoords().addAll(0, 0);
        //Now generate nodes for each point
        for (int p = 8; p < point3DList.size() * 7; p += 8) {
            //add each segment
            //Wind the next 8 vertices as a cube.  The cube itself will represent the data
            //Vertices wound counter-clockwise which is the default front face of any Triangle
            //Rear triangle faces should be wound clockwise to face away from center
            //TRR,BLR,BRR
            mesh.getFaces().addAll(p, 0, p + 3, 0, p + 2, 0);
            //BLR,TRR,TLR
            mesh.getFaces().addAll(p + 3, 0, p, 0, p + 1, 0);
            //left side faces
            //TLR,TLF,BLR
            mesh.getFaces().addAll(p + 1, 0, p + 5, 0, p + 3, 0);
            //TLF,BLR,BLF
            mesh.getFaces().addAll(p + 5, 0, p + 7, 0, p + 3, 0);
            //front side faces
            //TLF,BLF,TLR
            mesh.getFaces().addAll(p + 5, 0, p + 7, 0, p + 4, 0);
            //TRF,BLF,BRF
            mesh.getFaces().addAll(p + 4, 0, p + 7, 0, p + 6, 0);
            //front side faces
            //TRF,BRF,BRR
            mesh.getFaces().addAll(p + 4, 0, p + 6, 0, p + 2, 0);
            //TRF,BRR,TRR
            mesh.getFaces().addAll(p + 4, 0, p + 2, 0, p, 0);
            //Top faces
            //TRR,TLR,TRF
            mesh.getFaces().addAll(p, 0, p + 1, 0, p + 3, 0);
            //TLR,TLF,TRF
            mesh.getFaces().addAll(p + 1, 0, p + 5, 0, p + 3, 0);
            //bottom faces
            //BLR,BLF,BRF
            mesh.getFaces().addAll(p + 3, 0, p + 7, 0, p + 6, 0);
            //BLR,BRF,BRR
            mesh.getFaces().addAll(p + 3, 0, p + 6, 0, p + 2, 0);
        }
        //Need to add the mesh to a MeshView before adding to our 3D scene
        MeshView meshView = new MeshView(mesh);
        //Fill so that the line shows width
        meshView.setDrawMode(DrawMode.FILL);
        Color hsb = Color.hsb((new Double(i) / 12) * 360, 1.0, 1.0, 0.5);
        PhongMaterial material = new PhongMaterial(hsb);
        material.setDiffuseColor(hsb);
        material.setSpecularColor(hsb);
        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
        //            Group line = new Group();
        //            AmbientLight light = new AmbientLight(Color.WHITE);
        //            light.getScope().add(meshView);
        //            line.getChildren().add(light);
        //            line.getChildren().add(meshView);           
        getChildren().addAll(meshView);
    }
}
Also used : TriangleMesh(javafx.scene.shape.TriangleMesh) Point3D(org.fxyz.geometry.Point3D) Color(javafx.scene.paint.Color) ArrayList(java.util.ArrayList) PhongMaterial(javafx.scene.paint.PhongMaterial) MeshView(javafx.scene.shape.MeshView)

Example 2 with MeshView

use of javafx.scene.shape.MeshView in project FXyzLib by Birdasaur.

the class SurfacePlot method setHeightData.

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 {
        //show lines only by default
        meshView.setDrawMode(DrawMode.LINE);
    }
    //Removing culling to show back lines
    meshView.setCullFace(CullFace.BACK);
    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);
}
Also used : TriangleMesh(javafx.scene.shape.TriangleMesh) PhongMaterial(javafx.scene.paint.PhongMaterial) MeshView(javafx.scene.shape.MeshView)

Aggregations

PhongMaterial (javafx.scene.paint.PhongMaterial)2 MeshView (javafx.scene.shape.MeshView)2 TriangleMesh (javafx.scene.shape.TriangleMesh)2 ArrayList (java.util.ArrayList)1 Color (javafx.scene.paint.Color)1 Point3D (org.fxyz.geometry.Point3D)1