# Dynamic BufferGeometry disappears when re-setting the position buffer attribute

**URL:** <https://discourse.threejs.org/t/dynamic-buffergeometry-disappears-when-re-setting-the-position-buffer-attribute/49274>\
**Category:** Questions\
**Tags:** geometry\
**Created:** [March 18, 2023, 3:27pm UTC](https://discourse.threejs.org/t/dynamic-buffergeometry-disappears-when-re-setting-the-position-buffer-attribute/49274 "2023-03-18T15:27:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xortdsc](https://avatars.discourse-cdn.com/v4/letter/x/258eb7/32.png) [@xortdsc](https://discourse.threejs.org/u/xortdsc)\
**Post date:** [March 18, 2023, 3:27pm UTC](https://discourse.threejs.org/t/dynamic-buffergeometry-disappears-when-re-setting-the-position-buffer-attribute/49274/1 "2023-03-18T15:27:04Z")

</div>

I create a BufferGeometry and set its attributes like so:

```javascript
        this.clearGroups();
        this.addGroup(groupStart, groupCount, 0);
        const indexAttribute = new BufferAttribute(new Uint32Array(indices), 1);
        this.setIndex(indexAttribute);
        const positionAttribute = new BufferAttribute(new Float32Array(verticesCount * 3), 3);
        this.setAttribute('position', positionAttribute);

```

Now occasionally I recall this setup code if the number of vertices changes. The strange thing is that the mesh using this BufferGeometry will disappear if the number of vertices INCREASES to be more than the initial number. Even more strangely so, it will still be visible in shadow-maps and only the direct rendering will disappear.

However, if I add

```javascript
        for (const key of Object.keys(this.attributes)) {
            this.deleteAttribute(key);
        }

```

next to the

```javascript
        this.clearGroups();

```

call, effectively removing all attributes first before setting a new one, it will work properly.  
What I suspect is, that the new position-buffer-attribute never gets re-set (that’s why it works when the number of vertices DECREASES (still working on the original buffer), but won’t work when INCREASING them.  
Is this intended behaviour ?

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 20, 2023, 10:29am UTC](https://discourse.threejs.org/t/dynamic-buffergeometry-disappears-when-re-setting-the-position-buffer-attribute/49274/2 "2023-03-20T10:29:21Z")

</div>

> [@xortdsc](#):
>
> Now occasionally I recall this setup code if the number of vertices changes.

It is not intended to overwrite existing attributes with different sizes. Call `dispose()` on the old geometry and then create a new one.

Ideally, you avoid all of this and create a geometry with sufficiently large buffers right from the beginning. If you don’t need all of its space, use `setDrawRange()` to define what parts of the buffer should be rendered.

---

<div class="post-metadata">

**Author:** ![xortdsc](https://avatars.discourse-cdn.com/v4/letter/x/258eb7/32.png) [@xortdsc](https://discourse.threejs.org/u/xortdsc)\
**Post date:** [March 20, 2023, 5:44pm UTC](https://discourse.threejs.org/t/dynamic-buffergeometry-disappears-when-re-setting-the-position-buffer-attribute/49274/3 "2023-03-20T17:44:41Z")

</div>

Okay. Do you see a problem with the workaround of removing the attributes before re-setting them as stated in the workaround ?  
This way I could handle everything inside the custom geometry class and the user of that class doesn’t need to worry about recreating it under certain conditions.

---

<div class="post-metadata">

**Author:** ![Mugen87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/mugen87/32/5645_2.png) [@Mugen87](https://discourse.threejs.org/u/Mugen87)\
**Post date:** [March 20, 2023, 6:02pm UTC](https://discourse.threejs.org/t/dynamic-buffergeometry-disappears-when-re-setting-the-position-buffer-attribute/49274/4 "2023-03-20T18:02:08Z")

</div>

> [@xortdsc](#):
>
> Do you see a problem with the workaround of removing the attributes before re-setting them as stated in the workaround ?

I don’t recommend doing changing a geometry configuration after the initial render. Ideally, you set attribute once. This will be the most performant solution.

---

<div class="post-metadata">

**Author:** ![trusktr](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/trusktr/32/13726_2.png) [@trusktr](https://discourse.threejs.org/u/trusktr)\
**Post date:** [January 27, 2025, 1:33am UTC](https://discourse.threejs.org/t/dynamic-buffergeometry-disappears-when-re-setting-the-position-buffer-attribute/49274/5 "2025-01-27T01:33:48Z")

</div>

Looks like this might just be a missing feature (hoping it isn’t a limitation of webgl?). I asked a new question here:

> [@Error: THREE.WebGLAttributes: Resizing buffer attributes is not supported](https://discourse.threejs.org/t/error-three-webglattributes-resizing-buffer-attributes-is-not-supported/77109):
>
> I have been wondering if I could update an attribute buffer. I tried here, but after 2 seconds when the resize attempt happens, you’ll see this in console: Error: THREE.WebGLAttributes: The size of the buffer attribute's array buffer does not match the original size. Resizing buffer attributes is not supported. Is this not supported due to technical limitations with webgl, or is it just not implemented in Three.js yet? If the latter, is it open for pull request?
