Trouble reconstructing geometry from web worker

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 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 ?

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.

3 Likes

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

1 Like