Threejs Object3D.raycast source code

Hi there, I wonder how threejs raycast is returning the face intersect on raycast intersect objects. However, when diving into the git repo, I came up with the surprise that the code source for that method is not posted in github:

Is there anywhere I can see the original source code. By the way, I’m working on a code to get faces from a mesh.

1 Like

Different object types implement that method. It gets overriden by object type I think… so you want to look at Mesh probably.

edit: three.js/src/objects/Mesh.js at e14d2b13903e18c39edb6e738367e69facadc7e2 · mrdoob/three.js · GitHub

and

2 Likes

Thanks for your prompt reply manthrax!

1 Like

you’re welcome. :slight_smile: hope it helps.

It’s weird that they are using the X coordinates
[three.js/src/objects/Mesh.js at e14d2b13903e18c39edb6e738367e69facadc7e2 · mrdoob/three.js · GitHub]

of vertices as the index in Mesh.getVertexPosition(a, _vA )
[three.js/src/objects/Mesh.js at e14d2b13903e18c39edb6e738367e69facadc7e2 · mrdoob/three.js · GitHub]

Yup. It does look weird But I think its just a hack.

The bufferGeometry type has accessors for values out of them…
but it doesn’t have an accessor for getting just a single value out…

And an index buffer is just an array of 3 indices per triangle…
with a stride of 1 i guess…

so using .getX like that is equivalent to saying
a=index.array[j]
b=index.array[j+1]
c=index.array[j+2]

It’s ugly but I guess it works… and if that accessor is in the hot code path, it saves a few bytes of js code and might be faster than spelling out the logic… i dunno.

i.e. so as to your question… it’s not using vertices. it’s grabbing vertex indices from the index buffer.

1 Like

Well I was not believing that until now,
thanks for clarifications manthrax!

1 Like

Hi @manthrax
sorry to bother you again
Do you mind taking a look a this other post. It’s related to this issue and it has info about what I was trying to achieve. Unfortunately, I’m blocked.

All the best!