Before reading - please keep in mind this is based entirely on my personal experience (Also want to have something to link when explaining questions sometimes )
When helping with projects and answering questions - sometimes it seems to me that at some point, either from old 3D tutorials, or from previous experiences with OpenGL, or just because for a new person it may be more intuitive to search docs for "translate"
instead of "position.set"
- newcomers fallback to using translateX / translateY / translateZ / rotateX / rotateY / rotateZ as if these were “the way” of moving things around the scene.
It seems like a similar issue to when people starting with 3D fallback to using .obj / .dae
format - because “it’s the simplest, so it has to be the easiest to use for a beginner, right?”, while in practice these formats can be harder to work with than glTFs / GLBs.
To make my point a little more visual, say we have 4 boxes:
We apply the following transformations to them (in order from left to right):
// redBox
mesh.translateZ(1.0);
mesh.rotateX(-Math.PI / 2.0);
mesh.translateZ(1.0);
mesh.rotateY(Math.PI / 4.0);
mesh.translateZ(1.0);
// greenBox
mesh.position.z += 1.0;
mesh.rotation.x += -Math.PI / 2.0;
mesh.position.z += 1.0;
mesh.rotation.y += Math.PI / 4.0;
mesh.position.z += 1.0;
// blueBox (Just swapping the order of operations from redBox)
mesh.rotateX(-Math.PI / 2.0);
mesh.translateZ(1.0);
mesh.rotateY(Math.PI / 4.0);
mesh.translateZ(1.0);
mesh.translateZ(1.0);
// pinkBox (Similarly, just swapping the order from blueBox)
mesh.rotation.x += -Math.PI / 2.0;
mesh.position.z += 1.0;
mesh.rotation.y += Math.PI / 4.0;
mesh.position.z += 1.0;
mesh.position.z += 1.0;
How easily can you predict where each of the boxes will be? And how easily could you predict if these were run inside the animation loop, incrementally building up the changes - for frame 2, frame 3, etc.?
Answer & Codepen
redBox and blueBox fly places, while greenBox and pinkBox appear exactly where expected - regardless of order in which we move them on axes.
Moreover, were you to run these in a loop, you can easily simplify the mutation of position and rotation vectors to just:
// Each frame the object rotates on X axis, on Y axis a little slower, and moves `3.0` on Z axis
mesh.rotation.x += -Math.PI / 2.0;
mesh.rotation.y += Math.PI / 4.0;
mesh.position.z += 3.0;
Object-space transforms cannot be intuitively simplified as their result depends on the object transformation from the previous frame.
A person learning 3D and three’s API could easily just assume that position.set
and translateX
are the same thing, not giving position.set
even a try - after all, it’s longer to type And as in the example above - modifying the position directly is in practice more predictable and better in nearly all scenarios (if not straight up “in all scenarios”.)
Would any actual value be lost if translate / rotate methods were someway openly discouraged in the documentation ? Is there any practical use-case that makes them even a viable alternative to modifying position / rotation with vector operations? Would it maybe make sense to rename object-space translation methods to translateLocalX/Y/Z
and make translateX/Y/Z
work in parent-space?