I've added transform controls to a sphere, so it can be moved around my scene. I have also added orbit controls to pan around my scene, which works fine. When the scene is first loaded up there is no lag and it works great, but as soon as I move the sphere, there is significant lag.
I can deal with this, but what I really want to fix is that even once I deselect the sphere and am not moving it anymore, there continues to be lag when just using the orbit controls.
- Why does the lag continue even after I am not actively using the transform controls?
var renderer, scene, camera, controls;
init();
var geometry = new THREE.SphereGeometry( .025, 5, 5 );
var material = new THREE.MeshPhongMaterial( {color: 0xffffff} );
var sphere = new THREE.Mesh( geometry, material );
addControls(sphere,"translate");
scene.add(sphere);
render();
function addControls(object, type) {
var transformControl = new THREE.TransformControls( camera, renderer.domElement );
transformControl.addEventListener( 'change', render );
transformControl.addEventListener( 'dragging-changed', function ( event ) {
controls.enabled = ! event.value;
} );
transformControl.attach( object );
transformControl.setMode( type );
transformControl.setSpace( "local" );
scene.add( transformControl );
}
function init() {
// Renderer
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(WIDTH, HEIGHT);
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(0xffffff);
document.body.appendChild(renderer.domElement);
// Camera
camera = new THREE.PerspectiveCamera(100, WIDTH / HEIGHT, 0.1, 1000);
camera.position.set(500, 0, 0);
// Orbit Controls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.maxDistance = 5;
// Scene
scene = new THREE.Scene();
}
function render() {
renderer.render(scene, camera);
requestAnimationFrame(render);
}
I have tried removing the transform controls from the scene by placing the following inside addControls():
window.addEventListener( 'keypress', function ( event ) {
transformControl.detach( object );
transformControl.dispose();
scene.remove(transformControl);
} );
After I had moved the sphere around a bit, I would press a key and the transform controls would disappear, but the lag continued. I expected that there may be some lag when moving the sphere, but that this would go away when it was not being moved.
Instead, there continues to be lag even when I am only using orbit controls.
- How can I minimize this lag?