简体   繁体   English

jscript垃圾收集器有多快? Three.js矩阵旋转

[英]How fast is the jscript garbage collector? Three.js Matrix Rotations

I am using these two functions in order to rotate my objects in my three.js scenes 我正在使用这两个函数来旋转我的Three.js场景中的对象

    // 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;
    }

where rotationMatrix is a global variable. 其中rotationMatrix是全局变量。 I am unexperienced with Javascript and web app development, but I talked to someone who seemed to convey to me that because sometimes I call these functions once a frame I should be concerned about the fact that it creates new objects each call. 我对Javascript和Web应用程序开发没有经验,但是我与一位似乎向我传达信息的人交谈,因为有时我将这些函数称为一个框架,所以我应该担心它每次调用都会创建新对象的事实。 This has not given me problems so far, but should I be concerned that the garbage collector will not be able to keep up eventually ? 到目前为止,这还没有给我带来任何问题,但是我是否应该担心垃圾收集器最终将无法跟上 I understand that the delete keyword here unlike in C++ only deletes the reference. 我知道,这里的delete关键字不同于C ++,只会删除引用。 Does this help when I call it at the end of each function? 当我在每个函数的末尾调用它时,这有帮助吗? Also is there anything I can do other than that to make this function more efficient so that it can be called as many times as possible without slowing things down or eventually making the browser take up too much memory. 除此以外,我还可以做些其他事来提高此功能的效率,以便可以尽可能多地调用它,而不会减慢速度或最终使浏览器占用太多内存。

After fiddling with this a bit further I have learned a lot about how to use the Chrome Developer Tools. 经过进一步的摆弄之后,我已经学到了很多有关如何使用Chrome开发者工具的知识。 Also I have rewritten these functions to be what I feel could be slightly more efficient. 另外,我将这些功能重写为我认为可能会更高效的功能。 I will have to see how it scales, but the profiling tools seem to show that these are a bit better on memory and the garbage collector. 我将不得不看看它如何扩展,但是性能分析工具似乎表明它们在内存和垃圾收集器上要好一些。

The function for rotating in world space however requires a copy in order not to create a new matrix. 但是,在世界空间中旋转的功能需要复制,以便不创建新的矩阵。 In short if we do not create a new matrix each call, we cannot simply copy the reference, we must copy the matrix. 简而言之,如果我们不为每个调用创建一个新的矩阵,就不能简单地复制引用,而必须复制矩阵。 Basically it comes down to whether or not this copy or the garbage collector overhead is going to be more expensive. 基本上,这取决于此副本或垃圾收集器的开销是否会更昂贵。

// Rotate an object around an arbitrary axis in object space
var rotObjectMatrix= new THREE.Matrix4();
function rotateAroundObjectAxis(object, axis, radians) {
    rotObjectMatrix.makeRotationAxis(axis.normalize(), radians);
    object.matrix.multiplySelf(rotObjectMatrix);      // post-multiply
    object.rotation.getRotationFromMatrix(object.matrix, object.scale);
}

var rotWorldMatrix = new THREE.Matrix4();
// Rotate an object around an arbitrary axis in world space       
function rotateAroundWorldAxis(object, axis, radians) {
    rotWorldMatrix.makeRotationAxis(axis.normalize(), radians);
    rotWorldMatrix.multiplySelf(object.matrix);        // pre-multiply
    object.matrix.copy(rotWorldMatrix);
    object.rotation.getRotationFromMatrix(object.matrix, object.scale);
}

I am not sure whether this will be better or not. 我不确定这是否会更好。 I took these screenshots. 我拍了这些截图。 This one was using the original functions and this one is with these new functions . 这是使用原始功能,而这是这些新功能 Seems to suggest slightly better performance with the new ones. 似乎建议新的性能稍好一些。 I'm not sure if I can notice a difference. 我不确定是否可以看到区别。

EDIT: I came up with a different function once I became aware of the updateMatrix function. 编辑:一旦我意识到updateMatrix函数,我想出了一个不同的函数。 This avoids the use of copying the entire matrix. 这避免了复制整个矩阵。 Here is the new world rotation function: 这是新的世界旋转功能:

/** Adds a rotation of rad to the obj's rotation about world's axis */
var rotWorldMatrix = new THREE.Matrix4();
function rotateWorldAxis(obj, axis, rad) {
    rotWorldMatrix.makeRotationAxis(axis.normalize(), rad);
    obj.updateMatrix();
    rotWorldMatrix.multiplySelf(obj.matrix); // pre-multiply
    obj.rotation.getRotationFromMatrix(rotWorldMatrix, obj.scale);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM