You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
7.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. function init() {
  2. const container = document.getElementById('globe')
  3. const canvas = container.getElementsByTagName('canvas')[0]
  4. const width = 1000
  5. const height = 800
  6. const globeRadius = 200
  7. const globeSegments = 64
  8. const globeWidth = 4098 / 2
  9. const globeHeight = 1968 / 2
  10. const traceSteps = 10;
  11. const groups = {
  12. globe: null,
  13. globePoints: null,
  14. tracePoints: null,
  15. traceLines: null,
  16. }
  17. let data, scene, renderer, globe
  18. const camera = {
  19. object: null,
  20. orbitControls: null,
  21. angles: {
  22. current: {
  23. azimuthal: null,
  24. polar: null,
  25. },
  26. target: {
  27. azimuthal: null,
  28. polar: null,
  29. },
  30. },
  31. transition: {
  32. current: 0,
  33. target: 30,
  34. },
  35. }
  36. function setup() {
  37. scene = new THREE.Scene()
  38. camera.object = new THREE.PerspectiveCamera(45, width / height, 1, 4000)
  39. camera.object.position.z = -400
  40. renderer = new THREE.WebGLRenderer({
  41. canvas: canvas,
  42. antialias: true,
  43. opacity: 1,
  44. })
  45. renderer.setSize(width, height)
  46. setupGlobe()
  47. setupOrbitControls()
  48. render()
  49. }
  50. setup()
  51. function setupGlobe() {
  52. const canvasSize = 128
  53. const textureCanvas = document.createElement('canvas')
  54. textureCanvas.width = canvasSize
  55. textureCanvas.height = canvasSize
  56. const canvasContext = textureCanvas.getContext('2d')
  57. canvasContext.rect(0, 0, canvasSize, canvasSize)
  58. const texture = new THREE.Texture(textureCanvas)
  59. const geometry = new THREE.SphereGeometry(
  60. globeRadius,
  61. globeSegments,
  62. globeSegments
  63. )
  64. const material = new THREE.MeshBasicMaterial({
  65. map: texture,
  66. transparent: true,
  67. opacity: 0.5,
  68. })
  69. globe = new THREE.Mesh(geometry, material)
  70. groups.globe = globe
  71. groups.globe.name = 'Globe'
  72. scene.add(groups.globe)
  73. addPoints()
  74. }
  75. function addPoints() {
  76. const mergedGeometry = new THREE.Geometry()
  77. const pingGeometry = new THREE.SphereGeometry(0.5, 5, 5)
  78. const material = new THREE.MeshBasicMaterial({
  79. color: '#626177',
  80. })
  81. for (let point of pointdata.points) {
  82. const pos = convertFlatCoordsToSphereCoords(point.x, point.y)
  83. if (pos.x && pos.y && pos.z) {
  84. pingGeometry.translate(pos.x, pos.y, pos.z)
  85. mergedGeometry.merge(pingGeometry)
  86. pingGeometry.translate(-pos.x, -pos.y, -pos.z)
  87. }
  88. }
  89. const total = new THREE.Mesh(mergedGeometry, material)
  90. groups.globePoints = total
  91. groups.globePoints.name = 'Globe Points'
  92. scene.add(groups.globePoints)
  93. }
  94. function addTracePoints(tracepoints) {
  95. console.log(tracepoints);
  96. const mergedGeometry = new THREE.Geometry()
  97. const pingGeometry = new THREE.SphereGeometry(1.9, 5, 5)
  98. const material = new THREE.MeshBasicMaterial({
  99. color: '#FF0000',
  100. })
  101. for (let point of tracepoints.points) {
  102. const pos = convertLatLngToSphereCoords(point.x, point.y)
  103. if (pos.x && pos.y && pos.z) {
  104. pingGeometry.translate(pos.x, pos.y, pos.z)
  105. mergedGeometry.merge(pingGeometry)
  106. pingGeometry.translate(-pos.x, -pos.y, -pos.z)
  107. }
  108. }
  109. const total = new THREE.Mesh(mergedGeometry, material)
  110. groups.tracePoints = total
  111. groups.tracePoints.name = 'Trace Points'
  112. scene.add(groups.tracePoints)
  113. groups.tracePoints.rotation.y = groups.globePoints.rotation.y - 0.05
  114. //drawTraceLines(tracepoints);
  115. }
  116. /*
  117. function calculateLine(a, b) {
  118. var line = [];
  119. if (a < b) {
  120. var step = (a - b) / (traceSteps - 1);
  121. } else {
  122. var step = (b - a) / (traceSteps - 1);
  123. }
  124. var k;
  125. for (k = 0; k < traceSteps ; k++) {
  126. line.push(a + (step * k));
  127. }
  128. return line;
  129. }
  130. function drawTraceLines(tracepoints) {
  131. const mergedGeometry = new THREE.Geometry()
  132. const pingGeometry = new THREE.SphereGeometry(1.9, 5, 5)
  133. const material = new THREE.MeshBasicMaterial({
  134. color: '#FF4c4c',
  135. })
  136. var i;
  137. for (i = 0; i < tracepoints.points.length; i++) {
  138. if (typeof tracepoints.points[i+1] === 'undefined') {
  139. break;
  140. }
  141. var x1 = tracepoints.points[i].x;
  142. var x2 = tracepoints.points[i+1].x;
  143. xarray = calculateLine(x1, x2);
  144. var y1 = tracepoints.points[i].y;
  145. var y2 = tracepoints.points[i+1].y;
  146. yarray = calculateLine(y1, y2);
  147. var j;
  148. for (j = 0; j < xarray.length; j++) {
  149. const pos = convertLatLngToSphereCoords(xarray[j], yarray[j])
  150. if (pos.x && pos.y && pos.z) {
  151. pingGeometry.translate(pos.x, pos.y, pos.z)
  152. mergedGeometry.merge(pingGeometry)
  153. pingGeometry.translate(-pos.x, -pos.y, -pos.z)
  154. }
  155. }
  156. }
  157. const total = new THREE.Mesh(mergedGeometry, material)
  158. groups.traceLines = total
  159. groups.traceLines.name = 'Trace Lines'
  160. scene.add(groups.traceLines)
  161. groups.traceLines.rotation.y = groups.globePoints.rotation.y - 0.05
  162. }
  163. */
  164. init.addTracePoints = addTracePoints;
  165. function convertLatLngToSphereCoords(latitude, longitude) {
  166. const phi = (latitude * Math.PI) / 180
  167. const theta = ((longitude - 180) * Math.PI) / 180
  168. const x = -(globeRadius + -1) * Math.cos(phi) * Math.cos(theta)
  169. const y = (globeRadius + -1) * Math.sin(phi)
  170. const z = (globeRadius + -1) * Math.cos(phi) * Math.sin(theta)
  171. return new THREE.Vector3(x, y, z)
  172. }
  173. function convertFlatCoordsToSphereCoords(x, y) {
  174. let latitude = ((x - globeWidth) / globeWidth) * -180
  175. let longitude = ((y - globeHeight) / globeHeight) * -90
  176. latitude = (latitude * Math.PI) / 180 //(latitude / 180) * Math.PI
  177. longitude = (longitude * Math.PI) / 180 //(longitude / 180) * Math.PI
  178. const radius = Math.cos(longitude) * globeRadius
  179. const targetX = Math.cos(latitude) * radius
  180. const targetY = Math.sin(longitude) * globeRadius
  181. const targetZ = Math.sin(latitude) * radius
  182. return {
  183. x: targetX,
  184. y: targetY,
  185. z: targetZ,
  186. }
  187. }
  188. function convertLatLngToFlatCoords(latitude, longitude) {
  189. const x = Math.round((longitude + 180) * (globeWidth / 360)) * 2
  190. const y = Math.round((-1 * latitude + 90) * (globeHeight / 180)) * 2
  191. return { x, y }
  192. }
  193. function getProjectedPosition(
  194. width,
  195. height,
  196. position,
  197. contentWidth,
  198. contentHeight
  199. ) {
  200. position = position.clone()
  201. var projected = position.project(camera.object)
  202. return {
  203. x: projected.x * width + width - contentWidth / 2,
  204. y: -(projected.y * height) + height - contentHeight - 10, // -10 for a small offset
  205. }
  206. }
  207. function returnCameraAngles(x, y) {
  208. let targetAzimuthalAngle = ((x - globeWidth) / globeWidth) * Math.PI
  209. targetAzimuthalAngle = targetAzimuthalAngle + Math.PI / 2
  210. targetAzimuthalAngle += 0.3
  211. let targetPolarAngle = (y / (globeHeight * 2)) * Math.PI
  212. targetPolarAngle += 0.1 // Add a small vertical offset
  213. return {
  214. azimuthal: targetAzimuthalAngle,
  215. polar: targetPolarAngle,
  216. }
  217. }
  218. function convertLatLngToSphereCoords(latitude, longitude) {
  219. const phi = (latitude * Math.PI) / 180
  220. const theta = ((longitude - 180) * Math.PI) / 180
  221. const x = -(globeRadius + -1) * Math.cos(phi) * Math.cos(theta)
  222. const y = (globeRadius + -1) * Math.sin(phi)
  223. const z = (globeRadius + -1) * Math.cos(phi) * Math.sin(theta)
  224. return new THREE.Vector3(x, y, z)
  225. }
  226. function easeInOutCubic(t) {
  227. return t < 0.5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
  228. }
  229. function getRandomNumberBetween(min, max) {
  230. return Math.floor(Math.random() * (max - min + 1) + min)
  231. }
  232. function setupOrbitControls() {
  233. camera.orbitControls = new THREE.OrbitControls(camera.object, canvas)
  234. camera.orbitControls.enableKeys = false
  235. camera.orbitControls.enablePan = false
  236. camera.orbitControls.enableZoom = false
  237. camera.orbitControls.enableDamping = false
  238. camera.orbitControls.enableRotate = false
  239. camera.object.position.z = -550
  240. camera.orbitControls.update()
  241. }
  242. function render() {
  243. renderer.render(scene, camera.object)
  244. requestAnimationFrame(render)
  245. //animate()
  246. groups.globePoints.rotation.y += 0.01
  247. if (groups.tracePoints !== null) {
  248. groups.tracePoints.rotation.y += 0.01
  249. }
  250. /*
  251. if (groups.traceLines !== null) {
  252. groups.traceLines.rotation.y += 0.01
  253. }
  254. */
  255. }
  256. }