I am trying to implement rotation of an object in my project when the "x" key is pressed using a Matrix4. The current code I have rotates the object, however, I am facing an issue where the rotation speed increases over time the longer I hold down the "x" key. How can I resolve this issue?
var r1 = 1, r5 = 1, r9 = 1, r2 = 0, r3 = 0, r4 = 0, r6 = 0, r7 = 0, r8 = 0;
var Rx = 0, Ry = 0, Rz = 0;
var rotation = new THREE.Matrix4();
function keyDown(event){
if(event.key == 'x'){
mesh.matrixAutoUpdate = false;
Rx += Math.PI/180 //1 degree per key press
r5 = Math.cos(Rx);
r6 = -Math.sin(Rx);
r8 = Math.sin(Rx);
r9 = Math.cos(Rx);
rotation.set( r1, r2, r3, 0,
r4, r5, r6, 0,
r7, r8, r9, 0,
0, 0, 0, 1 );
mesh.applyMatrix4(rotation);
}
if(event.key == 'y'){
}
if(event.key == 'z'){
}
}
let loader = new THREE.STLLoader();
var material = new THREE.MeshLambertMaterial({color: 0x818181});
loader.load('STL/test.stl', function (geometry) {
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
});
window.addEventListener("keypress", keyDown);