raycast method

  1. @override
void raycast(
  1. Raycaster raycaster,
  2. List<Intersection> intersects
)
override

Implementation

@override
void raycast(Raycaster raycaster, List<Intersection> intersects) {
  _worldScale.setFromMatrixScale(matrixWorld);

  _viewWorldMatrix.copy(raycaster.camera?.matrixWorld ?? Matrix4());
  modelViewMatrix.multiplyMatrices(raycaster.camera?.matrixWorldInverse ?? Matrix4(), matrixWorld);

  _mvPosition.setFromMatrixPosition(modelViewMatrix);

  if (raycaster.camera?.type == "PerspectiveCamera" && material.sizeAttenuation == false) {
    _worldScale.multiplyScalar(-_mvPosition.z);
  }

  var rotation = material.rotation;
  double? sin, cos;

  if (rotation != 0) {
    cos = Math.cos(rotation);
    sin = Math.sin(rotation);
  }

  var center = this.center;

  transformVertex(_spritevA.set(-0.5, -0.5, 0), _mvPosition, center, _worldScale, sin, cos);
  transformVertex(_spritevB.set(0.5, -0.5, 0), _mvPosition, center, _worldScale, sin, cos);
  transformVertex(_spritevC.set(0.5, 0.5, 0), _mvPosition, center, _worldScale, sin, cos);

  _spriteuvA.set(0, 0);
  _spriteuvB.set(1, 0);
  _spriteuvC.set(1, 1);

  // check first triangle
  var intersect = raycaster.ray.intersectTriangle(_spritevA, _spritevB, _spritevC, false, _intersectPoint);

  if (intersect == null) {
    // check second triangle
    transformVertex(_spritevB.set(-0.5, 0.5, 0), _mvPosition, center, _worldScale, sin, cos);
    _spriteuvB.set(0, 1);

    intersect = raycaster.ray.intersectTriangle(_spritevA, _spritevC, _spritevB, false, _intersectPoint);
    if (intersect == null) {
      return;
    }
  }

  var distance = raycaster.ray.origin.distanceTo(_intersectPoint);

  if (distance < raycaster.near || distance > raycaster.far) return;

  intersects.add(Intersection({
    "distance": distance,
    "point": _intersectPoint.clone(),
    "uv": Triangle.staticGetUV(
        _intersectPoint, _spritevA, _spritevB, _spritevC, _spriteuvA, _spriteuvB, _spriteuvC, Vector2(null, null)),
    "face": null,
    "object": this
  }));
}