One key feature lacking in OBJLoader
is indexing, which could significantly enhance performance by reducing the number of vertices needed. For example, exporting a cube from Blender resulted in 36 indices (6 vertices per cube face, 6 faces = 36 vertices), whereas with indexing, the same cube would only require 24 vertices.
While indexing does increase the size of the object due to the additional indices, the savings can be substantial for complex meshes where many faces share the same vertices. Expanding on the intricacies of indexing is beyond the scope of this response, but here's a simple example using a plane:
var planeGeo = new THREE.BufferGeometry();
planeGeo.addAttribute("position", new THREE.BufferAttribute(new Float32Array([
-10, 10, 0,
-10, -10, 0,
10, -10, 0,
10, 10, 0
]), 3));
planeGeo.addAttribute("normal", new THREE.BufferAttribute(new Float32Array([
0, 0, 1,
0, 0, 1,
0, 0, 1,
0, 0, 1
]), 3));
planeGeo.setIndex(new THREE.BufferAttribute(new Float32Array([
// triangle 1
0, 1, 2,
// triangle 2
3, 2, 1
]), 1));
By reusing vertex indices, we can minimize duplication in the position
buffer.
It's important to note that indices should correspond to vertex/normal pairs. If a vertex has multiple normals, you'll still need separate vertex values in the position
buffer.
To leverage this optimization, consider modifying OBJLoader
to generate indexed BufferGeometry
objects or develop a new parsing function altogether. The latter approach may offer greater flexibility, especially when incorporating support for additional file formats.