如何在 javafx 中 "add" 两个三角形网格在一起?

How to "add" two Triangle Meshes together in javafx?

我正在制作由两个函数生成的体积的 3D 模型:一个作为基础(在 x 轴的交点),另一个作为 volume.This 的高度,通过近似实现小零件的体积,绘制该网格,然后将所有网格加在一起。但是,我不知道如何将网格添加到一起。

这是我的网格创建器:

 private TriangleMesh createVolume(double start, double end, double numSteps, PolynomialFunction base, PolynomialFunction height) {
    TriangleMesh m = new TriangleMesh();

    double stepSize = (end-start)/numSteps;

    for(double i = start; i < end; i += stepSize) {

        double x = i;
        double x2 = x+stepSize;
        double gx = height.value(x);
        double gx2 = height.value(x2);
        double fx = base.value(x);
        double fx2 = base.value(x2);


        TriangleMesh t = createVolumeSection(x,x2,gx,gx2,fx,fx2);
        m.getPoints().addAll(t.getPoints());
        m.getTexCoords().addAll(t.getTexCoords());
        m.getFaces().addAll(t.getFaces());
    }

    return m;
}


private TriangleMesh createVolumeSection(double xVal, double x2Val, double gxVal, double gx2Val,
                                         double fxVal, double fx2Val){
    TriangleMesh m = new TriangleMesh();
    float x = ((float)xVal) ;
    float x2 = ((float)x2Val);
    float gx = ((float)gxVal);
    float gx2 = ((float)gx2Val);
    float fx = ((float)fxVal);
    float fx2 = ((float)fx2Val);

    //create Points
    m.getPoints().addAll(
            x,  0,  0,      // A = 0
            x,  0,  gx,     // B = 1
            x2, 0,  0,      // C = 2
            x2, 0,  gx2,    // D = 3
            x,  fx, 0,      // E = 4
            x,  fx, gx,     // F = 5
            x2, fx2,0,      // G = 6
            x2, fx2,gx2     // H = 7
    );

    m.getTexCoords().addAll(0,0);

    m.getFaces().addAll(
            0 , 0 , 1 , 0 , 3 , 0 ,     // A-B-D
            0 , 0 , 3 , 0 , 2 , 0 ,     // A-D-C
            0 , 0 , 2 , 0 , 6 , 0 ,     // A-C-G
            0 , 0 , 6 , 0 , 4 , 0 ,     // A-G-E
            0 , 0 , 4 , 0 , 1 , 0 ,     // A-E-B
            1 , 0 , 4 , 0 , 5 , 0 ,     // B-E-F
            1 , 0 , 5 , 0 , 7 , 0 ,     // B-F-H
            1 , 0 , 7 , 0 , 3 , 0 ,     // B-H-D
            3 , 0 , 7 , 0 , 6 , 0 ,     // D-H-G
            3 , 0 , 6 , 0 , 2 , 0 ,     // D-G-C
            6 , 0 , 7 , 0 , 5 , 0 ,     // G-H-F
            6 , 0 , 5 , 0 , 4 , 0       // G-F-E
    );

    return m ;
}

本应创建一系列直角梯形棱镜,但最终只绘制了系列中的第一个网格。

有人可以帮忙吗?

提前致谢。

您的方法几乎是正确的,但是您未能更新人脸数组的信息。

随着您添加的每个体积部分,您将新的顶点添加到最终的网格中,因此面数组上的顶点索引应该相应地移动。为此,保留为 n-1 卷添加的总点数的计数器:

private int points=0;

private TriangleMesh createVolume(double start, double end, double numSteps, Function<Number,Number> base, Function<Number,Number> height) {
    ...
    TriangleMesh t = createVolumeSection(x,x2,gx,gx2,fx,fx2);
    m.getPoints().addAll(t.getPoints());
    points=m.getPoints().size()/3;
    ...
}

然后修改faces数组:

private TriangleMesh createVolumeSection(double xVal, double x2Val, double gxVal, double gx2Val,
                                         double fxVal, double fx2Val){
    TriangleMesh m = new TriangleMesh();
    ...
    m.getFaces().addAll(
            points+0 , 0 , points+1 , 0 , points+3 , 0 ,     // A-B-D
            points+0 , 0 , points+3 , 0 , points+2 , 0 ,     // A-D-C
            points+0 , 0 , points+2 , 0 , points+6 , 0 ,     // A-C-G
            points+0 , 0 , points+6 , 0 , points+4 , 0 ,     // A-G-E
            points+0 , 0 , points+4 , 0 , points+1 , 0 ,     // A-E-B
            points+1 , 0 , points+4 , 0 , points+5 , 0 ,     // B-E-F
            points+1 , 0 , points+5 , 0 , points+7 , 0 ,     // B-F-H
            points+1 , 0 , points+7 , 0 , points+3 , 0 ,     // B-H-D
            points+3 , 0 , points+7 , 0 , points+6 , 0 ,     // D-H-G
            points+3 , 0 , points+6 , 0 , points+2 , 0 ,     // D-G-C
            points+6 , 0 , points+7 , 0 , points+5 , 0 ,     // G-H-F
            points+6 , 0 , points+5 , 0 , points+4 , 0       // G-F-E
    );

    return m ;
}