1

I've spend 7 hours so far trying every combination from the answers found here already:

How to get the absolute position of a vertex in three.js?

I'm trying to find the vertex positions of the geometry of a plane, which is inside an Object3D, which is inside a group.

I can get the global coordinates of the Object3D without issue, even when the group or the Object3D are moved around, but the vertices either only give me their local coordinates, or give me garbage numbers that don't correlate to their position, or are undefined, or tell me that it can't find this or that thing.

Any help would be greatly appreciated.

Josh Bowman
  • 140
  • 2
  • 8

1 Answers1

2

You can use somewhat of a shortcut - localToWorld() on Object3D. You have to make sure that the matrix is updated which usually happens automatically when you render. To be on the safe side make a copy of the Vector3.

yourObject3d.updateMatrixWorld()
yourObject3d.localToWorld( new THREE.Vector3().copy(yourGeometry.vertices[someVertex]) )
pailhead
  • 5,162
  • 2
  • 25
  • 46
  • 3
    While this code snippet may be the solution, [including an explanation](https://meta.stackexchange.com/questions/114762/explaining-entirely-%E2%80%8C%E2%80%8Bcode-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – Narendra Jadhav Jul 25 '18 at 06:29
  • OMG! THANK YOU!!! This worked! I feel like i typed this kind of thing every which way i could think of except I had no idea to do it like this, and i still don't understand why my other attempts didn't work which had the same but with the variables declared outside of the .localToWorld() and then added to it, when this works like this. – Josh Bowman Jul 25 '18 at 08:13
  • 2
    It might be that the `copy()` did the trick. If you don't have much three.js experience it's easy to mutate the wrong thing. – pailhead Jul 25 '18 at 08:25
  • daaaamn! you're right! I used clone() in several of my attempts because of the other answers to the same question @_@ live and learn! – Josh Bowman Jul 25 '18 at 08:58
  • this can kill javascripts garbage collection, however, for testing and development better to err on the side of safety – pailhead Jul 25 '18 at 09:20
  • "someVertex" are the [x,y,z] (coordinates) of each vertex in a 3D object? – Donovant Apr 25 '22 at 14:28