4

I'm working on a 3D project in JavaFX 8. I have built a Car 3d Model with several TriangleMesh objects I'm also using several other JavaFX 'Shape 3D's to create the wheels and axles.

The problem is that the MeshViews elements seem transparent. I can see the other Shape3D objects thru it

enter image description here 2 Cylinders are visible even though the MeshView is in front of it

Here is an example of one of the TriangleMesh's I made

// =============================  ROOF ============================= // 

    TriangleMesh roofMesh = new TriangleMesh(VertexFormat.POINT_TEXCOORD);

    roofMesh.getPoints().addAll(
            /* X */ -roofWidth/2.f,  /* Y */    roofHeight + wheelDiameter / 2  + wheelGap + doorHeight, /* Z */ - roofLength/2,    //PT0
            /* X */ roofWidth/2.f,   /* Y */    roofHeight + wheelDiameter / 2  + wheelGap + doorHeight, /* Z */ - roofLength/2,    //PT1
            /* X */ -roofWidth/2.f,  /* Y */    roofHeight + wheelDiameter / 2  + wheelGap + doorHeight, /* Z */  roofLength/2,     //PT2
            /* X */ roofWidth/2.f,   /* Y */    roofHeight + wheelDiameter / 2  + wheelGap + doorHeight, /* Z */  roofLength/2      //PT3
            );

    roofMesh.getTexCoords().addAll(
            0, 0,  //  t0
            1, 0,  //  t1
            0, 1,  //  t2
            1, 1   //  t3
            );

    roofMesh.getFaces().addAll(
            1,1, 0,0,2,2,
            3,3, 1,2 ,2,1
            );

After Creating the mesh I'm creating a new MeshView object

        meshViewMap.put("roof",          new MeshView(roofMesh));

I have also applied a Material to the MeshViews:

private void setTexColor(Shape3D shape, Color c, String imagePath )
{
    PhongMaterial pm = new PhongMaterial();
    pm.setDiffuseColor(c);
    pm.setSpecularColor(c);
    shape.setMaterial(pm);
}

These are the Cylinder that you can see in the image:

    //Create Axles
            Cylinder frontCylinder = new Cylinder(0.5, bodyWidth);
            Cylinder rearCylinder = new Cylinder(0.5, bodyWidth);
            PhongMaterial cylinderMat = new PhongMaterial();
            cylinderMat.setDiffuseColor(Color.BLACK);
            cylinderMat.setSpecularColor(Color.BLACK);

            frontCylinder.setMaterial(cylinderMat);
            rearCylinder.setMaterial(cylinderMat);

            frontCylinder.setRotate(90);
            rearCylinder.setRotate(90);
            frontCylinder.setTranslateZ( 0.7f * (bodyLength/2 + hoodLength/2));
            rearCylinder.setTranslateZ( -0.4f * (bodyLength/2 + hoodLength/2));

            frontCylinder.setTranslateY(wheelDiameter/2);
            rearCylinder.setTranslateY(wheelDiameter/2);
            this.getChildren().add(frontCylinder);
            this.getChildren().add(rearCylinder);

I have tried to set the opacity to 1 even though it is the default value.

Java Version 8.0.121-b13

Enosh Cohen
  • 369
  • 2
  • 11
  • 4
    Did you enable the depth buffer (https://docs.oracle.com/javase/8/javafx/api/javafx/scene/Scene.html#Scene-javafx.scene.Parent-double-double-boolean-) ? – fabian Mar 31 '17 at 15:34
  • THANK YOU @fabian! you saved me many hours of frustration ! Just needed to add the flag in the Scene constructor! I tried to find a way to change this property after creating the object and it seems that there is no other way to change this feature but the constructor – Enosh Cohen Mar 31 '17 at 15:55

1 Answers1

3

By default, a JavaFX Scene doesn't include a depth buffer. When used for 3D, this may result in weird Escherian artifacts where objects or surfaces farther from the camera are drawn on top of those closer to the camera.

An application may request depth buffer support or scene anti-aliasing support at the creation of a Scene. A scene with only 2D shapes and without any 3D transforms does not need a depth buffer nor scene anti-aliasing support.

To enable the depth buffer, use one of the constructors that takes a boolean depthBuffer argument.

For a SubScene, the corresponding constructor also requires a SceneAntialiasing argument. (The default value would be SceneAntialiasing.DISABLED.)

(Based on Fabian's comment, for those who don't look closely at comments.)

David Moles
  • 48,006
  • 27
  • 136
  • 235