Utilizing these two functions enables me to rotate objects within my three.js scenes:
// Rotate an object around an arbitrary axis in object space
function rotateAroundObjectAxis(object, axis, radians) {
rotationMatrix = new THREE.Matrix4();
rotationMatrix.makeRotationAxis(axis.normalize(), radians);
object.matrix.multiplySelf(rotationMatrix); // post-multiply
object.rotation.getRotationFromMatrix(object.matrix, object.scale);
delete rotationMatrix;
}
// Rotate an object around an arbitrary axis in world space
function rotateAroundWorldAxis(object, axis, radians) {
rotationMatrix = new THREE.Matrix4();
rotationMatrix.makeRotationAxis(axis.normalize(), radians);
rotationMatrix.multiplySelf(object.matrix); // pre-multiply
object.matrix = rotationMatrix;
object.rotation.getRotationFromMatrix(object.matrix, object.scale);
delete rotationMatrix;
}
rotationMatrix is a global variable that I utilize. Despite not having encountered any issues thus far, my lack of Javascript and web app development expertise led me to talk to someone who suggested that creating new objects with each function call, particularly since they are called once per frame at times, could pose concerns regarding garbage collection efficiency. Should I worry about the garbage collector's ability to manage this over time? I am aware that the delete keyword functions differently than in C++ as it only removes references. Can using it at the end of each function help alleviate potential issues? Additionally, what other measures can I take to improve the efficiency of these functions, allowing them to be utilized frequently without causing performance degradation or excessive memory consumption by the browser.