Object3D’s localToWorld and worldToLocal methods are really handy shortcuts, when like me you’re too lazy/focus/stupid to deal with .applyMatrix4 and .matrixWorld/.matrixWorldInverse[1].
And I was this morning: I wanted to express a vector vA of a meshA, in another meshB to obtain vB.
I didn’t want to think at all, so my brain ran to my best friend .world/localToSomething… Until I realize I didn’t care about the "world" part in the method name ![]()
What I wanted was literally .localToLocal! But sadly there was no such method…
Or maybe not yet!
What about a such utility method:
const _m1 = new THREE.Matrix4()
Object3D.prototype.localToLocal = function (v, obj) {
this.updateWorldMatrix( true, false );
obj.updateWorldMatrix( true, false );
const objMatrixWorldInverse = _m1.copy(obj.matrixWorld).invert()
return v.clone()
.applyMatrix4(this.matrixWorld)
.applyMatrix4(objMatrixWorldInverse);
};
or 2nd impl alternative (simpler thx to our best friends):
Object3D.prototype.localToLocal = function (v, obj) {
const v_world = this.localToWorld(v);
return obj.worldToLocal(v_world);
};
in my case, I could then turn off the brain and just consume it with:
const v_B = meshA.localToLocal(v_A, meshB)
TS version
declare module "three" {
export interface Object3D {
localToLocal: (v: THREE.Vector3, obj: THREE.Object3D) => THREE.Vector3;
}
}
THREE.Object3D.prototype.localToLocal = function (v: THREE.Vector3, obj: THREE.Object3D) {
const v_world = this.localToWorld(v);
return obj.worldToLocal(v_world);
};