Sean D
Sean D

Reputation: 376

ThreeJS raycasting is different in R81 than R71

I have a plane with a mesh on it. My code draws a ball when the user double clicks on the mesh. This works just fine in R71 but as soon as I switched to R81 raycaster doesn't return an intersect. Here's the code:

In init():

// Plane
plane = new THREE.Mesh(
    new THREE.PlaneBufferGeometry( 1000, 1000, 3, 3 ),
    new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: .5, transparent: true } )
);

plane.visible = false;
scene.add( plane ); 
planes.push(plane);

In doubleClickEvent():

event.preventDefault();

var mouse = new THREE.Vector2((event.clientX  / window.innerWidth ) * 2 - 1, -(((event.clientY / window.innerHeight ) * 2 - 1)));
var directionVector = new THREE.Vector3();
directionVector.set(mouse.x, mouse.y, 0.1);
directionVector.unproject(camera);
directionVector.sub(camera.position);           
directionVector.normalize();
raycaster.set(camera.position, directionVector);
intersects = raycaster.intersectObjects(planes);
if (intersects.length) {
    var sphereParent = new THREE.Object3D();
    var sphereGeometry = new THREE.SphereGeometry(.1, 16, 8);
    var sphereMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff });
    var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
    sphereParent.add(sphere);
    sphereParent.position.set(intersects[0].point.x, intersects[0].point.y, 0.0);
    scene.add(sphereParent);
    objects.push(sphereParent);
}

If I change

intersects = raycaster.intersectObjects(planes); 

to

intersects = raycaster.intersectObjects(scene.children);

the ball gets drawn but it gets drawn on the wrong position.

Any ideas?

Upvotes: 0

Views: 92

Answers (1)

Sean D
Sean D

Reputation: 376

I found the answer. The reason why the raycast isn't working is because the plane's visibility is false. The solution is to change the visibility of the material visibility rather the plane.

Upvotes: 2

Related Questions