大家好,我正在开发一个基于Three.js
的项目,用户可以悬停在一个镶嵌面上,每个网格应该在与不可见球体相交时被推开,并在离开其区域时回到原始位置。我参考了这个和这个示例,但由于不知道如何在Three.js
中使其工作而卡住了。
在我的代码中,我能够悬停在网格的每个面上,将其变为红色并在鼠标移开后恢复其原始颜色。但我无法用类似的方式控制位置。这是我认为问题所在的代码部分:
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。