简体   繁体   中英

Combining JavaFX 3D Shapes seem transparent

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

在此输入图像描述 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

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM