UNPKG

itowns

Version:

A JS/WebGL framework for 3D geospatial data visualization

73 lines (55 loc) 2 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var THREE = _interopRequireWildcard(require("three")); function Sphere(center, radius) { this.center = center || new THREE.Vector3(); this.radius = radius || 1.0; } Sphere.prototype.constructor = Sphere; Sphere.prototype.setCenter = function (center) { this.center.copy(center); }; Sphere.prototype.setRadius = function (radius) { this.radius = radius; }; var vector = new THREE.Vector3(); var pc = new THREE.Vector3(); Sphere.prototype.intersectWithRayNoMiss = function (ray) { ray.closestPointToPoint(this.center, pc); var a = pc.length(); var d; var b; // TODO: recompute mirror ray // If the ray miss sphere, we recompute the new ray with point symetric to tangent sphere if (a > this.radius) { // mirror point is symetric of pc // The mirror ray must pass through the point mirrorPoint var mirrorPoint = pc.clone().setLength(this.radius * 2 - a); // Compute the new direction d = ray.direction.subVectors(mirrorPoint, ray.origin).normalize(); // Classic intersection with the new ray ray.closestPointToPoint(this.center, pc); a = pc.length(); b = Math.sqrt(this.radius * this.radius - a * a); d.setLength(b); return vector.addVectors(pc, d); } // TODO: check all intersections : if (ray.origin.length() > this.radius) d = ray.direction.clone(); b = Math.sqrt(this.radius * this.radius - a * a); d.setLength(b); return vector.subVectors(pc, d); }; Sphere.prototype.intersectWithRay = function (ray) { ray.closestPointToPoint(this.center, pc); var a = pc.length(); if (a > this.radius) { return undefined; } var d = ray.direction.clone(); var b = Math.sqrt(this.radius * this.radius - a * a); d.setLength(b); return vector.subVectors(pc, d); }; var _default = Sphere; exports["default"] = _default;