mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.32k stars 35.35k forks source link

Raycaster doesn't return UV2 coordinates #13649

Closed darkf closed 5 years ago

darkf commented 6 years ago
Description of the problem

The raycaster provides UV coordinates of the hit point, but not UV2 (second layer, i.e. faceVertexUvs[1]) coordinates.

Three.js version
Browser
OS
Hardware Requirements (graphics card, VR Device, ...)
WestLangley commented 6 years ago

Can you please explain your use case?

darkf commented 6 years ago

Yes, my scene has a lightmap (and other textures in lightmap/UV2 space) which I want to sample and draw on as a result of the raycast, a la Tag, Portal 2, Splatoon, etc. For this I need the UV2 coordinates of where it hit -- and since Three already provides UV coordinates, I figure it should provide them on all layers anyway.

It would be fine (preferable even) if it were upon request instead, e.g. having a getTextureCoords method of some sort, that way unnecessary calculations are not being done every cast.

WestLangley commented 6 years ago

What is your current workaround?

Mugen87 commented 6 years ago

Um, I guess the change in Mesh.raycast() should be manageable to support this.

WestLangley commented 6 years ago

I think the OP should respond to my question so we can decide if the workaround is adequate for this edge case.

darkf commented 6 years ago

I don't have a workaround right now, hence the request. I typically use Three for my 3D needs, and I looked to see if it had it, which it did not. It seems like a reasonable feature to have (if you're going to return UVs it may as well be on all layers...).

WestLangley commented 6 years ago

if you're going to return UVs it may as well be on all layers It would be fine (preferable even) if it were upon request instead, e.g. having a getTextureCoords method of some sort

That does seem reasonable.

On the other hand, there has been no demand for this feature previously.

manthrax commented 6 years ago

Another use case for this is looking up the texel in a lightmap raycasted below a character, as a quick and dirty query for "is this character in shadow and what color is the lighting here".. the resulting color of which you can then use to tint/shade the character. You can even use multiple casts to take samples from a few different directions. I've used this in PC and console games in the past and it can be a cheap and easy way to fake env based lighting for some setups. Alternatively, we could allow the retrieval of the second set of UVs to be a method on the collision result or something. Would be nice to to expose the barycentric UV calculation via some utility anyway.

Mugen87 commented 5 years ago

Created a PR since I recently needed this feature^^