screenSpace.js 759 Bytes
Newer Older
Athanasios's avatar
Athanasios committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

const screenSpaceError = (context, bs) => {

  let c = context.camera;
  let frustum = context.offCenterFrustum;
  let buffer = context.drawingBuffer;

  let cameraToSphere = bs.center.subtract(c.position);

  let cameraToSphereProj = c.direction.clone();
  cameraToSphereProj.multiplyScalar(c.direction.dot(cameraToSphere));
  let distance = cameraToSphereProj.magnitude();
  let inverseNear = 1.0 / frustum.near;
  let tanTheta = frustum.top * inverseNear;
  let pixelHeight = 2.0 * distance * tanTheta / buffer.height;
  tanTheta = frustum.right * inverseNear;
  let pixelWidth = 2.0 * distance * tanTheta / buffer.width;
  return Math.max(bs.diameter / pixelWidth, bs.diameter / pixelHeight);
}

module.exports = { screenSpaceError };