0

My goal is to create Boxes with individual images on each side, which I have been completing by using a TriangleMesh to create the boxes, and using a single texture to map the points over the Mesh.

The issue is that I have been experiencing heavy overhead (high ram, long load times and slowness when moving objects in the world) from creating these TriangleMesh, and I'm not sure why fully, but here are a few of my guesses.

  1. I initially tried to use a total of 8 points and reuse the points when creating the individual faces, and wrap the textures, but for some reason the texcoords did not like using only 8 points. As I look at Box's class I see they use 24 points as well as I do, so it seems we cannot reuse points.

    2.a I am loading the 3 images into an hbox and then taking a snapshot to then map the coordinates. I am wondering if somehow snapshot is causing issues, or if there is some issue with the amount of pixels.

2.b This further comes into question when I tried turning multiple boxes that were stacked together into one large box and it still having a ton of slowness, if not more. I would take a snapshot of x number of images and then map them onto this one big Box. i.e., if I had 5x5x5 it would be one box with 5 images mapped on each side. Even with 1 single mesh it still causes slowness. It speeds up if I edit the "fitWidth/fitHeight" of the imageView which causes a quality change. To me, it seems like the pixel amounts are the cause. The thing is, if I'm taking the same images and just multiplying them, why is it causing slowness? Especially since when doing a normal box, the single image I was using had more pixels than the other images, so I would think it would be faster by doing the trianglemesh myself.

I don't really have a working example since this was added to an already working program that has data, but I will try to create something if need be.

I'm just trying to figure out why making a TriangleMesh on my own, which has similar code to the Box's code except.

  1. without the face smoothing group, but not sure that matters.

  2. My face array is different, and a bit confused why they have 24 points but the face array only goes from 1-8

                int[] faces = {
                2, 2, 1, 1, 0, 0, //front
                2, 2, 3, 3, 1, 1,
    
                6, 6, 5, 5, 4, 4, //right
                6, 6, 7, 7, 5, 5, 
    
                10, 10, 9, 9, 8, 8, //back
                10, 10, 11, 11, 9, 9,
    
                14, 14, 13, 13, 12, 12,//left
                14 ,14, 15, 15, 13, 13,
    
                18, 18, 17, 17, 16, 16,//top
                18, 18, 19, 19, 17, 17,
    
                22, 22, 21, 21, 20, 20, //bottom
                22, 22, 23, 23, 21, 21`
    
    1. My texCoords are 6 sets of pairs instead of the 1 set here.

static TriangleMesh createMesh(float w, float h, float d) {

// NOTE: still create mesh for degenerated box
float hw = w / 2f;
float hh = h / 2f;
float hd = d / 2f;

float points[] = {
    -hw, -hh, -hd,
     hw, -hh, -hd,
     hw,  hh, -hd,
    -hw,  hh, -hd,
    -hw, -hh,  hd,
     hw, -hh,  hd,
     hw,  hh,  hd,
    -hw,  hh,  hd};

float texCoords[] = {0, 0, 1, 0, 1, 1, 0, 1};

// Specifies hard edges.
int faceSmoothingGroups[] = {
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};

int faces[] = {
    0, 0, 2, 2, 1, 1,
    2, 2, 0, 0, 3, 3,
    1, 0, 6, 2, 5, 1,
    6, 2, 1, 0, 2, 3,
    5, 0, 7, 2, 4, 1,
    7, 2, 5, 0, 6, 3,
    4, 0, 3, 2, 0, 1,
    3, 2, 4, 0, 7, 3,
    3, 0, 6, 2, 2, 1,
    6, 2, 3, 0, 7, 3,
    4, 0, 1, 2, 5, 1,
    1, 2, 4, 0, 0, 3,
};

TriangleMesh mesh = new TriangleMesh(true);
mesh.getPoints().setAll(points);
mesh.getTexCoords().setAll(texCoords);
mesh.getFaces().setAll(faces);
mesh.getFaceSmoothingGroups().setAll(faceSmoothingGroups);

return mesh;

}

I also had heard that there were improvements to 3D with Version 9. I have been trying to update the past few days to 11 with issues (I had tried 10 before but a lot of my code needed fixings), and 9 isn't downloading so I wanted to ask before trying to put in more effort to get 11 working if it will in fact improve the situation, but I am still wondering why trianglmesh is so slowed comparatively. I'm maybe making 1000 boxes max.

Thank you

EDIT:

Example of how I create the images.

     public BoxMesh(width, height,depth, int stack)
    {
        float width = width;
        float height = height;
        float depth = depth
        List<ImageView> imageList = new ArrayList();


        imageList.add(new ImageView(new Image("file:"C:\\file1.jpg"));

            HBox h = new HBox();
               Image image = null;
            for(int i = 0; i < stack; i++)
            {

             image = new Image("file:"C:\\file2.jpg");


               ImageView iv = new ImageView(image);
               iv.setFitWidth(image.getWidth()/2);  //decreases quality to
               iv.setFitHeight(image.getHeight()/2);  //speed up program
               h.getChildren().add(iv);
            }  
            imageList.add(new ImageView(h.snapshot(null,null)));


            VBox v = new VBox();
              Image image = null;
             for(int i = 0; i < stack; i++)
            {

               image = new Image("file:"C:\\file3.jpg");


                ImageView iv = new ImageView(image);
                iv.setFitWidth(image.getWidth()/2);
                iv.setFitHeight(image.getHeight()/2);
               v.getChildren().add(iv);

            }

             imageList.add(new ImageView(v.snapshot(null, null)));

        PhongMaterial p = new PhongMaterial();
       HBox hb = new HBox();


     hb.getChildren().addAll(imageList);

    WritableImage snapshot = hb.snapshot(null, null);
    if(snapshot.isError())
    {
        System.out.println(snapshot.exceptionProperty().getValue());
    }
    p.setDiffuseMap(snapshot);
  • 1
    The relevant part is how you deal with the images and snapshots. If image size is big that could be a reason. If you can post an MVCE or at least a code snippet of how you do that, it will really help. – José Pereda Feb 26 '19 at 08:51
  • @JoséPereda Thank you for your comment, I will edit in the necessary information to create a working example. I have an HBox where I add ImageView children for each side and then would perform the mesh information. With the thought of making 1 mesh for the boxes I took a VBox for the top and bottom images and combined the images together similarly to the HBox, and then took a snapshot of that to add to the HBox. I also did similarly the same with the sides but used an HBox. So there are a bunch of imageviews added to hbox/vbox. The sizes should be the same regardless. Thank you ! –  Feb 26 '19 at 23:22
  • I've done something similar [here](https://stackoverflow.com/questions/48618329/how-to-add-text-to-each-face-of-a-box-javafx). – José Pereda Feb 26 '19 at 23:28
  • @JoséPereda thanks, I'll look into it. I took my code and edited it a bit (since there was stuff that wasn't needed), so hopefully this example makes sense and looks good. I appreciate the help, thank you! I also wanted to ask if you have any idea about the differences between Java versions and speed in regards to 3D? I heard Java 9 helps with 3D overhead, but I am not sure what has been done since then. EDIT: I've seen examples similar to that. I created the textures as a line, instead of a "cross" does the cross pattern matter? It seems the mapping coordinates are what matters. –  Feb 26 '19 at 23:35
  • I don't notice that much of a difference between 8 and 9/10/11, but definitely worth trying, it is not so difficult to try out. – José Pereda Feb 26 '19 at 23:42
  • I had some issues with 10 previously, 11 had issues and 9 I could only find in the archieves that required login to Oracle so I didn't want to spend a bunch of time if it wasn't going to work, thanks. My previous post I worded it in a way that made it seem like I didn't edit my OP. The OP has the snapshot code. I can also upload the code for how I create the texcoords and finding the sides image. It seems that everyone goes the cross route, but not sure why it would make a difference. Thank you again. –  Feb 26 '19 at 23:45
  • Your edited code shows you use too many nodes and snapshots, also I can't see the dimensions of the images you are using, so I can't really test and reproduce your issue. – José Pereda Feb 26 '19 at 23:50
  • I figured that would probably be the issue, but I'm curious why the nodes and snapsnots are a problem? The images aren't very big, no more than 500x500, usually it's something like 250x100, 50x100, 250x75 for a box. It would add up with multiple sides and tops to a bigger resolution though, but maybe the snapshot/nodes are more than without it, even if it's one mesh compared to many if I do a lets say 1x5x5. (wide, high, deep) –  Feb 26 '19 at 23:59
  • @JoséPereda sorry to get back to you so late, but I put this on hold for awhile. I ended up getting it working well by duplicating the mesh into multiple MeshViews. I only do 1 box, instead of trying to create multiple sides/tops to make longer boxes. It's interesting that there was so much of a slowness difference, but it seems reusing the meshes are the key to avoiding slowdowns. I would think a single mesh would have worked better, but as mentioned above it made things much worse. Thanks for your help! –  Mar 15 '19 at 00:32

0 Answers0