WebGPU r176: Issue with Grid-Making Routine

I ran into an interesting problem with WebGPU r176 and a custom grid-making routine that still works fine with WebGL2 r177.

With this WebGPU r176 program, the airplane incorrectly appears behind the grid. However, with this WebGPU r175 program and this WebGL2 r177 program, the airplane correctly appears in front of the grid.

The grid is created using this interesting subroutine (which I did not write):

function ToQuads(g) {
    let p = g.parameters;
    let segmentsX = (g.type == "TorusBufferGeometry" ? p.tubularSegments : p.radialSegments) || p.widthSegments || p.thetaSegments || (p.points.length - 1) || 1;
    let segmentsY = (g.type == "TorusBufferGeometry" ? p.radialSegments : p.tubularSegments) || p.heightSegments || p.phiSegments || p.segments || 1;
    let indices = [];
    for (let i = 0; i < segmentsY + 1; i++) {
        let index11 = 0;
        let index12 = 0;
        for (let j = 0; j < segmentsX; j++) {
            index11 = (segmentsX + 1) * i + j;
            index12 = index11 + 1;
            let index21 = index11;
            let index22 = index11 + (segmentsX + 1);
            indices.push(index11, index12);
            if (index22 < ((segmentsX + 1) * (segmentsY + 1) - 1)) {
                indices.push(index21, index22);
            }
        }
        if ((index12 + segmentsX + 1) <= ((segmentsX + 1) * (segmentsY + 1) - 1)) {
            indices.push(index12, index12 + segmentsX + 1);
        }
    }
    g.setIndex(indices);
}

Can this subroutine be saved? Or are there better alternatives with WebGPU?

Addition: I have confirmed that this subroutine still works with WebGL r177.

This subroutine seems like it could be saved. In JavaScript though, one convention is to use capital letters for classes and lowercase for functions so i’d save it as toQuads(g){}

1 Like

Thanks. I was not aware of that convention.

I have no idea how the subroutine works, especially the role of the TorusBufferGeometry. (I seem to recall reading that you can use any geometry and that appears to be true.) I think I got it from prisoner849, but he may have gotten it from somewhere else. It was offered as a way to remove the diagonals from planes when displaying them in a wireframe view.