To optimize performance, utilize the existing geometry buffers:
geometryGroup.__vertexArray
geometryGroup.__normalArray
These buffers are initialized in WebGLRenderer.initMeshBuffers
.
Here's how it operates:
Begin by creating a Worker and importing three.js using
importScripts("/js/lib/mrdoob-three.js-35db421/build/three.js");
In the worker, instantiate another copy of the geometry for processing.
Trigger an initial rendering in the main thread with renderer.render(scene, camera);
to make the buffers accessible...
Transfer the necessary buffers from the main thread to the worker
Perform the intensive geometry operations in the worker thread
Manually fill the required buffers (see WebGLRenderer.setMeshBuffers)
for example:
var vertexArray = new Float32Array(vertexBuffer);
var normalArray = new Float32Array(normalBuffer);
var vertices : Array = geometry.vertices;
var obj_faces : Array = geometry.faces;
var offset = 0;
var offset_normal = 0;
for (f in 0...obj_faces.length) {
var face = obj_faces[ f ];
var v1 = vertices[ face.a ];
var v2 = vertices[ face.b ];
var v3 = vertices[ face.c ];
var v4 = vertices[ face.d ];
vertexArray[ offset ] = v1.x;
vertexArray[ offset + 1 ] = v1.y;
vertexArray[ offset + 2 ] = v1.z;
vertexArray[ offset + 3 ] = v2.x;
vertexArray[ offset + 4 ] = v2.y;
vertexArray[ offset + 5 ] = v2.z;
vertexArray[ offset + 6 ] = v3.x;
vertexArray[ offset + 7 ] = v3.y;
vertexArray[ offset + 8 ] = v3.z;
vertexArray[ offset + 9 ] = v4.x;
vertexArray[ offset + 10 ] = v4.y;
vertexArray[ offset + 11 ] = v4.z;
offset += 12;
}
Send the buffers back to the main thread and update the geometry there:
var geometryGroup = mesh.geometry.geometryGroupsList[0];
var _gl = renderer.context;
_gl.bindBuffer(_gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
_gl.bufferData(_gl.ARRAY_BUFFER, transferVertexArray, _gl.DYNAMIC_DRAW );
This method is effective for intricate geometry operations. Understanding how the buffers are generated and utilized by WebGLRenderer
is crucial.