Skip to content

Instantly share code, notes, and snippets.

@zhaocnus
Last active November 18, 2017 01:28
Show Gist options
  • Select an option

  • Save zhaocnus/245203611f28fda6fb80d3a993b8c3de to your computer and use it in GitHub Desktop.

Select an option

Save zhaocnus/245203611f28fda6fb80d3a993b8c3de to your computer and use it in GitHub Desktop.
webgl-globe
export function coordinateToPosition(lat, lng, radius) {
const phi = (90 - lat) * DEGREE_TO_RADIAN;
const theta = (lng + 180) * DEGREE_TO_RADIAN;
return new THREE.Vector3(
- radius * Math.sin(phi) * Math.cos(theta),
radius * Math.cos(phi),
radius * Math.sin(phi) * Math.sin(theta)
);
}
export function clamp(num, min, max) {
return num <= min ? min : (num >= max ? max : num);
}
export function getSplineFromCoords(coords) {
const startLat = coords[0];
const startLng = coords[1];
const endLat = coords[2];
const endLng = coords[3];
// spline vertices
const start = coordinateToPosition(startLat, startLng, GLOBE_RADIUS);
const end = coordinateToPosition(endLat, endLng, GLOBE_RADIUS);
const altitude = clamp(start.distanceTo(end) * .75, CURVE_MIN_ALTITUDE, CURVE_MAX_ALTITUDE);
const interpolate = geoInterpolate([startLng, startLat], [endLng, endLat]);
const midCoord1 = interpolate(0.25);
const midCoord2 = interpolate(0.75);
const mid1 = coordinateToPosition(midCoord1[1], midCoord1[0], GLOBE_RADIUS + altitude);
const mid2 = coordinateToPosition(midCoord2[1], midCoord2[0], GLOBE_RADIUS + altitude);
return {
start,
end,
spline: new THREE.CubicBezierCurve3(start, mid1, mid2, end)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment