# Trouble reconstructing geometry from web worker

**URL:** <https://discourse.threejs.org/t/trouble-reconstructing-geometry-from-web-worker/21423>\
**Category:** Questions\
**Tags:** webworker\
**Created:** [December 9, 2020, 1:45pm UTC](https://discourse.threejs.org/t/trouble-reconstructing-geometry-from-web-worker/21423 "2020-12-09T13:45:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![felixmariotto](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/felixmariotto/32/5928_2.png) [@felixmariotto](https://discourse.threejs.org/u/felixmariotto)\
**Post date:** [December 9, 2020, 1:45pm UTC](https://discourse.threejs.org/t/trouble-reconstructing-geometry-from-web-worker/21423/1 "2020-12-09T13:45:34Z")

</div>

Hi,

I need to transfer large geometries from a web worker to the main thread. I don’t have any control of what geometries are sent to me, but it can be quite large ( up to ~3 million polygons, with normals and UVs ). I merge the geometries received in the worker before to send it to the main thread.

this is how I send the geometry to the main thread ( after merging ) :

```
// in the web worker thread

const arrayBuffers = [];

for ( let attributeName of Object.keys( geometry.attributes ) ) {

	arrayBuffers.push( geometry.attributes[attributeName].array.buffer )

}

postMessage( { geometry }, arrayBuffers );

```

Notice that I use the [Transferable](https://developer.mozilla.org/en-US/docs/Web/API/Transferable) functionality for the array buffers, in order to avoid cloning this large chunk of data, because I would get out of memory errors otherwise.

Then in the main thread, I listen for the worker message, and recontruct a geometry this way :

```
// in the main thread

worker.onmessage = (e) => {

	const shallowGeometry = e.data.geometry;

	//

	const geometry = new THREE.BufferGeometry();

	for ( let attributeName of Object.keys( shallowGeometry.attributes ) ) {

		const shallowAttribute = shallowGeometry.attributes[attributeName];

		const attribute = new THREE.BufferAttribute(
			shallowAttribute.array,
			shallowAttribute.itemSize,
			false
		);

		geometry.setAttribute( attributeName, attribute );

	}

	//

	geometry.groups = shallowGeometry.groups;

	//

	const mesh = new THREE.Mesh( geometry, material );

	scene.add( mesh )

}

```

When testing on my laptop, I don’t have any problem ( but the rendering of 3M polygons ). However when testing on my mobile, I get this error :

```
[.WebGL-0xd1474c00]GL ERROR :GL_INVALID_OPERATION : glDrawArrays: attempt to access out of range vertices in attribute 0

```

Strangely, this error only show up for large models. Smaller models ( ~50 000 polygons ) work fine, no errors.

I tried to look around for some clue, but couldn’t fix this on my own so far… I seems that I’m doing something wrong with how I create my bufferAttributes.

Chrome for Android version 86.0.4240.198

Anybody can help me ?

---

<div class="post-metadata">

**Author:** ![felixmariotto](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/felixmariotto/32/5928_2.png) [@felixmariotto](https://discourse.threejs.org/u/felixmariotto)\
**Post date:** [December 9, 2020, 2:54pm UTC](https://discourse.threejs.org/t/trouble-reconstructing-geometry-from-web-worker/21423/2 "2020-12-09T14:54:14Z")

</div>

OK I found what my issue was, and it’s not related to the transfering of data from the web worker to the main thread.

The big FBX file I was testing has one geometry without `uv` attribute, so when I merged all the geometries, the `uv` attribute count was not the same as the `position` and `normal` attribute count. I assume it’s what caused the problem, because now that I add a `uv` attribute to any geometry that doesn’t have one, I don’t have the error message anymore.

---

<div class="post-metadata">

**Author:** ![donmccurdy](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.threejs.org/donmccurdy/32/33209_2.png) [@donmccurdy](https://discourse.threejs.org/u/donmccurdy)\
**Post date:** [December 9, 2020, 7:57pm UTC](https://discourse.threejs.org/t/trouble-reconstructing-geometry-from-web-worker/21423/3 "2020-12-09T19:57:29Z")

</div>

Also note that if your model has an `.index` property or morph targets, they’d need to be transferred too. Otherwise, looks right!
