hi! I am new in threejs. I am already doing a world textured map(using D3 and THREE.js) on React. The world is already working but I need to put a red dot on the world mesh when cursor is over it, and later check it this point is over a country and highlight it. This is how I am doing it:
var wireframedMesh = new THREE.Mesh(new THREE.SphereGeometry(200, sphereSegments,sphereSegments), wireframeMaterial)
world.add(wireframedMesh)
mouseVector.x = mouse.x
mouseVector.y = mouse.y
raycaster.setFromCamera(mouseVector, camera)
let target = raycaster.intersectObject(world, true)
target.length > 0 && highlightCounty(pickedBaryCoord)
}
var highlightCounty = (barycoord) => {
var map, material;
Geometry/material for the point that follows cursor
var latlng = convertoLatLng(barycoord,200); // Get point in latitude/longitude coordinates format
var country = geo.search(latlng[0], latlng[1]);
if (laserPoint) {
wireframedMesh.remove(laserPoint);
}
laserPoint = new THREE.Mesh(pointGeometry, pointMaterial);
laserPoint.position.copy(convertToXYZ(latlng, 200));
wireframedMesh.add(laserPoint);
}
function convertoLatLng(barycoord, radius) {
radius = radius || 200;
barycoord.normalize();
var lng = -( Math.atan2( -barycoord.z, -barycoord.x ) ) - Math.PI / 2;
//to bind between -PI / PI
if( lng < - Math.PI )lng += Math.PI * 2;
var p = new THREE.Vector3( barycoord.x, 0, barycoord.z );
//project on the unit sphere
p.normalize();
var lat = Math.acos( p.dot( barycoord ) );
if( barycoord.y < 0 ) lat *= -1;
return [lng, lat ];
}
export function convertToXYZ(point, radius) {
radius = radius || 200;
var out = new THREE.Vector3();
//flips the Y axis
var lat = Math.PI / 2 - point[0]
//distribute to sphere
out.set(
Math.sin( lat ) * Math.sin( point[1] ) * radius,
Math.cos( lat ) * radius,
Math.sin( lat ) * Math.cos( point[1] ) * radius
);
return out;
}
I had to edit the raycaster prototipe to get barycoords
THREE.Mesh.prototype.raycast = (function () {
var originalRaycast = THREE.Mesh.prototype.raycast;
var localPoint = new THREE.Vector3 (), coordinates = new THREE.Vector3 ();
return function (raycaster, intersects) {
originalRaycast.call (this, raycaster, intersects);
for (var i = 0, n = intersects.length; i < n; i++) {
if (this === intersects[i].object) {
this.worldToLocal (localPoint.copy (intersects[i].point));
var face = intersects[i].face, faceIndex = intersects[i].faceIndex
// before THREE.Triangle.barycoordFromPoint
THREE.Triangle.getBarycoord(localPoint,
this.geometry.vertices[face.a],
this.geometry.vertices[face.b],
this.geometry.vertices[face.c],
coordinates);
pickedBaryCoord = coordinates
}}}
})();
The laserPoint is adding on the wireframe mesh but it is not accurate with the mouse position. Anyone with some help or reference? Thanks!