threeJS Raycaster.set() not detecting intersections ( plane )

Viewed 22
  const origin = new THREE.Vector3(...point, -100),
        direction = new THREE.Vector3(0, 0, 1)    // is a normalized vector
  const raycaster = new THREE.Raycaster( origin, direction );
  const intersects = raycaster.intersectObject( dem );  // is a planeGeometry (mesh)

  const length = 2000;
  const hex = 0xff0000;

  const arrowHelper = new THREE.ArrowHelper( direction, origin, length, hex );
        cx3d.addObject( arrowHelper );

  if( intersects.length ){
    console.log( intersects[0] )
  }

now intersects = [], but point is in the plane mesh, so I need help, thank everyone

like this https://jsfiddle.net/Hoshua/obm8ke0q/2/

1 Answers

You need to update your mesh matrix world with mesh.updateMatrixWorld();. See below.

function seta(){
  const origin = new THREE.Vector3(0.5, 0, 0),
        direction = new THREE.Vector3(-1, 0, 0)
  const raycaster = new THREE.Raycaster( origin, direction );
  mesh.updateMatrixWorld(); // here
  const intersects = raycaster.intersectObject( mesh );

  const length = 1.5;
  const hex = 0xff0000;

  const arrowHelper = new THREE.ArrowHelper( direction, origin, length, hex );
  scene.add( arrowHelper );
    
  console.log(intersects)
  
}

Not sure if link will work. In case: https://jsfiddle.net/4n0tgwuo/

Output:

[{
  distance: 0.5,
  face: {
    a: 0,
    b: 2,
    c: 1,
    materialIndex: 0,
    normal: { ... }
  },
  faceIndex: 0,
  object: {
    animations: [],
    castShadow: false,
    children: [],
    frustumCulled: true,
    geometry: { ... },
    isMesh: true,
    isObject3D: true,
    layers: { ... },
    material: { ... },
    matrix: { ... },
    matrixAutoUpdate: true,
    matrixWorld: { ... },
    matrixWorldNeedsUpdate: false,
    name: "",
    parent: { ... },
    position: { ... },
    quaternion: { ... },
    receiveShadow: false,
    renderOrder: 0,
    rotation: { ... },
    scale: { ... },
    type: "Mesh",
    up: { ... },
    userData: { ... },
    uuid: "cb14eb31-b325-4252-8fb6-0d4417cbf39d",
    visible: true
  },
  point: {
    isVector3: true,
    x: 0,
    y: 0,
    z: 0
  },
  uv: {
    isVector2: true,
    x: 0.5,
    y: 0.5
  }
}, {
  distance: 0.5,
  face: {
    a: 2,
    b: 3,
    c: 1,
    materialIndex: 0,
    normal: { ... }
  },
  faceIndex: 1,
  object: [circular object Object],
  point: {
    isVector3: true,
    x: 0,
    y: 0,
    z: 0
  },
  uv: {
    isVector2: true,
    x: 0.5,
    y: 0.5
  }
}]
Related