【发布时间】:2016-06-29 13:35:24
【问题描述】:
大家好,我正在Three.js 开发一个项目,用户可以在其中hover 镶嵌面,并且每个网格在与不可见的球体相交时都应该被推开,当它在外面时回到原来的位置从它的区域。我使用this 和this example 作为参考,但我被卡住了,因为我不知道如何使它在Three.js 中工作。
在我的代码中,我可以hover 网格的每个面,使其变为红色并在鼠标消失后恢复其原始颜色。但我不能对这个职位做类似的事情。这是我认为问题所在的代码部分:
if (intersects.length > 0) {
// if the closest object intersected is not the currently stored intersection object
if (intersects[0].object != INTERSECTED) {
// Restore previous intersection objects (if they exist) to their original color
if (INTERSECTED) {
INTERSECTED.face.color.setHex(INTERSECTED.currentHex);
}
// Intersected elements
INTERSECTED = intersects[0];
var intGeometry = INTERSECTED.object.geometry;
var intFace = INTERSECTED.face;
// Difference between intersected faces and geometry
INTERSECTED.currentHex = intFace.color.getHex();
intFace.color.setHex(0xc0392b);
intGeometry.colorsNeedUpdate = true;
// Identify the vertices of each face
var intVertices = intGeometry.vertices;
var v1 = intVertices[intFace.a],
v2 = intVertices[intFace.a],
v3 = intVertices[intFace.a];
// Calculate the centroid of the selected face
var intPosition = new THREE.Vector3();
intPosition.x = (v1.x + v2.x + v3.x) / 3;
intPosition.y = (v1.y + v2.y + v3.y) / 3;
intPosition.z = (v1.z + v2.z + v3.z) / 3;
console.log(intPosition);
}
}
通过console.log() 我可以看到人脸及其位置都被正确识别,但是球体没有正确跟踪鼠标,我需要位置方面的帮助。这是带有完整代码的JSFiddle。
【问题讨论】:
-
要让球体跟随鼠标,您需要将屏幕坐标转换为threejs的世界位置。 Check this link 。并更新fiddle here,更改代码从第 375 行开始。
-
谢谢你!这是another question问题的一部分,我问过,如果你愿意,你可以在那里发布答案并获得奖励。
标签: javascript three.js