Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

World to Local #1833

Merged
merged 1 commit into from

4 participants

@zz85

As per commit notes.

There should be the convenience methods for switching between the scene/world/global/absolute coordinates to the local/child/relative coordinates with Object3D, which is part of the scene graph, rather than the Vector class.

Hopefully helps resolves issues: #1752, #1043, 025fb62 and baf0f65

@zz85 zz85 worldToLocal() and localToWorld() coordinates conversion for Object3D.
I struggled with this for quite a while, but I think this should be the right thing - having convenience methods for switching between the scene/world/global/absolute coordinates to the local/child/relative coordinates right with Object3D, which is part of the scene graph, rather than the Vector class.

Related issues: #1752, #1043, commits 025fb62 and baf0f65
da0373a
@zz85 zz85 referenced this pull request
Closed

Absolute Object3D.lookAt? #1752

@mrdoob
Owner

Thanks for fighting with this :)

@mrdoob mrdoob merged commit da0373a into mrdoob:dev
@WestLangley
Collaborator

What was the rationale for adding __inverseMatrixWorld as a property of the object inside worldToLocal() ?

@zz85

yeah! \o/ \o

@WestLangley the rationale was not to create new Matrix objects to be created for every worldToLocal() is called, and neither do we want to have an additional new Matrix object created for every Object3D generated, so its generated on ad-hoc basics once and cached.

@WestLangley
Collaborator

@zz85 I hear you, but why not create just one?

worldToLocal: function ( vector ) {

    return THREE.Object3D.__mat4.getInverse( this.matrixWorld ).multiplyVector3( vector );

}

THREE.Object3D.__mat4 = new THREE.Matrix4();
@zz85

oh yes, i think that should actually work better too, thanks! :)

@zz85

hmm... then again, i was probably thinking along the lines that matrices were usually kept for its own object purposes, for example matrixRotationWorld. but since inverseMatrixWorld is never used, we could use the __mat4 as you have proposed..

@zz85 zz85 referenced this pull request from a commit in zz85/three.js
@zz85 zz85 uses one instance of THREE.Matrix4 as proposed by @WestLangley in #1833 c7fc6af
@alteredq

Hmmm, could we maybe have some separate steps for this?

I'm just thinking for some use case where you would do many of object.worldToLocal in a row, it seems very very wasteful to compute inverse matrix every time.

@zz85

@alteredq what would you mean separate steps?

@alteredq

Something that would allow to compute inverse world matrix only once per frame instead of in every call to worldToLocal.

If object transform didn't change, and you would need to compute worldToLocal many times for different points, there is no need to compute expensive matrix inverse every time.

@zz85

hmm... perhaps we could have

worldToLocal( vector, recalculate )

where recalculate set to true will force recalculation?

so

toLocal = new THREE.Vector();
object.worldToLocal(toLocal); // this will recalculate by default

for (i=0;i<100;i++) {
    object.worldToLocal(toLocal, false); // this will prevent inverse matrix calculation 
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 28, 2012
  1. @zz85

    worldToLocal() and localToWorld() coordinates conversion for Object3D.

    zz85 authored
    I struggled with this for quite a while, but I think this should be the right thing - having convenience methods for switching between the scene/world/global/absolute coordinates to the local/child/relative coordinates right with Object3D, which is part of the scene graph, rather than the Vector class.
    
    Related issues: #1752, #1043, commits 025fb62 and baf0f65
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 0 deletions.
  1. +14 −0 src/core/Object3D.js
View
14 src/core/Object3D.js
@@ -263,6 +263,20 @@ THREE.Object3D.prototype = {
}
+ },
+
+ worldToLocal: function ( vector ) {
+
+ if ( !this.__inverseMatrixWorld ) this.__inverseMatrixWorld = new THREE.Matrix4();
+
+ return this.__inverseMatrixWorld.getInverse( this.matrixWorld ).multiplyVector3( vector );
+
+ },
+
+ localToWorld: function ( vector ) {
+
+ return this.matrixWorld.multiplyVector3( vector );
+
}
};
Something went wrong with that request. Please try again.