GreasedLineMesh .setPoints() won't create additional lines?

I’m not sure if this is a bug or by design. I can create a GreasedLineMesh with an arbitrary number of line segments and it renders fine:

However, if I use the same array of points and pass it to setPoints on an existing line that only has two line segments it only draws the first two line segments of my array.

For each additional set of points that I add to the original line, setPoints will increase the number of line segments it draws:

Does the design of setPoints presume that you never exceed the original number of lines? Or is this a bug?

1 Like

cc @roland

2 Likes

@apowers313

When you change the number of points in the line instance GreasedLine doesn’t know anything about the widths of the newly added lines. The existing lines created using the builder function CreateGreasedLine are displayed because the widths table is filled upon calling this function.

It actually updates the mesh (wireframe = true on the screenshot so you can see the missing lines thinner) but the widths table for the newly added lines is filled with zeros and this is why the newly added lines are not rendered.

It happens here:

It is and it is not a bug.

I really don’t know what led me to set the widths to 0 :crazy_face: A better option could be to set it to the default width value of 1. Or maybe the setPoints function should have a second parameter for the widths. So this can be considered as a bug.

A workaround is to set the widths manually:

    const widths = new Array(myPoints.length * 4).fill(1) 
    // array length must be number of lines * 4
    line2.widths = widths
    line2.setPoints(myPoints);

Thank you for reporting the issue. I’ll fix this.

1 Like

Thanks! You’re awesome. :vulcan_salute:

1 Like

A compliment in return: yoo too :wink:

:vulcan_salute:

1 Like

I’ve decided not to automatically set the widths if the number of the points changes when the setPoints function is used. setPoints was intended to use when you want to move the existing line points. If you move the lines each frame, updating the widths will generate unnecessary overhead and the setPointswill still doesn’t know anything about the the newly added lines widths.

You’ll have to update the widths manually if you change the number of points.

However I’ve added a new helper function GetPointsCount(points: number[][]): number which will count the number of points. You can pass this value into the CompleteGreasedLineWidthTable function which will create the missing entries:

https://doc.babylonjs.com/typedoc/modules/BABYLON#CompleteGreasedLineWidthTable

https://doc.babylonjs.com/typedoc/enums/BABYLON.GreasedLineMeshWidthDistribution

So your code will look like (sets the new lines upper and lower width to 2):

    const pointsCount = BABYLON.GetPointsCount(myPoints);
    line2.widths = BABYLON.CompleteGreasedLineWidthTable(
        pointsCount, 
        line2.widths, 
        BABYLON.GreasedLineMeshWidthDistribution.WIDTH_DISTRIBUTION_START, 
        2, 
        2);
    line2.addPoints(myPoints);

Or you can create the widths table manually according to your needs.

3 Likes