Greased line disposal not working as expected

Hello, I’m a long time user of Babylon but first time poster here :waving_hand:

I am having some issues using greased lines in my project. I think I found a bug or at least unexpected behavior. Please point me in the right direction if this is not a bug.

Long story short, I am using greased lines in multiple different places in my project, and I use BABYLON.CreateGreasedLine to create the lines. Below is simplified version of what I’m trying to do. (You can find the link to the playground further below).

    const points1 = [-1, 0, 0, 1, 0, 0];
    const line1 = BABYLON.CreateGreasedLine("line1", { points: points1 }, {
        color: BABYLON.Color3.Red(),
        sizeAttenuation: true,
        width: 5,
        materialType: BABYLON.GreasedLineMeshMaterialType.MATERIAL_TYPE_SIMPLE,
    });

    const points2 = [-1, 1, 0, 1, 1, 0];
    const line2 = BABYLON.CreateGreasedLine("line2", { points: points2 }, {
        color: BABYLON.Color3.Green(),
        sizeAttenuation: true,
        width: 5,
        materialType: BABYLON.GreasedLineMeshMaterialType.MATERIAL_TYPE_SIMPLE,
    });

This all works fine, until I want to dispose one of the lines.
When I call line1.dispose() the line is disposed as expected. The material that was created for line1 is not disposed, this is also expected.
However, I want to dispose the material.
To dispose the material, we would need to do the following:

line1.dispose(/*doNotRecurse*/ false, /*disposeMaterialAndTextures*/ true)
// Or simply
line1.material.dispose()
line1.dispose()

When I do this, line2 also disappears. However, both the node and material for line2 can still be found in the Babylon inspector. I cannot find any linkage between the materials for line1 and line2 that would explain this.

Here is a minimal reproducible example playground: https://playground.babylonjs.com/#ME2NKI#6

At the start we have line1 (red) and line2 (green)

Pressing dispose line behaves as expected. The material for line1 is still there, and line2 is untouched.

Starting from scratch and pressing dispose line + material removes both lines, but the inspector shows that the node and material for line2 still exists.

I would expect line2 to still be rendered in this case.
Any help would be greatly appreciated :smiling_face:

1 Like

Hello and welcome ! :slight_smile:


I had a look, and indeed I agree it seems to be a bug. Let’s ping @roland which is the guy behind the GreasedLine stuff. I just reviewed the source code and I think I found the issue :

  • The dispose function of the GreasedLineSimpleMaterial disposes the colorsTexture
  • The colorsTexture is common to both instances (which is weird :thinking: )
const m1 = line1.material;
const m2 = line2.material;
console.log(m1.colorsTexture === m2.colorsTexture);

This will return true while I think it should not.

Cloning the colorsTexture will solve the issue by the way :

But still, I think a fix is necessary on the source code side
@roland, I tried commenting the dispose line this._colorsTexture?.dispose(); it does solve the issue, but I think the fix should be about not having the same reference on both materials

For example, adding a .clone() on this line does work :

2 Likes

Hi @Thor! Welcome to the forum!

@Tricotou thanks for investigating :wink:

Actually, only BABYLON.GreasedLineMeshMaterialType.MATERIAL_TYPE_SIMPLE is affected by this bug. The issue lies in the material’s initialization code. The texture should only be created when the colors property is used during the line’s creation.

2 Likes
3 Likes