【问题标题】:three.js get mouse coortinates on texturethree.js 在纹理上获取鼠标坐标
【发布时间】:2025-12-10 00:35:01
【问题描述】:

我想创建一个像这样的交互式全景图:

http://mrdoob.github.io/three.js/examples/webgl_panorama_equirectangular.html

但我想让用户与之交互。

是否可以在鼠标移动时从纹理中获取坐标,以使用 three.js 创建类似于 3d 图像映射的东西?

谢谢!

【问题讨论】:

  • 您使用的 360 度图像背后的故事是什么?

标签: javascript 3d three.js webgl


【解决方案1】:

检查这个:Translating mouse location to Object coordinates

当找到射线相交的三角形时,您可以收集该三角形顶点的 3d 坐标和 UV。那里有足够的数据来计算交点的 UV 位置。

伪代码:

x0 = mouse.x; y0 = mouse.y;
vec3 samplePoint = unproject(x0, y0);  // returns (x, y, near-plane-z) coords
ray.origin = camera.position;
ray.direction = samplePoint - camera.position;
var triangleComplexObj = check_for_intersections( scene, ray );  // returns triangle, it's vertices and their UV and 3d coord and intersection coord
calulateUV( triangleComplexObj , triangleComplexObj.intersectionPoint );

理解起来可能有点复杂,但应该可以解决问题。希望这会有所帮助。

【讨论】:

  • 我做过类似的事情,但是光线投射器返回一个没有值的数组。这是我的代码: var samplePoint = new THREE.Vector3(x0, y0, 0); var raycast = new THREE.Raycaster(o.camera.position, samplePoint.sub(o.camera.position)); var c = raycast.intersectObject(o.mesh, false);控制台.log(c);
  • 我认为raycaster.intersectObject返回空数组的原因是因为相机在球体内