I am attempting to create a visible point on a sphere using three.js
. This point should be where the line extending from the camera intersects the sphere.
The idea came from a helpful thread on Stack Overflow.
You can view my progress on this link
In the code provided, I have hidden the drawPointIntersection()
function which is called after the render()
function. This is where I handle the logic for drawing the point.
Below is the relevant section of code :
function drawPointIntersection() {
// Direction of camera
var direction = new THREE.Vector3(0, 0, -1);
var startPoint = camera.position.clone();
var ray = new THREE.Raycaster(startPoint, direction);
// Get point on sphere where camera direction intersects
var rayIntersects = ray.intersectObject(scene, true);
// Calculate distance between camera and point of intersection
console.log(rayIntersects[0]);
// Create point on sphere for camera direction
var dotGeometry = new THREE.Geometry();
// Trying to find correct syntax with coordinates of intersection point
//dotGeometry.vertices.push(new THREE.Vector3(0, 0, 0));
dotGeometry.vertices.push(new THREE.Vector3(rayIntersects[1]);
//dotGeometry.vertices.push(new THREE.Vector3(rayIntersects.point.x, rayIntersects.point.y, rayIntersects.point.z);
var dotMaterial = new THREE.PointsMaterial({size: 10, sizeAttenuation: false});
var dot = new THREE.Points(dotGeometry, dotMaterial);
scene.add(dot);
}
I have experimented with different syntax options to retrieve the coordinates of the point returned by ray.intersectObject(scene, true)
but none have succeeded :
dotGeometry.vertices.push(new THREE.Vector3(0, 0, 0));
dotGeometry.vertices.push(new THREE.Vector3(rayIntersects[1]);
dotGeometry.vertices.push(new THREE.Vector3(rayIntersects.point.x, rayIntersects.point.y, rayIntersects.point.z);
Note that the camera is moving around the sphere.
I am unsure why it isn't working, and would appreciate any guidance on how to obtain these coordinates to draw the point on the sphere using the THREE.Points
method in three.js R75.
Thank you in advance