Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javafxfix";看穿;三角形网格_Java_Javafx - Fatal编程技术网

javafxfix";看穿;三角形网格

javafxfix";看穿;三角形网格,java,javafx,Java,Javafx,所以我将glTF格式导入JavaFX。我正在经历一种奇怪的效果,我可以通过三角形网格看到它的其他部分/通过它看到其他网格。 几张照片 如果有人知道这个问题的解决方案,那就太棒了。我已经试过使用CullFace.Front和CullFace.Back 以下是生成三角形网格的代码: package fx; import gltf.GLTFParseException; import gltf.mesh.GLTFMesh; import gltf.mesh.GLTFMeshPrimitive; i

所以我将glTF格式导入JavaFX。我正在经历一种奇怪的效果,我可以通过三角形网格看到它的其他部分/通过它看到其他网格。 几张照片

如果有人知道这个问题的解决方案,那就太棒了。我已经试过使用CullFace.Front和CullFace.Back

以下是生成三角形网格的代码:

package fx;

import gltf.GLTFParseException;
import gltf.mesh.GLTFMesh;
import gltf.mesh.GLTFMeshPrimitive;
import javafx.scene.Group;
import javafx.scene.shape.CullFace;
import javafx.scene.shape.MeshView;
import javafx.scene.shape.TriangleMesh;

import java.util.Arrays;

public class FXglTFMesh extends Group {

public static FXglTFMesh fromGLTFMesh(GLTFMesh mesh) throws GLTFParseException {
    FXglTFMesh fxMesh = new FXglTFMesh();


    GLTFMeshPrimitive[] primitives = mesh.getPrimitives();
    MeshView[] meshViews = new MeshView[primitives.length];
    for (int i = 0;i < meshViews.length; i++){
        meshViews[i] = fromPrimitive(primitives[i]);
    }

    FXglTFMaterial material;

    fxMesh.getChildren().addAll(meshViews);
    return fxMesh;
}

private static MeshView fromPrimitive(GLTFMeshPrimitive primitive) throws GLTFParseException {
    TriangleMesh mesh = new TriangleMesh();
    MeshView view = new MeshView(mesh);
    view.setCullFace(CullFace.BACK);

    //Reading texture coords
    float[][] texCoords = convertArrayToNested(2, 
    primitive.getAttribute().getTexCoord_0().readDataAsFloats());
    mesh.getTexCoords().addAll(primitive.getAttribute().getTexCoord_0().readDataAsFloats());

    if (primitive.getIndices() != null){
        //Mesh is indexed
        mesh.getPoints().addAll(primitive.getAttribute().getPosition().readDataAsFloats());

        int[] indices = primitive.getIndices().readDataAsInts();

        for (int i = 0;i < indices.length; i+=3){
            mesh.getFaces().addAll(indices[i], 0, indices[i+1], 0, indices[i+2], 0);
        }
    } else {
        //Mesh is not indexed
        //Parse the vertices and faces
        float[][] vertices = convertArrayToNested(3, 
primitive.getAttribute().getPosition().readDataAsFloats());

        for (int i = 0;i < vertices.length; i+=3){
            mesh.getPoints().addAll(vertices[i]);
            mesh.getPoints().addAll(vertices[i+1]);
            mesh.getPoints().addAll(vertices[i+2]);
            //add 3 points
            mesh.getFaces().addAll(i,i,  i+1,i+1,  i+2,i+2 ); //Add those three points as face
        }
    }
    //Material
    FXglTFMaterial material = FXglTFMaterial.fromGLTFMaterial(primitive.getMaterial());
    view.setMaterial(material);
    return view;
}

private static float[][] convertArrayToNested(int factor, float[] array){
    float[][] floats = new float[array.length / factor][];

    for (int i = 0;i < floats.length; i++){
        int dataOffset = i * factor;
        floats[i] = Arrays.copyOfRange(array, dataOffset, dataOffset+factor);
    }

    return floats;
}
package-fx;
导入gltf.GLTFParseException;
导入gltf.mesh.GLTFMesh;
导入gltf.mesh.gltfmesh原语;
导入javafx.scene.Group;
导入javafx.scene.shape.CullFace;
导入javafx.scene.shape.MeshView;
导入javafx.scene.shape.TriangleMesh;
导入java.util.array;
公共类FXglTFMesh扩展组{
公共静态FXglTFMesh fromGLTFMesh(GLTFMesh mesh)引发GLTFParseException{
FXglTFMesh fxMesh=新的FXglTFMesh();
GLTFMeshPrimitives[]primitives=mesh.getPrimitives();
MeshView[]MeshView=新的MeshView[primitives.length];
对于(int i=0;i
}


完整代码:(开发分支)

看起来没有使用深度缓冲来确保前面的三角形在顶部渲染。您的代码似乎只是按照网格中存储它们的顺序进行渲染。基于此,@CatCraft959似乎是正确的。您需要创建场景。@CatCraft959非常感谢您解决了这个问题:D@Slaw非常感谢您进一步解释catCraft所说的话。这帮了大忙