I'm using three.js and instancing (as in this example), but I'm having the same problem others have reported: the objects are randomly clipped and keep disappearing from the camera
- Mesh suddenly disappears in three.js. Clipping?
- Three.js buffergeometry disappears after moving camera to close
The proposed workarounds are to set
my_instanced_object.frustumCulled = false;
but this means rendering every single object per each frame, and with a lot of objects this is killing my framerate.
What are my alternatives to this? How can I have proper frustum culling if I'm using instancing?
I'm adding the code I'm using in case someone wanted to take a look
var geometry = new THREE.InstancedBufferGeometry();
geometry.maxInstancedCount = all_meshes_data.length;
geometry.addAttribute( 'position', mesh.geometry.attributes.position );
geometry.addAttribute( 'normal', mesh.geometry.attributes.normal );
geometry.addAttribute( 'uv', mesh.geometry.attributes.uv );
var offsets = new THREE.InstancedBufferAttribute( new Float32Array( all_meshes_data.length * 3 ), 3, 1 );
for ( var i = 0, ul = all_meshes_data.length; i < ul; i++ ) { // Populate all instancing positions (where to spawn instances)
offsets.setXYZ( i, all_meshes_data[i].x, all_meshes_data[i].y, all_meshes_data[i].z );
}
geometry.addAttribute( 'offset', offsets );
var instanceMaterial = new THREE.RawShaderMaterial( {
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent,
transparent: true
} );
geometry.computeVertexNormals();
geometry.boundingSphere = new THREE.Sphere( new THREE.Vector3(), 50 ); // Not working, it works just for a 0;0;0 world positioned mesh that is the 'base' of all of the instanced ones
var instanced_mesh = new THREE.Mesh( geometry, instanceMaterial );
//instanced_mesh.frustumCulled = false; // Works, but the scene becomes very slow (rendering everything even if not in sight)
scene.add( instanced_mesh );