Repro: https://playground.babylonjs.com/#H1LRZ3#801
If you create a greased line using materialType:2, then dispose(false,true) and create again, the greased line is no longer visible. Works fine for other material types. wai?
Thanks for reporting, it’s a bug. Here’s the fix:
master ← Popov72:fix/greasedline-simple-material-dispose
opened 10:27AM - 07 Apr 26 UTC
## Description
Fixes a bug where creating a `GreasedLine` with `materialType: 2… ` (`MATERIAL_TYPE_SIMPLE`), disposing it, and creating a new one resulted in the second line being invisible.
**Forum report:** https://forum.babylonjs.com/t/63082
**Playground repro:** https://playground.babylonjs.com/#H1LRZ3#801
## Root Cause
`GreasedLineSimpleMaterial.dispose()` unconditionally called `this._colorsTexture?.dispose()`. When no custom colors are provided, `_colorsTexture` holds a reference to the globally shared `GreasedLineMaterialDefaults.EmptyColorsTexture`. Disposing it turned the static reference into a **zombie** — the object is non-null but its internal GPU texture is released.
On the next `CreateGreasedLine` call, `PrepareEmptyColorsTexture()` checks `if (!EmptyColorsTexture)` — since the reference is non-null (just disposed), it returns the dead texture. The shader binds a destroyed texture and the line renders as invisible.
Material types 0 (`MATERIAL_TYPE_STANDARD`) and 1 (`MATERIAL_TYPE_PBR`) are unaffected because `GreasedLinePluginMaterial` never assigns the shared texture to its own `colorsTexture` field.
## Fix
Guard `dispose()` and `setColors()` in `GreasedLineSimpleMaterial` to skip disposing `_colorsTexture` when it is the shared `EmptyColorsTexture`. The shared texture is already cleaned up via `engine.onDisposeObservable` when the engine itself is disposed.
## Changes
| File | Change |
|------|--------|
| `packages/dev/core/src/Materials/GreasedLine/greasedLineSimpleMaterial.ts` | Guard `dispose()` and `setColors()` against disposing shared texture |
| `packages/dev/core/test/unit/Meshes/greasedLine.test.ts` | Regression tests for dispose/recreate cycle |
## Testing
- [x] New unit tests verify the shared `EmptyColorsTexture` survives material dispose
- [x] All 2400 existing unit tests pass
4 Likes
Tested in v9.3.1. Working now, thks!