New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Vector3.applyEuler() and Vector3.applyAxisAngle #2920
Conversation
This is difficult. The "best" solution I can think of right now is this: THREE.Quaternion.__vq1 = new THREE.Quaternion(); But would be great to find an alternative to this pattern eventually. |
Would it be possible to use a JavaScript closure style pattern? I notice |
Here is one place that uses pre-allocated temporaries in the current THREE.js style:
I have rewritten it using a closure:
The variables declared in the wrapping function, even though that function is executed right away and never stored, are forever accessible within the body of the internal function. This is a way of declaring function static variables that are private to that internal function -- they are not accessible from any other location. Thus it is also a safer style of coding, but it is a more complex style of JavaScript. |
I was aware of the pattern but didn't think of it for this. |
Here's a jsperf for truncated versions of the functions in @bhouston's comment: http://jsperf.com/three-js-closure-vs-pre-allocated. So far looks like different browsers prefer different version. More data welcome. |
@tapio Very useful. But those tests you wrote are likely be dominated by the Math.random() calls -- Random is super slow. I'd suggest a new test that preallocates an array of random numbers (10000 or more) that you then read from in the performance tests just incrementing through the array each time you need a random number. |
Updated to pre-allocate random: http://jsperf.com/three-js-closure-vs-pre-allocated/2 |
Even if slower, I would prefer the closure approach for sanity reasons. However, we would still have the problem that applyEuler: ( function ( v, eulerOrder ) {
var quaternion = new THREE.Quaternion();
return function ( v, eulerOrder ) {
return this.applyQuaternion( quaternion.setFromEuler( v, eulerOrder ) );
}
} )(), |
@mrdoob, Sorry about that. I guess I am showing my lack of JavaScript experience as my solution didn't actually fix the issue at hand. :-/ I wonder if one could actually delay the realization of the prototypes until after the fact. Sort of like.
I wouldn't be surprised if there is some other type of JavaScript paradigm that I am not aware of that can handle this situation -- some other method of predefining types. One method to avoid circular dependencies would be to put the method that involve both a simple and a more complex type on the class of the more complex type. Thus methods that involve both a Vector3 and a Quaternion would go on the Quaternion class. This would ensure that complex classes are dependent upon simpler classes but not vice versa... but this would be a large change to Three.js that is likely to be disruptive at this point. |
@mrdoob Why not just move In other words, push whatever problem we think we still have down the road. ;-) |
@WestLangley that is an easy solution! |
That's how I fixed most of these issues. @WestLangley @bhouston |
Sure! ^^ |
@jdolan It should be equally easy to rotate your cameras using Three.js. I'd suggest looking at some of the examples such as the three.js/examples/webgl_camera.html It shows do to do this. In the future I'd suggest looking at StackExchange for this type of help, see here: http://stackoverflow.com/questions/tagged/three.js |
We now have all four rotation representations represented in the form of Vector3.apply*();
Caveat: I had trouble getting this to build, as I needed to declare within
Vector3.js
the following:But due to the build order, it was forward-referencing
Quaternion
. Not good.., so I declared it inQuaternion.js
, instead. I know we don't want to do that. This needs to be fixed, somehow.I think the solution is to change the build order, but that can be a Catch-22...
I'd appreciate help with this one. :-)